blob: 31ceaee66335a3de99dd6e8333c17f1788bb0f8c [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>
Josh Gaof61f4142019-10-22 12:30:30 -070022#include <fs_mgr_overlayfs.h>
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080023#include <libavb_user/libavb_user.h>
Josh Gaof61f4142019-10-22 12:30:30 -070024
Yi-Yo Chiang7fd9d4f2022-08-08 13:36:50 +080025using namespace std::string_literals;
26
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080027namespace {
Josh Gaof61f4142019-10-22 12:30:30 -070028
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080029#ifdef ALLOW_DISABLE_VERITY
30const bool kAllowDisableVerity = true;
31#else
32const bool kAllowDisableVerity = false;
33#endif
Josh Gaof61f4142019-10-22 12:30:30 -070034
Josh Gaof61f4142019-10-22 12:30:30 -070035/* Helper function to get A/B suffix, if any. If the device isn't
36 * using A/B the empty string is returned. Otherwise either "_a",
37 * "_b", ... is returned.
38 */
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080039std::string get_ab_suffix() {
Josh Gaof61f4142019-10-22 12:30:30 -070040 return android::base::GetProperty("ro.boot.slot_suffix", "");
41}
42
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080043bool is_avb_device_locked() {
Josh Gaof61f4142019-10-22 12:30:30 -070044 return android::base::GetProperty("ro.boot.vbmeta.device_state", "") == "locked";
45}
46
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080047bool is_debuggable() {
48 return android::base::GetBoolProperty("ro.debuggable", false);
49}
50
51bool is_using_avb() {
52 // Figure out if we're using VB1.0 or VB2.0 (aka AVB) - by
53 // contract, androidboot.vbmeta.digest is set by the bootloader
54 // when using AVB).
55 return !android::base::GetProperty("ro.boot.vbmeta.digest", "").empty();
56}
57
58bool overlayfs_setup(bool enable) {
Josh Gaof61f4142019-10-22 12:30:30 -070059 auto change = false;
60 errno = 0;
61 if (enable ? fs_mgr_overlayfs_teardown(nullptr, &change)
Ed Chen19814012019-12-17 08:53:13 +000062 : fs_mgr_overlayfs_setup(nullptr, nullptr, &change)) {
Josh Gaof61f4142019-10-22 12:30:30 -070063 if (change) {
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080064 LOG(INFO) << (enable ? "disabling" : "using") << " overlayfs";
Josh Gaof61f4142019-10-22 12:30:30 -070065 }
66 } else if (errno) {
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080067 PLOG(ERROR) << "Failed to " << (enable ? "teardown" : "setup") << " overlayfs";
Josh Gaof61f4142019-10-22 12:30:30 -070068 }
69 return change;
70}
71
72/* Use AVB to turn verity on/off */
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080073bool set_avb_verity_enabled_state(AvbOps* ops, bool enable_verity) {
Josh Gaof61f4142019-10-22 12:30:30 -070074 std::string ab_suffix = get_ab_suffix();
75 bool verity_enabled;
76
77 if (is_avb_device_locked()) {
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080078 LOG(ERROR) << "Device is locked. Please unlock the device first";
Josh Gaof61f4142019-10-22 12:30:30 -070079 return false;
80 }
81
82 if (!avb_user_verity_get(ops, ab_suffix.c_str(), &verity_enabled)) {
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080083 LOG(ERROR) << "Error getting verity state";
Josh Gaof61f4142019-10-22 12:30:30 -070084 return false;
85 }
86
87 if ((verity_enabled && enable_verity) || (!verity_enabled && !enable_verity)) {
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080088 LOG(INFO) << "verity is already " << (verity_enabled ? "enabled" : "disabled");
Josh Gaof61f4142019-10-22 12:30:30 -070089 return false;
90 }
91
92 if (!avb_user_verity_set(ops, ab_suffix.c_str(), enable_verity)) {
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080093 LOG(ERROR) << "Error setting verity state";
Josh Gaof61f4142019-10-22 12:30:30 -070094 return false;
95 }
96
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +080097 LOG(INFO) << "Successfully " << (enable_verity ? "enabled" : "disabled") << " verity";
Josh Gaof61f4142019-10-22 12:30:30 -070098 return true;
99}
100
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800101void MyLogger(android::base::LogId id, android::base::LogSeverity severity, const char* tag,
102 const char* file, unsigned int line, const char* message) {
103 // Hide log starting with '[fs_mgr]' unless it's an error.
104 if (severity == android::base::ERROR || message[0] != '[') {
105 fprintf(stderr, "%s\n", message);
106 }
107 static auto logd = android::base::LogdLogger();
108 logd(id, severity, tag, file, line, message);
109}
110
111} // namespace
112
Josh Gaof61f4142019-10-22 12:30:30 -0700113int main(int argc, char* argv[]) {
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800114 android::base::InitLogging(argv, MyLogger);
115
Josh Gaof61f4142019-10-22 12:30:30 -0700116 if (argc == 0) {
117 LOG(FATAL) << "set-verity-state called with empty argv";
118 }
119
Yi-Yo Chiang7fd9d4f2022-08-08 13:36:50 +0800120 bool enable = false;
Josh Gaof61f4142019-10-22 12:30:30 -0700121 std::string procname = android::base::Basename(argv[0]);
122 if (procname == "enable-verity") {
Yi-Yo Chiang7fd9d4f2022-08-08 13:36:50 +0800123 enable = true;
Josh Gaof61f4142019-10-22 12:30:30 -0700124 } else if (procname == "disable-verity") {
Yi-Yo Chiang7fd9d4f2022-08-08 13:36:50 +0800125 enable = false;
126 } else if (argc == 2 && (argv[1] == "1"s || argv[1] == "0"s)) {
127 enable = (argv[1] == "1"s);
128 } else {
129 printf("usage: %s [1|0]\n", argv[0]);
130 return 1;
Josh Gaof61f4142019-10-22 12:30:30 -0700131 }
132
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800133 if (!kAllowDisableVerity || !is_debuggable()) {
134 errno = EPERM;
135 PLOG(ERROR) << "Cannot disable/enable verity on user build";
136 return 1;
Josh Gaof61f4142019-10-22 12:30:30 -0700137 }
138
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800139 if (getuid() != 0) {
140 errno = EACCES;
141 PLOG(ERROR) << "Must be running as root (adb root)";
142 return 1;
Josh Gaof61f4142019-10-22 12:30:30 -0700143 }
144
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800145 if (!is_using_avb()) {
146 LOG(ERROR) << "Expected AVB device, VB1.0 is no longer supported";
147 return 1;
Josh Gaof61f4142019-10-22 12:30:30 -0700148 }
Yi-Yo Chiang6bb1acb2022-08-08 13:36:50 +0800149
150 std::unique_ptr<AvbOps, decltype(&avb_ops_user_free)> ops(avb_ops_user_new(), &avb_ops_user_free);
151 if (!ops) {
152 LOG(ERROR) << "Error getting AVB ops";
153 return 1;
154 }
155
156 bool any_changed = set_avb_verity_enabled_state(ops.get(), enable);
Yi-Yo Chiang4ef9fcc2022-08-03 19:40:45 +0800157 any_changed |= overlayfs_setup(enable);
Josh Gaof61f4142019-10-22 12:30:30 -0700158
159 if (any_changed) {
160 printf("Now reboot your device for settings to take effect\n");
161 }
162
163 return 0;
164}