blob: 1ebfd232df1c9f4e7f79b471d6b6b8b50a938228 [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
89 overlayfs_setup(enable_verity);
90 printf("Successfully %s verity\n", enable_verity ? "enabled" : "disabled");
91 return true;
92}
93
94int main(int argc, char* argv[]) {
95 if (argc == 0) {
96 LOG(FATAL) << "set-verity-state called with empty argv";
97 }
98
99 std::optional<bool> enable_opt;
100 std::string procname = android::base::Basename(argv[0]);
101 if (procname == "enable-verity") {
102 enable_opt = true;
103 } else if (procname == "disable-verity") {
104 enable_opt = false;
105 }
106
107 if (!enable_opt.has_value()) {
108 if (argc != 2) {
109 printf("usage: %s [1|0]\n", argv[0]);
110 return 1;
111 }
112
113 if (strcmp(argv[1], "1") == 0) {
114 enable_opt = true;
115 } else if (strcmp(argv[1], "0") == 0) {
116 enable_opt = false;
117 } else {
118 printf("usage: %s [1|0]\n", argv[0]);
119 return 1;
120 }
121 }
122
123 bool enable = enable_opt.value();
124
125 bool any_changed = false;
126
127 // Figure out if we're using VB1.0 or VB2.0 (aka AVB) - by
128 // contract, androidboot.vbmeta.digest is set by the bootloader
129 // when using AVB).
130 bool using_avb = !android::base::GetProperty("ro.boot.vbmeta.digest", "").empty();
131
132 // If using AVB, dm-verity is used on any build so we want it to
133 // be possible to disable/enable on any build (except USER). For
134 // VB1.0 dm-verity is only enabled on certain builds.
135 if (!using_avb) {
136 if (!kAllowDisableVerity) {
137 printf("%s only works for userdebug builds\n", argv[0]);
138 }
139
140 if (!android::base::GetBoolProperty("ro.secure", false)) {
141 overlayfs_setup(enable);
142 printf("verity not enabled - ENG build\n");
143 return 0;
144 }
145 }
146
147 // Should never be possible to disable dm-verity on a USER build
148 // regardless of using AVB or VB1.0.
149 if (!__android_log_is_debuggable()) {
150 printf("verity cannot be disabled/enabled - USER build\n");
151 return 0;
152 }
153
154 if (using_avb) {
155 // Yep, the system is using AVB.
156 AvbOps* ops = avb_ops_user_new();
157 if (ops == nullptr) {
158 printf("Error getting AVB ops\n");
159 return 1;
160 }
161 if (set_avb_verity_enabled_state(ops, enable)) {
162 any_changed = true;
163 }
164 avb_ops_user_free(ops);
Josh Gaof61f4142019-10-22 12:30:30 -0700165 }
166 if (!any_changed) any_changed = overlayfs_setup(enable);
167
168 if (any_changed) {
169 printf("Now reboot your device for settings to take effect\n");
170 }
171
172 return 0;
173}