xref: /aosp_15_r20/system/security/fsverity_init/fsverity_init.cpp (revision e1997b9af69e3155ead6e072d106a0077849ffba)
1*e1997b9aSAndroid Build Coastguard Worker /*
2*e1997b9aSAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*e1997b9aSAndroid Build Coastguard Worker  *
4*e1997b9aSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*e1997b9aSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*e1997b9aSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*e1997b9aSAndroid Build Coastguard Worker  *
8*e1997b9aSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*e1997b9aSAndroid Build Coastguard Worker  *
10*e1997b9aSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*e1997b9aSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*e1997b9aSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e1997b9aSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*e1997b9aSAndroid Build Coastguard Worker  * limitations under the License.
15*e1997b9aSAndroid Build Coastguard Worker  */
16*e1997b9aSAndroid Build Coastguard Worker 
17*e1997b9aSAndroid Build Coastguard Worker //
18*e1997b9aSAndroid Build Coastguard Worker // fsverity_init is a tool for loading X.509 certificates into the kernel keyring used by the
19*e1997b9aSAndroid Build Coastguard Worker // fsverity builtin signature verification kernel feature
20*e1997b9aSAndroid Build Coastguard Worker // (https://www.kernel.org/doc/html/latest/filesystems/fsverity.html#built-in-signature-verification).
21*e1997b9aSAndroid Build Coastguard Worker // Starting in Android 14, Android has actually stopped using this feature, as it was too inflexible
22*e1997b9aSAndroid Build Coastguard Worker // and caused problems.  It has been replaced by userspace signature verification.  Also, some uses
23*e1997b9aSAndroid Build Coastguard Worker // of fsverity in Android are now for integrity-only use cases.
24*e1997b9aSAndroid Build Coastguard Worker //
25*e1997b9aSAndroid Build Coastguard Worker // Regardless, there may exist fsverity files on-disk that were created by Android 13 or earlier.
26*e1997b9aSAndroid Build Coastguard Worker // These files still have builtin signatures.  If the kernel is an older kernel that still has
27*e1997b9aSAndroid Build Coastguard Worker // CONFIG_FS_VERITY_BUILTIN_SIGNATURES enabled, these files cannot be opened unless the
28*e1997b9aSAndroid Build Coastguard Worker // corresponding key is in the ".fs-verity" keyring.  Therefore, this tool still has to exist and be
29*e1997b9aSAndroid Build Coastguard Worker // used to load keys into the kernel, even though this has no security purpose anymore.
30*e1997b9aSAndroid Build Coastguard Worker //
31*e1997b9aSAndroid Build Coastguard Worker // This tool can be removed as soon as all supported kernels are guaranteed to have
32*e1997b9aSAndroid Build Coastguard Worker // CONFIG_FS_VERITY_BUILTIN_SIGNATURES disabled, or alternatively as soon as support for upgrades
33*e1997b9aSAndroid Build Coastguard Worker // from Android 13 or earlier is no longer required.
34*e1997b9aSAndroid Build Coastguard Worker //
35*e1997b9aSAndroid Build Coastguard Worker 
36*e1997b9aSAndroid Build Coastguard Worker #define LOG_TAG "fsverity_init"
37*e1997b9aSAndroid Build Coastguard Worker 
38*e1997b9aSAndroid Build Coastguard Worker #include <sys/types.h>
39*e1997b9aSAndroid Build Coastguard Worker 
40*e1997b9aSAndroid Build Coastguard Worker #include <filesystem>
41*e1997b9aSAndroid Build Coastguard Worker #include <string>
42*e1997b9aSAndroid Build Coastguard Worker 
43*e1997b9aSAndroid Build Coastguard Worker #include <android-base/file.h>
44*e1997b9aSAndroid Build Coastguard Worker #include <android-base/logging.h>
45*e1997b9aSAndroid Build Coastguard Worker #include <android-base/strings.h>
46*e1997b9aSAndroid Build Coastguard Worker #include <android_security_flag.h>
47*e1997b9aSAndroid Build Coastguard Worker #include <log/log.h>
48*e1997b9aSAndroid Build Coastguard Worker #include <mini_keyctl_utils.h>
49*e1997b9aSAndroid Build Coastguard Worker 
LoadKeyFromFile(key_serial_t keyring_id,const char * keyname,const std::string & path)50*e1997b9aSAndroid Build Coastguard Worker void LoadKeyFromFile(key_serial_t keyring_id, const char* keyname, const std::string& path) {
51*e1997b9aSAndroid Build Coastguard Worker     LOG(INFO) << "LoadKeyFromFile path=" << path << " keyname=" << keyname;
52*e1997b9aSAndroid Build Coastguard Worker     std::string content;
53*e1997b9aSAndroid Build Coastguard Worker     if (!android::base::ReadFileToString(path, &content)) {
54*e1997b9aSAndroid Build Coastguard Worker         LOG(ERROR) << "Failed to read key from " << path;
55*e1997b9aSAndroid Build Coastguard Worker         return;
56*e1997b9aSAndroid Build Coastguard Worker     }
57*e1997b9aSAndroid Build Coastguard Worker     if (add_key("asymmetric", keyname, content.c_str(), content.size(), keyring_id) < 0) {
58*e1997b9aSAndroid Build Coastguard Worker         PLOG(ERROR) << "Failed to add key from " << path;
59*e1997b9aSAndroid Build Coastguard Worker     }
60*e1997b9aSAndroid Build Coastguard Worker }
61*e1997b9aSAndroid Build Coastguard Worker 
LoadKeyFromDirectory(key_serial_t keyring_id,const char * keyname_prefix,const char * dir)62*e1997b9aSAndroid Build Coastguard Worker void LoadKeyFromDirectory(key_serial_t keyring_id, const char* keyname_prefix, const char* dir) {
63*e1997b9aSAndroid Build Coastguard Worker     if (!std::filesystem::exists(dir)) {
64*e1997b9aSAndroid Build Coastguard Worker         return;
65*e1997b9aSAndroid Build Coastguard Worker     }
66*e1997b9aSAndroid Build Coastguard Worker     int counter = 0;
67*e1997b9aSAndroid Build Coastguard Worker     for (const auto& entry : std::filesystem::directory_iterator(dir)) {
68*e1997b9aSAndroid Build Coastguard Worker         if (!android::base::EndsWithIgnoreCase(entry.path().c_str(), ".der")) continue;
69*e1997b9aSAndroid Build Coastguard Worker         std::string keyname = keyname_prefix + std::to_string(counter);
70*e1997b9aSAndroid Build Coastguard Worker         counter++;
71*e1997b9aSAndroid Build Coastguard Worker         LoadKeyFromFile(keyring_id, keyname.c_str(), entry.path());
72*e1997b9aSAndroid Build Coastguard Worker     }
73*e1997b9aSAndroid Build Coastguard Worker }
74*e1997b9aSAndroid Build Coastguard Worker 
LoadKeyFromVerifiedPartitions(key_serial_t keyring_id)75*e1997b9aSAndroid Build Coastguard Worker void LoadKeyFromVerifiedPartitions(key_serial_t keyring_id) {
76*e1997b9aSAndroid Build Coastguard Worker     // NB: Directories need to be synced with FileIntegrityService.java in
77*e1997b9aSAndroid Build Coastguard Worker     // frameworks/base.
78*e1997b9aSAndroid Build Coastguard Worker     LoadKeyFromDirectory(keyring_id, "fsv_system_", "/system/etc/security/fsverity");
79*e1997b9aSAndroid Build Coastguard Worker     LoadKeyFromDirectory(keyring_id, "fsv_product_", "/product/etc/security/fsverity");
80*e1997b9aSAndroid Build Coastguard Worker }
81*e1997b9aSAndroid Build Coastguard Worker 
main(int argc,const char ** argv)82*e1997b9aSAndroid Build Coastguard Worker int main(int argc, const char** argv) {
83*e1997b9aSAndroid Build Coastguard Worker     if (android::security::flag::deprecate_fsverity_init()) {
84*e1997b9aSAndroid Build Coastguard Worker         // Don't load keys to the built-in fs-verity keyring in kernel. This will make existing
85*e1997b9aSAndroid Build Coastguard Worker         // files not readable. We expect to only enable the flag when there are no such files or
86*e1997b9aSAndroid Build Coastguard Worker         // when failure is ok (e.g. with a fallback).
87*e1997b9aSAndroid Build Coastguard Worker         return 0;
88*e1997b9aSAndroid Build Coastguard Worker     }
89*e1997b9aSAndroid Build Coastguard Worker 
90*e1997b9aSAndroid Build Coastguard Worker     if (argc < 2) {
91*e1997b9aSAndroid Build Coastguard Worker         LOG(ERROR) << "Not enough arguments";
92*e1997b9aSAndroid Build Coastguard Worker         return -1;
93*e1997b9aSAndroid Build Coastguard Worker     }
94*e1997b9aSAndroid Build Coastguard Worker 
95*e1997b9aSAndroid Build Coastguard Worker     key_serial_t keyring_id = android::GetKeyringId(".fs-verity");
96*e1997b9aSAndroid Build Coastguard Worker     if (keyring_id < 0) {
97*e1997b9aSAndroid Build Coastguard Worker         // This is expected on newer kernels.  See comment at the beginning of this file.
98*e1997b9aSAndroid Build Coastguard Worker         LOG(DEBUG) << "no initialization required";
99*e1997b9aSAndroid Build Coastguard Worker         return 0;
100*e1997b9aSAndroid Build Coastguard Worker     }
101*e1997b9aSAndroid Build Coastguard Worker 
102*e1997b9aSAndroid Build Coastguard Worker     const std::string_view command = argv[1];
103*e1997b9aSAndroid Build Coastguard Worker 
104*e1997b9aSAndroid Build Coastguard Worker     if (command == "--load-verified-keys") {
105*e1997b9aSAndroid Build Coastguard Worker         LoadKeyFromVerifiedPartitions(keyring_id);
106*e1997b9aSAndroid Build Coastguard Worker     } else {
107*e1997b9aSAndroid Build Coastguard Worker         LOG(ERROR) << "Unknown argument(s).";
108*e1997b9aSAndroid Build Coastguard Worker         return -1;
109*e1997b9aSAndroid Build Coastguard Worker     }
110*e1997b9aSAndroid Build Coastguard Worker 
111*e1997b9aSAndroid Build Coastguard Worker     return 0;
112*e1997b9aSAndroid Build Coastguard Worker }
113