blob: b81fb223ce5bb795c0e0923a296d6c3d284989b5 [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
17#define LOG_TAG "fsverity_init"
18
19#include <sys/types.h>
20
21#include <filesystem>
Victor Hsiehd0a4b202019-08-26 11:26:27 -070022#include <string>
Victor Hsiehd0a4b202019-08-26 11:26:27 -070023
24#include <android-base/file.h>
25#include <android-base/logging.h>
26#include <android-base/properties.h>
27#include <android-base/strings.h>
Victor Hsiehd0a4b202019-08-26 11:26:27 -070028#include <log/log.h>
29#include <mini_keyctl_utils.h>
Victor Hsiehd0a4b202019-08-26 11:26:27 -070030
31bool LoadKeyToKeyring(key_serial_t keyring_id, const char* desc, const char* data, size_t size) {
32 key_serial_t key = add_key("asymmetric", desc, data, size, keyring_id);
33 if (key < 0) {
34 PLOG(ERROR) << "Failed to add key";
35 return false;
36 }
37 return true;
38}
39
Martijn Coenen0aeee3d2020-11-30 10:06:06 +010040void LoadKeyFromStdin(key_serial_t keyring_id, const char* keyname) {
41 std::string content;
42 if (!android::base::ReadFdToString(STDIN_FILENO, &content)) {
43 LOG(ERROR) << "Failed to read key from stdin";
44 return;
45 }
46 if (!LoadKeyToKeyring(keyring_id, keyname, content.c_str(), content.size())) {
47 LOG(ERROR) << "Failed to load key from stdin";
48 }
49}
50
51void LoadKeyFromFile(key_serial_t keyring_id, const char* keyname, const std::string& path) {
52 std::string content;
53 if (!android::base::ReadFileToString(path, &content)) {
54 LOG(ERROR) << "Failed to read key from " << path;
55 return;
56 }
57 if (!LoadKeyToKeyring(keyring_id, keyname, content.c_str(), content.size())) {
58 LOG(ERROR) << "Failed to load key from " << path;
59 }
60}
61
Victor Hsieh753ac2a2020-04-03 15:17:37 -070062void LoadKeyFromDirectory(key_serial_t keyring_id, const char* keyname, const char* dir) {
Victor Hsiehd0a4b202019-08-26 11:26:27 -070063 if (!std::filesystem::exists(dir)) {
Victor Hsiehd0a4b202019-08-26 11:26:27 -070064 return;
65 }
66 for (const auto& entry : std::filesystem::directory_iterator(dir)) {
67 if (!android::base::EndsWithIgnoreCase(entry.path().c_str(), ".der")) continue;
Martijn Coenen0aeee3d2020-11-30 10:06:06 +010068
69 LoadKeyFromFile(keyring_id, keyname, entry.path());
Victor Hsiehd0a4b202019-08-26 11:26:27 -070070 }
71}
72
Victor Hsieh753ac2a2020-04-03 15:17:37 -070073void LoadKeyFromVerifiedPartitions(key_serial_t keyring_id) {
74 // NB: Directories need to be synced with FileIntegrityService.java in
75 // frameworks/base.
76 LoadKeyFromDirectory(keyring_id, "fsv_system", "/system/etc/security/fsverity");
77 LoadKeyFromDirectory(keyring_id, "fsv_product", "/product/etc/security/fsverity");
78}
79
Martijn Coenen0aeee3d2020-11-30 10:06:06 +010080int main(int argc, const char** argv) {
81 if (argc < 2) {
82 LOG(ERROR) << "Not enough arguments";
83 return -1;
84 }
85
Victor Hsiehd0a4b202019-08-26 11:26:27 -070086 key_serial_t keyring_id = android::GetKeyringId(".fs-verity");
87 if (keyring_id < 0) {
88 LOG(ERROR) << "Failed to find .fs-verity keyring id";
89 return -1;
90 }
91
Martijn Coenen0aeee3d2020-11-30 10:06:06 +010092 const std::string_view command = argv[1];
Victor Hsiehd0a4b202019-08-26 11:26:27 -070093
Martijn Coenen0aeee3d2020-11-30 10:06:06 +010094 if (command == "--load-verified-keys") {
95 LoadKeyFromVerifiedPartitions(keyring_id);
96 } else if (command == "--load-extra-key") {
97 if (argc != 3) {
98 LOG(ERROR) << "--load-extra-key requires <key_name> argument.";
99 return -1;
Victor Hsiehd0a4b202019-08-26 11:26:27 -0700100 }
Martijn Coenen0aeee3d2020-11-30 10:06:06 +0100101 LoadKeyFromStdin(keyring_id, argv[2]);
102 } else if (command == "--lock") {
103 // Requires files backed by fs-verity to be verified with a key in .fs-verity
104 // keyring.
105 if (!android::base::WriteStringToFile("1", "/proc/sys/fs/verity/require_signatures")) {
106 PLOG(ERROR) << "Failed to enforce fs-verity signature";
107 }
108
109 if (!android::base::GetBoolProperty("ro.debuggable", false)) {
110 if (keyctl_restrict_keyring(keyring_id, nullptr, nullptr) < 0) {
111 PLOG(ERROR) << "Cannot restrict .fs-verity keyring";
112 }
113 }
114 } else {
115 LOG(ERROR) << "Unknown argument(s).";
116 return -1;
Victor Hsiehd0a4b202019-08-26 11:26:27 -0700117 }
Martijn Coenen0aeee3d2020-11-30 10:06:06 +0100118
Victor Hsiehd0a4b202019-08-26 11:26:27 -0700119 return 0;
120}