blob: e1322632be1f9b4527c14593d3b312bf8cfdadd3 [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
Josh Gaof61f4142019-10-22 12:30:30 -070027#ifdef ALLOW_DISABLE_VERITY
28static const bool kAllowDisableVerity = true;
29#else
30static const bool kAllowDisableVerity = false;
31#endif
32
Josh Gaof61f4142019-10-22 12:30:30 -070033static void suggest_run_adb_root() {
34 if (getuid() != 0) printf("Maybe run adb root?\n");
35}
36
Josh Gaof61f4142019-10-22 12:30:30 -070037/* Helper function to get A/B suffix, if any. If the device isn't
38 * using A/B the empty string is returned. Otherwise either "_a",
39 * "_b", ... is returned.
40 */
41static std::string get_ab_suffix() {
42 return android::base::GetProperty("ro.boot.slot_suffix", "");
43}
44
45static bool is_avb_device_locked() {
46 return android::base::GetProperty("ro.boot.vbmeta.device_state", "") == "locked";
47}
48
49static bool overlayfs_setup(bool enable) {
50 auto change = false;
51 errno = 0;
52 if (enable ? fs_mgr_overlayfs_teardown(nullptr, &change)
Ed Chen19814012019-12-17 08:53:13 +000053 : fs_mgr_overlayfs_setup(nullptr, nullptr, &change)) {
Josh Gaof61f4142019-10-22 12:30:30 -070054 if (change) {
55 printf("%s overlayfs\n", enable ? "disabling" : "using");
56 }
57 } else if (errno) {
58 printf("Overlayfs %s failed with error %s\n", enable ? "teardown" : "setup", strerror(errno));
59 suggest_run_adb_root();
60 }
61 return change;
62}
63
64/* Use AVB to turn verity on/off */
65static bool set_avb_verity_enabled_state(AvbOps* ops, bool enable_verity) {
66 std::string ab_suffix = get_ab_suffix();
67 bool verity_enabled;
68
69 if (is_avb_device_locked()) {
70 printf("Device is locked. Please unlock the device first\n");
71 return false;
72 }
73
74 if (!avb_user_verity_get(ops, ab_suffix.c_str(), &verity_enabled)) {
75 printf("Error getting verity state. Try adb root first?\n");
76 return false;
77 }
78
79 if ((verity_enabled && enable_verity) || (!verity_enabled && !enable_verity)) {
80 printf("verity is already %s\n", verity_enabled ? "enabled" : "disabled");
81 return false;
82 }
83
84 if (!avb_user_verity_set(ops, ab_suffix.c_str(), enable_verity)) {
85 printf("Error setting verity\n");
86 return false;
87 }
88
Josh Gaof61f4142019-10-22 12:30:30 -070089 printf("Successfully %s verity\n", enable_verity ? "enabled" : "disabled");
90 return true;
91}
92
93int main(int argc, char* argv[]) {
94 if (argc == 0) {
95 LOG(FATAL) << "set-verity-state called with empty argv";
96 }
97
98 std::optional<bool> enable_opt;
99 std::string procname = android::base::Basename(argv[0]);
100 if (procname == "enable-verity") {
101 enable_opt = true;
102 } else if (procname == "disable-verity") {
103 enable_opt = false;
104 }
105
106 if (!enable_opt.has_value()) {
107 if (argc != 2) {
108 printf("usage: %s [1|0]\n", argv[0]);
109 return 1;
110 }
111
112 if (strcmp(argv[1], "1") == 0) {
113 enable_opt = true;
114 } else if (strcmp(argv[1], "0") == 0) {
115 enable_opt = false;
116 } else {
117 printf("usage: %s [1|0]\n", argv[0]);
118 return 1;
119 }
120 }
121
122 bool enable = enable_opt.value();
123
Josh Gaof61f4142019-10-22 12:30:30 -0700124 // Figure out if we're using VB1.0 or VB2.0 (aka AVB) - by
125 // contract, androidboot.vbmeta.digest is set by the bootloader
126 // when using AVB).
127 bool using_avb = !android::base::GetProperty("ro.boot.vbmeta.digest", "").empty();
128
129 // If using AVB, dm-verity is used on any build so we want it to
130 // be possible to disable/enable on any build (except USER). For
131 // VB1.0 dm-verity is only enabled on certain builds.
132 if (!using_avb) {
133 if (!kAllowDisableVerity) {
134 printf("%s only works for userdebug builds\n", argv[0]);
135 }
136
137 if (!android::base::GetBoolProperty("ro.secure", false)) {
138 overlayfs_setup(enable);
139 printf("verity not enabled - ENG build\n");
140 return 0;
141 }
142 }
143
144 // Should never be possible to disable dm-verity on a USER build
145 // regardless of using AVB or VB1.0.
146 if (!__android_log_is_debuggable()) {
147 printf("verity cannot be disabled/enabled - USER build\n");
148 return 0;
149 }
150
Yi-Yo Chiang4ef9fcc2022-08-03 19:40:45 +0800151 bool any_changed = false;
Josh Gaof61f4142019-10-22 12:30:30 -0700152 if (using_avb) {
153 // Yep, the system is using AVB.
154 AvbOps* ops = avb_ops_user_new();
155 if (ops == nullptr) {
156 printf("Error getting AVB ops\n");
157 return 1;
158 }
Yi-Yo Chiang4ef9fcc2022-08-03 19:40:45 +0800159 any_changed |= set_avb_verity_enabled_state(ops, enable);
Josh Gaof61f4142019-10-22 12:30:30 -0700160 avb_ops_user_free(ops);
Josh Gaof61f4142019-10-22 12:30:30 -0700161 }
Yi-Yo Chiang4ef9fcc2022-08-03 19:40:45 +0800162 any_changed |= overlayfs_setup(enable);
Josh Gaof61f4142019-10-22 12:30:30 -0700163
164 if (any_changed) {
165 printf("Now reboot your device for settings to take effect\n");
166 }
167
168 return 0;
169}