blob: b4a6a545fea843fd50ab9c8ca1317f3b00743e85 [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
33using android::base::ErrnoError;
34using android::base::Error;
35using android::base::Result;
36using android::base::unique_fd;
37
38#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
39#define cpu_to_le16(v) ((__force __le16)(uint16_t)(v))
40#define le16_to_cpu(v) ((__force uint16_t)(__le16)(v))
41#else
42#define cpu_to_le16(v) ((__force __le16)__builtin_bswap16(v))
43#define le16_to_cpu(v) (__builtin_bswap16((__force uint16_t)(v)))
44#endif
45
46struct fsverity_signed_digest {
47 char magic[8]; /* must be "FSVerity" */
48 __le16 digest_algorithm;
49 __le16 digest_size;
50 __u8 digest[];
51};
52
53static int read_callback(void* file, void* buf, size_t count) {
54 int* fd = (int*)file;
55 if (TEMP_FAILURE_RETRY(read(*fd, buf, count)) < 0) return errno ? -errno : -EIO;
56 return 0;
57}
58
59static Result<std::vector<uint8_t>> createDigest(const std::string& path) {
60 struct stat filestat;
61 unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC)));
62
63 stat(path.c_str(), &filestat);
64 struct libfsverity_merkle_tree_params params = {
65 .version = 1,
66 .hash_algorithm = FS_VERITY_HASH_ALG_SHA256,
67 .file_size = static_cast<uint64_t>(filestat.st_size),
68 .block_size = 4096,
69 };
70
71 struct libfsverity_digest* digest;
72 libfsverity_compute_digest(&fd, &read_callback, &params, &digest);
73
74 return std::vector<uint8_t>(&digest->digest[0], &digest->digest[32]);
75}
76
77static Result<std::vector<uint8_t>> signDigest(const KeymasterSigningKey& key,
78 const std::vector<uint8_t>& digest) {
George Burgess IVcad5b122021-02-20 22:42:27 -080079 fsverity_signed_digest* d;
Martijn Coenen95194842020-09-24 16:56:46 +020080 size_t signed_digest_size = sizeof(*d) + digest.size();
George Burgess IVcad5b122021-02-20 22:42:27 -080081 std::unique_ptr<uint8_t[]> digest_buffer{new uint8_t[signed_digest_size]};
82 d = (fsverity_signed_digest*)digest_buffer.get();
Martijn Coenen95194842020-09-24 16:56:46 +020083
84 memcpy(d->magic, "FSVerity", 8);
85 d->digest_algorithm = cpu_to_le16(FS_VERITY_HASH_ALG_SHA256);
86 d->digest_size = cpu_to_le16(digest.size());
87 memcpy(d->digest, digest.data(), digest.size());
88
89 auto signed_digest = key.sign(std::string((char*)d, signed_digest_size));
90 if (!signed_digest.ok()) {
91 return signed_digest.error();
92 }
93
94 return std::vector<uint8_t>(signed_digest->begin(), signed_digest->end());
95}
96
97Result<void> enableFsVerity(const std::string& path, const KeymasterSigningKey& key) {
98 auto digest = createDigest(path);
99 if (!digest.ok()) {
100 return digest.error();
101 }
102
103 auto signed_digest = signDigest(key, digest.value());
104 if (!signed_digest.ok()) {
105 return signed_digest.error();
106 }
107
108 auto pkcs7_data = createPkcs7(signed_digest.value());
109
110 struct fsverity_enable_arg arg = {.version = 1};
111
112 arg.sig_ptr = (uint64_t)pkcs7_data->data();
113 arg.sig_size = pkcs7_data->size();
114 arg.hash_algorithm = FS_VERITY_HASH_ALG_SHA256;
115 arg.block_size = 4096;
116
117 unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC)));
118 int ret = ioctl(fd, FS_IOC_ENABLE_VERITY, &arg);
119
120 if (ret != 0) {
121 return ErrnoError() << "Failed to call FS_IOC_ENABLE_VERITY on " << path;
122 }
123
124 return {};
125}
126
127Result<void> addFilesToVerityRecursive(const std::string& path, const KeymasterSigningKey& key) {
128 std::error_code ec;
129
130 auto it = std::filesystem::recursive_directory_iterator(path, ec);
131 auto end = std::filesystem::recursive_directory_iterator();
132
133 while (!ec && it != end) {
134 if (it->is_regular_file()) {
135 LOG(INFO) << "Adding " << it->path() << " to fs-verity...";
136 auto result = enableFsVerity(it->path(), key);
137 if (!result.ok()) {
138 return result.error();
139 }
140 }
141 ++it;
142 }
143
144 return {};
145}
146
147Result<bool> isFileInVerity(const std::string& path) {
148 unsigned int flags;
149
150 unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC)));
151 if (fd < 0) {
152 return ErrnoError() << "Failed to open " << path;
153 }
154
155 int ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
156 if (ret < 0) {
157 return ErrnoError() << "Failed to FS_IOC_GETFLAGS for " << path;
158 }
159
160 return (flags & FS_VERITY_FL);
161}
162
163Result<void> verifyAllFilesInVerity(const std::string& path) {
164 std::error_code ec;
165
166 auto it = std::filesystem::recursive_directory_iterator(path, ec);
167 auto end = std::filesystem::recursive_directory_iterator();
168
169 while (!ec && it != end) {
170 if (it->is_regular_file()) {
171 // Verify
172 auto result = isFileInVerity(it->path());
173 if (!result.ok()) {
174 return result.error();
175 }
176 if (!*result) {
177 return Error() << "File " << it->path() << " not in fs-verity";
178 }
179 } // TODO reject other types besides dirs?
180 ++it;
181 }
182
183 return {};
184}