Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2018 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 "update_engine/payload_consumer/verity_writer_android.h" |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | |
| 21 | #include <algorithm> |
| 22 | #include <memory> |
| 23 | |
| 24 | #include <base/logging.h> |
| 25 | #include <base/posix/eintr_wrapper.h> |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 26 | #include <fec/ecc.h> |
| 27 | extern "C" { |
| 28 | #include <fec.h> |
| 29 | } |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 30 | |
| 31 | #include "update_engine/common/utils.h" |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame^] | 32 | #include "update_engine/payload_consumer/cached_file_descriptor.h" |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 33 | #include "update_engine/payload_consumer/file_descriptor.h" |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 34 | |
| 35 | namespace chromeos_update_engine { |
| 36 | |
| 37 | namespace verity_writer { |
| 38 | std::unique_ptr<VerityWriterInterface> CreateVerityWriter() { |
| 39 | return std::make_unique<VerityWriterAndroid>(); |
| 40 | } |
| 41 | } // namespace verity_writer |
| 42 | |
| 43 | bool VerityWriterAndroid::Init(const InstallPlan::Partition& partition) { |
| 44 | partition_ = &partition; |
| 45 | |
xunchang | 32e8204 | 2019-04-25 10:37:36 -0700 | [diff] [blame] | 46 | if (partition_->hash_tree_size != 0 || partition_->fec_size != 0) { |
| 47 | utils::SetBlockDeviceReadOnly(partition_->target_path, false); |
| 48 | } |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 49 | if (partition_->hash_tree_size != 0) { |
| 50 | auto hash_function = |
| 51 | HashTreeBuilder::HashFunction(partition_->hash_tree_algorithm); |
| 52 | if (hash_function == nullptr) { |
| 53 | LOG(ERROR) << "Verity hash algorithm not supported: " |
| 54 | << partition_->hash_tree_algorithm; |
| 55 | return false; |
| 56 | } |
| 57 | hash_tree_builder_ = std::make_unique<HashTreeBuilder>( |
| 58 | partition_->block_size, hash_function); |
| 59 | TEST_AND_RETURN_FALSE(hash_tree_builder_->Initialize( |
| 60 | partition_->hash_tree_data_size, partition_->hash_tree_salt)); |
| 61 | if (hash_tree_builder_->CalculateSize(partition_->hash_tree_data_size) != |
| 62 | partition_->hash_tree_size) { |
| 63 | LOG(ERROR) << "Verity hash tree size does not match, stored: " |
| 64 | << partition_->hash_tree_size << ", calculated: " |
| 65 | << hash_tree_builder_->CalculateSize( |
| 66 | partition_->hash_tree_data_size); |
| 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | bool VerityWriterAndroid::Update(uint64_t offset, |
| 74 | const uint8_t* buffer, |
| 75 | size_t size) { |
| 76 | if (partition_->hash_tree_size != 0) { |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame^] | 77 | const uint64_t hash_tree_data_end = |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 78 | partition_->hash_tree_data_offset + partition_->hash_tree_data_size; |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame^] | 79 | const uint64_t start_offset = |
| 80 | std::max(offset, partition_->hash_tree_data_offset); |
| 81 | if (offset + size > hash_tree_data_end) { |
| 82 | LOG(WARNING) |
| 83 | << "Reading past hash_tree_data_end, something is probably " |
| 84 | "wrong, might cause incorrect hash of partitions. offset: " |
| 85 | << offset << " size: " << size |
| 86 | << " hash_tree_data_end: " << hash_tree_data_end; |
| 87 | } |
| 88 | const uint64_t end_offset = std::min(offset + size, hash_tree_data_end); |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 89 | if (start_offset < end_offset) { |
| 90 | TEST_AND_RETURN_FALSE(hash_tree_builder_->Update( |
| 91 | buffer + start_offset - offset, end_offset - start_offset)); |
| 92 | |
| 93 | if (end_offset == hash_tree_data_end) { |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame^] | 94 | LOG(INFO) |
| 95 | << "Read everything before hash tree. Ready to write hash tree."; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | } |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame^] | 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | bool VerityWriterAndroid::Finalize(FileDescriptorPtr read_fd, |
| 104 | FileDescriptorPtr write_fd) { |
| 105 | // All hash tree data blocks has been hashed, write hash tree to disk. |
| 106 | LOG(INFO) << "Writing verity hash tree to " << partition_->target_path; |
| 107 | TEST_AND_RETURN_FALSE(hash_tree_builder_->BuildHashTree()); |
| 108 | TEST_AND_RETURN_FALSE_ERRNO( |
| 109 | write_fd->Seek(partition_->hash_tree_offset, SEEK_SET)); |
| 110 | auto success = |
| 111 | hash_tree_builder_->WriteHashTree([write_fd](auto data, auto size) { |
| 112 | return utils::WriteAll(write_fd, data, size); |
| 113 | }); |
| 114 | // hashtree builder already prints error messages. |
| 115 | TEST_AND_RETURN_FALSE(success); |
| 116 | hash_tree_builder_.reset(); |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 117 | if (partition_->fec_size != 0) { |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame^] | 118 | LOG(INFO) << "Writing verity FEC to " << partition_->target_path; |
| 119 | TEST_AND_RETURN_FALSE(EncodeFEC(read_fd, |
| 120 | write_fd, |
| 121 | partition_->fec_data_offset, |
| 122 | partition_->fec_data_size, |
| 123 | partition_->fec_offset, |
| 124 | partition_->fec_size, |
| 125 | partition_->fec_roots, |
| 126 | partition_->block_size, |
| 127 | false /* verify_mode */)); |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 128 | } |
| 129 | return true; |
| 130 | } |
| 131 | |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 132 | bool VerityWriterAndroid::EncodeFEC(FileDescriptorPtr read_fd, |
| 133 | FileDescriptorPtr write_fd, |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 134 | uint64_t data_offset, |
| 135 | uint64_t data_size, |
| 136 | uint64_t fec_offset, |
| 137 | uint64_t fec_size, |
| 138 | uint32_t fec_roots, |
| 139 | uint32_t block_size, |
| 140 | bool verify_mode) { |
| 141 | TEST_AND_RETURN_FALSE(data_size % block_size == 0); |
| 142 | TEST_AND_RETURN_FALSE(fec_roots >= 0 && fec_roots < FEC_RSM); |
| 143 | // This is the N in RS(M, N), which is the number of bytes for each rs block. |
| 144 | size_t rs_n = FEC_RSM - fec_roots; |
| 145 | uint64_t rounds = utils::DivRoundUp(data_size / block_size, rs_n); |
| 146 | TEST_AND_RETURN_FALSE(rounds * fec_roots * block_size == fec_size); |
| 147 | |
| 148 | std::unique_ptr<void, decltype(&free_rs_char)> rs_char( |
| 149 | init_rs_char(FEC_PARAMS(fec_roots)), &free_rs_char); |
| 150 | TEST_AND_RETURN_FALSE(rs_char != nullptr); |
| 151 | |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame^] | 152 | // Cache at most 1MB of fec data, in VABC, we need to re-open fd if we |
| 153 | // perform a read() operation after write(). So reduce the number of writes |
| 154 | // can save unnecessary re-opens. |
| 155 | write_fd = std::make_shared<CachedFileDescriptor>(write_fd, 1 * (1 << 20)); |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 156 | for (size_t i = 0; i < rounds; i++) { |
| 157 | // Encodes |block_size| number of rs blocks each round so that we can read |
| 158 | // one block each time instead of 1 byte to increase random read |
| 159 | // performance. This uses about 1 MiB memory for 4K block size. |
| 160 | brillo::Blob rs_blocks(block_size * rs_n); |
| 161 | for (size_t j = 0; j < rs_n; j++) { |
| 162 | brillo::Blob buffer(block_size, 0); |
| 163 | uint64_t offset = |
| 164 | fec_ecc_interleave(i * rs_n * block_size + j, rs_n, rounds); |
| 165 | // Don't read past |data_size|, treat them as 0. |
| 166 | if (offset < data_size) { |
| 167 | ssize_t bytes_read = 0; |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 168 | TEST_AND_RETURN_FALSE(utils::PReadAll(read_fd, |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 169 | buffer.data(), |
| 170 | buffer.size(), |
| 171 | data_offset + offset, |
| 172 | &bytes_read)); |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 173 | TEST_AND_RETURN_FALSE(bytes_read >= 0); |
| 174 | TEST_AND_RETURN_FALSE(static_cast<size_t>(bytes_read) == buffer.size()); |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 175 | } |
| 176 | for (size_t k = 0; k < buffer.size(); k++) { |
| 177 | rs_blocks[k * rs_n + j] = buffer[k]; |
| 178 | } |
| 179 | } |
| 180 | brillo::Blob fec(block_size * fec_roots); |
| 181 | for (size_t j = 0; j < block_size; j++) { |
| 182 | // Encode [j * rs_n : (j + 1) * rs_n) in |rs_blocks| and write |fec_roots| |
| 183 | // number of parity bytes to |j * fec_roots| in |fec|. |
| 184 | encode_rs_char(rs_char.get(), |
| 185 | rs_blocks.data() + j * rs_n, |
| 186 | fec.data() + j * fec_roots); |
| 187 | } |
| 188 | |
| 189 | if (verify_mode) { |
| 190 | brillo::Blob fec_read(fec.size()); |
| 191 | ssize_t bytes_read = 0; |
| 192 | TEST_AND_RETURN_FALSE(utils::PReadAll( |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 193 | read_fd, fec_read.data(), fec_read.size(), fec_offset, &bytes_read)); |
| 194 | TEST_AND_RETURN_FALSE(bytes_read >= 0); |
| 195 | TEST_AND_RETURN_FALSE(static_cast<size_t>(bytes_read) == fec_read.size()); |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 196 | TEST_AND_RETURN_FALSE(fec == fec_read); |
| 197 | } else { |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 198 | CHECK(write_fd); |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame^] | 199 | write_fd->Seek(fec_offset, SEEK_SET); |
| 200 | if (!utils::WriteAll(write_fd, fec.data(), fec.size())) { |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 201 | PLOG(ERROR) << "EncodeFEC write() failed"; |
| 202 | return false; |
| 203 | } |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 204 | } |
| 205 | fec_offset += fec.size(); |
| 206 | } |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame^] | 207 | write_fd->Flush(); |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 208 | return true; |
| 209 | } |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 210 | |
| 211 | bool VerityWriterAndroid::EncodeFEC(const std::string& path, |
| 212 | uint64_t data_offset, |
| 213 | uint64_t data_size, |
| 214 | uint64_t fec_offset, |
| 215 | uint64_t fec_size, |
| 216 | uint32_t fec_roots, |
| 217 | uint32_t block_size, |
| 218 | bool verify_mode) { |
| 219 | FileDescriptorPtr fd(new EintrSafeFileDescriptor()); |
| 220 | TEST_AND_RETURN_FALSE( |
| 221 | fd->Open(path.c_str(), verify_mode ? O_RDONLY : O_RDWR)); |
| 222 | return EncodeFEC(fd, |
| 223 | fd, |
| 224 | data_offset, |
| 225 | data_size, |
| 226 | fec_offset, |
| 227 | fec_size, |
| 228 | fec_roots, |
| 229 | block_size, |
| 230 | verify_mode); |
| 231 | } |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 232 | } // namespace chromeos_update_engine |