blob: 01d8977cc01c2d22d850186ac93fe6e47890fbda [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 }
70 return true;
71}
72
73bool VerityWriterAndroid::Update(uint64_t offset,
74 const uint8_t* buffer,
75 size_t size) {
76 if (partition_->hash_tree_size != 0) {
Kelvin Zhang7f925672021-03-15 13:37:40 -040077 const uint64_t hash_tree_data_end =
Sen Jiang57f91802017-11-14 17:42:13 -080078 partition_->hash_tree_data_offset + partition_->hash_tree_data_size;
Kelvin Zhang7f925672021-03-15 13:37:40 -040079 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 Jiang57f91802017-11-14 17:42:13 -080089 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 Zhang7f925672021-03-15 13:37:40 -040094 LOG(INFO)
95 << "Read everything before hash tree. Ready to write hash tree.";
Sen Jiang57f91802017-11-14 17:42:13 -080096 }
97 }
98 }
Kelvin Zhang7f925672021-03-15 13:37:40 -040099
100 return true;
101}
102
103bool 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 Jiang57f91802017-11-14 17:42:13 -0800117 if (partition_->fec_size != 0) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400118 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 Jiang57f91802017-11-14 17:42:13 -0800128 }
129 return true;
130}
131
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500132bool VerityWriterAndroid::EncodeFEC(FileDescriptorPtr read_fd,
133 FileDescriptorPtr write_fd,
Sen Jianga778e5b2018-09-13 11:42:56 -0700134 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 Zhang7f925672021-03-15 13:37:40 -0400152 // 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 Jianga778e5b2018-09-13 11:42:56 -0700156 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 Zhangb138ab52020-11-06 15:56:41 -0500168 TEST_AND_RETURN_FALSE(utils::PReadAll(read_fd,
Sen Jianga778e5b2018-09-13 11:42:56 -0700169 buffer.data(),
170 buffer.size(),
171 data_offset + offset,
172 &bytes_read));
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500173 TEST_AND_RETURN_FALSE(bytes_read >= 0);
174 TEST_AND_RETURN_FALSE(static_cast<size_t>(bytes_read) == buffer.size());
Sen Jianga778e5b2018-09-13 11:42:56 -0700175 }
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 Zhangb138ab52020-11-06 15:56:41 -0500193 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 Jianga778e5b2018-09-13 11:42:56 -0700196 TEST_AND_RETURN_FALSE(fec == fec_read);
197 } else {
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500198 CHECK(write_fd);
Kelvin Zhang7f925672021-03-15 13:37:40 -0400199 write_fd->Seek(fec_offset, SEEK_SET);
200 if (!utils::WriteAll(write_fd, fec.data(), fec.size())) {
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500201 PLOG(ERROR) << "EncodeFEC write() failed";
202 return false;
203 }
Sen Jianga778e5b2018-09-13 11:42:56 -0700204 }
205 fec_offset += fec.size();
206 }
Kelvin Zhang7f925672021-03-15 13:37:40 -0400207 write_fd->Flush();
Sen Jianga778e5b2018-09-13 11:42:56 -0700208 return true;
209}
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500210
211bool 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 Jiang57f91802017-11-14 17:42:13 -0800232} // namespace chromeos_update_engine