blob: 4761b53395c37214a4d689df863648e9e448d5b1 [file] [log] [blame]
Victor Hsiehd0a4b202019-08-26 11:26:27 -07001/*
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 Biggers31b47512023-07-06 17:10:28 +000017//
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 Hsiehd0a4b202019-08-26 11:26:27 -070036#define LOG_TAG "fsverity_init"
37
38#include <sys/types.h>
39
40#include <filesystem>
Victor Hsiehd0a4b202019-08-26 11:26:27 -070041#include <string>
Victor Hsiehd0a4b202019-08-26 11:26:27 -070042
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 Hsiehd0a4b202019-08-26 11:26:27 -070047#include <log/log.h>
48#include <mini_keyctl_utils.h>
Victor Hsiehd0a4b202019-08-26 11:26:27 -070049
50bool 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 Stokes246a7f12021-06-10 14:30:53 +010059bool LoadKeyFromStdin(key_serial_t keyring_id, const char* keyname) {
Martijn Coenen0aeee3d2020-11-30 10:06:06 +010060 std::string content;
61 if (!android::base::ReadFdToString(STDIN_FILENO, &content)) {
62 LOG(ERROR) << "Failed to read key from stdin";
Alan Stokes246a7f12021-06-10 14:30:53 +010063 return false;
Martijn Coenen0aeee3d2020-11-30 10:06:06 +010064 }
65 if (!LoadKeyToKeyring(keyring_id, keyname, content.c_str(), content.size())) {
66 LOG(ERROR) << "Failed to load key from stdin";
Alan Stokes246a7f12021-06-10 14:30:53 +010067 return false;
Martijn Coenen0aeee3d2020-11-30 10:06:06 +010068 }
Alan Stokes246a7f12021-06-10 14:30:53 +010069 return true;
Martijn Coenen0aeee3d2020-11-30 10:06:06 +010070}
71
72void LoadKeyFromFile(key_serial_t keyring_id, const char* keyname, const std::string& path) {
Kohsuke Yatoha51ce462021-03-30 23:38:41 +000073 LOG(INFO) << "LoadKeyFromFile path=" << path << " keyname=" << keyname;
Martijn Coenen0aeee3d2020-11-30 10:06:06 +010074 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 Yatoha51ce462021-03-30 23:38:41 +000084void LoadKeyFromDirectory(key_serial_t keyring_id, const char* keyname_prefix, const char* dir) {
Victor Hsiehd0a4b202019-08-26 11:26:27 -070085 if (!std::filesystem::exists(dir)) {
Victor Hsiehd0a4b202019-08-26 11:26:27 -070086 return;
87 }
Kohsuke Yatoha51ce462021-03-30 23:38:41 +000088 int counter = 0;
Victor Hsiehd0a4b202019-08-26 11:26:27 -070089 for (const auto& entry : std::filesystem::directory_iterator(dir)) {
90 if (!android::base::EndsWithIgnoreCase(entry.path().c_str(), ".der")) continue;
Kohsuke Yatoha51ce462021-03-30 23:38:41 +000091 std::string keyname = keyname_prefix + std::to_string(counter);
92 counter++;
93 LoadKeyFromFile(keyring_id, keyname.c_str(), entry.path());
Victor Hsiehd0a4b202019-08-26 11:26:27 -070094 }
95}
96
Victor Hsieh753ac2a2020-04-03 15:17:37 -070097void LoadKeyFromVerifiedPartitions(key_serial_t keyring_id) {
98 // NB: Directories need to be synced with FileIntegrityService.java in
99 // frameworks/base.
Kohsuke Yatoha51ce462021-03-30 23:38:41 +0000100 LoadKeyFromDirectory(keyring_id, "fsv_system_", "/system/etc/security/fsverity");
101 LoadKeyFromDirectory(keyring_id, "fsv_product_", "/product/etc/security/fsverity");
Victor Hsieh753ac2a2020-04-03 15:17:37 -0700102}
Eric Biggers5024ce52023-07-06 16:54:39 +0000103
104int 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 Biggers31b47512023-07-06 17:10:28 +0000112 // This is expected on newer kernels. See comment at the beginning of this file.
113 LOG(DEBUG) << "no initialization required";
114 return 0;
Eric Biggers5024ce52023-07-06 16:54:39 +0000115 }
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}