blob: 579d3d85641e1c2b1f0df14da368b7886a3757aa [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) {
79 struct fsverity_signed_digest* d = NULL;
80 size_t signed_digest_size = sizeof(*d) + digest.size();
81 d = (struct fsverity_signed_digest*)malloc(signed_digest_size);
82
83 memcpy(d->magic, "FSVerity", 8);
84 d->digest_algorithm = cpu_to_le16(FS_VERITY_HASH_ALG_SHA256);
85 d->digest_size = cpu_to_le16(digest.size());
86 memcpy(d->digest, digest.data(), digest.size());
87
88 auto signed_digest = key.sign(std::string((char*)d, signed_digest_size));
89 if (!signed_digest.ok()) {
90 return signed_digest.error();
91 }
92
93 return std::vector<uint8_t>(signed_digest->begin(), signed_digest->end());
94}
95
96Result<void> enableFsVerity(const std::string& path, const KeymasterSigningKey& key) {
97 auto digest = createDigest(path);
98 if (!digest.ok()) {
99 return digest.error();
100 }
101
102 auto signed_digest = signDigest(key, digest.value());
103 if (!signed_digest.ok()) {
104 return signed_digest.error();
105 }
106
107 auto pkcs7_data = createPkcs7(signed_digest.value());
108
109 struct fsverity_enable_arg arg = {.version = 1};
110
111 arg.sig_ptr = (uint64_t)pkcs7_data->data();
112 arg.sig_size = pkcs7_data->size();
113 arg.hash_algorithm = FS_VERITY_HASH_ALG_SHA256;
114 arg.block_size = 4096;
115
116 unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC)));
117 int ret = ioctl(fd, FS_IOC_ENABLE_VERITY, &arg);
118
119 if (ret != 0) {
120 return ErrnoError() << "Failed to call FS_IOC_ENABLE_VERITY on " << path;
121 }
122
123 return {};
124}
125
126Result<void> addFilesToVerityRecursive(const std::string& path, const KeymasterSigningKey& key) {
127 std::error_code ec;
128
129 auto it = std::filesystem::recursive_directory_iterator(path, ec);
130 auto end = std::filesystem::recursive_directory_iterator();
131
132 while (!ec && it != end) {
133 if (it->is_regular_file()) {
134 LOG(INFO) << "Adding " << it->path() << " to fs-verity...";
135 auto result = enableFsVerity(it->path(), key);
136 if (!result.ok()) {
137 return result.error();
138 }
139 }
140 ++it;
141 }
142
143 return {};
144}
145
146Result<bool> isFileInVerity(const std::string& path) {
147 unsigned int flags;
148
149 unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC)));
150 if (fd < 0) {
151 return ErrnoError() << "Failed to open " << path;
152 }
153
154 int ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
155 if (ret < 0) {
156 return ErrnoError() << "Failed to FS_IOC_GETFLAGS for " << path;
157 }
158
159 return (flags & FS_VERITY_FL);
160}
161
162Result<void> verifyAllFilesInVerity(const std::string& path) {
163 std::error_code ec;
164
165 auto it = std::filesystem::recursive_directory_iterator(path, ec);
166 auto end = std::filesystem::recursive_directory_iterator();
167
168 while (!ec && it != end) {
169 if (it->is_regular_file()) {
170 // Verify
171 auto result = isFileInVerity(it->path());
172 if (!result.ok()) {
173 return result.error();
174 }
175 if (!*result) {
176 return Error() << "File " << it->path() << " not in fs-verity";
177 }
178 } // TODO reject other types besides dirs?
179 ++it;
180 }
181
182 return {};
183}