Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 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 | |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG ADB |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
Dan Albert | 59bcbe2 | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
| 22 | #include <inttypes.h> |
David Zeuthen | 499ad6c | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 23 | #include <libavb_user/libavb_user.h> |
Dan Albert | 59bcbe2 | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 24 | #include <stdarg.h> |
Dan Albert | 59bcbe2 | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <sys/stat.h> |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 27 | |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 28 | #include "android-base/properties.h" |
| 29 | #include "android-base/stringprintf.h" |
Steven Moreland | b087d30 | 2017-04-13 23:48:57 -0700 | [diff] [blame] | 30 | #include <log/log_properties.h> |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 31 | |
| 32 | #include "adb.h" |
Elliott Hughes | fb59684 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 33 | #include "adb_io.h" |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 34 | #include "adb_unique_fd.h" |
Dan Albert | 59bcbe2 | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 35 | #include "fs_mgr.h" |
Elliott Hughes | dedf767 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 36 | #include "remount_service.h" |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 37 | |
Sami Tolvanen | 778f500 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 38 | #include "fec/io.h" |
| 39 | |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 40 | struct fstab *fstab; |
| 41 | |
Dan Albert | 59bcbe2 | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 42 | #ifdef ALLOW_ADBD_DISABLE_VERITY |
| 43 | static const bool kAllowDisableVerity = true; |
| 44 | #else |
| 45 | static const bool kAllowDisableVerity = false; |
| 46 | #endif |
| 47 | |
Paul Lawrence | 8ba2b02 | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 48 | /* Turn verity on/off */ |
David Zeuthen | 499ad6c | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 49 | static bool set_verity_enabled_state(int fd, const char* block_device, const char* mount_point, |
| 50 | bool enable) { |
Elliott Hughes | 689011f | 2015-05-11 11:55:25 -0700 | [diff] [blame] | 51 | if (!make_block_device_writable(block_device)) { |
Elliott Hughes | fb59684 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 52 | WriteFdFmt(fd, "Could not make block device %s writable (%s).\n", |
| 53 | block_device, strerror(errno)); |
David Zeuthen | 499ad6c | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 54 | return false; |
Sami Tolvanen | 5638a1a | 2015-01-02 13:30:50 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Sami Tolvanen | 778f500 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 57 | fec::io fh(block_device, O_RDWR); |
| 58 | |
| 59 | if (!fh) { |
Elliott Hughes | fb59684 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 60 | WriteFdFmt(fd, "Could not open block device %s (%s).\n", block_device, strerror(errno)); |
Sami Tolvanen | 778f500 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 61 | WriteFdFmt(fd, "Maybe run adb root?\n"); |
David Zeuthen | 499ad6c | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 62 | return false; |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Sami Tolvanen | 778f500 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 65 | fec_verity_metadata metadata; |
| 66 | |
| 67 | if (!fh.get_verity_metadata(metadata)) { |
| 68 | WriteFdFmt(fd, "Couldn't find verity metadata!\n"); |
David Zeuthen | 499ad6c | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 69 | return false; |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Sami Tolvanen | 778f500 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 72 | if (!enable && metadata.disabled) { |
Elliott Hughes | fb59684 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 73 | WriteFdFmt(fd, "Verity already disabled on %s\n", mount_point); |
David Zeuthen | 499ad6c | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 74 | return false; |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Sami Tolvanen | 778f500 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 77 | if (enable && !metadata.disabled) { |
Elliott Hughes | fb59684 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 78 | WriteFdFmt(fd, "Verity already enabled on %s\n", mount_point); |
David Zeuthen | 499ad6c | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 79 | return false; |
Paul Lawrence | 8ba2b02 | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Sami Tolvanen | 778f500 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 82 | if (!fh.set_verity_status(enable)) { |
Elliott Hughes | fb59684 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 83 | 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 Zeuthen | 499ad6c | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 86 | return false; |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Elliott Hughes | fb59684 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 89 | WriteFdFmt(fd, "Verity %s on %s\n", enable ? "enabled" : "disabled", mount_point); |
David Zeuthen | 499ad6c | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 90 | 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. |
| 96 | * |
| 97 | * Note that since sometime in O androidboot.slot_suffix is deprecated |
| 98 | * and androidboot.slot should be used instead. Since bootloaders may |
| 99 | * be out of sync with the OS, we check both and for extra safety |
| 100 | * prepend a leading underscore if there isn't one already. |
| 101 | */ |
| 102 | static std::string get_ab_suffix() { |
| 103 | std::string ab_suffix = android::base::GetProperty("ro.boot.slot_suffix", ""); |
| 104 | if (ab_suffix == "") { |
| 105 | ab_suffix = android::base::GetProperty("ro.boot.slot", ""); |
| 106 | } |
| 107 | if (ab_suffix.size() > 0 && ab_suffix[0] != '_') { |
| 108 | ab_suffix = std::string("_") + ab_suffix; |
| 109 | } |
| 110 | return ab_suffix; |
| 111 | } |
| 112 | |
| 113 | /* Use AVB to turn verity on/off */ |
| 114 | static bool set_avb_verity_enabled_state(int fd, AvbOps* ops, bool enable_verity) { |
| 115 | std::string ab_suffix = get_ab_suffix(); |
| 116 | |
| 117 | bool verity_enabled; |
| 118 | if (!avb_user_verity_get(ops, ab_suffix.c_str(), &verity_enabled)) { |
| 119 | WriteFdFmt(fd, "Error getting verity state\n"); |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | if ((verity_enabled && enable_verity) || (!verity_enabled && !enable_verity)) { |
| 124 | WriteFdFmt(fd, "verity is already %s\n", verity_enabled ? "enabled" : "disabled"); |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | if (!avb_user_verity_set(ops, ab_suffix.c_str(), enable_verity)) { |
| 129 | WriteFdFmt(fd, "Error setting verity\n"); |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | WriteFdFmt(fd, "Successfully %s verity\n", enable_verity ? "enabled" : "disabled"); |
| 134 | return true; |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 137 | void set_verity_enabled_state_service(int fd, void* cookie) { |
| 138 | unique_fd closer(fd); |
David Zeuthen | 499ad6c | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 139 | bool any_changed = false; |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 140 | |
Paul Lawrence | 8ba2b02 | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 141 | bool enable = (cookie != NULL); |
David Zeuthen | 71dfb9c | 2017-05-17 10:36:35 -0400 | [diff] [blame^] | 142 | |
| 143 | // Figure out if we're using VB1.0 or VB2.0 (aka AVB) - by |
| 144 | // contract, androidboot.vbmeta.digest is set by the bootloader |
| 145 | // when using AVB). |
| 146 | bool using_avb = !android::base::GetProperty("ro.boot.vbmeta.digest", "").empty(); |
| 147 | |
| 148 | // If using AVB, dm-verity is used on any build so we want it to |
| 149 | // be possible to disable/enable on any build (except USER). For |
| 150 | // VB1.0 dm-verity is only enabled on certain builds. |
| 151 | if (!using_avb) { |
| 152 | if (!kAllowDisableVerity) { |
| 153 | WriteFdFmt(fd, "%s-verity only works for userdebug builds\n", |
| 154 | enable ? "enable" : "disable"); |
| 155 | } |
| 156 | |
| 157 | if (!android::base::GetBoolProperty("ro.secure", false)) { |
| 158 | WriteFdFmt(fd, "verity not enabled - ENG build\n"); |
| 159 | return; |
| 160 | } |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 161 | } |
| 162 | |
David Zeuthen | 71dfb9c | 2017-05-17 10:36:35 -0400 | [diff] [blame^] | 163 | // Should never be possible to disable dm-verity on a USER build |
| 164 | // regardless of using AVB or VB1.0. |
Mark Salyzyn | c75f65f | 2016-03-28 15:52:13 -0700 | [diff] [blame] | 165 | if (!__android_log_is_debuggable()) { |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 166 | WriteFdFmt(fd, "verity cannot be disabled/enabled - USER build\n"); |
| 167 | return; |
| 168 | } |
| 169 | |
David Zeuthen | 71dfb9c | 2017-05-17 10:36:35 -0400 | [diff] [blame^] | 170 | if (using_avb) { |
| 171 | // Yep, the system is using AVB. |
David Zeuthen | 499ad6c | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 172 | AvbOps* ops = avb_ops_user_new(); |
| 173 | if (ops == nullptr) { |
| 174 | WriteFdFmt(fd, "Error getting AVB ops\n"); |
| 175 | return; |
| 176 | } |
| 177 | if (set_avb_verity_enabled_state(fd, ops, enable)) { |
| 178 | any_changed = true; |
| 179 | } |
| 180 | avb_ops_user_free(ops); |
| 181 | } else { |
| 182 | // Not using AVB - assume VB1.0. |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 183 | |
David Zeuthen | 499ad6c | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 184 | // read all fstab entries at once from all sources |
| 185 | fstab = fs_mgr_read_fstab_default(); |
| 186 | if (!fstab) { |
| 187 | WriteFdFmt(fd, "Failed to read fstab\nMaybe run adb root?\n"); |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | // Loop through entries looking for ones that vold manages. |
| 192 | for (int i = 0; i < fstab->num_entries; i++) { |
| 193 | if (fs_mgr_is_verified(&fstab->recs[i])) { |
| 194 | if (set_verity_enabled_state(fd, fstab->recs[i].blk_device, |
| 195 | fstab->recs[i].mount_point, enable)) { |
| 196 | any_changed = true; |
| 197 | } |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if (any_changed) { |
| 203 | WriteFdFmt(fd, "Now reboot your device for settings to take effect\n"); |
| 204 | } |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 205 | } |