blob: 3f75dca91a0aba32e4b35c22902186e941f14d24 [file] [log] [blame]
Jeff Vander Stoep3fc82ea2021-11-22 14:45:42 +01001/*
2 * Copyright (C) 2021 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#include <string>
18
19#include <android-base/file.h>
20#include <android-base/logging.h>
21#include <android-base/properties.h>
22#include <fsverity_init.h>
23#include <log/log.h>
24#include <mini_keyctl_utils.h>
25
26int main(int argc, const char** argv) {
27 if (argc < 2) {
28 LOG(ERROR) << "Not enough arguments";
29 return -1;
30 }
31
32 key_serial_t keyring_id = android::GetKeyringId(".fs-verity");
33 if (keyring_id < 0) {
34 LOG(ERROR) << "Failed to find .fs-verity keyring id";
35 return -1;
36 }
37
38 const std::string_view command = argv[1];
39
40 if (command == "--load-verified-keys") {
41 LoadKeyFromVerifiedPartitions(keyring_id);
42 } else if (command == "--load-extra-key") {
43 if (argc != 3) {
44 LOG(ERROR) << "--load-extra-key requires <key_name> argument.";
45 return -1;
46 }
47 if (!LoadKeyFromStdin(keyring_id, argv[2])) {
48 return -1;
49 }
50 } else if (command == "--lock") {
51 // Requires files backed by fs-verity to be verified with a key in .fs-verity
52 // keyring.
53 if (!android::base::WriteStringToFile("1", "/proc/sys/fs/verity/require_signatures")) {
54 PLOG(ERROR) << "Failed to enforce fs-verity signature";
55 }
56
57 if (!android::base::GetBoolProperty("ro.debuggable", false)) {
58 if (keyctl_restrict_keyring(keyring_id, nullptr, nullptr) < 0) {
59 PLOG(ERROR) << "Cannot restrict .fs-verity keyring";
60 }
61 }
62 } else {
63 LOG(ERROR) << "Unknown argument(s).";
64 return -1;
65 }
66
67 return 0;
68}