1 /*
2 * Copyright (C) 2014 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 #define TRACE_TAG ADB
18
19 #include "set_verity_enable_state_service.h"
20 #include "sysdeps.h"
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <inttypes.h>
25 #include <libavb_user/libavb_user.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <sys/mount.h>
29 #include <sys/stat.h>
30
31 #include <android-base/properties.h>
32 #include <android-base/stringprintf.h>
33 #include <fs_mgr.h>
34 #include <fs_mgr_overlayfs.h>
35 #include <fstab/fstab.h>
36 #include <log/log_properties.h>
37
38 #include "adb.h"
39 #include "adb_io.h"
40 #include "adb_unique_fd.h"
41
42 #include "fec/io.h"
43
44 #ifdef ALLOW_ADBD_DISABLE_VERITY
45 static const bool kAllowDisableVerity = true;
46 #else
47 static const bool kAllowDisableVerity = false;
48 #endif
49
suggest_run_adb_root(int fd)50 void suggest_run_adb_root(int fd) {
51 if (getuid() != 0) WriteFdExactly(fd, "Maybe run adb root?\n");
52 }
53
make_block_device_writable(const std::string & dev)54 static bool make_block_device_writable(const std::string& dev) {
55 unique_fd fd(unix_open(dev, O_RDONLY | O_CLOEXEC));
56 if (fd == -1) {
57 return false;
58 }
59
60 int OFF = 0;
61 bool result = (ioctl(fd, BLKROSET, &OFF) != -1);
62 return result;
63 }
64
65 /* Turn verity on/off */
set_verity_enabled_state(int fd,const char * block_device,const char * mount_point,bool enable)66 static bool set_verity_enabled_state(int fd, const char* block_device, const char* mount_point,
67 bool enable) {
68 if (!make_block_device_writable(block_device)) {
69 WriteFdFmt(fd, "Could not make block device %s writable (%s).\n",
70 block_device, strerror(errno));
71 return false;
72 }
73
74 fec::io fh(block_device, O_RDWR);
75
76 if (!fh) {
77 WriteFdFmt(fd, "Could not open block device %s (%s).\n", block_device, strerror(errno));
78 suggest_run_adb_root(fd);
79 return false;
80 }
81
82 fec_verity_metadata metadata;
83
84 if (!fh.get_verity_metadata(metadata)) {
85 WriteFdExactly(fd, "Couldn't find verity metadata!\n");
86 return false;
87 }
88
89 if (!enable && metadata.disabled) {
90 WriteFdFmt(fd, "Verity already disabled on %s\n", mount_point);
91 return false;
92 }
93
94 if (enable && !metadata.disabled) {
95 WriteFdFmt(fd, "Verity already enabled on %s\n", mount_point);
96 return false;
97 }
98
99 if (!fh.set_verity_status(enable)) {
100 WriteFdFmt(fd, "Could not set verity %s flag on device %s with error %s\n",
101 enable ? "enabled" : "disabled",
102 block_device, strerror(errno));
103 return false;
104 }
105
106 auto change = false;
107 errno = 0;
108 if (enable ? fs_mgr_overlayfs_teardown(mount_point, &change)
109 : fs_mgr_overlayfs_setup(nullptr, mount_point, &change)) {
110 if (change) {
111 WriteFdFmt(fd, "%s overlayfs for %s\n", enable ? "disabling" : "using", mount_point);
112 }
113 } else if (errno) {
114 WriteFdFmt(fd, "Overlayfs %s for %s failed with error %s\n", enable ? "teardown" : "setup",
115 mount_point, strerror(errno));
116 }
117 WriteFdFmt(fd, "Verity %s on %s\n", enable ? "enabled" : "disabled", mount_point);
118 return true;
119 }
120
121 /* Helper function to get A/B suffix, if any. If the device isn't
122 * using A/B the empty string is returned. Otherwise either "_a",
123 * "_b", ... is returned.
124 */
get_ab_suffix()125 static std::string get_ab_suffix() {
126 return android::base::GetProperty("ro.boot.slot_suffix", "");
127 }
128
is_avb_device_locked()129 static bool is_avb_device_locked() {
130 return android::base::GetProperty("ro.boot.vbmeta.device_state", "") == "locked";
131 }
132
overlayfs_setup(int fd,bool enable)133 static bool overlayfs_setup(int fd, bool enable) {
134 auto change = false;
135 errno = 0;
136 if (enable ? fs_mgr_overlayfs_teardown(nullptr, &change)
137 : fs_mgr_overlayfs_setup(nullptr, nullptr, &change)) {
138 if (change) {
139 WriteFdFmt(fd, "%s overlayfs\n", enable ? "disabling" : "using");
140 }
141 } else if (errno) {
142 WriteFdFmt(fd, "Overlayfs %s failed with error %s\n", enable ? "teardown" : "setup",
143 strerror(errno));
144 suggest_run_adb_root(fd);
145 }
146 return change;
147 }
148
149 /* Use AVB to turn verity on/off */
set_avb_verity_enabled_state(int fd,AvbOps * ops,bool enable_verity)150 static bool set_avb_verity_enabled_state(int fd, AvbOps* ops, bool enable_verity) {
151 std::string ab_suffix = get_ab_suffix();
152 bool verity_enabled;
153
154 if (is_avb_device_locked()) {
155 WriteFdExactly(fd, "Device is locked. Please unlock the device first\n");
156 return false;
157 }
158
159 if (!avb_user_verity_get(ops, ab_suffix.c_str(), &verity_enabled)) {
160 WriteFdExactly(fd, "Error getting verity state. Try adb root first?\n");
161 return false;
162 }
163
164 if ((verity_enabled && enable_verity) || (!verity_enabled && !enable_verity)) {
165 WriteFdFmt(fd, "verity is already %s\n", verity_enabled ? "enabled" : "disabled");
166 return false;
167 }
168
169 if (!avb_user_verity_set(ops, ab_suffix.c_str(), enable_verity)) {
170 WriteFdExactly(fd, "Error setting verity\n");
171 return false;
172 }
173
174 overlayfs_setup(fd, enable_verity);
175 WriteFdFmt(fd, "Successfully %s verity\n", enable_verity ? "enabled" : "disabled");
176 return true;
177 }
178
set_verity_enabled_state_service(unique_fd fd,bool enable)179 void set_verity_enabled_state_service(unique_fd fd, bool enable) {
180 bool any_changed = false;
181
182 // Figure out if we're using VB1.0 or VB2.0 (aka AVB) - by
183 // contract, androidboot.vbmeta.digest is set by the bootloader
184 // when using AVB).
185 bool using_avb = !android::base::GetProperty("ro.boot.vbmeta.digest", "").empty();
186
187 // If using AVB, dm-verity is used on any build so we want it to
188 // be possible to disable/enable on any build (except USER). For
189 // VB1.0 dm-verity is only enabled on certain builds.
190 if (!using_avb) {
191 if (!kAllowDisableVerity) {
192 WriteFdFmt(fd.get(), "%s-verity only works for userdebug builds\n",
193 enable ? "enable" : "disable");
194 }
195
196 if (!android::base::GetBoolProperty("ro.secure", false)) {
197 overlayfs_setup(fd, enable);
198 WriteFdExactly(fd.get(), "verity not enabled - ENG build\n");
199 return;
200 }
201 }
202
203 // Should never be possible to disable dm-verity on a USER build
204 // regardless of using AVB or VB1.0.
205 if (!__android_log_is_debuggable()) {
206 WriteFdExactly(fd.get(), "verity cannot be disabled/enabled - USER build\n");
207 return;
208 }
209
210 if (using_avb) {
211 // Yep, the system is using AVB.
212 AvbOps* ops = avb_ops_user_new();
213 if (ops == nullptr) {
214 WriteFdExactly(fd.get(), "Error getting AVB ops\n");
215 return;
216 }
217 if (set_avb_verity_enabled_state(fd.get(), ops, enable)) {
218 any_changed = true;
219 }
220 avb_ops_user_free(ops);
221 } else {
222 // Not using AVB - assume VB1.0.
223
224 // read all fstab entries at once from all sources
225 android::fs_mgr::Fstab fstab;
226 if (!android::fs_mgr::ReadDefaultFstab(&fstab)) {
227 WriteFdExactly(fd.get(), "Failed to read fstab\n");
228 suggest_run_adb_root(fd.get());
229 return;
230 }
231
232 // Loop through entries looking for ones that verity manages.
233 for (const auto& entry : fstab) {
234 if (entry.fs_mgr_flags.verify) {
235 if (set_verity_enabled_state(fd.get(), entry.blk_device.c_str(),
236 entry.mount_point.c_str(), enable)) {
237 any_changed = true;
238 }
239 }
240 }
241 }
242 if (!any_changed) any_changed = overlayfs_setup(fd, enable);
243
244 if (any_changed) {
245 WriteFdExactly(fd.get(), "Now reboot your device for settings to take effect\n");
246 }
247 }
248