1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <android-base/file.h>
18 #include <android-base/stringprintf.h>
19 #include <utils/FileMap.h>
20 
21 #ifdef __ANDROID__
22 #include "incfs_inline.h"
23 #endif
24 
25 #include "util/map_ptr.h"
26 
27 namespace android::incfs {
28 IncFsFileMap::IncFsFileMap() noexcept = default;
29 IncFsFileMap::IncFsFileMap(IncFsFileMap&&) noexcept = default;
30 IncFsFileMap& IncFsFileMap::operator =(IncFsFileMap&&) noexcept = default;
31 IncFsFileMap::~IncFsFileMap() noexcept = default;
32 
unsafe_data() const33 const void* IncFsFileMap::unsafe_data() const {
34     return map_->getDataPtr();
35 }
36 
length() const37 size_t IncFsFileMap::length() const {
38     return map_->getDataLength();
39 }
40 
offset() const41 off64_t IncFsFileMap::offset() const {
42     return map_->getDataOffset();
43 }
44 
file_name() const45 const char* IncFsFileMap::file_name() const {
46     return map_->getFileName();
47 }
48 
Create(int fd,off64_t offset,size_t length,const char * file_name)49 bool IncFsFileMap::Create(int fd, off64_t offset, size_t length, const char* file_name) {
50     return Create(fd, offset, length, file_name, true /* verify */);
51 }
52 
53 #ifdef __ANDROID__
IsVerificationEnabled(int fd)54 static bool IsVerificationEnabled(int fd) {
55     return isIncFsFd(fd) && isFullyLoaded(fd) != LoadingState::Full;
56 }
57 
58 using data_block_index_t = uint32_t;
59 
get_block_index(const uint8_t * ptr,const uint8_t * start_block_ptr)60 static data_block_index_t get_block_index(const uint8_t* ptr, const uint8_t* start_block_ptr) {
61     return (ptr - start_block_ptr) / INCFS_DATA_FILE_BLOCK_SIZE;
62 }
63 
Create(int fd,off64_t offset,size_t length,const char * file_name,bool verify)64 bool IncFsFileMap::Create(int fd, off64_t offset, size_t length, const char* file_name,
65                           bool verify) {
66     return CreateForceVerification(fd, offset, length, file_name,
67                                    verify && IsVerificationEnabled(fd));
68 }
69 
CreateForceVerification(int fd,off64_t offset,size_t length,const char * file_name,bool verify)70 bool IncFsFileMap::CreateForceVerification(int fd, off64_t offset, size_t length,
71                                            const char* file_name, bool verify) {
72     map_ = std::make_unique<android::FileMap>();
73     if (!map_->create(file_name, fd, offset, length, true /* readOnly */)) {
74         return false;
75     }
76 
77     fd_ = fd;
78     verification_enabled_ = verify;
79     if (verification_enabled_) {
80         // Initialize the block cache with enough buckets to hold all of the blocks within the
81         // memory-mapped region.
82         size_t offset_diff = offset % INCFS_DATA_FILE_BLOCK_SIZE;
83         size_t base_length_ = length + offset_diff;
84         start_block_offset_ = offset - offset_diff;
85         start_block_ptr_ = reinterpret_cast<const uint8_t*>(map_->getDataPtr()) - offset_diff;
86 
87         const size_t bucket_count = (base_length_ / INCFS_DATA_FILE_BLOCK_SIZE) / kBucketBits;
88         loaded_blocks_ = std::vector<std::atomic<bucket_t> >(bucket_count + 1U);
89     }
90     return true;
91 }
92 
Verify(const uint8_t * const & data_start,const uint8_t * const & data_end,const uint8_t ** prev_verified_block) const93 bool IncFsFileMap::Verify(const uint8_t* const& data_start, const uint8_t* const& data_end,
94                           const uint8_t** prev_verified_block) const {
95     const data_block_index_t start_index = get_block_index(data_start, start_block_ptr_);
96     const data_block_index_t end_index = get_block_index(data_end - 1U, start_block_ptr_);
97 
98     bool success = true;
99     // Retrieve the set of the required blocks that must be present in order to read the data
100     // safely.
101     for (data_block_index_t curr_index = start_index; curr_index <= end_index; ++curr_index) {
102         const size_t i = curr_index / kBucketBits;
103         const bucket_t present_bit = 1U << (curr_index % kBucketBits);
104         std::atomic<bucket_t>& bucket = loaded_blocks_[i];
105         if (LIKELY(bucket.load(std::memory_order_relaxed) & present_bit)) {
106             continue;
107         }
108 
109         // Touch all of the blocks with pread to ensure that the region of data is fully present.
110         uint8_t value;
111         const off64_t read_offset = (curr_index * INCFS_DATA_FILE_BLOCK_SIZE) + start_block_offset_;
112         if (UNLIKELY(TEMP_FAILURE_RETRY(pread64(fd_, &value, 1U, read_offset)) <= 0)) {
113             success = false;
114             break;
115         }
116 
117         bucket.fetch_or(present_bit, std::memory_order_relaxed);
118     }
119 
120     if (UNLIKELY(!success)) {
121         // Log the region of the file that could not be fully loaded.
122         size_t start_offset = (data_start - start_block_ptr_) + map_->getDataOffset();
123         size_t end_offset = (data_end - start_block_ptr_) + map_->getDataOffset();
124         std::string location = file_name() ? base::StringPrintf("path %s", file_name())
125                                            : base::StringPrintf("fd %d", fd_);
126         const std::string message =
127                 base::StringPrintf("region 0x%016zx - 0x%016zx of %s not fully loaded",
128                                    start_offset, end_offset, location.c_str());
129         LOG(WARNING) << message;
130         return false;
131     }
132 
133     // Update the previous verified block pointer to optimize repeated verifies on the same block.
134     *prev_verified_block = start_block_ptr_ + (end_index * INCFS_DATA_FILE_BLOCK_SIZE);
135     return true;
136 }
137 
138 #else
Create(int fd,off64_t offset,size_t length,const char * file_name,bool verify)139 bool IncFsFileMap::Create(int fd, off64_t offset, size_t length, const char* file_name,
140                           bool verify) {
141     return CreateForceVerification(fd, offset, length, file_name, verify);
142 }
143 
CreateForceVerification(int fd,off64_t offset,size_t length,const char * file_name,bool)144 bool IncFsFileMap::CreateForceVerification(int fd, off64_t offset, size_t length,
145                                            const char* file_name, bool /* verify */) {
146     map_ = std::make_unique<android::FileMap>();
147     return map_->create(file_name, fd, offset, length, true /* readOnly */);
148 }
149 
Verify(const uint8_t * const &,const uint8_t * const &,const uint8_t **) const150 bool IncFsFileMap::Verify(const uint8_t* const& /* data_start */,
151                           const uint8_t* const& /* data_end */,
152                           const uint8_t** /* prev_verified_block */) const {
153     return true;
154 }
155 #endif
156 
157 } // namespace android::incfs