blob: 4b7bd5ad10a24eaf4d4c2597ed793ef27e89d781 [file] [log] [blame]
Josh Gaof61f4142019-10-22 12:30:30 -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
Josh Gaof61f4142019-10-22 12:30:30 -070017#include <stdio.h>
Josh Gaof61f4142019-10-22 12:30:30 -070018
19#include <android-base/file.h>
20#include <android-base/logging.h>
21#include <android-base/properties.h>
Yi-Yo Chiang1305c152022-08-10 12:53:39 +080022#include <binder/ProcessState.h>
Josh Gaof61f4142019-10-22 12:30:30 -070023#include <fs_mgr_overlayfs.h>
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080024#include <libavb_user/libavb_user.h>
Josh Gaof61f4142019-10-22 12:30:30 -070025
Yi-Yo Chiang7fd9d4f2022-08-08 13:36:50 +080026using namespace std::string_literals;
27
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080028namespace {
Josh Gaof61f4142019-10-22 12:30:30 -070029
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080030#ifdef ALLOW_DISABLE_VERITY
31const bool kAllowDisableVerity = true;
32#else
33const bool kAllowDisableVerity = false;
34#endif
Josh Gaof61f4142019-10-22 12:30:30 -070035
Josh Gaof61f4142019-10-22 12:30:30 -070036/* Helper function to get A/B suffix, if any. If the device isn't
37 * using A/B the empty string is returned. Otherwise either "_a",
38 * "_b", ... is returned.
39 */
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080040std::string get_ab_suffix() {
Josh Gaof61f4142019-10-22 12:30:30 -070041 return android::base::GetProperty("ro.boot.slot_suffix", "");
42}
43
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080044bool is_avb_device_locked() {
Josh Gaof61f4142019-10-22 12:30:30 -070045 return android::base::GetProperty("ro.boot.vbmeta.device_state", "") == "locked";
46}
47
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080048bool is_debuggable() {
49 return android::base::GetBoolProperty("ro.debuggable", false);
50}
51
52bool is_using_avb() {
53 // Figure out if we're using VB1.0 or VB2.0 (aka AVB) - by
54 // contract, androidboot.vbmeta.digest is set by the bootloader
55 // when using AVB).
56 return !android::base::GetProperty("ro.boot.vbmeta.digest", "").empty();
57}
58
59bool overlayfs_setup(bool enable) {
Josh Gaof61f4142019-10-22 12:30:30 -070060 auto change = false;
61 errno = 0;
62 if (enable ? fs_mgr_overlayfs_teardown(nullptr, &change)
David Andersone9e3f6e2022-08-04 11:18:01 -070063 : fs_mgr_overlayfs_setup(nullptr, &change)) {
Josh Gaof61f4142019-10-22 12:30:30 -070064 if (change) {
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080065 LOG(INFO) << (enable ? "disabling" : "using") << " overlayfs";
Josh Gaof61f4142019-10-22 12:30:30 -070066 }
67 } else if (errno) {
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080068 PLOG(ERROR) << "Failed to " << (enable ? "teardown" : "setup") << " overlayfs";
Josh Gaof61f4142019-10-22 12:30:30 -070069 }
70 return change;
71}
72
73/* Use AVB to turn verity on/off */
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080074bool set_avb_verity_enabled_state(AvbOps* ops, bool enable_verity) {
Josh Gaof61f4142019-10-22 12:30:30 -070075 std::string ab_suffix = get_ab_suffix();
76 bool verity_enabled;
77
78 if (is_avb_device_locked()) {
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080079 LOG(ERROR) << "Device is locked. Please unlock the device first";
Josh Gaof61f4142019-10-22 12:30:30 -070080 return false;
81 }
82
83 if (!avb_user_verity_get(ops, ab_suffix.c_str(), &verity_enabled)) {
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080084 LOG(ERROR) << "Error getting verity state";
Josh Gaof61f4142019-10-22 12:30:30 -070085 return false;
86 }
87
88 if ((verity_enabled && enable_verity) || (!verity_enabled && !enable_verity)) {
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080089 LOG(INFO) << "verity is already " << (verity_enabled ? "enabled" : "disabled");
Josh Gaof61f4142019-10-22 12:30:30 -070090 return false;
91 }
92
93 if (!avb_user_verity_set(ops, ab_suffix.c_str(), enable_verity)) {
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080094 LOG(ERROR) << "Error setting verity state";
Josh Gaof61f4142019-10-22 12:30:30 -070095 return false;
96 }
97
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080098 LOG(INFO) << "Successfully " << (enable_verity ? "enabled" : "disabled") << " verity";
Josh Gaof61f4142019-10-22 12:30:30 -070099 return true;
100}
101
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800102void MyLogger(android::base::LogId id, android::base::LogSeverity severity, const char* tag,
103 const char* file, unsigned int line, const char* message) {
104 // Hide log starting with '[fs_mgr]' unless it's an error.
105 if (severity == android::base::ERROR || message[0] != '[') {
106 fprintf(stderr, "%s\n", message);
107 }
108 static auto logd = android::base::LogdLogger();
109 logd(id, severity, tag, file, line, message);
110}
111
112} // namespace
113
Josh Gaof61f4142019-10-22 12:30:30 -0700114int main(int argc, char* argv[]) {
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800115 android::base::InitLogging(argv, MyLogger);
116
Josh Gaof61f4142019-10-22 12:30:30 -0700117 if (argc == 0) {
118 LOG(FATAL) << "set-verity-state called with empty argv";
119 }
120
Yi-Yo Chiang7fd9d4f2022-08-08 13:36:50 +0800121 bool enable = false;
Josh Gaof61f4142019-10-22 12:30:30 -0700122 std::string procname = android::base::Basename(argv[0]);
123 if (procname == "enable-verity") {
Yi-Yo Chiang7fd9d4f2022-08-08 13:36:50 +0800124 enable = true;
Josh Gaof61f4142019-10-22 12:30:30 -0700125 } else if (procname == "disable-verity") {
Yi-Yo Chiang7fd9d4f2022-08-08 13:36:50 +0800126 enable = false;
127 } else if (argc == 2 && (argv[1] == "1"s || argv[1] == "0"s)) {
128 enable = (argv[1] == "1"s);
129 } else {
130 printf("usage: %s [1|0]\n", argv[0]);
131 return 1;
Josh Gaof61f4142019-10-22 12:30:30 -0700132 }
133
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800134 if (!kAllowDisableVerity || !is_debuggable()) {
135 errno = EPERM;
136 PLOG(ERROR) << "Cannot disable/enable verity on user build";
137 return 1;
Josh Gaof61f4142019-10-22 12:30:30 -0700138 }
139
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800140 if (getuid() != 0) {
141 errno = EACCES;
142 PLOG(ERROR) << "Must be running as root (adb root)";
143 return 1;
Josh Gaof61f4142019-10-22 12:30:30 -0700144 }
145
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800146 if (!is_using_avb()) {
147 LOG(ERROR) << "Expected AVB device, VB1.0 is no longer supported";
148 return 1;
Josh Gaof61f4142019-10-22 12:30:30 -0700149 }
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800150
151 std::unique_ptr<AvbOps, decltype(&avb_ops_user_free)> ops(avb_ops_user_new(), &avb_ops_user_free);
152 if (!ops) {
153 LOG(ERROR) << "Error getting AVB ops";
154 return 1;
155 }
156
Yi-Yo Chiang1305c152022-08-10 12:53:39 +0800157 // Start a threadpool to service waitForService() callbacks as
158 // fs_mgr_overlayfs_* might call waitForService() to get the image service.
159 android::ProcessState::self()->startThreadPool();
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800160 bool any_changed = set_avb_verity_enabled_state(ops.get(), enable);
Yi-Yo Chiang4ef9fcc2022-08-03 19:40:45 +0800161 any_changed |= overlayfs_setup(enable);
Josh Gaof61f4142019-10-22 12:30:30 -0700162
163 if (any_changed) {
164 printf("Now reboot your device for settings to take effect\n");
165 }
166
167 return 0;
168}