Victor Hsieh | d0a4b20 | 2019-08-26 11:26:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Eric Biggers | 31b4751 | 2023-07-06 17:10:28 +0000 | [diff] [blame^] | 17 | // |
| 18 | // fsverity_init is a tool for loading X.509 certificates into the kernel keyring used by the |
| 19 | // fsverity builtin signature verification kernel feature |
| 20 | // (https://www.kernel.org/doc/html/latest/filesystems/fsverity.html#built-in-signature-verification). |
| 21 | // Starting in Android 14, Android has actually stopped using this feature, as it was too inflexible |
| 22 | // and caused problems. It has been replaced by userspace signature verification. Also, some uses |
| 23 | // of fsverity in Android are now for integrity-only use cases. |
| 24 | // |
| 25 | // Regardless, there may exist fsverity files on-disk that were created by Android 13 or earlier. |
| 26 | // These files still have builtin signatures. If the kernel is an older kernel that still has |
| 27 | // CONFIG_FS_VERITY_BUILTIN_SIGNATURES enabled, these files cannot be opened unless the |
| 28 | // corresponding key is in the ".fs-verity" keyring. Therefore, this tool still has to exist and be |
| 29 | // used to load keys into the kernel, even though this has no security purpose anymore. |
| 30 | // |
| 31 | // This tool can be removed as soon as all supported kernels are guaranteed to have |
| 32 | // CONFIG_FS_VERITY_BUILTIN_SIGNATURES disabled, or alternatively as soon as support for upgrades |
| 33 | // from Android 13 or earlier is no longer required. |
| 34 | // |
| 35 | |
Victor Hsieh | d0a4b20 | 2019-08-26 11:26:27 -0700 | [diff] [blame] | 36 | #define LOG_TAG "fsverity_init" |
| 37 | |
| 38 | #include <sys/types.h> |
| 39 | |
| 40 | #include <filesystem> |
Victor Hsieh | d0a4b20 | 2019-08-26 11:26:27 -0700 | [diff] [blame] | 41 | #include <string> |
Victor Hsieh | d0a4b20 | 2019-08-26 11:26:27 -0700 | [diff] [blame] | 42 | |
| 43 | #include <android-base/file.h> |
| 44 | #include <android-base/logging.h> |
| 45 | #include <android-base/properties.h> |
| 46 | #include <android-base/strings.h> |
Victor Hsieh | d0a4b20 | 2019-08-26 11:26:27 -0700 | [diff] [blame] | 47 | #include <log/log.h> |
| 48 | #include <mini_keyctl_utils.h> |
Victor Hsieh | d0a4b20 | 2019-08-26 11:26:27 -0700 | [diff] [blame] | 49 | |
| 50 | bool LoadKeyToKeyring(key_serial_t keyring_id, const char* desc, const char* data, size_t size) { |
| 51 | key_serial_t key = add_key("asymmetric", desc, data, size, keyring_id); |
| 52 | if (key < 0) { |
| 53 | PLOG(ERROR) << "Failed to add key"; |
| 54 | return false; |
| 55 | } |
| 56 | return true; |
| 57 | } |
| 58 | |
Alan Stokes | 246a7f1 | 2021-06-10 14:30:53 +0100 | [diff] [blame] | 59 | bool LoadKeyFromStdin(key_serial_t keyring_id, const char* keyname) { |
Martijn Coenen | 0aeee3d | 2020-11-30 10:06:06 +0100 | [diff] [blame] | 60 | std::string content; |
| 61 | if (!android::base::ReadFdToString(STDIN_FILENO, &content)) { |
| 62 | LOG(ERROR) << "Failed to read key from stdin"; |
Alan Stokes | 246a7f1 | 2021-06-10 14:30:53 +0100 | [diff] [blame] | 63 | return false; |
Martijn Coenen | 0aeee3d | 2020-11-30 10:06:06 +0100 | [diff] [blame] | 64 | } |
| 65 | if (!LoadKeyToKeyring(keyring_id, keyname, content.c_str(), content.size())) { |
| 66 | LOG(ERROR) << "Failed to load key from stdin"; |
Alan Stokes | 246a7f1 | 2021-06-10 14:30:53 +0100 | [diff] [blame] | 67 | return false; |
Martijn Coenen | 0aeee3d | 2020-11-30 10:06:06 +0100 | [diff] [blame] | 68 | } |
Alan Stokes | 246a7f1 | 2021-06-10 14:30:53 +0100 | [diff] [blame] | 69 | return true; |
Martijn Coenen | 0aeee3d | 2020-11-30 10:06:06 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void LoadKeyFromFile(key_serial_t keyring_id, const char* keyname, const std::string& path) { |
Kohsuke Yatoh | a51ce46 | 2021-03-30 23:38:41 +0000 | [diff] [blame] | 73 | LOG(INFO) << "LoadKeyFromFile path=" << path << " keyname=" << keyname; |
Martijn Coenen | 0aeee3d | 2020-11-30 10:06:06 +0100 | [diff] [blame] | 74 | std::string content; |
| 75 | if (!android::base::ReadFileToString(path, &content)) { |
| 76 | LOG(ERROR) << "Failed to read key from " << path; |
| 77 | return; |
| 78 | } |
| 79 | if (!LoadKeyToKeyring(keyring_id, keyname, content.c_str(), content.size())) { |
| 80 | LOG(ERROR) << "Failed to load key from " << path; |
| 81 | } |
| 82 | } |
| 83 | |
Kohsuke Yatoh | a51ce46 | 2021-03-30 23:38:41 +0000 | [diff] [blame] | 84 | void LoadKeyFromDirectory(key_serial_t keyring_id, const char* keyname_prefix, const char* dir) { |
Victor Hsieh | d0a4b20 | 2019-08-26 11:26:27 -0700 | [diff] [blame] | 85 | if (!std::filesystem::exists(dir)) { |
Victor Hsieh | d0a4b20 | 2019-08-26 11:26:27 -0700 | [diff] [blame] | 86 | return; |
| 87 | } |
Kohsuke Yatoh | a51ce46 | 2021-03-30 23:38:41 +0000 | [diff] [blame] | 88 | int counter = 0; |
Victor Hsieh | d0a4b20 | 2019-08-26 11:26:27 -0700 | [diff] [blame] | 89 | for (const auto& entry : std::filesystem::directory_iterator(dir)) { |
| 90 | if (!android::base::EndsWithIgnoreCase(entry.path().c_str(), ".der")) continue; |
Kohsuke Yatoh | a51ce46 | 2021-03-30 23:38:41 +0000 | [diff] [blame] | 91 | std::string keyname = keyname_prefix + std::to_string(counter); |
| 92 | counter++; |
| 93 | LoadKeyFromFile(keyring_id, keyname.c_str(), entry.path()); |
Victor Hsieh | d0a4b20 | 2019-08-26 11:26:27 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Victor Hsieh | 753ac2a | 2020-04-03 15:17:37 -0700 | [diff] [blame] | 97 | void LoadKeyFromVerifiedPartitions(key_serial_t keyring_id) { |
| 98 | // NB: Directories need to be synced with FileIntegrityService.java in |
| 99 | // frameworks/base. |
Kohsuke Yatoh | a51ce46 | 2021-03-30 23:38:41 +0000 | [diff] [blame] | 100 | LoadKeyFromDirectory(keyring_id, "fsv_system_", "/system/etc/security/fsverity"); |
| 101 | LoadKeyFromDirectory(keyring_id, "fsv_product_", "/product/etc/security/fsverity"); |
Victor Hsieh | 753ac2a | 2020-04-03 15:17:37 -0700 | [diff] [blame] | 102 | } |
Eric Biggers | 5024ce5 | 2023-07-06 16:54:39 +0000 | [diff] [blame] | 103 | |
| 104 | int main(int argc, const char** argv) { |
| 105 | if (argc < 2) { |
| 106 | LOG(ERROR) << "Not enough arguments"; |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | key_serial_t keyring_id = android::GetKeyringId(".fs-verity"); |
| 111 | if (keyring_id < 0) { |
Eric Biggers | 31b4751 | 2023-07-06 17:10:28 +0000 | [diff] [blame^] | 112 | // This is expected on newer kernels. See comment at the beginning of this file. |
| 113 | LOG(DEBUG) << "no initialization required"; |
| 114 | return 0; |
Eric Biggers | 5024ce5 | 2023-07-06 16:54:39 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | const std::string_view command = argv[1]; |
| 118 | |
| 119 | if (command == "--load-verified-keys") { |
| 120 | LoadKeyFromVerifiedPartitions(keyring_id); |
| 121 | } else if (command == "--load-extra-key") { |
| 122 | if (argc != 3) { |
| 123 | LOG(ERROR) << "--load-extra-key requires <key_name> argument."; |
| 124 | return -1; |
| 125 | } |
| 126 | if (!LoadKeyFromStdin(keyring_id, argv[2])) { |
| 127 | return -1; |
| 128 | } |
| 129 | } else if (command == "--lock") { |
| 130 | if (!android::base::GetBoolProperty("ro.debuggable", false)) { |
| 131 | if (keyctl_restrict_keyring(keyring_id, nullptr, nullptr) < 0) { |
| 132 | PLOG(ERROR) << "Cannot restrict .fs-verity keyring"; |
| 133 | } |
| 134 | } |
| 135 | } else { |
| 136 | LOG(ERROR) << "Unknown argument(s)."; |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | } |