| Martijn Coenen | 9519484 | 2020-09-24 16:56:46 +0200 | [diff] [blame] | 1 | /* |
| 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 Coenen | 5588e49 | 2021-02-25 14:33:44 +0100 | [diff] [blame^] | 33 | #define FS_VERITY_MAX_DIGEST_SIZE 64 |
| 34 | |
| Martijn Coenen | 9519484 | 2020-09-24 16:56:46 +0200 | [diff] [blame] | 35 | using android::base::ErrnoError; |
| 36 | using android::base::Error; |
| 37 | using android::base::Result; |
| 38 | using 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 | |
| 48 | struct fsverity_signed_digest { |
| 49 | char magic[8]; /* must be "FSVerity" */ |
| 50 | __le16 digest_algorithm; |
| 51 | __le16 digest_size; |
| 52 | __u8 digest[]; |
| 53 | }; |
| 54 | |
| Martijn Coenen | 5588e49 | 2021-02-25 14:33:44 +0100 | [diff] [blame^] | 55 | static 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 Coenen | 9519484 | 2020-09-24 16:56:46 +0200 | [diff] [blame] | 63 | static 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 Coenen | 5588e49 | 2021-02-25 14:33:44 +0100 | [diff] [blame^] | 69 | Result<std::vector<uint8_t>> createDigest(const std::string& path) { |
| Martijn Coenen | 9519484 | 2020-09-24 16:56:46 +0200 | [diff] [blame] | 70 | 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, ¶ms, &digest); |
| 83 | |
| 84 | return std::vector<uint8_t>(&digest->digest[0], &digest->digest[32]); |
| 85 | } |
| 86 | |
| 87 | static Result<std::vector<uint8_t>> signDigest(const KeymasterSigningKey& key, |
| 88 | const std::vector<uint8_t>& digest) { |
| George Burgess IV | cad5b12 | 2021-02-20 22:42:27 -0800 | [diff] [blame] | 89 | fsverity_signed_digest* d; |
| Martijn Coenen | 9519484 | 2020-09-24 16:56:46 +0200 | [diff] [blame] | 90 | size_t signed_digest_size = sizeof(*d) + digest.size(); |
| George Burgess IV | cad5b12 | 2021-02-20 22:42:27 -0800 | [diff] [blame] | 91 | std::unique_ptr<uint8_t[]> digest_buffer{new uint8_t[signed_digest_size]}; |
| 92 | d = (fsverity_signed_digest*)digest_buffer.get(); |
| Martijn Coenen | 9519484 | 2020-09-24 16:56:46 +0200 | [diff] [blame] | 93 | |
| 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 Coenen | 5588e49 | 2021-02-25 14:33:44 +0100 | [diff] [blame^] | 107 | Result<std::string> enableFsVerity(const std::string& path, const KeymasterSigningKey& key) { |
| Martijn Coenen | 9519484 | 2020-09-24 16:56:46 +0200 | [diff] [blame] | 108 | 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 Coenen | 5588e49 | 2021-02-25 14:33:44 +0100 | [diff] [blame^] | 134 | // Return the root hash as a hex string |
| 135 | return toHex(digest.value()); |
| Martijn Coenen | 9519484 | 2020-09-24 16:56:46 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| Martijn Coenen | 5588e49 | 2021-02-25 14:33:44 +0100 | [diff] [blame^] | 138 | Result<std::map<std::string, std::string>> |
| 139 | addFilesToVerityRecursive(const std::string& path, const KeymasterSigningKey& key) { |
| 140 | std::map<std::string, std::string> digests; |
| Martijn Coenen | 9519484 | 2020-09-24 16:56:46 +0200 | [diff] [blame] | 141 | 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 Coenen | 5588e49 | 2021-02-25 14:33:44 +0100 | [diff] [blame^] | 153 | digests[it->path()] = *result; |
| Martijn Coenen | 9519484 | 2020-09-24 16:56:46 +0200 | [diff] [blame] | 154 | } |
| 155 | ++it; |
| 156 | } |
| Martijn Coenen | 5588e49 | 2021-02-25 14:33:44 +0100 | [diff] [blame^] | 157 | if (ec) { |
| 158 | return Error() << "Failed to iterate " << path << ": " << ec; |
| 159 | } |
| Martijn Coenen | 9519484 | 2020-09-24 16:56:46 +0200 | [diff] [blame] | 160 | |
| Martijn Coenen | 5588e49 | 2021-02-25 14:33:44 +0100 | [diff] [blame^] | 161 | return digests; |
| Martijn Coenen | 9519484 | 2020-09-24 16:56:46 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| Martijn Coenen | 5588e49 | 2021-02-25 14:33:44 +0100 | [diff] [blame^] | 164 | Result<std::string> isFileInVerity(const std::string& path) { |
| Martijn Coenen | 9519484 | 2020-09-24 16:56:46 +0200 | [diff] [blame] | 165 | 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 Coenen | 5588e49 | 2021-02-25 14:33:44 +0100 | [diff] [blame^] | 176 | if (!(flags & FS_VERITY_FL)) { |
| 177 | return Error() << "File is not in fs-verity: " << path; |
| 178 | } |
| Martijn Coenen | 9519484 | 2020-09-24 16:56:46 +0200 | [diff] [blame] | 179 | |
| Martijn Coenen | 5588e49 | 2021-02-25 14:33:44 +0100 | [diff] [blame^] | 180 | 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 Coenen | 9519484 | 2020-09-24 16:56:46 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| Martijn Coenen | 5588e49 | 2021-02-25 14:33:44 +0100 | [diff] [blame^] | 192 | Result<std::map<std::string, std::string>> verifyAllFilesInVerity(const std::string& path) { |
| 193 | std::map<std::string, std::string> digests; |
| Martijn Coenen | 9519484 | 2020-09-24 16:56:46 +0200 | [diff] [blame] | 194 | 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 Coenen | 5588e49 | 2021-02-25 14:33:44 +0100 | [diff] [blame^] | 206 | digests[it->path()] = *result; |
| Martijn Coenen | 9519484 | 2020-09-24 16:56:46 +0200 | [diff] [blame] | 207 | } // TODO reject other types besides dirs? |
| 208 | ++it; |
| 209 | } |
| Martijn Coenen | 5588e49 | 2021-02-25 14:33:44 +0100 | [diff] [blame^] | 210 | if (ec) { |
| 211 | return Error() << "Failed to iterate " << path << ": " << ec; |
| 212 | } |
| Martijn Coenen | 9519484 | 2020-09-24 16:56:46 +0200 | [diff] [blame] | 213 | |
| Martijn Coenen | 5588e49 | 2021-02-25 14:33:44 +0100 | [diff] [blame^] | 214 | return digests; |
| Martijn Coenen | 9519484 | 2020-09-24 16:56:46 +0200 | [diff] [blame] | 215 | } |