blob: 797118d445f438add18e3291fbf98896f864c05a [file] [log] [blame]
Yunkai Lim0d850132023-07-26 06:21:30 +00001/*
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
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
36#define LOG_TAG "fsverity_init"
37
38#include <sys/types.h>
39
40#include <filesystem>
41#include <string>
42
43#include <android-base/file.h>
44#include <android-base/logging.h>
45#include <android-base/strings.h>
46#include <log/log.h>
47#include <mini_keyctl_utils.h>
48
49void LoadKeyFromFile(key_serial_t keyring_id, const char* keyname, const std::string& path) {
50 LOG(INFO) << "LoadKeyFromFile path=" << path << " keyname=" << keyname;
51 std::string content;
52 if (!android::base::ReadFileToString(path, &content)) {
53 LOG(ERROR) << "Failed to read key from " << path;
54 return;
55 }
56 if (add_key("asymmetric", keyname, content.c_str(), content.size(), keyring_id) < 0) {
57 PLOG(ERROR) << "Failed to add key from " << path;
58 }
59}
60
61void LoadKeyFromDirectory(key_serial_t keyring_id, const char* keyname_prefix, const char* dir) {
62 if (!std::filesystem::exists(dir)) {
63 return;
64 }
65 int counter = 0;
66 for (const auto& entry : std::filesystem::directory_iterator(dir)) {
67 if (!android::base::EndsWithIgnoreCase(entry.path().c_str(), ".der")) continue;
68 std::string keyname = keyname_prefix + std::to_string(counter);
69 counter++;
70 LoadKeyFromFile(keyring_id, keyname.c_str(), entry.path());
71 }
72}
73
74void LoadKeyFromVerifiedPartitions(key_serial_t keyring_id) {
75 // NB: Directories need to be synced with FileIntegrityService.java in
76 // frameworks/base.
77 LoadKeyFromDirectory(keyring_id, "fsv_system_", "/system/etc/security/fsverity");
78 LoadKeyFromDirectory(keyring_id, "fsv_product_", "/product/etc/security/fsverity");
79}
80
81int main(int argc, const char** argv) {
82 if (argc < 2) {
83 LOG(ERROR) << "Not enough arguments";
84 return -1;
85 }
86
87 key_serial_t keyring_id = android::GetKeyringId(".fs-verity");
88 if (keyring_id < 0) {
89 // This is expected on newer kernels. See comment at the beginning of this file.
90 LOG(DEBUG) << "no initialization required";
91 return 0;
92 }
93
94 const std::string_view command = argv[1];
95
96 if (command == "--load-verified-keys") {
97 LoadKeyFromVerifiedPartitions(keyring_id);
98 } else {
99 LOG(ERROR) << "Unknown argument(s).";
100 return -1;
101 }
102
103 return 0;
104}