blob: 0fcf89b7cc841fe669eca2e3060df77ab85a6113 [file] [log] [blame]
Paul Lawrenceec900bb2014-10-09 14:22:49 +00001/*
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
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG ADB
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
Dan Albert1f09bda2015-01-26 17:49:17 -080021#include <fcntl.h>
22#include <inttypes.h>
David Zeuthen1e033d62017-04-12 16:07:06 -040023#include <libavb_user/libavb_user.h>
Dan Albert1f09bda2015-01-26 17:49:17 -080024#include <stdarg.h>
Dan Albert1f09bda2015-01-26 17:49:17 -080025#include <stdio.h>
26#include <sys/stat.h>
Paul Lawrenceec900bb2014-10-09 14:22:49 +000027
Elliott Hughesffdec182016-09-23 15:40:03 -070028#include "android-base/properties.h"
29#include "android-base/stringprintf.h"
Steven Morelandd73be1b2017-04-13 23:48:57 -070030#include <log/log_properties.h>
Dan Albert33134262015-03-19 15:21:08 -070031
32#include "adb.h"
Elliott Hughesab52c182015-05-01 17:04:38 -070033#include "adb_io.h"
Elliott Hughesffdec182016-09-23 15:40:03 -070034#include "adb_unique_fd.h"
Dan Albert1f09bda2015-01-26 17:49:17 -080035#include "fs_mgr.h"
Elliott Hughesec7a6672015-03-16 21:58:32 +000036#include "remount_service.h"
Paul Lawrenceec900bb2014-10-09 14:22:49 +000037
Sami Tolvanen8ad80762015-10-20 13:24:24 +010038#include "fec/io.h"
39
Paul Lawrenceec900bb2014-10-09 14:22:49 +000040struct fstab *fstab;
41
Dan Albert1f09bda2015-01-26 17:49:17 -080042#ifdef ALLOW_ADBD_DISABLE_VERITY
43static const bool kAllowDisableVerity = true;
44#else
45static const bool kAllowDisableVerity = false;
46#endif
47
Paul Lawrence982089d2014-12-03 15:31:57 -080048/* Turn verity on/off */
David Zeuthen1e033d62017-04-12 16:07:06 -040049static bool set_verity_enabled_state(int fd, const char* block_device, const char* mount_point,
50 bool enable) {
Elliott Hughes9aa4fda2015-05-11 11:55:25 -070051 if (!make_block_device_writable(block_device)) {
Elliott Hughesab52c182015-05-01 17:04:38 -070052 WriteFdFmt(fd, "Could not make block device %s writable (%s).\n",
53 block_device, strerror(errno));
David Zeuthen1e033d62017-04-12 16:07:06 -040054 return false;
Sami Tolvanen13449cd2015-01-02 13:30:50 +000055 }
56
Sami Tolvanen8ad80762015-10-20 13:24:24 +010057 fec::io fh(block_device, O_RDWR);
58
59 if (!fh) {
Elliott Hughesab52c182015-05-01 17:04:38 -070060 WriteFdFmt(fd, "Could not open block device %s (%s).\n", block_device, strerror(errno));
Sami Tolvanen8ad80762015-10-20 13:24:24 +010061 WriteFdFmt(fd, "Maybe run adb root?\n");
David Zeuthen1e033d62017-04-12 16:07:06 -040062 return false;
Paul Lawrenceec900bb2014-10-09 14:22:49 +000063 }
64
Sami Tolvanen8ad80762015-10-20 13:24:24 +010065 fec_verity_metadata metadata;
66
67 if (!fh.get_verity_metadata(metadata)) {
68 WriteFdFmt(fd, "Couldn't find verity metadata!\n");
David Zeuthen1e033d62017-04-12 16:07:06 -040069 return false;
Paul Lawrenceec900bb2014-10-09 14:22:49 +000070 }
71
Sami Tolvanen8ad80762015-10-20 13:24:24 +010072 if (!enable && metadata.disabled) {
Elliott Hughesab52c182015-05-01 17:04:38 -070073 WriteFdFmt(fd, "Verity already disabled on %s\n", mount_point);
David Zeuthen1e033d62017-04-12 16:07:06 -040074 return false;
Paul Lawrenceec900bb2014-10-09 14:22:49 +000075 }
76
Sami Tolvanen8ad80762015-10-20 13:24:24 +010077 if (enable && !metadata.disabled) {
Elliott Hughesab52c182015-05-01 17:04:38 -070078 WriteFdFmt(fd, "Verity already enabled on %s\n", mount_point);
David Zeuthen1e033d62017-04-12 16:07:06 -040079 return false;
Paul Lawrence982089d2014-12-03 15:31:57 -080080 }
81
Sami Tolvanen8ad80762015-10-20 13:24:24 +010082 if (!fh.set_verity_status(enable)) {
Elliott Hughesab52c182015-05-01 17:04:38 -070083 WriteFdFmt(fd, "Could not set verity %s flag on device %s with error %s\n",
84 enable ? "enabled" : "disabled",
85 block_device, strerror(errno));
David Zeuthen1e033d62017-04-12 16:07:06 -040086 return false;
Paul Lawrenceec900bb2014-10-09 14:22:49 +000087 }
88
Elliott Hughesab52c182015-05-01 17:04:38 -070089 WriteFdFmt(fd, "Verity %s on %s\n", enable ? "enabled" : "disabled", mount_point);
David Zeuthen1e033d62017-04-12 16:07:06 -040090 return true;
91}
92
93/* Helper function to get A/B suffix, if any. If the device isn't
94 * using A/B the empty string is returned. Otherwise either "_a",
95 * "_b", ... is returned.
David Zeuthen1e033d62017-04-12 16:07:06 -040096 */
97static std::string get_ab_suffix() {
Tom Cherry211a4a52017-09-25 16:04:30 +000098 return android::base::GetProperty("ro.boot.slot_suffix", "");
David Zeuthen1e033d62017-04-12 16:07:06 -040099}
100
Bowgo Tsai45a00fd2018-03-12 16:26:23 +0800101static bool is_avb_device_locked() {
102 return android::base::GetProperty("ro.boot.vbmeta.device_state", "") == "locked";
103}
104
David Zeuthen1e033d62017-04-12 16:07:06 -0400105/* Use AVB to turn verity on/off */
106static bool set_avb_verity_enabled_state(int fd, AvbOps* ops, bool enable_verity) {
107 std::string ab_suffix = get_ab_suffix();
David Zeuthen1e033d62017-04-12 16:07:06 -0400108 bool verity_enabled;
Bowgo Tsai45a00fd2018-03-12 16:26:23 +0800109
110 if (is_avb_device_locked()) {
111 WriteFdFmt(fd, "Device is locked. Please unlock the device first\n");
112 return false;
113 }
114
David Zeuthen1e033d62017-04-12 16:07:06 -0400115 if (!avb_user_verity_get(ops, ab_suffix.c_str(), &verity_enabled)) {
Bowgo Tsai45a00fd2018-03-12 16:26:23 +0800116 WriteFdFmt(fd, "Error getting verity state. Try adb root first?\n");
David Zeuthen1e033d62017-04-12 16:07:06 -0400117 return false;
118 }
119
120 if ((verity_enabled && enable_verity) || (!verity_enabled && !enable_verity)) {
121 WriteFdFmt(fd, "verity is already %s\n", verity_enabled ? "enabled" : "disabled");
122 return false;
123 }
124
125 if (!avb_user_verity_set(ops, ab_suffix.c_str(), enable_verity)) {
126 WriteFdFmt(fd, "Error setting verity\n");
127 return false;
128 }
129
130 WriteFdFmt(fd, "Successfully %s verity\n", enable_verity ? "enabled" : "disabled");
131 return true;
Paul Lawrenceec900bb2014-10-09 14:22:49 +0000132}
133
Elliott Hughesffdec182016-09-23 15:40:03 -0700134void set_verity_enabled_state_service(int fd, void* cookie) {
135 unique_fd closer(fd);
David Zeuthen1e033d62017-04-12 16:07:06 -0400136 bool any_changed = false;
Elliott Hughesffdec182016-09-23 15:40:03 -0700137
Paul Lawrence982089d2014-12-03 15:31:57 -0800138 bool enable = (cookie != NULL);
David Zeuthenabddbc22017-05-17 10:36:35 -0400139
140 // Figure out if we're using VB1.0 or VB2.0 (aka AVB) - by
141 // contract, androidboot.vbmeta.digest is set by the bootloader
142 // when using AVB).
143 bool using_avb = !android::base::GetProperty("ro.boot.vbmeta.digest", "").empty();
144
145 // If using AVB, dm-verity is used on any build so we want it to
146 // be possible to disable/enable on any build (except USER). For
147 // VB1.0 dm-verity is only enabled on certain builds.
148 if (!using_avb) {
149 if (!kAllowDisableVerity) {
150 WriteFdFmt(fd, "%s-verity only works for userdebug builds\n",
151 enable ? "enable" : "disable");
152 }
153
154 if (!android::base::GetBoolProperty("ro.secure", false)) {
155 WriteFdFmt(fd, "verity not enabled - ENG build\n");
156 return;
157 }
Paul Lawrenceec900bb2014-10-09 14:22:49 +0000158 }
159
David Zeuthenabddbc22017-05-17 10:36:35 -0400160 // Should never be possible to disable dm-verity on a USER build
161 // regardless of using AVB or VB1.0.
Mark Salyzyn97787a02016-03-28 15:52:13 -0700162 if (!__android_log_is_debuggable()) {
Elliott Hughesffdec182016-09-23 15:40:03 -0700163 WriteFdFmt(fd, "verity cannot be disabled/enabled - USER build\n");
164 return;
165 }
166
David Zeuthenabddbc22017-05-17 10:36:35 -0400167 if (using_avb) {
168 // Yep, the system is using AVB.
David Zeuthen1e033d62017-04-12 16:07:06 -0400169 AvbOps* ops = avb_ops_user_new();
170 if (ops == nullptr) {
171 WriteFdFmt(fd, "Error getting AVB ops\n");
172 return;
173 }
174 if (set_avb_verity_enabled_state(fd, ops, enable)) {
175 any_changed = true;
176 }
177 avb_ops_user_free(ops);
178 } else {
179 // Not using AVB - assume VB1.0.
Elliott Hughesffdec182016-09-23 15:40:03 -0700180
David Zeuthen1e033d62017-04-12 16:07:06 -0400181 // read all fstab entries at once from all sources
182 fstab = fs_mgr_read_fstab_default();
183 if (!fstab) {
184 WriteFdFmt(fd, "Failed to read fstab\nMaybe run adb root?\n");
185 return;
186 }
187
188 // Loop through entries looking for ones that vold manages.
189 for (int i = 0; i < fstab->num_entries; i++) {
190 if (fs_mgr_is_verified(&fstab->recs[i])) {
191 if (set_verity_enabled_state(fd, fstab->recs[i].blk_device,
192 fstab->recs[i].mount_point, enable)) {
193 any_changed = true;
194 }
Elliott Hughesffdec182016-09-23 15:40:03 -0700195 }
196 }
197 }
198
199 if (any_changed) {
200 WriteFdFmt(fd, "Now reboot your device for settings to take effect\n");
201 }
Paul Lawrenceec900bb2014-10-09 14:22:49 +0000202}