blob: 3d0b85a9f933261bbc90b2c7f06dc3d85c7b3c1b [file] [log] [blame]
Martijn Coenen95194842020-09-24 16:56:46 +02001/*
2 * Copyright (C) 2020 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 <filesystem>
18#include <string>
19
20#include <fcntl.h>
21#include <linux/fs.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24
25#include <android-base/logging.h>
26#include <android-base/unique_fd.h>
27#include <libfsverity.h>
28#include <linux/fsverity.h>
29
30#include "CertUtils.h"
31#include "KeymasterSigningKey.h"
32
Martijn Coenen5588e492021-02-25 14:33:44 +010033#define FS_VERITY_MAX_DIGEST_SIZE 64
34
Martijn Coenen95194842020-09-24 16:56:46 +020035using android::base::ErrnoError;
36using android::base::Error;
37using android::base::Result;
38using android::base::unique_fd;
39
40#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
41#define cpu_to_le16(v) ((__force __le16)(uint16_t)(v))
42#define le16_to_cpu(v) ((__force uint16_t)(__le16)(v))
43#else
44#define cpu_to_le16(v) ((__force __le16)__builtin_bswap16(v))
45#define le16_to_cpu(v) (__builtin_bswap16((__force uint16_t)(v)))
46#endif
47
48struct fsverity_signed_digest {
49 char magic[8]; /* must be "FSVerity" */
50 __le16 digest_algorithm;
51 __le16 digest_size;
52 __u8 digest[];
53};
54
Martijn Coenen5588e492021-02-25 14:33:44 +010055static std::string toHex(const std::vector<uint8_t>& data) {
56 std::stringstream ss;
57 for (auto it = data.begin(); it != data.end(); ++it) {
58 ss << std::setfill('0') << std::setw(2) << std::hex << static_cast<unsigned>(*it);
59 }
60 return ss.str();
61}
62
Martijn Coenen95194842020-09-24 16:56:46 +020063static int read_callback(void* file, void* buf, size_t count) {
64 int* fd = (int*)file;
65 if (TEMP_FAILURE_RETRY(read(*fd, buf, count)) < 0) return errno ? -errno : -EIO;
66 return 0;
67}
68
Martijn Coenen5588e492021-02-25 14:33:44 +010069Result<std::vector<uint8_t>> createDigest(const std::string& path) {
Martijn Coenen95194842020-09-24 16:56:46 +020070 struct stat filestat;
71 unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC)));
72
73 stat(path.c_str(), &filestat);
74 struct libfsverity_merkle_tree_params params = {
75 .version = 1,
76 .hash_algorithm = FS_VERITY_HASH_ALG_SHA256,
77 .file_size = static_cast<uint64_t>(filestat.st_size),
78 .block_size = 4096,
79 };
80
81 struct libfsverity_digest* digest;
82 libfsverity_compute_digest(&fd, &read_callback, &params, &digest);
83
84 return std::vector<uint8_t>(&digest->digest[0], &digest->digest[32]);
85}
86
87static Result<std::vector<uint8_t>> signDigest(const KeymasterSigningKey& key,
88 const std::vector<uint8_t>& digest) {
George Burgess IVcad5b122021-02-20 22:42:27 -080089 fsverity_signed_digest* d;
Martijn Coenen95194842020-09-24 16:56:46 +020090 size_t signed_digest_size = sizeof(*d) + digest.size();
George Burgess IVcad5b122021-02-20 22:42:27 -080091 std::unique_ptr<uint8_t[]> digest_buffer{new uint8_t[signed_digest_size]};
92 d = (fsverity_signed_digest*)digest_buffer.get();
Martijn Coenen95194842020-09-24 16:56:46 +020093
94 memcpy(d->magic, "FSVerity", 8);
95 d->digest_algorithm = cpu_to_le16(FS_VERITY_HASH_ALG_SHA256);
96 d->digest_size = cpu_to_le16(digest.size());
97 memcpy(d->digest, digest.data(), digest.size());
98
99 auto signed_digest = key.sign(std::string((char*)d, signed_digest_size));
100 if (!signed_digest.ok()) {
101 return signed_digest.error();
102 }
103
104 return std::vector<uint8_t>(signed_digest->begin(), signed_digest->end());
105}
106
Martijn Coenen5588e492021-02-25 14:33:44 +0100107Result<std::string> enableFsVerity(const std::string& path, const KeymasterSigningKey& key) {
Martijn Coenen95194842020-09-24 16:56:46 +0200108 auto digest = createDigest(path);
109 if (!digest.ok()) {
110 return digest.error();
111 }
112
113 auto signed_digest = signDigest(key, digest.value());
114 if (!signed_digest.ok()) {
115 return signed_digest.error();
116 }
117
118 auto pkcs7_data = createPkcs7(signed_digest.value());
119
120 struct fsverity_enable_arg arg = {.version = 1};
121
122 arg.sig_ptr = (uint64_t)pkcs7_data->data();
123 arg.sig_size = pkcs7_data->size();
124 arg.hash_algorithm = FS_VERITY_HASH_ALG_SHA256;
125 arg.block_size = 4096;
126
127 unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC)));
128 int ret = ioctl(fd, FS_IOC_ENABLE_VERITY, &arg);
129
130 if (ret != 0) {
131 return ErrnoError() << "Failed to call FS_IOC_ENABLE_VERITY on " << path;
132 }
133
Martijn Coenen5588e492021-02-25 14:33:44 +0100134 // Return the root hash as a hex string
135 return toHex(digest.value());
Martijn Coenen95194842020-09-24 16:56:46 +0200136}
137
Martijn Coenen5588e492021-02-25 14:33:44 +0100138Result<std::map<std::string, std::string>>
139addFilesToVerityRecursive(const std::string& path, const KeymasterSigningKey& key) {
140 std::map<std::string, std::string> digests;
Martijn Coenen95194842020-09-24 16:56:46 +0200141 std::error_code ec;
142
143 auto it = std::filesystem::recursive_directory_iterator(path, ec);
144 auto end = std::filesystem::recursive_directory_iterator();
145
146 while (!ec && it != end) {
147 if (it->is_regular_file()) {
148 LOG(INFO) << "Adding " << it->path() << " to fs-verity...";
149 auto result = enableFsVerity(it->path(), key);
150 if (!result.ok()) {
151 return result.error();
152 }
Martijn Coenen5588e492021-02-25 14:33:44 +0100153 digests[it->path()] = *result;
Martijn Coenen95194842020-09-24 16:56:46 +0200154 }
155 ++it;
156 }
Martijn Coenen5588e492021-02-25 14:33:44 +0100157 if (ec) {
158 return Error() << "Failed to iterate " << path << ": " << ec;
159 }
Martijn Coenen95194842020-09-24 16:56:46 +0200160
Martijn Coenen5588e492021-02-25 14:33:44 +0100161 return digests;
Martijn Coenen95194842020-09-24 16:56:46 +0200162}
163
Martijn Coenen5588e492021-02-25 14:33:44 +0100164Result<std::string> isFileInVerity(const std::string& path) {
Martijn Coenen95194842020-09-24 16:56:46 +0200165 unsigned int flags;
166
167 unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC)));
168 if (fd < 0) {
169 return ErrnoError() << "Failed to open " << path;
170 }
171
172 int ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
173 if (ret < 0) {
174 return ErrnoError() << "Failed to FS_IOC_GETFLAGS for " << path;
175 }
Martijn Coenen5588e492021-02-25 14:33:44 +0100176 if (!(flags & FS_VERITY_FL)) {
177 return Error() << "File is not in fs-verity: " << path;
178 }
Martijn Coenen95194842020-09-24 16:56:46 +0200179
Martijn Coenen5588e492021-02-25 14:33:44 +0100180 struct fsverity_digest* d;
181 d = (struct fsverity_digest*)malloc(sizeof(*d) + FS_VERITY_MAX_DIGEST_SIZE);
182 d->digest_size = FS_VERITY_MAX_DIGEST_SIZE;
183 ret = ioctl(fd, FS_IOC_MEASURE_VERITY, d);
184 if (ret < 0) {
185 return ErrnoError() << "Failed to FS_IOC_MEASURE_VERITY for " << path;
186 }
187 std::vector<uint8_t> digest_vector(&d->digest[0], &d->digest[d->digest_size]);
188
189 return toHex(digest_vector);
Martijn Coenen95194842020-09-24 16:56:46 +0200190}
191
Martijn Coenen5588e492021-02-25 14:33:44 +0100192Result<std::map<std::string, std::string>> verifyAllFilesInVerity(const std::string& path) {
193 std::map<std::string, std::string> digests;
Martijn Coenen95194842020-09-24 16:56:46 +0200194 std::error_code ec;
195
196 auto it = std::filesystem::recursive_directory_iterator(path, ec);
197 auto end = std::filesystem::recursive_directory_iterator();
198
199 while (!ec && it != end) {
200 if (it->is_regular_file()) {
201 // Verify
202 auto result = isFileInVerity(it->path());
203 if (!result.ok()) {
204 return result.error();
205 }
Martijn Coenen5588e492021-02-25 14:33:44 +0100206 digests[it->path()] = *result;
Martijn Coenen95194842020-09-24 16:56:46 +0200207 } // TODO reject other types besides dirs?
208 ++it;
209 }
Martijn Coenen5588e492021-02-25 14:33:44 +0100210 if (ec) {
211 return Error() << "Failed to iterate " << path << ": " << ec;
212 }
Martijn Coenen95194842020-09-24 16:56:46 +0200213
Martijn Coenen5588e492021-02-25 14:33:44 +0100214 return digests;
Martijn Coenen95194842020-09-24 16:56:46 +0200215}