blob: 31d28753d2a5ad57b9ad0911243f5381862d8155 [file] [log] [blame]
Sen Jiang57f91802017-11-14 17:42:13 -08001//
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>
Daniel Zhengbd16b012022-09-28 23:40:05 +000023#include <utility>
Sen Jiang57f91802017-11-14 17:42:13 -080024
25#include <base/logging.h>
26#include <base/posix/eintr_wrapper.h>
Sen Jianga778e5b2018-09-13 11:42:56 -070027#include <fec/ecc.h>
28extern "C" {
29#include <fec.h>
30}
Sen Jiang57f91802017-11-14 17:42:13 -080031
32#include "update_engine/common/utils.h"
Kelvin Zhang7f925672021-03-15 13:37:40 -040033#include "update_engine/payload_consumer/cached_file_descriptor.h"
Kelvin Zhangb138ab52020-11-06 15:56:41 -050034#include "update_engine/payload_consumer/file_descriptor.h"
Sen Jiang57f91802017-11-14 17:42:13 -080035
36namespace chromeos_update_engine {
37
Daniel Zhengbd16b012022-09-28 23:40:05 +000038bool IncrementalEncodeFEC::Init(const uint64_t _data_offset,
39 const uint64_t _data_size,
40 const uint64_t _fec_offset,
41 const uint64_t _fec_size,
42 const uint64_t _fec_roots,
43 const uint64_t _block_size,
44 const bool _verify_mode) {
45 current_step_ = EncodeFECStep::kInitFDStep;
46 data_offset_ = _data_offset;
47 data_size_ = _data_size;
48 fec_offset_ = _fec_offset;
49 fec_size_ = _fec_size;
50 fec_roots_ = _fec_roots;
51 block_size_ = _block_size;
52 verify_mode_ = _verify_mode;
53 current_round_ = 0;
54 // This is the N in RS(M, N), which is the number of bytes for each rs block.
55 rs_n_ = FEC_RSM - fec_roots_;
56 rs_char_.reset(init_rs_char(FEC_PARAMS(fec_roots_)));
57 rs_blocks_.resize(block_size_ * rs_n_);
58 buffer_.resize(block_size_, 0);
59 fec_.resize(block_size_ * fec_roots_);
60 fec_read_.resize(fec_.size());
61 TEST_AND_RETURN_FALSE(data_size_ % block_size_ == 0);
62 TEST_AND_RETURN_FALSE(fec_roots_ >= 0 && fec_roots_ < FEC_RSM);
63
64 num_rounds_ = utils::DivRoundUp(data_size_ / block_size_, rs_n_);
65 TEST_AND_RETURN_FALSE(num_rounds_ * fec_roots_ * block_size_ == fec_size_);
66 TEST_AND_RETURN_FALSE(rs_char_ != nullptr);
67 return true;
68}
69
70bool IncrementalEncodeFEC::Compute(FileDescriptor* _read_fd,
71 FileDescriptor* _write_fd) {
72 if (current_step_ == EncodeFECStep::kInitFDStep) {
73 read_fd_ = _read_fd;
74 write_fd_ = _write_fd;
75 cache_fd_.SetFD(write_fd_);
76 write_fd_ = &cache_fd_;
77 } else if (current_step_ == EncodeFECStep::kEncodeRoundStep) {
78 // Encodes |block_size| number of rs blocks each round so that we can read
79 // one block each time instead of 1 byte to increase random read
80 // performance. This uses about 1 MiB memory for 4K block size.
81 for (size_t j = 0; j < rs_n_; j++) {
82 uint64_t offset = fec_ecc_interleave(
83 current_round_ * rs_n_ * block_size_ + j, rs_n_, num_rounds_);
84 // Don't read past |data_size|, treat them as 0.
85 if (offset >= data_size_) {
86 std::fill(buffer_.begin(), buffer_.end(), 0);
87 } else {
88 ssize_t bytes_read = 0;
89 TEST_AND_RETURN_FALSE(utils::PReadAll(read_fd_,
90 buffer_.data(),
91 buffer_.size(),
92 data_offset_ + offset,
93 &bytes_read));
94 TEST_AND_RETURN_FALSE(bytes_read >= 0);
95 TEST_AND_RETURN_FALSE(static_cast<size_t>(bytes_read) ==
96 buffer_.size());
97 }
98 for (size_t k = 0; k < buffer_.size(); k++) {
99 rs_blocks_[k * rs_n_ + j] = buffer_[k];
100 }
101 }
102 for (size_t j = 0; j < block_size_; j++) {
103 // Encode [j * rs_n_ : (j + 1) * rs_n_) in |rs_blocks| and write
104 // |fec_roots| number of parity bytes to |j * fec_roots| in |fec|.
105 encode_rs_char(rs_char_.get(),
106 rs_blocks_.data() + j * rs_n_,
107 fec_.data() + j * fec_roots_);
108 }
109
110 if (verify_mode_) {
111 ssize_t bytes_read = 0;
112 TEST_AND_RETURN_FALSE(utils::PReadAll(read_fd_,
113 fec_read_.data(),
114 fec_read_.size(),
115 fec_offset_,
116 &bytes_read));
117 TEST_AND_RETURN_FALSE(bytes_read >= 0);
118 TEST_AND_RETURN_FALSE(static_cast<size_t>(bytes_read) ==
119 fec_read_.size());
120 TEST_AND_RETURN_FALSE(fec_ == fec_read_);
121 } else {
122 CHECK(write_fd_);
123 write_fd_->Seek(fec_offset_, SEEK_SET);
124 if (!utils::WriteAll(write_fd_, fec_.data(), fec_.size())) {
125 PLOG(ERROR) << "EncodeFEC write() failed";
126 return false;
127 }
128 }
129 fec_offset_ += fec_.size();
130 current_round_++;
131 } else if (current_step_ == EncodeFECStep::kWriteStep) {
132 write_fd_->Flush();
133 }
134 UpdateState();
135 return true;
136}
137// update the current state of EncodeFEC. Can be changed to have smaller steps
138void IncrementalEncodeFEC::UpdateState() {
139 if (current_step_ == EncodeFECStep::kInitFDStep) {
140 current_step_ = EncodeFECStep::kEncodeRoundStep;
141 } else if (current_step_ == EncodeFECStep::kEncodeRoundStep &&
142 current_round_ == num_rounds_) {
143 current_step_ = EncodeFECStep::kWriteStep;
144 } else if (current_step_ == EncodeFECStep::kWriteStep) {
145 current_step_ = EncodeFECStep::kComplete;
146 }
147}
148
149bool IncrementalEncodeFEC::Finished() const {
150 return current_step_ == EncodeFECStep::kComplete;
151}
152
Daniel Zheng3efb3f22022-12-19 22:14:20 +0000153double IncrementalEncodeFEC::ReportProgress() const {
154 return static_cast<double>(current_round_) / num_rounds_;
155}
156
Sen Jiang57f91802017-11-14 17:42:13 -0800157namespace verity_writer {
158std::unique_ptr<VerityWriterInterface> CreateVerityWriter() {
159 return std::make_unique<VerityWriterAndroid>();
160}
161} // namespace verity_writer
162
163bool VerityWriterAndroid::Init(const InstallPlan::Partition& partition) {
164 partition_ = &partition;
Daniel Zhengbd16b012022-09-28 23:40:05 +0000165 LOG(INFO) << "Initializing Incremental EncodeFEC";
166 TEST_AND_RETURN_FALSE(encodeFEC_.Init(partition_->fec_data_offset,
167 partition_->fec_data_size,
168 partition_->fec_offset,
169 partition_->fec_size,
170 partition_->fec_roots,
171 partition_->block_size,
172 false /* verify_mode */));
173 hash_tree_written_ = false;
Sen Jiang57f91802017-11-14 17:42:13 -0800174 if (partition_->hash_tree_size != 0) {
175 auto hash_function =
176 HashTreeBuilder::HashFunction(partition_->hash_tree_algorithm);
177 if (hash_function == nullptr) {
178 LOG(ERROR) << "Verity hash algorithm not supported: "
179 << partition_->hash_tree_algorithm;
180 return false;
181 }
182 hash_tree_builder_ = std::make_unique<HashTreeBuilder>(
183 partition_->block_size, hash_function);
184 TEST_AND_RETURN_FALSE(hash_tree_builder_->Initialize(
185 partition_->hash_tree_data_size, partition_->hash_tree_salt));
186 if (hash_tree_builder_->CalculateSize(partition_->hash_tree_data_size) !=
187 partition_->hash_tree_size) {
188 LOG(ERROR) << "Verity hash tree size does not match, stored: "
189 << partition_->hash_tree_size << ", calculated: "
190 << hash_tree_builder_->CalculateSize(
191 partition_->hash_tree_data_size);
192 return false;
193 }
194 }
Kelvin Zhangeeec3812021-03-16 14:01:47 -0400195 total_offset_ = 0;
Sen Jiang57f91802017-11-14 17:42:13 -0800196 return true;
197}
198
Kelvin Zhangeeec3812021-03-16 14:01:47 -0400199bool VerityWriterAndroid::Update(const uint64_t offset,
Sen Jiang57f91802017-11-14 17:42:13 -0800200 const uint8_t* buffer,
201 size_t size) {
Kelvin Zhangeeec3812021-03-16 14:01:47 -0400202 if (offset != total_offset_) {
203 LOG(ERROR) << "Sequential read expected, expected to read at: "
204 << total_offset_ << " actual read occurs at: " << offset;
205 return false;
206 }
Sen Jiang57f91802017-11-14 17:42:13 -0800207 if (partition_->hash_tree_size != 0) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400208 const uint64_t hash_tree_data_end =
Sen Jiang57f91802017-11-14 17:42:13 -0800209 partition_->hash_tree_data_offset + partition_->hash_tree_data_size;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400210 const uint64_t start_offset =
211 std::max(offset, partition_->hash_tree_data_offset);
212 if (offset + size > hash_tree_data_end) {
213 LOG(WARNING)
214 << "Reading past hash_tree_data_end, something is probably "
215 "wrong, might cause incorrect hash of partitions. offset: "
216 << offset << " size: " << size
217 << " hash_tree_data_end: " << hash_tree_data_end;
218 }
219 const uint64_t end_offset = std::min(offset + size, hash_tree_data_end);
Sen Jiang57f91802017-11-14 17:42:13 -0800220 if (start_offset < end_offset) {
221 TEST_AND_RETURN_FALSE(hash_tree_builder_->Update(
222 buffer + start_offset - offset, end_offset - start_offset));
223
224 if (end_offset == hash_tree_data_end) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400225 LOG(INFO)
226 << "Read everything before hash tree. Ready to write hash tree.";
Sen Jiang57f91802017-11-14 17:42:13 -0800227 }
228 }
229 }
Kelvin Zhangeeec3812021-03-16 14:01:47 -0400230 total_offset_ += size;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400231
232 return true;
233}
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800234bool VerityWriterAndroid::Finalize(FileDescriptor* read_fd,
235 FileDescriptor* write_fd) {
Kelvin Zhangeeec3812021-03-16 14:01:47 -0400236 const auto hash_tree_data_end =
237 partition_->hash_tree_data_offset + partition_->hash_tree_data_size;
238 if (total_offset_ < hash_tree_data_end) {
239 LOG(ERROR) << "Read up to " << total_offset_
240 << " when we are expecting to read everything "
241 "before "
242 << hash_tree_data_end;
243 return false;
244 }
Kelvin Zhang7f925672021-03-15 13:37:40 -0400245 // All hash tree data blocks has been hashed, write hash tree to disk.
Kelvin Zhang91e839c2022-04-05 14:25:00 -0700246 LOG(INFO) << "Writing verity hash tree to "
247 << partition_->readonly_target_path;
Kelvin Zhange4bffe62022-03-07 09:08:38 -0800248 if (hash_tree_builder_) {
249 TEST_AND_RETURN_FALSE(hash_tree_builder_->BuildHashTree());
250 TEST_AND_RETURN_FALSE_ERRNO(
251 write_fd->Seek(partition_->hash_tree_offset, SEEK_SET));
252 auto success =
253 hash_tree_builder_->WriteHashTree([write_fd](auto data, auto size) {
254 return utils::WriteAll(write_fd, data, size);
255 });
256 // hashtree builder already prints error messages.
257 TEST_AND_RETURN_FALSE(success);
258 hash_tree_builder_.reset();
259 }
Sen Jiang57f91802017-11-14 17:42:13 -0800260 if (partition_->fec_size != 0) {
Kelvin Zhang91e839c2022-04-05 14:25:00 -0700261 LOG(INFO) << "Writing verity FEC to " << partition_->readonly_target_path;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400262 TEST_AND_RETURN_FALSE(EncodeFEC(read_fd,
263 write_fd,
264 partition_->fec_data_offset,
265 partition_->fec_data_size,
266 partition_->fec_offset,
267 partition_->fec_size,
268 partition_->fec_roots,
269 partition_->block_size,
270 false /* verify_mode */));
Sen Jiang57f91802017-11-14 17:42:13 -0800271 }
272 return true;
273}
274
Daniel Zhengbd16b012022-09-28 23:40:05 +0000275bool VerityWriterAndroid::IncrementalFinalize(FileDescriptor* read_fd,
276 FileDescriptor* write_fd) {
277 if (!hash_tree_written_) {
278 LOG(INFO) << "Completing prework in Finalize";
279 const auto hash_tree_data_end =
280 partition_->hash_tree_data_offset + partition_->hash_tree_data_size;
281 if (total_offset_ < hash_tree_data_end) {
282 LOG(ERROR) << "Read up to " << total_offset_
283 << " when we are expecting to read everything "
284 "before "
285 << hash_tree_data_end;
286 return false;
287 }
288 // All hash tree data blocks has been hashed, write hash tree to disk.
289 LOG(INFO) << "Writing verity hash tree to "
290 << partition_->readonly_target_path;
291 if (hash_tree_builder_) {
292 TEST_AND_RETURN_FALSE(hash_tree_builder_->BuildHashTree());
293 TEST_AND_RETURN_FALSE_ERRNO(
294 write_fd->Seek(partition_->hash_tree_offset, SEEK_SET));
295 auto success =
296 hash_tree_builder_->WriteHashTree([write_fd](auto data, auto size) {
297 return utils::WriteAll(write_fd, data, size);
298 });
299 // hashtree builder already prints error messages.
300 TEST_AND_RETURN_FALSE(success);
301 hash_tree_builder_.reset();
302 }
303 hash_tree_written_ = true;
304 if (partition_->fec_size != 0) {
305 LOG(INFO) << "Writing verity FEC to " << partition_->readonly_target_path;
306 }
307 }
308 if (partition_->fec_size != 0) {
309 TEST_AND_RETURN_FALSE(encodeFEC_.Compute(read_fd, write_fd));
310 }
311 return true;
312}
313bool VerityWriterAndroid::FECFinished() const {
314 if (encodeFEC_.Finished()) {
315 return true;
316 }
317 return false;
318}
Daniel Zheng3efb3f22022-12-19 22:14:20 +0000319
320double VerityWriterAndroid::GetProgress() {
321 return encodeFEC_.ReportProgress();
322}
323
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800324bool VerityWriterAndroid::EncodeFEC(FileDescriptor* read_fd,
325 FileDescriptor* write_fd,
Sen Jianga778e5b2018-09-13 11:42:56 -0700326 uint64_t data_offset,
327 uint64_t data_size,
328 uint64_t fec_offset,
329 uint64_t fec_size,
330 uint32_t fec_roots,
331 uint32_t block_size,
332 bool verify_mode) {
333 TEST_AND_RETURN_FALSE(data_size % block_size == 0);
334 TEST_AND_RETURN_FALSE(fec_roots >= 0 && fec_roots < FEC_RSM);
Daniel Zhengbd16b012022-09-28 23:40:05 +0000335 // This is the N in RS(M, N), which is the number of bytes for each rs
336 // block.
Sen Jianga778e5b2018-09-13 11:42:56 -0700337 size_t rs_n = FEC_RSM - fec_roots;
338 uint64_t rounds = utils::DivRoundUp(data_size / block_size, rs_n);
339 TEST_AND_RETURN_FALSE(rounds * fec_roots * block_size == fec_size);
340
341 std::unique_ptr<void, decltype(&free_rs_char)> rs_char(
342 init_rs_char(FEC_PARAMS(fec_roots)), &free_rs_char);
343 TEST_AND_RETURN_FALSE(rs_char != nullptr);
Kelvin Zhang9792f082022-01-26 16:48:24 -0800344 // Cache at most 1MB of fec data, in VABC, we need to re-open fd if we
345 // perform a read() operation after write(). So reduce the number of writes
346 // can save unnecessary re-opens.
347 UnownedCachedFileDescriptor cache_fd(write_fd, 1 * (1 << 20));
348 write_fd = &cache_fd;
Sen Jianga778e5b2018-09-13 11:42:56 -0700349
Sen Jianga778e5b2018-09-13 11:42:56 -0700350 for (size_t i = 0; i < rounds; i++) {
351 // Encodes |block_size| number of rs blocks each round so that we can read
352 // one block each time instead of 1 byte to increase random read
353 // performance. This uses about 1 MiB memory for 4K block size.
354 brillo::Blob rs_blocks(block_size * rs_n);
355 for (size_t j = 0; j < rs_n; j++) {
356 brillo::Blob buffer(block_size, 0);
357 uint64_t offset =
358 fec_ecc_interleave(i * rs_n * block_size + j, rs_n, rounds);
359 // Don't read past |data_size|, treat them as 0.
360 if (offset < data_size) {
361 ssize_t bytes_read = 0;
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500362 TEST_AND_RETURN_FALSE(utils::PReadAll(read_fd,
Sen Jianga778e5b2018-09-13 11:42:56 -0700363 buffer.data(),
364 buffer.size(),
365 data_offset + offset,
366 &bytes_read));
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500367 TEST_AND_RETURN_FALSE(bytes_read >= 0);
368 TEST_AND_RETURN_FALSE(static_cast<size_t>(bytes_read) == buffer.size());
Sen Jianga778e5b2018-09-13 11:42:56 -0700369 }
370 for (size_t k = 0; k < buffer.size(); k++) {
371 rs_blocks[k * rs_n + j] = buffer[k];
372 }
373 }
374 brillo::Blob fec(block_size * fec_roots);
375 for (size_t j = 0; j < block_size; j++) {
Daniel Zhengbd16b012022-09-28 23:40:05 +0000376 // Encode [j * rs_n : (j + 1) * rs_n) in |rs_blocks| and write
377 // |fec_roots| number of parity bytes to |j * fec_roots| in |fec|.
Sen Jianga778e5b2018-09-13 11:42:56 -0700378 encode_rs_char(rs_char.get(),
379 rs_blocks.data() + j * rs_n,
380 fec.data() + j * fec_roots);
381 }
382
383 if (verify_mode) {
384 brillo::Blob fec_read(fec.size());
385 ssize_t bytes_read = 0;
386 TEST_AND_RETURN_FALSE(utils::PReadAll(
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500387 read_fd, fec_read.data(), fec_read.size(), fec_offset, &bytes_read));
388 TEST_AND_RETURN_FALSE(bytes_read >= 0);
389 TEST_AND_RETURN_FALSE(static_cast<size_t>(bytes_read) == fec_read.size());
Sen Jianga778e5b2018-09-13 11:42:56 -0700390 TEST_AND_RETURN_FALSE(fec == fec_read);
391 } else {
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500392 CHECK(write_fd);
Kelvin Zhang7f925672021-03-15 13:37:40 -0400393 write_fd->Seek(fec_offset, SEEK_SET);
394 if (!utils::WriteAll(write_fd, fec.data(), fec.size())) {
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500395 PLOG(ERROR) << "EncodeFEC write() failed";
396 return false;
397 }
Sen Jianga778e5b2018-09-13 11:42:56 -0700398 }
399 fec_offset += fec.size();
400 }
Kelvin Zhang7f925672021-03-15 13:37:40 -0400401 write_fd->Flush();
Sen Jianga778e5b2018-09-13 11:42:56 -0700402 return true;
403}
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500404
405bool VerityWriterAndroid::EncodeFEC(const std::string& path,
406 uint64_t data_offset,
407 uint64_t data_size,
408 uint64_t fec_offset,
409 uint64_t fec_size,
410 uint32_t fec_roots,
411 uint32_t block_size,
412 bool verify_mode) {
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800413 EintrSafeFileDescriptor fd;
414 TEST_AND_RETURN_FALSE(fd.Open(path.c_str(), verify_mode ? O_RDONLY : O_RDWR));
415 return EncodeFEC(&fd,
416 &fd,
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500417 data_offset,
418 data_size,
419 fec_offset,
420 fec_size,
421 fec_roots,
422 block_size,
423 verify_mode);
424}
Sen Jiang57f91802017-11-14 17:42:13 -0800425} // namespace chromeos_update_engine