blob: de9a45259482409db7f8a425ffe487d026b1419d [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
Yi-Yo Chiang86674952022-08-17 04:12:28 +080017#include <getopt.h>
Josh Gaof61f4142019-10-22 12:30:30 -070018#include <stdio.h>
Josh Gaof61f4142019-10-22 12:30:30 -070019
20#include <android-base/file.h>
21#include <android-base/logging.h>
22#include <android-base/properties.h>
Yi-Yo Chiang1305c152022-08-10 12:53:39 +080023#include <binder/ProcessState.h>
Yi-Yo Chiang86674952022-08-17 04:12:28 +080024#include <cutils/android_reboot.h>
Josh Gaof61f4142019-10-22 12:30:30 -070025#include <fs_mgr_overlayfs.h>
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080026#include <libavb_user/libavb_user.h>
Josh Gaof61f4142019-10-22 12:30:30 -070027
Yi-Yo Chiang7fd9d4f2022-08-08 13:36:50 +080028using namespace std::string_literals;
29
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080030namespace {
Josh Gaof61f4142019-10-22 12:30:30 -070031
Yi-Yo Chiang86674952022-08-17 04:12:28 +080032void print_usage() {
33 printf(
34 "Usage:\n"
35 "\tdisable-verity\n"
36 "\tenable-verity\n"
37 "\tset-verity-state [0|1]\n"
38 "Options:\n"
39 "\t-h --help\tthis help\n"
40 "\t-R --reboot\tautomatic reboot if needed for new settings to take effect\n"
41 "\t-v --verbose\tbe noisy\n");
42}
43
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080044#ifdef ALLOW_DISABLE_VERITY
45const bool kAllowDisableVerity = true;
46#else
47const bool kAllowDisableVerity = false;
48#endif
Josh Gaof61f4142019-10-22 12:30:30 -070049
Josh Gaof61f4142019-10-22 12:30:30 -070050/* Helper function to get A/B suffix, if any. If the device isn't
51 * using A/B the empty string is returned. Otherwise either "_a",
52 * "_b", ... is returned.
53 */
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080054std::string get_ab_suffix() {
Josh Gaof61f4142019-10-22 12:30:30 -070055 return android::base::GetProperty("ro.boot.slot_suffix", "");
56}
57
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080058bool is_avb_device_locked() {
Josh Gaof61f4142019-10-22 12:30:30 -070059 return android::base::GetProperty("ro.boot.vbmeta.device_state", "") == "locked";
60}
61
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080062bool is_debuggable() {
63 return android::base::GetBoolProperty("ro.debuggable", false);
64}
65
66bool is_using_avb() {
67 // Figure out if we're using VB1.0 or VB2.0 (aka AVB) - by
68 // contract, androidboot.vbmeta.digest is set by the bootloader
69 // when using AVB).
70 return !android::base::GetProperty("ro.boot.vbmeta.digest", "").empty();
71}
72
Yi-Yo Chiang86674952022-08-17 04:12:28 +080073[[noreturn]] void reboot(const std::string& name) {
74 LOG(INFO) << "Rebooting device for new settings to take effect";
75 ::sync();
76 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot," + name);
77 ::sleep(60);
78 LOG(ERROR) << "Failed to reboot";
79 ::exit(1);
80}
81
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080082bool overlayfs_setup(bool enable) {
Josh Gaof61f4142019-10-22 12:30:30 -070083 auto change = false;
84 errno = 0;
Yi-Yo Chiang258f0232022-08-08 19:23:48 +080085 if (enable ? fs_mgr_overlayfs_setup(nullptr, &change)
86 : fs_mgr_overlayfs_teardown(nullptr, &change)) {
Josh Gaof61f4142019-10-22 12:30:30 -070087 if (change) {
Yi-Yo Chiang258f0232022-08-08 19:23:48 +080088 LOG(INFO) << (enable ? "Enabled" : "Disabled") << " overlayfs";
Josh Gaof61f4142019-10-22 12:30:30 -070089 }
Yi-Yo Chiang258f0232022-08-08 19:23:48 +080090 } else {
91 LOG(ERROR) << "Failed to " << (enable ? "enable" : "disable") << " overlayfs";
Josh Gaof61f4142019-10-22 12:30:30 -070092 }
93 return change;
94}
95
Yi-Yo Chiang258f0232022-08-08 19:23:48 +080096struct SetVerityStateResult {
97 bool success = false;
98 bool want_reboot = false;
99};
100
Josh Gaof61f4142019-10-22 12:30:30 -0700101/* Use AVB to turn verity on/off */
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800102SetVerityStateResult SetVerityState(bool enable_verity) {
Josh Gaof61f4142019-10-22 12:30:30 -0700103 std::string ab_suffix = get_ab_suffix();
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800104 bool verity_enabled = false;
Josh Gaof61f4142019-10-22 12:30:30 -0700105
106 if (is_avb_device_locked()) {
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800107 LOG(ERROR) << "Device must be bootloader unlocked to change verity state";
108 return {};
Josh Gaof61f4142019-10-22 12:30:30 -0700109 }
110
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800111 std::unique_ptr<AvbOps, decltype(&avb_ops_user_free)> ops(avb_ops_user_new(), &avb_ops_user_free);
112 if (!ops) {
113 LOG(ERROR) << "Error getting AVB ops";
114 return {};
115 }
116
117 if (!avb_user_verity_get(ops.get(), ab_suffix.c_str(), &verity_enabled)) {
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800118 LOG(ERROR) << "Error getting verity state";
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800119 return {};
Josh Gaof61f4142019-10-22 12:30:30 -0700120 }
121
122 if ((verity_enabled && enable_verity) || (!verity_enabled && !enable_verity)) {
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800123 LOG(INFO) << "Verity is already " << (verity_enabled ? "enabled" : "disabled");
124 return {.success = true, .want_reboot = false};
Josh Gaof61f4142019-10-22 12:30:30 -0700125 }
126
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800127 if (!avb_user_verity_set(ops.get(), ab_suffix.c_str(), enable_verity)) {
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800128 LOG(ERROR) << "Error setting verity state";
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800129 return {};
Josh Gaof61f4142019-10-22 12:30:30 -0700130 }
131
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800132 LOG(INFO) << "Successfully " << (enable_verity ? "enabled" : "disabled") << " verity";
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800133 return {.success = true, .want_reboot = true};
Josh Gaof61f4142019-10-22 12:30:30 -0700134}
135
Yi-Yo Chiang86674952022-08-17 04:12:28 +0800136class MyLogger {
137 public:
138 explicit MyLogger(bool verbose) : verbose_(verbose) {}
139
140 void operator()(android::base::LogId id, android::base::LogSeverity severity, const char* tag,
141 const char* file, unsigned int line, const char* message) {
142 // Hide log starting with '[fs_mgr]' unless it's an error.
143 if (verbose_ || severity >= android::base::ERROR || message[0] != '[') {
144 fprintf(stderr, "%s\n", message);
145 }
146 logd_(id, severity, tag, file, line, message);
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800147 }
Yi-Yo Chiang86674952022-08-17 04:12:28 +0800148
149 private:
150 android::base::LogdLogger logd_;
151 bool verbose_;
152};
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800153
154} // namespace
155
Josh Gaof61f4142019-10-22 12:30:30 -0700156int main(int argc, char* argv[]) {
Yi-Yo Chiang86674952022-08-17 04:12:28 +0800157 bool auto_reboot = false;
158 bool verbose = false;
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800159
Yi-Yo Chiang86674952022-08-17 04:12:28 +0800160 struct option longopts[] = {
161 {"help", no_argument, nullptr, 'h'},
162 {"reboot", no_argument, nullptr, 'R'},
163 {"verbose", no_argument, nullptr, 'v'},
164 {0, 0, nullptr, 0},
165 };
166 for (int opt; (opt = ::getopt_long(argc, argv, "hRv", longopts, nullptr)) != -1;) {
167 switch (opt) {
168 case 'h':
169 print_usage();
170 return 0;
171 case 'R':
172 auto_reboot = true;
173 break;
174 case 'v':
175 verbose = true;
176 break;
177 default:
178 print_usage();
179 return 1;
180 }
Josh Gaof61f4142019-10-22 12:30:30 -0700181 }
182
Yi-Yo Chiang86674952022-08-17 04:12:28 +0800183 android::base::InitLogging(argv, MyLogger(verbose));
184
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800185 bool enable_verity = false;
Yi-Yo Chiang86674952022-08-17 04:12:28 +0800186 const std::string progname = getprogname();
187 if (progname == "enable-verity") {
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800188 enable_verity = true;
Yi-Yo Chiang86674952022-08-17 04:12:28 +0800189 } else if (progname == "disable-verity") {
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800190 enable_verity = false;
Yi-Yo Chiang86674952022-08-17 04:12:28 +0800191 } else if (optind < argc && (argv[optind] == "1"s || argv[optind] == "0"s)) {
192 // progname "set-verity-state"
193 enable_verity = (argv[optind] == "1"s);
Yi-Yo Chiang7fd9d4f2022-08-08 13:36:50 +0800194 } else {
Yi-Yo Chiang86674952022-08-17 04:12:28 +0800195 print_usage();
Yi-Yo Chiang7fd9d4f2022-08-08 13:36:50 +0800196 return 1;
Josh Gaof61f4142019-10-22 12:30:30 -0700197 }
198
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800199 if (!kAllowDisableVerity || !is_debuggable()) {
200 errno = EPERM;
201 PLOG(ERROR) << "Cannot disable/enable verity on user build";
202 return 1;
Josh Gaof61f4142019-10-22 12:30:30 -0700203 }
204
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800205 if (getuid() != 0) {
206 errno = EACCES;
207 PLOG(ERROR) << "Must be running as root (adb root)";
208 return 1;
Josh Gaof61f4142019-10-22 12:30:30 -0700209 }
210
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800211 if (!is_using_avb()) {
212 LOG(ERROR) << "Expected AVB device, VB1.0 is no longer supported";
213 return 1;
Josh Gaof61f4142019-10-22 12:30:30 -0700214 }
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800215
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800216 int exit_code = 0;
217 bool want_reboot = false;
218
219 auto ret = SetVerityState(enable_verity);
220 if (ret.success) {
221 want_reboot |= ret.want_reboot;
222 } else {
223 exit_code = 1;
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800224 }
225
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800226 // Disable any overlayfs unconditionally if we want verity enabled.
227 // Enable overlayfs only if verity is successfully disabled or is already disabled.
228 if (enable_verity || ret.success) {
229 // Start a threadpool to service waitForService() callbacks as
230 // fs_mgr_overlayfs_* might call waitForService() to get the image service.
231 android::ProcessState::self()->startThreadPool();
232 want_reboot |= overlayfs_setup(!enable_verity);
Josh Gaof61f4142019-10-22 12:30:30 -0700233 }
234
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800235 if (want_reboot) {
Yi-Yo Chiang86674952022-08-17 04:12:28 +0800236 if (auto_reboot) {
237 reboot(progname);
238 }
Yi-Yo Chiang258f0232022-08-08 19:23:48 +0800239 printf("Reboot the device for new settings to take effect\n");
240 }
241
242 return exit_code;
Josh Gaof61f4142019-10-22 12:30:30 -0700243}