blob: 52757b618487a09d22dca299ed1ca76e36c88a58 [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;
Yi-Yo Chiang258f0232022-08-08 19:23:48 +080062 if (enable ? fs_mgr_overlayfs_setup(nullptr, &change)
63 : fs_mgr_overlayfs_teardown(nullptr, &change)) {
Josh Gaof61f4142019-10-22 12:30:30 -070064 if (change) {
Yi-Yo Chiang258f0232022-08-08 19:23:48 +080065 LOG(INFO) << (enable ? "Enabled" : "Disabled") << " overlayfs";
Josh Gaof61f4142019-10-22 12:30:30 -070066 }
Yi-Yo Chiang258f0232022-08-08 19:23:48 +080067 } else {
68 LOG(ERROR) << "Failed to " << (enable ? "enable" : "disable") << " overlayfs";
Josh Gaof61f4142019-10-22 12:30:30 -070069 }
70 return change;
71}
72
Yi-Yo Chiang258f0232022-08-08 19:23:48 +080073struct SetVerityStateResult {
74 bool success = false;
75 bool want_reboot = false;
76};
77
Josh Gaof61f4142019-10-22 12:30:30 -070078/* Use AVB to turn verity on/off */
Yi-Yo Chiang258f0232022-08-08 19:23:48 +080079SetVerityStateResult SetVerityState(bool enable_verity) {
Josh Gaof61f4142019-10-22 12:30:30 -070080 std::string ab_suffix = get_ab_suffix();
Yi-Yo Chiang258f0232022-08-08 19:23:48 +080081 bool verity_enabled = false;
Josh Gaof61f4142019-10-22 12:30:30 -070082
83 if (is_avb_device_locked()) {
Yi-Yo Chiang258f0232022-08-08 19:23:48 +080084 LOG(ERROR) << "Device must be bootloader unlocked to change verity state";
85 return {};
Josh Gaof61f4142019-10-22 12:30:30 -070086 }
87
Yi-Yo Chiang258f0232022-08-08 19:23:48 +080088 std::unique_ptr<AvbOps, decltype(&avb_ops_user_free)> ops(avb_ops_user_new(), &avb_ops_user_free);
89 if (!ops) {
90 LOG(ERROR) << "Error getting AVB ops";
91 return {};
92 }
93
94 if (!avb_user_verity_get(ops.get(), ab_suffix.c_str(), &verity_enabled)) {
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080095 LOG(ERROR) << "Error getting verity state";
Yi-Yo Chiang258f0232022-08-08 19:23:48 +080096 return {};
Josh Gaof61f4142019-10-22 12:30:30 -070097 }
98
99 if ((verity_enabled && enable_verity) || (!verity_enabled && !enable_verity)) {
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800100 LOG(INFO) << "Verity is already " << (verity_enabled ? "enabled" : "disabled");
101 return {.success = true, .want_reboot = false};
Josh Gaof61f4142019-10-22 12:30:30 -0700102 }
103
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800104 if (!avb_user_verity_set(ops.get(), ab_suffix.c_str(), enable_verity)) {
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800105 LOG(ERROR) << "Error setting verity state";
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800106 return {};
Josh Gaof61f4142019-10-22 12:30:30 -0700107 }
108
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800109 LOG(INFO) << "Successfully " << (enable_verity ? "enabled" : "disabled") << " verity";
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800110 return {.success = true, .want_reboot = true};
Josh Gaof61f4142019-10-22 12:30:30 -0700111}
112
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800113void MyLogger(android::base::LogId id, android::base::LogSeverity severity, const char* tag,
114 const char* file, unsigned int line, const char* message) {
115 // Hide log starting with '[fs_mgr]' unless it's an error.
116 if (severity == android::base::ERROR || message[0] != '[') {
117 fprintf(stderr, "%s\n", message);
118 }
119 static auto logd = android::base::LogdLogger();
120 logd(id, severity, tag, file, line, message);
121}
122
123} // namespace
124
Josh Gaof61f4142019-10-22 12:30:30 -0700125int main(int argc, char* argv[]) {
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800126 android::base::InitLogging(argv, MyLogger);
127
Josh Gaof61f4142019-10-22 12:30:30 -0700128 if (argc == 0) {
129 LOG(FATAL) << "set-verity-state called with empty argv";
130 }
131
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800132 bool enable_verity = false;
Josh Gaof61f4142019-10-22 12:30:30 -0700133 std::string procname = android::base::Basename(argv[0]);
134 if (procname == "enable-verity") {
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800135 enable_verity = true;
Josh Gaof61f4142019-10-22 12:30:30 -0700136 } else if (procname == "disable-verity") {
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800137 enable_verity = false;
Yi-Yo Chiang7fd9d4f2022-08-08 13:36:50 +0800138 } else if (argc == 2 && (argv[1] == "1"s || argv[1] == "0"s)) {
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800139 enable_verity = (argv[1] == "1"s);
Yi-Yo Chiang7fd9d4f2022-08-08 13:36:50 +0800140 } else {
141 printf("usage: %s [1|0]\n", argv[0]);
142 return 1;
Josh Gaof61f4142019-10-22 12:30:30 -0700143 }
144
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800145 if (!kAllowDisableVerity || !is_debuggable()) {
146 errno = EPERM;
147 PLOG(ERROR) << "Cannot disable/enable verity on user build";
148 return 1;
Josh Gaof61f4142019-10-22 12:30:30 -0700149 }
150
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800151 if (getuid() != 0) {
152 errno = EACCES;
153 PLOG(ERROR) << "Must be running as root (adb root)";
154 return 1;
Josh Gaof61f4142019-10-22 12:30:30 -0700155 }
156
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800157 if (!is_using_avb()) {
158 LOG(ERROR) << "Expected AVB device, VB1.0 is no longer supported";
159 return 1;
Josh Gaof61f4142019-10-22 12:30:30 -0700160 }
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800161
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800162 int exit_code = 0;
163 bool want_reboot = false;
164
165 auto ret = SetVerityState(enable_verity);
166 if (ret.success) {
167 want_reboot |= ret.want_reboot;
168 } else {
169 exit_code = 1;
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800170 }
171
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800172 // Disable any overlayfs unconditionally if we want verity enabled.
173 // Enable overlayfs only if verity is successfully disabled or is already disabled.
174 if (enable_verity || ret.success) {
175 // Start a threadpool to service waitForService() callbacks as
176 // fs_mgr_overlayfs_* might call waitForService() to get the image service.
177 android::ProcessState::self()->startThreadPool();
178 want_reboot |= overlayfs_setup(!enable_verity);
Josh Gaof61f4142019-10-22 12:30:30 -0700179 }
180
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800181 if (want_reboot) {
182 printf("Reboot the device for new settings to take effect\n");
183 }
184
185 return exit_code;
Josh Gaof61f4142019-10-22 12:30:30 -0700186}