blob: 3ceeb70548434bb473776fc638fa0897ac7ce0e2 [file] [log] [blame]
Greg Kerr019d59c2016-11-17 14:28:49 -08001// Copyright 2016 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Chan045849f2017-12-18 17:27:07 -08005#include "imageloader/component.h"
Greg Kerr019d59c2016-11-17 14:28:49 -08006
7#include <fcntl.h>
8
9#include <algorithm>
10#include <string>
Eric Caruso089bbff2017-03-21 11:34:15 -070011#include <utility>
Greg Kerr019d59c2016-11-17 14:28:49 -080012#include <vector>
13
14#include <base/files/file.h>
Eric Caruso089bbff2017-03-21 11:34:15 -070015#include <base/files/file_enumerator.h>
Greg Kerr019d59c2016-11-17 14:28:49 -080016#include <base/files/file_path.h>
17#include <base/files/file_util.h>
18#include <base/files/scoped_file.h>
19#include <base/json/json_string_value_serializer.h>
20#include <base/logging.h>
21#include <base/numerics/safe_conversions.h>
Greg Kerr019d59c2016-11-17 14:28:49 -080022#include <base/posix/eintr_wrapper.h>
Eric Caruso089bbff2017-03-21 11:34:15 -070023#include <base/strings/string_number_conversions.h>
Greg Kerr019d59c2016-11-17 14:28:49 -080024#include <base/strings/string_util.h>
25#include <crypto/secure_hash.h>
26#include <crypto/sha2.h>
27#include <crypto/signature_verifier.h>
28
Greg Kerr09f06de2018-02-16 15:32:07 -080029#include "imageloader/helper_process_proxy.h"
Greg Kerr9944e242017-01-26 15:09:31 -080030
Greg Kerr019d59c2016-11-17 14:28:49 -080031namespace imageloader {
32
33namespace {
34
35// The name of the imageloader manifest file.
36constexpr char kManifestName[] = "imageloader.json";
37// The name of the fingerprint file.
38constexpr char kFingerprintName[] = "manifest.fingerprint";
39// The manifest signature.
Eric Caruso0b79bc82017-03-21 13:44:34 -070040constexpr char kManifestSignatureNamePattern[] = "imageloader.sig.[1-2]";
Xiaochu Liuc2264342017-08-14 16:37:42 -070041// The name of the image file (squashfs).
42constexpr char kImageFileNameSquashFS[] = "image.squash";
43// The name of the image file (ext4).
44constexpr char kImageFileNameExt4[] = "image.ext4";
Greg Kerr019d59c2016-11-17 14:28:49 -080045// The name of the table file.
46constexpr char kTableFileName[] = "table";
Greg Kerr019d59c2016-11-17 14:28:49 -080047
48base::FilePath GetManifestPath(const base::FilePath& component_dir) {
49 return component_dir.Append(kManifestName);
50}
51
Eric Caruso089bbff2017-03-21 11:34:15 -070052bool GetSignaturePath(const base::FilePath& component_dir,
53 base::FilePath* signature_path,
Eric Caruso9588e642017-04-07 15:18:45 -070054 size_t* key_number) {
Eric Caruso089bbff2017-03-21 11:34:15 -070055 DCHECK(signature_path);
56 DCHECK(key_number);
57
Greg Kerr09f06de2018-02-16 15:32:07 -080058 base::FileEnumerator files(component_dir, false,
Eric Caruso089bbff2017-03-21 11:34:15 -070059 base::FileEnumerator::FileType::FILES,
60 kManifestSignatureNamePattern);
61 for (base::FilePath path = files.Next(); !path.empty(); path = files.Next()) {
62 // Extract the key number.
63 std::string key_ext = path.FinalExtension();
64 if (key_ext.empty())
65 continue;
66
Eric Caruso9588e642017-04-07 15:18:45 -070067 size_t ext_number;
68 if (!base::StringToSizeT(key_ext.substr(1), &ext_number))
Eric Caruso089bbff2017-03-21 11:34:15 -070069 continue;
70
71 *signature_path = path;
72 *key_number = ext_number;
73 return true;
74 }
75 return false;
76}
77
78base::FilePath GetSignaturePathForKey(const base::FilePath& component_dir,
Eric Caruso9588e642017-04-07 15:18:45 -070079 size_t key_number) {
Eric Caruso089bbff2017-03-21 11:34:15 -070080 std::string signature_name(kManifestSignatureNamePattern);
81 signature_name =
82 signature_name.substr(0, signature_name.find_last_of('.') + 1);
Hidehiko Abe0deb0542019-08-15 01:56:10 +090083 return component_dir.Append(signature_name +
84 base::NumberToString(key_number));
Greg Kerr019d59c2016-11-17 14:28:49 -080085}
86
87base::FilePath GetFingerprintPath(const base::FilePath& component_dir) {
88 return component_dir.Append(kFingerprintName);
89}
90
91base::FilePath GetTablePath(const base::FilePath& component_dir) {
92 return component_dir.Append(kTableFileName);
93}
94
Xiaochu Liuc2264342017-08-14 16:37:42 -070095base::FilePath GetImagePath(const base::FilePath& component_dir,
Xiaochu Liue61e1d62018-11-12 13:20:09 -080096 FileSystem fs_type) {
97 if (fs_type == FileSystem::kExt4) {
Xiaochu Liuc2264342017-08-14 16:37:42 -070098 return component_dir.Append(kImageFileNameExt4);
Xiaochu Liue61e1d62018-11-12 13:20:09 -080099 } else if (fs_type == FileSystem::kSquashFS) {
Xiaochu Liuc2264342017-08-14 16:37:42 -0700100 return component_dir.Append(kImageFileNameSquashFS);
Greg Kerr09f06de2018-02-16 15:32:07 -0800101 } else {
Xiaochu Liuc2264342017-08-14 16:37:42 -0700102 NOTREACHED();
103 return base::FilePath();
104 }
Greg Kerr019d59c2016-11-17 14:28:49 -0800105}
106
107bool WriteFileToDisk(const base::FilePath& path, const std::string& contents) {
Greg Kerr09f06de2018-02-16 15:32:07 -0800108 base::ScopedFD fd(HANDLE_EINTR(open(
109 path.value().c_str(), O_CREAT | O_WRONLY | O_EXCL, kComponentFilePerms)));
Greg Kerr019d59c2016-11-17 14:28:49 -0800110 if (!fd.is_valid()) {
111 PLOG(ERROR) << "Error creating file for " << path.value();
112 return false;
113 }
114
115 base::File file(fd.release());
116 int size = base::checked_cast<int>(contents.size());
117 return file.Write(0, contents.data(), contents.size()) == size;
118}
119
Greg Kerr019d59c2016-11-17 14:28:49 -0800120bool GetAndVerifyTable(const base::FilePath& path,
121 const std::vector<uint8_t>& hash,
122 std::string* out_table) {
123 std::string table;
124 if (!base::ReadFileToStringWithMaxSize(path, &table, kMaximumFilesize)) {
125 return false;
126 }
127
128 std::vector<uint8_t> table_hash(crypto::kSHA256Length);
129 crypto::SHA256HashString(table, table_hash.data(), table_hash.size());
130 if (table_hash != hash) {
131 LOG(ERROR) << "dm-verity table file has the wrong hash.";
132 return false;
133 }
134
135 out_table->assign(table);
136 return true;
137}
138
139} // namespace
140
Eric Caruso089bbff2017-03-21 11:34:15 -0700141Component::Component(const base::FilePath& component_dir, int key_number)
142 : component_dir_(component_dir), key_number_(key_number) {}
Greg Kerr019d59c2016-11-17 14:28:49 -0800143
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700144std::unique_ptr<Component> Component::Create(
Greg Kerr09f06de2018-02-16 15:32:07 -0800145 const base::FilePath& component_dir, const Keys& public_keys) {
Eric Caruso089bbff2017-03-21 11:34:15 -0700146 base::FilePath signature_path;
Eric Caruso9588e642017-04-07 15:18:45 -0700147 size_t key_number;
Eric Caruso089bbff2017-03-21 11:34:15 -0700148 if (!GetSignaturePath(component_dir, &signature_path, &key_number)) {
149 LOG(ERROR) << "Could not find manifest signature";
150 return nullptr;
151 }
Eric Caruso0b79bc82017-03-21 13:44:34 -0700152 if (key_number < 1 || key_number > public_keys.size()) {
153 LOG(ERROR) << "Invalid key number";
154 return nullptr;
155 }
Eric Caruso089bbff2017-03-21 11:34:15 -0700156
157 std::unique_ptr<Component> component(
158 new Component(component_dir, key_number));
Eric Caruso0b79bc82017-03-21 13:44:34 -0700159 if (!component->LoadManifest(public_keys[key_number - 1]))
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700160 return nullptr;
161 return component;
Greg Kerr019d59c2016-11-17 14:28:49 -0800162}
163
Xiaochu Liue61e1d62018-11-12 13:20:09 -0800164const Manifest& Component::manifest() {
Greg Kerr019d59c2016-11-17 14:28:49 -0800165 return manifest_;
166}
167
Greg Kerr09f06de2018-02-16 15:32:07 -0800168bool Component::Mount(HelperProcessProxy* mounter,
169 const base::FilePath& dest_dir) {
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700170 // Read the table in and verify the hash.
Greg Kerr019d59c2016-11-17 14:28:49 -0800171 std::string table;
Xiaochu Liuc209aab2018-06-19 13:42:15 -0700172 if (!GetAndVerifyTable(GetTablePath(component_dir_), manifest_.table_sha256(),
Greg Kerr019d59c2016-11-17 14:28:49 -0800173 &table)) {
174 LOG(ERROR) << "Could not read and verify dm-verity table.";
175 return false;
176 }
177
Xiaochu Liuc209aab2018-06-19 13:42:15 -0700178 base::FilePath image_path(GetImagePath(component_dir_, manifest_.fs_type()));
Greg Kerr019d59c2016-11-17 14:28:49 -0800179 base::File image(image_path, base::File::FLAG_OPEN | base::File::FLAG_READ);
180 if (!image.IsValid()) {
181 LOG(ERROR) << "Could not open image file.";
182 return false;
183 }
184 base::ScopedFD image_fd(image.TakePlatformFile());
185
Xiaochu Liuc2264342017-08-14 16:37:42 -0700186 return mounter->SendMountCommand(image_fd.get(), dest_dir.value(),
Xiaochu Liuc209aab2018-06-19 13:42:15 -0700187 manifest_.fs_type(), table);
Greg Kerr019d59c2016-11-17 14:28:49 -0800188}
189
190bool Component::LoadManifest(const std::vector<uint8_t>& public_key) {
191 if (!base::ReadFileToStringWithMaxSize(GetManifestPath(component_dir_),
192 &manifest_raw_, kMaximumFilesize)) {
193 LOG(ERROR) << "Could not read manifest file.";
194 return false;
195 }
Eric Caruso089bbff2017-03-21 11:34:15 -0700196 if (!base::ReadFileToStringWithMaxSize(
Greg Kerr09f06de2018-02-16 15:32:07 -0800197 GetSignaturePathForKey(component_dir_, key_number_), &manifest_sig_,
198 kMaximumFilesize)) {
Greg Kerr019d59c2016-11-17 14:28:49 -0800199 LOG(ERROR) << "Could not read signature file.";
200 return false;
201 }
202
203 crypto::SignatureVerifier verifier;
204
205 if (!verifier.VerifyInit(
206 crypto::SignatureVerifier::ECDSA_SHA256,
207 reinterpret_cast<const uint8_t*>(manifest_sig_.data()),
208 base::checked_cast<int>(manifest_sig_.size()), public_key.data(),
209 base::checked_cast<int>(public_key.size()))) {
210 LOG(ERROR) << "Failed to initialize signature verification.";
211 return false;
212 }
213
214 verifier.VerifyUpdate(reinterpret_cast<const uint8_t*>(manifest_raw_.data()),
215 base::checked_cast<int>(manifest_raw_.size()));
216
217 if (!verifier.VerifyFinal()) {
218 LOG(ERROR) << "Manifest failed signature verification.";
219 return false;
220 }
Xiaochu Liuc209aab2018-06-19 13:42:15 -0700221 return manifest_.ParseManifest(manifest_raw_);
Greg Kerr019d59c2016-11-17 14:28:49 -0800222}
223
224bool Component::CopyTo(const base::FilePath& dest_dir) {
Greg Kerr019d59c2016-11-17 14:28:49 -0800225 if (!WriteFileToDisk(GetManifestPath(dest_dir), manifest_raw_) ||
Eric Caruso089bbff2017-03-21 11:34:15 -0700226 !WriteFileToDisk(GetSignaturePathForKey(dest_dir, key_number_),
Greg Kerr09f06de2018-02-16 15:32:07 -0800227 manifest_sig_)) {
Greg Kerr019d59c2016-11-17 14:28:49 -0800228 LOG(ERROR) << "Could not write manifest and signature to disk.";
229 return false;
230 }
231
232 base::FilePath table_src(GetTablePath(component_dir_));
233 base::FilePath table_dest(GetTablePath(dest_dir));
Xiaochu Liuc209aab2018-06-19 13:42:15 -0700234 if (!CopyComponentFile(table_src, table_dest, manifest_.table_sha256())) {
Greg Kerr019d59c2016-11-17 14:28:49 -0800235 LOG(ERROR) << "Could not copy table file.";
236 return false;
237 }
238
Xiaochu Liuc209aab2018-06-19 13:42:15 -0700239 base::FilePath image_src(GetImagePath(component_dir_, manifest_.fs_type()));
240 base::FilePath image_dest(GetImagePath(dest_dir, manifest_.fs_type()));
241 if (!CopyComponentFile(image_src, image_dest, manifest_.image_sha256())) {
Greg Kerr019d59c2016-11-17 14:28:49 -0800242 LOG(ERROR) << "Could not copy image file.";
243 return false;
244 }
245
246 if (!CopyFingerprintFile(component_dir_, dest_dir)) {
247 LOG(ERROR) << "Could not copy manifest.fingerprint file.";
248 return false;
249 }
250
251 return true;
252}
253
254bool Component::CopyComponentFile(const base::FilePath& src,
Eric Caruso355e37c2017-03-15 14:31:41 -0700255 const base::FilePath& dest_path,
256 const std::vector<uint8_t>& expected_hash) {
Greg Kerr019d59c2016-11-17 14:28:49 -0800257 base::File file(src, base::File::FLAG_OPEN | base::File::FLAG_READ);
Eric Caruso355e37c2017-03-15 14:31:41 -0700258 if (!file.IsValid())
259 return false;
Greg Kerr019d59c2016-11-17 14:28:49 -0800260
261 base::ScopedFD dest(
262 HANDLE_EINTR(open(dest_path.value().c_str(), O_CREAT | O_WRONLY | O_EXCL,
263 kComponentFilePerms)));
Eric Caruso355e37c2017-03-15 14:31:41 -0700264 if (!dest.is_valid())
265 return false;
Greg Kerr019d59c2016-11-17 14:28:49 -0800266
267 base::File out_file(dest.release());
268 std::unique_ptr<crypto::SecureHash> sha256(
269 crypto::SecureHash::Create(crypto::SecureHash::SHA256));
270
271 std::vector<uint8_t> file_hash(crypto::kSHA256Length);
272 if (!ReadHashAndCopyFile(&file, &file_hash, &out_file)) {
273 LOG(ERROR) << "Failed to read image file.";
274 return false;
275 }
276
277 if (expected_hash != file_hash) {
278 LOG(ERROR) << "Image is corrupt or modified.";
279 return false;
280 }
281 return true;
282}
283
284bool Component::ReadHashAndCopyFile(base::File* file,
285 std::vector<uint8_t>* file_hash,
286 base::File* out_file) {
287 std::unique_ptr<crypto::SecureHash> sha256(
288 crypto::SecureHash::Create(crypto::SecureHash::SHA256));
289 int size = file->GetLength();
Eric Caruso355e37c2017-03-15 14:31:41 -0700290 if (size <= 0)
291 return false;
Greg Kerr019d59c2016-11-17 14:28:49 -0800292
293 int rv = 0, bytes_read = 0;
294 char buf[4096];
295 do {
296 int remaining = size - bytes_read;
297 int bytes_to_read =
298 std::min(remaining, base::checked_cast<int>(sizeof(buf)));
299
300 rv = file->ReadAtCurrentPos(buf, bytes_to_read);
Greg Kerr09f06de2018-02-16 15:32:07 -0800301 if (rv <= 0)
302 break;
Greg Kerr019d59c2016-11-17 14:28:49 -0800303
304 bytes_read += rv;
305 sha256->Update(buf, rv);
306 if (out_file) {
307 out_file->WriteAtCurrentPos(buf, rv);
308 }
309 } while (bytes_read <= size);
310
311 sha256->Finish(file_hash->data(), file_hash->size());
312 return bytes_read == size;
313}
314
315bool Component::CopyFingerprintFile(const base::FilePath& src,
316 const base::FilePath& dest) {
317 base::FilePath fingerprint_path(GetFingerprintPath(src));
318 if (base::PathExists(fingerprint_path)) {
319 std::string fingerprint_contents;
320 if (!base::ReadFileToStringWithMaxSize(
321 fingerprint_path, &fingerprint_contents, kMaximumFilesize)) {
322 return false;
323 }
324
Eric Caruso355e37c2017-03-15 14:31:41 -0700325 if (!IsValidFingerprintFile(fingerprint_contents))
326 return false;
Greg Kerr019d59c2016-11-17 14:28:49 -0800327
328 if (!WriteFileToDisk(GetFingerprintPath(dest), fingerprint_contents)) {
329 return false;
330 }
331 }
332 return true;
333}
334
335// The client inserts manifest.fingerprint into components after unpacking the
336// CRX. The file is used for delta updates. Since Chrome OS doesn't rely on it
Greg Kerr04c1cee2020-10-15 14:08:44 +0000337// for security of the disk image, we are fine with validating the contents
Greg Kerr019d59c2016-11-17 14:28:49 -0800338// and then preserving the unsigned file.
339bool Component::IsValidFingerprintFile(const std::string& contents) {
340 return contents.size() <= 256 &&
341 std::find_if_not(contents.begin(), contents.end(), [](char ch) {
342 return base::IsAsciiAlpha(ch) || base::IsAsciiDigit(ch) || ch == '.';
343 }) == contents.end();
344}
345
346} // namespace imageloader