blob: b669b4dea964b6f4ee40632d8c0220583df3cd27 [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>
23
24#include <base/logging.h>
25#include <base/posix/eintr_wrapper.h>
Sen Jianga778e5b2018-09-13 11:42:56 -070026#include <fec/ecc.h>
27extern "C" {
28#include <fec.h>
29}
Sen Jiang57f91802017-11-14 17:42:13 -080030
31#include "update_engine/common/utils.h"
Kelvin Zhang7f925672021-03-15 13:37:40 -040032#include "update_engine/payload_consumer/cached_file_descriptor.h"
Kelvin Zhangb138ab52020-11-06 15:56:41 -050033#include "update_engine/payload_consumer/file_descriptor.h"
Sen Jiang57f91802017-11-14 17:42:13 -080034
35namespace chromeos_update_engine {
36
37namespace verity_writer {
38std::unique_ptr<VerityWriterInterface> CreateVerityWriter() {
39 return std::make_unique<VerityWriterAndroid>();
40}
41} // namespace verity_writer
42
43bool VerityWriterAndroid::Init(const InstallPlan::Partition& partition) {
44 partition_ = &partition;
45
46 if (partition_->hash_tree_size != 0) {
47 auto hash_function =
48 HashTreeBuilder::HashFunction(partition_->hash_tree_algorithm);
49 if (hash_function == nullptr) {
50 LOG(ERROR) << "Verity hash algorithm not supported: "
51 << partition_->hash_tree_algorithm;
52 return false;
53 }
54 hash_tree_builder_ = std::make_unique<HashTreeBuilder>(
55 partition_->block_size, hash_function);
56 TEST_AND_RETURN_FALSE(hash_tree_builder_->Initialize(
57 partition_->hash_tree_data_size, partition_->hash_tree_salt));
58 if (hash_tree_builder_->CalculateSize(partition_->hash_tree_data_size) !=
59 partition_->hash_tree_size) {
60 LOG(ERROR) << "Verity hash tree size does not match, stored: "
61 << partition_->hash_tree_size << ", calculated: "
62 << hash_tree_builder_->CalculateSize(
63 partition_->hash_tree_data_size);
64 return false;
65 }
66 }
Kelvin Zhangeeec3812021-03-16 14:01:47 -040067 total_offset_ = 0;
Sen Jiang57f91802017-11-14 17:42:13 -080068 return true;
69}
70
Kelvin Zhangeeec3812021-03-16 14:01:47 -040071bool VerityWriterAndroid::Update(const uint64_t offset,
Sen Jiang57f91802017-11-14 17:42:13 -080072 const uint8_t* buffer,
73 size_t size) {
Kelvin Zhangeeec3812021-03-16 14:01:47 -040074 if (offset != total_offset_) {
75 LOG(ERROR) << "Sequential read expected, expected to read at: "
76 << total_offset_ << " actual read occurs at: " << offset;
77 return false;
78 }
Sen Jiang57f91802017-11-14 17:42:13 -080079 if (partition_->hash_tree_size != 0) {
Kelvin Zhang7f925672021-03-15 13:37:40 -040080 const uint64_t hash_tree_data_end =
Sen Jiang57f91802017-11-14 17:42:13 -080081 partition_->hash_tree_data_offset + partition_->hash_tree_data_size;
Kelvin Zhang7f925672021-03-15 13:37:40 -040082 const uint64_t start_offset =
83 std::max(offset, partition_->hash_tree_data_offset);
84 if (offset + size > hash_tree_data_end) {
85 LOG(WARNING)
86 << "Reading past hash_tree_data_end, something is probably "
87 "wrong, might cause incorrect hash of partitions. offset: "
88 << offset << " size: " << size
89 << " hash_tree_data_end: " << hash_tree_data_end;
90 }
91 const uint64_t end_offset = std::min(offset + size, hash_tree_data_end);
Sen Jiang57f91802017-11-14 17:42:13 -080092 if (start_offset < end_offset) {
93 TEST_AND_RETURN_FALSE(hash_tree_builder_->Update(
94 buffer + start_offset - offset, end_offset - start_offset));
95
96 if (end_offset == hash_tree_data_end) {
Kelvin Zhang7f925672021-03-15 13:37:40 -040097 LOG(INFO)
98 << "Read everything before hash tree. Ready to write hash tree.";
Sen Jiang57f91802017-11-14 17:42:13 -080099 }
100 }
101 }
Kelvin Zhangeeec3812021-03-16 14:01:47 -0400102 total_offset_ += size;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400103
104 return true;
105}
106
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800107bool VerityWriterAndroid::Finalize(FileDescriptor* read_fd,
108 FileDescriptor* write_fd) {
Kelvin Zhangeeec3812021-03-16 14:01:47 -0400109 const auto hash_tree_data_end =
110 partition_->hash_tree_data_offset + partition_->hash_tree_data_size;
111 if (total_offset_ < hash_tree_data_end) {
112 LOG(ERROR) << "Read up to " << total_offset_
113 << " when we are expecting to read everything "
114 "before "
115 << hash_tree_data_end;
116 return false;
117 }
Kelvin Zhang7f925672021-03-15 13:37:40 -0400118 // All hash tree data blocks has been hashed, write hash tree to disk.
119 LOG(INFO) << "Writing verity hash tree to " << partition_->target_path;
120 TEST_AND_RETURN_FALSE(hash_tree_builder_->BuildHashTree());
121 TEST_AND_RETURN_FALSE_ERRNO(
122 write_fd->Seek(partition_->hash_tree_offset, SEEK_SET));
123 auto success =
124 hash_tree_builder_->WriteHashTree([write_fd](auto data, auto size) {
125 return utils::WriteAll(write_fd, data, size);
126 });
127 // hashtree builder already prints error messages.
128 TEST_AND_RETURN_FALSE(success);
129 hash_tree_builder_.reset();
Sen Jiang57f91802017-11-14 17:42:13 -0800130 if (partition_->fec_size != 0) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400131 LOG(INFO) << "Writing verity FEC to " << partition_->target_path;
132 TEST_AND_RETURN_FALSE(EncodeFEC(read_fd,
133 write_fd,
134 partition_->fec_data_offset,
135 partition_->fec_data_size,
136 partition_->fec_offset,
137 partition_->fec_size,
138 partition_->fec_roots,
139 partition_->block_size,
140 false /* verify_mode */));
Sen Jiang57f91802017-11-14 17:42:13 -0800141 }
142 return true;
143}
144
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800145bool VerityWriterAndroid::EncodeFEC(FileDescriptor* read_fd,
146 FileDescriptor* write_fd,
Sen Jianga778e5b2018-09-13 11:42:56 -0700147 uint64_t data_offset,
148 uint64_t data_size,
149 uint64_t fec_offset,
150 uint64_t fec_size,
151 uint32_t fec_roots,
152 uint32_t block_size,
153 bool verify_mode) {
154 TEST_AND_RETURN_FALSE(data_size % block_size == 0);
155 TEST_AND_RETURN_FALSE(fec_roots >= 0 && fec_roots < FEC_RSM);
156 // This is the N in RS(M, N), which is the number of bytes for each rs block.
157 size_t rs_n = FEC_RSM - fec_roots;
158 uint64_t rounds = utils::DivRoundUp(data_size / block_size, rs_n);
159 TEST_AND_RETURN_FALSE(rounds * fec_roots * block_size == fec_size);
160
161 std::unique_ptr<void, decltype(&free_rs_char)> rs_char(
162 init_rs_char(FEC_PARAMS(fec_roots)), &free_rs_char);
163 TEST_AND_RETURN_FALSE(rs_char != nullptr);
Kelvin Zhang9792f082022-01-26 16:48:24 -0800164 // Cache at most 1MB of fec data, in VABC, we need to re-open fd if we
165 // perform a read() operation after write(). So reduce the number of writes
166 // can save unnecessary re-opens.
167 UnownedCachedFileDescriptor cache_fd(write_fd, 1 * (1 << 20));
168 write_fd = &cache_fd;
Sen Jianga778e5b2018-09-13 11:42:56 -0700169
Sen Jianga778e5b2018-09-13 11:42:56 -0700170 for (size_t i = 0; i < rounds; i++) {
171 // Encodes |block_size| number of rs blocks each round so that we can read
172 // one block each time instead of 1 byte to increase random read
173 // performance. This uses about 1 MiB memory for 4K block size.
174 brillo::Blob rs_blocks(block_size * rs_n);
175 for (size_t j = 0; j < rs_n; j++) {
176 brillo::Blob buffer(block_size, 0);
177 uint64_t offset =
178 fec_ecc_interleave(i * rs_n * block_size + j, rs_n, rounds);
179 // Don't read past |data_size|, treat them as 0.
180 if (offset < data_size) {
181 ssize_t bytes_read = 0;
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500182 TEST_AND_RETURN_FALSE(utils::PReadAll(read_fd,
Sen Jianga778e5b2018-09-13 11:42:56 -0700183 buffer.data(),
184 buffer.size(),
185 data_offset + offset,
186 &bytes_read));
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500187 TEST_AND_RETURN_FALSE(bytes_read >= 0);
188 TEST_AND_RETURN_FALSE(static_cast<size_t>(bytes_read) == buffer.size());
Sen Jianga778e5b2018-09-13 11:42:56 -0700189 }
190 for (size_t k = 0; k < buffer.size(); k++) {
191 rs_blocks[k * rs_n + j] = buffer[k];
192 }
193 }
194 brillo::Blob fec(block_size * fec_roots);
195 for (size_t j = 0; j < block_size; j++) {
196 // Encode [j * rs_n : (j + 1) * rs_n) in |rs_blocks| and write |fec_roots|
197 // number of parity bytes to |j * fec_roots| in |fec|.
198 encode_rs_char(rs_char.get(),
199 rs_blocks.data() + j * rs_n,
200 fec.data() + j * fec_roots);
201 }
202
203 if (verify_mode) {
204 brillo::Blob fec_read(fec.size());
205 ssize_t bytes_read = 0;
206 TEST_AND_RETURN_FALSE(utils::PReadAll(
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500207 read_fd, fec_read.data(), fec_read.size(), fec_offset, &bytes_read));
208 TEST_AND_RETURN_FALSE(bytes_read >= 0);
209 TEST_AND_RETURN_FALSE(static_cast<size_t>(bytes_read) == fec_read.size());
Sen Jianga778e5b2018-09-13 11:42:56 -0700210 TEST_AND_RETURN_FALSE(fec == fec_read);
211 } else {
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500212 CHECK(write_fd);
Kelvin Zhang7f925672021-03-15 13:37:40 -0400213 write_fd->Seek(fec_offset, SEEK_SET);
214 if (!utils::WriteAll(write_fd, fec.data(), fec.size())) {
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500215 PLOG(ERROR) << "EncodeFEC write() failed";
216 return false;
217 }
Sen Jianga778e5b2018-09-13 11:42:56 -0700218 }
219 fec_offset += fec.size();
220 }
Kelvin Zhang7f925672021-03-15 13:37:40 -0400221 write_fd->Flush();
Sen Jianga778e5b2018-09-13 11:42:56 -0700222 return true;
223}
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500224
225bool VerityWriterAndroid::EncodeFEC(const std::string& path,
226 uint64_t data_offset,
227 uint64_t data_size,
228 uint64_t fec_offset,
229 uint64_t fec_size,
230 uint32_t fec_roots,
231 uint32_t block_size,
232 bool verify_mode) {
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800233 EintrSafeFileDescriptor fd;
234 TEST_AND_RETURN_FALSE(fd.Open(path.c_str(), verify_mode ? O_RDONLY : O_RDWR));
235 return EncodeFEC(&fd,
236 &fd,
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500237 data_offset,
238 data_size,
239 fec_offset,
240 fec_size,
241 fec_roots,
242 block_size,
243 verify_mode);
244}
Sen Jiang57f91802017-11-14 17:42:13 -0800245} // namespace chromeos_update_engine