blob: 4b23b831c7c9cd84bcb15498636fa752bbdc003f [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
xunchang32e82042019-04-25 10:37:36 -070046 if (partition_->hash_tree_size != 0 || partition_->fec_size != 0) {
47 utils::SetBlockDeviceReadOnly(partition_->target_path, false);
48 }
Sen Jiang57f91802017-11-14 17:42:13 -080049 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 }
Kelvin Zhangeeec3812021-03-16 14:01:47 -040070 total_offset_ = 0;
Sen Jiang57f91802017-11-14 17:42:13 -080071 return true;
72}
73
Kelvin Zhangeeec3812021-03-16 14:01:47 -040074bool VerityWriterAndroid::Update(const uint64_t offset,
Sen Jiang57f91802017-11-14 17:42:13 -080075 const uint8_t* buffer,
76 size_t size) {
Kelvin Zhangeeec3812021-03-16 14:01:47 -040077 if (offset != total_offset_) {
78 LOG(ERROR) << "Sequential read expected, expected to read at: "
79 << total_offset_ << " actual read occurs at: " << offset;
80 return false;
81 }
Sen Jiang57f91802017-11-14 17:42:13 -080082 if (partition_->hash_tree_size != 0) {
Kelvin Zhang7f925672021-03-15 13:37:40 -040083 const uint64_t hash_tree_data_end =
Sen Jiang57f91802017-11-14 17:42:13 -080084 partition_->hash_tree_data_offset + partition_->hash_tree_data_size;
Kelvin Zhang7f925672021-03-15 13:37:40 -040085 const uint64_t start_offset =
86 std::max(offset, partition_->hash_tree_data_offset);
87 if (offset + size > hash_tree_data_end) {
88 LOG(WARNING)
89 << "Reading past hash_tree_data_end, something is probably "
90 "wrong, might cause incorrect hash of partitions. offset: "
91 << offset << " size: " << size
92 << " hash_tree_data_end: " << hash_tree_data_end;
93 }
94 const uint64_t end_offset = std::min(offset + size, hash_tree_data_end);
Sen Jiang57f91802017-11-14 17:42:13 -080095 if (start_offset < end_offset) {
96 TEST_AND_RETURN_FALSE(hash_tree_builder_->Update(
97 buffer + start_offset - offset, end_offset - start_offset));
98
99 if (end_offset == hash_tree_data_end) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400100 LOG(INFO)
101 << "Read everything before hash tree. Ready to write hash tree.";
Sen Jiang57f91802017-11-14 17:42:13 -0800102 }
103 }
104 }
Kelvin Zhangeeec3812021-03-16 14:01:47 -0400105 total_offset_ += size;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400106
107 return true;
108}
109
110bool VerityWriterAndroid::Finalize(FileDescriptorPtr read_fd,
111 FileDescriptorPtr write_fd) {
Kelvin Zhangeeec3812021-03-16 14:01:47 -0400112 const auto hash_tree_data_end =
113 partition_->hash_tree_data_offset + partition_->hash_tree_data_size;
114 if (total_offset_ < hash_tree_data_end) {
115 LOG(ERROR) << "Read up to " << total_offset_
116 << " when we are expecting to read everything "
117 "before "
118 << hash_tree_data_end;
119 return false;
120 }
Kelvin Zhang7f925672021-03-15 13:37:40 -0400121 // All hash tree data blocks has been hashed, write hash tree to disk.
122 LOG(INFO) << "Writing verity hash tree to " << partition_->target_path;
123 TEST_AND_RETURN_FALSE(hash_tree_builder_->BuildHashTree());
124 TEST_AND_RETURN_FALSE_ERRNO(
125 write_fd->Seek(partition_->hash_tree_offset, SEEK_SET));
126 auto success =
127 hash_tree_builder_->WriteHashTree([write_fd](auto data, auto size) {
128 return utils::WriteAll(write_fd, data, size);
129 });
130 // hashtree builder already prints error messages.
131 TEST_AND_RETURN_FALSE(success);
132 hash_tree_builder_.reset();
Sen Jiang57f91802017-11-14 17:42:13 -0800133 if (partition_->fec_size != 0) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400134 LOG(INFO) << "Writing verity FEC to " << partition_->target_path;
135 TEST_AND_RETURN_FALSE(EncodeFEC(read_fd,
136 write_fd,
137 partition_->fec_data_offset,
138 partition_->fec_data_size,
139 partition_->fec_offset,
140 partition_->fec_size,
141 partition_->fec_roots,
142 partition_->block_size,
143 false /* verify_mode */));
Sen Jiang57f91802017-11-14 17:42:13 -0800144 }
145 return true;
146}
147
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500148bool VerityWriterAndroid::EncodeFEC(FileDescriptorPtr read_fd,
149 FileDescriptorPtr write_fd,
Sen Jianga778e5b2018-09-13 11:42:56 -0700150 uint64_t data_offset,
151 uint64_t data_size,
152 uint64_t fec_offset,
153 uint64_t fec_size,
154 uint32_t fec_roots,
155 uint32_t block_size,
156 bool verify_mode) {
157 TEST_AND_RETURN_FALSE(data_size % block_size == 0);
158 TEST_AND_RETURN_FALSE(fec_roots >= 0 && fec_roots < FEC_RSM);
159 // This is the N in RS(M, N), which is the number of bytes for each rs block.
160 size_t rs_n = FEC_RSM - fec_roots;
161 uint64_t rounds = utils::DivRoundUp(data_size / block_size, rs_n);
162 TEST_AND_RETURN_FALSE(rounds * fec_roots * block_size == fec_size);
163
164 std::unique_ptr<void, decltype(&free_rs_char)> rs_char(
165 init_rs_char(FEC_PARAMS(fec_roots)), &free_rs_char);
166 TEST_AND_RETURN_FALSE(rs_char != nullptr);
167
Kelvin Zhang7f925672021-03-15 13:37:40 -0400168 // Cache at most 1MB of fec data, in VABC, we need to re-open fd if we
169 // perform a read() operation after write(). So reduce the number of writes
170 // can save unnecessary re-opens.
171 write_fd = std::make_shared<CachedFileDescriptor>(write_fd, 1 * (1 << 20));
Sen Jianga778e5b2018-09-13 11:42:56 -0700172 for (size_t i = 0; i < rounds; i++) {
173 // Encodes |block_size| number of rs blocks each round so that we can read
174 // one block each time instead of 1 byte to increase random read
175 // performance. This uses about 1 MiB memory for 4K block size.
176 brillo::Blob rs_blocks(block_size * rs_n);
177 for (size_t j = 0; j < rs_n; j++) {
178 brillo::Blob buffer(block_size, 0);
179 uint64_t offset =
180 fec_ecc_interleave(i * rs_n * block_size + j, rs_n, rounds);
181 // Don't read past |data_size|, treat them as 0.
182 if (offset < data_size) {
183 ssize_t bytes_read = 0;
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500184 TEST_AND_RETURN_FALSE(utils::PReadAll(read_fd,
Sen Jianga778e5b2018-09-13 11:42:56 -0700185 buffer.data(),
186 buffer.size(),
187 data_offset + offset,
188 &bytes_read));
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500189 TEST_AND_RETURN_FALSE(bytes_read >= 0);
190 TEST_AND_RETURN_FALSE(static_cast<size_t>(bytes_read) == buffer.size());
Sen Jianga778e5b2018-09-13 11:42:56 -0700191 }
192 for (size_t k = 0; k < buffer.size(); k++) {
193 rs_blocks[k * rs_n + j] = buffer[k];
194 }
195 }
196 brillo::Blob fec(block_size * fec_roots);
197 for (size_t j = 0; j < block_size; j++) {
198 // Encode [j * rs_n : (j + 1) * rs_n) in |rs_blocks| and write |fec_roots|
199 // number of parity bytes to |j * fec_roots| in |fec|.
200 encode_rs_char(rs_char.get(),
201 rs_blocks.data() + j * rs_n,
202 fec.data() + j * fec_roots);
203 }
204
205 if (verify_mode) {
206 brillo::Blob fec_read(fec.size());
207 ssize_t bytes_read = 0;
208 TEST_AND_RETURN_FALSE(utils::PReadAll(
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500209 read_fd, fec_read.data(), fec_read.size(), fec_offset, &bytes_read));
210 TEST_AND_RETURN_FALSE(bytes_read >= 0);
211 TEST_AND_RETURN_FALSE(static_cast<size_t>(bytes_read) == fec_read.size());
Sen Jianga778e5b2018-09-13 11:42:56 -0700212 TEST_AND_RETURN_FALSE(fec == fec_read);
213 } else {
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500214 CHECK(write_fd);
Kelvin Zhang7f925672021-03-15 13:37:40 -0400215 write_fd->Seek(fec_offset, SEEK_SET);
216 if (!utils::WriteAll(write_fd, fec.data(), fec.size())) {
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500217 PLOG(ERROR) << "EncodeFEC write() failed";
218 return false;
219 }
Sen Jianga778e5b2018-09-13 11:42:56 -0700220 }
221 fec_offset += fec.size();
222 }
Kelvin Zhang7f925672021-03-15 13:37:40 -0400223 write_fd->Flush();
Sen Jianga778e5b2018-09-13 11:42:56 -0700224 return true;
225}
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500226
227bool VerityWriterAndroid::EncodeFEC(const std::string& path,
228 uint64_t data_offset,
229 uint64_t data_size,
230 uint64_t fec_offset,
231 uint64_t fec_size,
232 uint32_t fec_roots,
233 uint32_t block_size,
234 bool verify_mode) {
235 FileDescriptorPtr fd(new EintrSafeFileDescriptor());
236 TEST_AND_RETURN_FALSE(
237 fd->Open(path.c_str(), verify_mode ? O_RDONLY : O_RDWR));
238 return EncodeFEC(fd,
239 fd,
240 data_offset,
241 data_size,
242 fec_offset,
243 fec_size,
244 fec_roots,
245 block_size,
246 verify_mode);
247}
Sen Jiang57f91802017-11-14 17:42:13 -0800248} // namespace chromeos_update_engine