blob: ac91ca08d0d19a0688b0e476b5486652c6c2f443 [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
17#include <errno.h>
Josh Gaof61f4142019-10-22 12:30:30 -070018#include <libavb_user/libavb_user.h>
Josh Gaof61f4142019-10-22 12:30:30 -070019#include <stdio.h>
Josh Gaof61f4142019-10-22 12:30:30 -070020
21#include <android-base/file.h>
22#include <android-base/logging.h>
23#include <android-base/properties.h>
Josh Gaof61f4142019-10-22 12:30:30 -070024#include <fs_mgr_overlayfs.h>
Josh Gaof61f4142019-10-22 12:30:30 -070025#include <log/log_properties.h>
26
Yi-Yo Chiang7fd9d4f2022-08-08 13:36:50 +080027using namespace std::string_literals;
28
Josh Gaof61f4142019-10-22 12:30:30 -070029#ifdef ALLOW_DISABLE_VERITY
30static const bool kAllowDisableVerity = true;
31#else
32static const bool kAllowDisableVerity = false;
33#endif
34
Josh Gaof61f4142019-10-22 12:30:30 -070035static void suggest_run_adb_root() {
36 if (getuid() != 0) printf("Maybe run adb root?\n");
37}
38
Josh Gaof61f4142019-10-22 12:30:30 -070039/* Helper function to get A/B suffix, if any. If the device isn't
40 * using A/B the empty string is returned. Otherwise either "_a",
41 * "_b", ... is returned.
42 */
43static std::string get_ab_suffix() {
44 return android::base::GetProperty("ro.boot.slot_suffix", "");
45}
46
47static bool is_avb_device_locked() {
48 return android::base::GetProperty("ro.boot.vbmeta.device_state", "") == "locked";
49}
50
51static bool overlayfs_setup(bool enable) {
52 auto change = false;
53 errno = 0;
54 if (enable ? fs_mgr_overlayfs_teardown(nullptr, &change)
David Andersone9e3f6e2022-08-04 11:18:01 -070055 : fs_mgr_overlayfs_setup(nullptr, &change)) {
Josh Gaof61f4142019-10-22 12:30:30 -070056 if (change) {
57 printf("%s overlayfs\n", enable ? "disabling" : "using");
58 }
59 } else if (errno) {
60 printf("Overlayfs %s failed with error %s\n", enable ? "teardown" : "setup", strerror(errno));
61 suggest_run_adb_root();
62 }
63 return change;
64}
65
66/* Use AVB to turn verity on/off */
67static bool set_avb_verity_enabled_state(AvbOps* ops, bool enable_verity) {
68 std::string ab_suffix = get_ab_suffix();
69 bool verity_enabled;
70
71 if (is_avb_device_locked()) {
72 printf("Device is locked. Please unlock the device first\n");
73 return false;
74 }
75
76 if (!avb_user_verity_get(ops, ab_suffix.c_str(), &verity_enabled)) {
77 printf("Error getting verity state. Try adb root first?\n");
78 return false;
79 }
80
81 if ((verity_enabled && enable_verity) || (!verity_enabled && !enable_verity)) {
82 printf("verity is already %s\n", verity_enabled ? "enabled" : "disabled");
83 return false;
84 }
85
86 if (!avb_user_verity_set(ops, ab_suffix.c_str(), enable_verity)) {
87 printf("Error setting verity\n");
88 return false;
89 }
90
Josh Gaof61f4142019-10-22 12:30:30 -070091 printf("Successfully %s verity\n", enable_verity ? "enabled" : "disabled");
92 return true;
93}
94
95int main(int argc, char* argv[]) {
96 if (argc == 0) {
97 LOG(FATAL) << "set-verity-state called with empty argv";
98 }
99
Yi-Yo Chiang7fd9d4f2022-08-08 13:36:50 +0800100 bool enable = false;
Josh Gaof61f4142019-10-22 12:30:30 -0700101 std::string procname = android::base::Basename(argv[0]);
102 if (procname == "enable-verity") {
Yi-Yo Chiang7fd9d4f2022-08-08 13:36:50 +0800103 enable = true;
Josh Gaof61f4142019-10-22 12:30:30 -0700104 } else if (procname == "disable-verity") {
Yi-Yo Chiang7fd9d4f2022-08-08 13:36:50 +0800105 enable = false;
106 } else if (argc == 2 && (argv[1] == "1"s || argv[1] == "0"s)) {
107 enable = (argv[1] == "1"s);
108 } else {
109 printf("usage: %s [1|0]\n", argv[0]);
110 return 1;
Josh Gaof61f4142019-10-22 12:30:30 -0700111 }
112
Josh Gaof61f4142019-10-22 12:30:30 -0700113 // Figure out if we're using VB1.0 or VB2.0 (aka AVB) - by
114 // contract, androidboot.vbmeta.digest is set by the bootloader
115 // when using AVB).
116 bool using_avb = !android::base::GetProperty("ro.boot.vbmeta.digest", "").empty();
117
118 // If using AVB, dm-verity is used on any build so we want it to
119 // be possible to disable/enable on any build (except USER). For
120 // VB1.0 dm-verity is only enabled on certain builds.
121 if (!using_avb) {
122 if (!kAllowDisableVerity) {
123 printf("%s only works for userdebug builds\n", argv[0]);
124 }
125
126 if (!android::base::GetBoolProperty("ro.secure", false)) {
127 overlayfs_setup(enable);
128 printf("verity not enabled - ENG build\n");
129 return 0;
130 }
131 }
132
133 // Should never be possible to disable dm-verity on a USER build
134 // regardless of using AVB or VB1.0.
135 if (!__android_log_is_debuggable()) {
136 printf("verity cannot be disabled/enabled - USER build\n");
137 return 0;
138 }
139
Yi-Yo Chiang4ef9fcc2022-08-03 19:40:45 +0800140 bool any_changed = false;
Josh Gaof61f4142019-10-22 12:30:30 -0700141 if (using_avb) {
142 // Yep, the system is using AVB.
143 AvbOps* ops = avb_ops_user_new();
144 if (ops == nullptr) {
145 printf("Error getting AVB ops\n");
146 return 1;
147 }
Yi-Yo Chiang4ef9fcc2022-08-03 19:40:45 +0800148 any_changed |= set_avb_verity_enabled_state(ops, enable);
Josh Gaof61f4142019-10-22 12:30:30 -0700149 avb_ops_user_free(ops);
Josh Gaof61f4142019-10-22 12:30:30 -0700150 }
Yi-Yo Chiang4ef9fcc2022-08-03 19:40:45 +0800151 any_changed |= overlayfs_setup(enable);
Josh Gaof61f4142019-10-22 12:30:30 -0700152
153 if (any_changed) {
154 printf("Now reboot your device for settings to take effect\n");
155 }
156
157 return 0;
158}