Paul Lawrence | ec900bb | 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 | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG ADB |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
Dan Albert | 1f09bda | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
| 22 | #include <inttypes.h> |
David Zeuthen | 1e033d6 | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 23 | #include <libavb_user/libavb_user.h> |
Dan Albert | 1f09bda | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 24 | #include <stdarg.h> |
Dan Albert | 1f09bda | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <sys/stat.h> |
Paul Lawrence | ec900bb | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 27 | |
Elliott Hughes | ffdec18 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 28 | #include "android-base/properties.h" |
| 29 | #include "android-base/stringprintf.h" |
Steven Moreland | d73be1b | 2017-04-13 23:48:57 -0700 | [diff] [blame] | 30 | #include <log/log_properties.h> |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 31 | |
| 32 | #include "adb.h" |
Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 33 | #include "adb_io.h" |
Elliott Hughes | ffdec18 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 34 | #include "adb_unique_fd.h" |
Dan Albert | 1f09bda | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 35 | #include "fs_mgr.h" |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 36 | #include "remount_service.h" |
Paul Lawrence | ec900bb | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 37 | |
Sami Tolvanen | 8ad8076 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 38 | #include "fec/io.h" |
| 39 | |
Paul Lawrence | ec900bb | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 40 | struct fstab *fstab; |
| 41 | |
Dan Albert | 1f09bda | 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 | 982089d | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 48 | /* Turn verity on/off */ |
David Zeuthen | 1e033d6 | 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 | 9aa4fda | 2015-05-11 11:55:25 -0700 | [diff] [blame] | 51 | if (!make_block_device_writable(block_device)) { |
Elliott Hughes | ab52c18 | 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 | 1e033d6 | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 54 | return false; |
Sami Tolvanen | 13449cd | 2015-01-02 13:30:50 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Sami Tolvanen | 8ad8076 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 57 | fec::io fh(block_device, O_RDWR); |
| 58 | |
| 59 | if (!fh) { |
Elliott Hughes | ab52c18 | 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 | 8ad8076 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 61 | WriteFdFmt(fd, "Maybe run adb root?\n"); |
David Zeuthen | 1e033d6 | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 62 | return false; |
Paul Lawrence | ec900bb | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Sami Tolvanen | 8ad8076 | 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 | 1e033d6 | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 69 | return false; |
Paul Lawrence | ec900bb | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Sami Tolvanen | 8ad8076 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 72 | if (!enable && metadata.disabled) { |
Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 73 | WriteFdFmt(fd, "Verity already disabled on %s\n", mount_point); |
David Zeuthen | 1e033d6 | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 74 | return false; |
Paul Lawrence | ec900bb | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Sami Tolvanen | 8ad8076 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 77 | if (enable && !metadata.disabled) { |
Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 78 | WriteFdFmt(fd, "Verity already enabled on %s\n", mount_point); |
David Zeuthen | 1e033d6 | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 79 | return false; |
Paul Lawrence | 982089d | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Sami Tolvanen | 8ad8076 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 82 | if (!fh.set_verity_status(enable)) { |
Elliott Hughes | ab52c18 | 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 | 1e033d6 | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 86 | return false; |
Paul Lawrence | ec900bb | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 89 | WriteFdFmt(fd, "Verity %s on %s\n", enable ? "enabled" : "disabled", mount_point); |
David Zeuthen | 1e033d6 | 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. |
David Zeuthen | 1e033d6 | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 96 | */ |
| 97 | static std::string get_ab_suffix() { |
Tom Cherry | 211a4a5 | 2017-09-25 16:04:30 +0000 | [diff] [blame] | 98 | return android::base::GetProperty("ro.boot.slot_suffix", ""); |
David Zeuthen | 1e033d6 | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* Use AVB to turn verity on/off */ |
| 102 | static bool set_avb_verity_enabled_state(int fd, AvbOps* ops, bool enable_verity) { |
| 103 | std::string ab_suffix = get_ab_suffix(); |
| 104 | |
| 105 | bool verity_enabled; |
| 106 | if (!avb_user_verity_get(ops, ab_suffix.c_str(), &verity_enabled)) { |
| 107 | WriteFdFmt(fd, "Error getting verity state\n"); |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | if ((verity_enabled && enable_verity) || (!verity_enabled && !enable_verity)) { |
| 112 | WriteFdFmt(fd, "verity is already %s\n", verity_enabled ? "enabled" : "disabled"); |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | if (!avb_user_verity_set(ops, ab_suffix.c_str(), enable_verity)) { |
| 117 | WriteFdFmt(fd, "Error setting verity\n"); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | WriteFdFmt(fd, "Successfully %s verity\n", enable_verity ? "enabled" : "disabled"); |
| 122 | return true; |
Paul Lawrence | ec900bb | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Elliott Hughes | ffdec18 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 125 | void set_verity_enabled_state_service(int fd, void* cookie) { |
| 126 | unique_fd closer(fd); |
David Zeuthen | 1e033d6 | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 127 | bool any_changed = false; |
Elliott Hughes | ffdec18 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 128 | |
Paul Lawrence | 982089d | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 129 | bool enable = (cookie != NULL); |
David Zeuthen | abddbc2 | 2017-05-17 10:36:35 -0400 | [diff] [blame] | 130 | |
| 131 | // Figure out if we're using VB1.0 or VB2.0 (aka AVB) - by |
| 132 | // contract, androidboot.vbmeta.digest is set by the bootloader |
| 133 | // when using AVB). |
| 134 | bool using_avb = !android::base::GetProperty("ro.boot.vbmeta.digest", "").empty(); |
| 135 | |
| 136 | // If using AVB, dm-verity is used on any build so we want it to |
| 137 | // be possible to disable/enable on any build (except USER). For |
| 138 | // VB1.0 dm-verity is only enabled on certain builds. |
| 139 | if (!using_avb) { |
| 140 | if (!kAllowDisableVerity) { |
| 141 | WriteFdFmt(fd, "%s-verity only works for userdebug builds\n", |
| 142 | enable ? "enable" : "disable"); |
| 143 | } |
| 144 | |
| 145 | if (!android::base::GetBoolProperty("ro.secure", false)) { |
| 146 | WriteFdFmt(fd, "verity not enabled - ENG build\n"); |
| 147 | return; |
| 148 | } |
Paul Lawrence | ec900bb | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 149 | } |
| 150 | |
David Zeuthen | abddbc2 | 2017-05-17 10:36:35 -0400 | [diff] [blame] | 151 | // Should never be possible to disable dm-verity on a USER build |
| 152 | // regardless of using AVB or VB1.0. |
Mark Salyzyn | 97787a0 | 2016-03-28 15:52:13 -0700 | [diff] [blame] | 153 | if (!__android_log_is_debuggable()) { |
Elliott Hughes | ffdec18 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 154 | WriteFdFmt(fd, "verity cannot be disabled/enabled - USER build\n"); |
| 155 | return; |
| 156 | } |
| 157 | |
David Zeuthen | abddbc2 | 2017-05-17 10:36:35 -0400 | [diff] [blame] | 158 | if (using_avb) { |
| 159 | // Yep, the system is using AVB. |
David Zeuthen | 1e033d6 | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 160 | AvbOps* ops = avb_ops_user_new(); |
| 161 | if (ops == nullptr) { |
| 162 | WriteFdFmt(fd, "Error getting AVB ops\n"); |
| 163 | return; |
| 164 | } |
| 165 | if (set_avb_verity_enabled_state(fd, ops, enable)) { |
| 166 | any_changed = true; |
| 167 | } |
| 168 | avb_ops_user_free(ops); |
| 169 | } else { |
| 170 | // Not using AVB - assume VB1.0. |
Elliott Hughes | ffdec18 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 171 | |
David Zeuthen | 1e033d6 | 2017-04-12 16:07:06 -0400 | [diff] [blame] | 172 | // read all fstab entries at once from all sources |
| 173 | fstab = fs_mgr_read_fstab_default(); |
| 174 | if (!fstab) { |
| 175 | WriteFdFmt(fd, "Failed to read fstab\nMaybe run adb root?\n"); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | // Loop through entries looking for ones that vold manages. |
| 180 | for (int i = 0; i < fstab->num_entries; i++) { |
| 181 | if (fs_mgr_is_verified(&fstab->recs[i])) { |
| 182 | if (set_verity_enabled_state(fd, fstab->recs[i].blk_device, |
| 183 | fstab->recs[i].mount_point, enable)) { |
| 184 | any_changed = true; |
| 185 | } |
Elliott Hughes | ffdec18 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if (any_changed) { |
| 191 | WriteFdFmt(fd, "Now reboot your device for settings to take effect\n"); |
| 192 | } |
Paul Lawrence | ec900bb | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 193 | } |