Paul Lawrence | 51d7970 | 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 | b4245aa | 2015-09-22 15:52:57 -0700 | [diff] [blame^] | 17 | #define TRACE_TAG ADB |
Dan Albert | dfa355b | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
Dan Albert | c6c0f88 | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
| 22 | #include <inttypes.h> |
| 23 | #include <stdarg.h> |
Dan Albert | c6c0f88 | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <sys/stat.h> |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 26 | |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 27 | #include "cutils/properties.h" |
Dan Albert | dfa355b | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 28 | |
| 29 | #include "adb.h" |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 30 | #include "adb_io.h" |
Dan Albert | bb9ffcc | 2015-01-26 17:13:54 -0800 | [diff] [blame] | 31 | #include "ext4_sb.h" |
Dan Albert | c6c0f88 | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 32 | #include "fs_mgr.h" |
Elliott Hughes | 1702259 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 33 | #include "remount_service.h" |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 34 | |
| 35 | #define FSTAB_PREFIX "/fstab." |
| 36 | struct fstab *fstab; |
| 37 | |
Dan Albert | c6c0f88 | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 38 | #ifdef ALLOW_ADBD_DISABLE_VERITY |
| 39 | static const bool kAllowDisableVerity = true; |
| 40 | #else |
| 41 | static const bool kAllowDisableVerity = false; |
| 42 | #endif |
| 43 | |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 44 | static int get_target_device_size(int fd, const char *blk_device, |
| 45 | uint64_t *device_size) |
| 46 | { |
| 47 | int data_device; |
| 48 | struct ext4_super_block sb; |
| 49 | struct fs_info info; |
| 50 | |
| 51 | info.len = 0; /* Only len is set to 0 to ask the device for real size. */ |
| 52 | |
| 53 | data_device = adb_open(blk_device, O_RDONLY | O_CLOEXEC); |
| 54 | if (data_device < 0) { |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 55 | WriteFdFmt(fd, "Error opening block device (%s)\n", strerror(errno)); |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 56 | return -1; |
| 57 | } |
| 58 | |
| 59 | if (lseek64(data_device, 1024, SEEK_SET) < 0) { |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 60 | WriteFdFmt(fd, "Error seeking to superblock\n"); |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 61 | adb_close(data_device); |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | if (adb_read(data_device, &sb, sizeof(sb)) != sizeof(sb)) { |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 66 | WriteFdFmt(fd, "Error reading superblock\n"); |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 67 | adb_close(data_device); |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | ext4_parse_sb(&sb, &info); |
| 72 | *device_size = info.len; |
| 73 | |
| 74 | adb_close(data_device); |
| 75 | return 0; |
| 76 | } |
| 77 | |
Paul Lawrence | 9fe32de | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 78 | /* Turn verity on/off */ |
| 79 | static int set_verity_enabled_state(int fd, const char *block_device, |
| 80 | const char* mount_point, bool enable) |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 81 | { |
| 82 | uint32_t magic_number; |
Paul Lawrence | 9fe32de | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 83 | const uint32_t new_magic = enable ? VERITY_METADATA_MAGIC_NUMBER |
| 84 | : VERITY_METADATA_MAGIC_DISABLE; |
Dan Albert | 8059453 | 2015-03-09 18:29:07 -0700 | [diff] [blame] | 85 | uint64_t device_length = 0; |
Sami Tolvanen | 6202d98 | 2015-01-02 13:30:50 +0000 | [diff] [blame] | 86 | int device = -1; |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 87 | int retval = -1; |
| 88 | |
Elliott Hughes | 10b52a6 | 2015-05-11 11:55:25 -0700 | [diff] [blame] | 89 | if (!make_block_device_writable(block_device)) { |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 90 | WriteFdFmt(fd, "Could not make block device %s writable (%s).\n", |
| 91 | block_device, strerror(errno)); |
Sami Tolvanen | 6202d98 | 2015-01-02 13:30:50 +0000 | [diff] [blame] | 92 | goto errout; |
| 93 | } |
| 94 | |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 95 | device = adb_open(block_device, O_RDWR | O_CLOEXEC); |
| 96 | if (device == -1) { |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 97 | WriteFdFmt(fd, "Could not open block device %s (%s).\n", block_device, strerror(errno)); |
| 98 | WriteFdFmt(fd, "Maybe run adb remount?\n"); |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 99 | goto errout; |
| 100 | } |
| 101 | |
| 102 | // find the start of the verity metadata |
| 103 | if (get_target_device_size(fd, (char*)block_device, &device_length) < 0) { |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 104 | WriteFdFmt(fd, "Could not get target device size.\n"); |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 105 | goto errout; |
| 106 | } |
| 107 | |
| 108 | if (lseek64(device, device_length, SEEK_SET) < 0) { |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 109 | WriteFdFmt(fd, "Could not seek to start of verity metadata block.\n"); |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 110 | goto errout; |
| 111 | } |
| 112 | |
| 113 | // check the magic number |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 114 | if (adb_read(device, &magic_number, sizeof(magic_number)) != sizeof(magic_number)) { |
| 115 | WriteFdFmt(fd, "Couldn't read magic number!\n"); |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 116 | goto errout; |
| 117 | } |
| 118 | |
Paul Lawrence | 9fe32de | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 119 | if (!enable && magic_number == VERITY_METADATA_MAGIC_DISABLE) { |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 120 | WriteFdFmt(fd, "Verity already disabled on %s\n", mount_point); |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 121 | goto errout; |
| 122 | } |
| 123 | |
Paul Lawrence | 9fe32de | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 124 | if (enable && magic_number == VERITY_METADATA_MAGIC_NUMBER) { |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 125 | WriteFdFmt(fd, "Verity already enabled on %s\n", mount_point); |
Paul Lawrence | 9fe32de | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 126 | goto errout; |
| 127 | } |
| 128 | |
| 129 | if (magic_number != VERITY_METADATA_MAGIC_NUMBER |
| 130 | && magic_number != VERITY_METADATA_MAGIC_DISABLE) { |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 131 | WriteFdFmt(fd, "Couldn't find verity metadata at offset %" PRIu64 "!\n", device_length); |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 132 | goto errout; |
| 133 | } |
| 134 | |
| 135 | if (lseek64(device, device_length, SEEK_SET) < 0) { |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 136 | WriteFdFmt(fd, "Could not seek to start of verity metadata block.\n"); |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 137 | goto errout; |
| 138 | } |
| 139 | |
Paul Lawrence | 9fe32de | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 140 | if (adb_write(device, &new_magic, sizeof(new_magic)) != sizeof(new_magic)) { |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 141 | WriteFdFmt(fd, "Could not set verity %s flag on device %s with error %s\n", |
| 142 | enable ? "enabled" : "disabled", |
| 143 | block_device, strerror(errno)); |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 144 | goto errout; |
| 145 | } |
| 146 | |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 147 | WriteFdFmt(fd, "Verity %s on %s\n", enable ? "enabled" : "disabled", mount_point); |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 148 | retval = 0; |
| 149 | errout: |
| 150 | if (device != -1) |
| 151 | adb_close(device); |
| 152 | return retval; |
| 153 | } |
| 154 | |
Paul Lawrence | 9fe32de | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 155 | void set_verity_enabled_state_service(int fd, void* cookie) |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 156 | { |
Paul Lawrence | 9fe32de | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 157 | bool enable = (cookie != NULL); |
Dan Albert | c6c0f88 | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 158 | if (kAllowDisableVerity) { |
| 159 | char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)]; |
| 160 | char propbuf[PROPERTY_VALUE_MAX]; |
| 161 | int i; |
| 162 | bool any_changed = false; |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 163 | |
Dan Albert | c6c0f88 | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 164 | property_get("ro.secure", propbuf, "0"); |
| 165 | if (strcmp(propbuf, "1")) { |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 166 | WriteFdFmt(fd, "verity not enabled - ENG build\n"); |
Dan Albert | c6c0f88 | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 167 | goto errout; |
| 168 | } |
| 169 | |
| 170 | property_get("ro.debuggable", propbuf, "0"); |
| 171 | if (strcmp(propbuf, "1")) { |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 172 | WriteFdFmt(fd, "verity cannot be disabled/enabled - USER build\n"); |
Dan Albert | c6c0f88 | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 173 | goto errout; |
| 174 | } |
| 175 | |
| 176 | property_get("ro.hardware", propbuf, ""); |
| 177 | snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s", |
| 178 | propbuf); |
| 179 | |
| 180 | fstab = fs_mgr_read_fstab(fstab_filename); |
| 181 | if (!fstab) { |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 182 | WriteFdFmt(fd, "Failed to open %s\nMaybe run adb root?\n", fstab_filename); |
Dan Albert | c6c0f88 | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 183 | goto errout; |
| 184 | } |
| 185 | |
| 186 | /* Loop through entries looking for ones that vold manages */ |
| 187 | for (i = 0; i < fstab->num_entries; i++) { |
| 188 | if(fs_mgr_is_verified(&fstab->recs[i])) { |
| 189 | if (!set_verity_enabled_state(fd, fstab->recs[i].blk_device, |
| 190 | fstab->recs[i].mount_point, |
| 191 | enable)) { |
| 192 | any_changed = true; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if (any_changed) { |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 198 | WriteFdFmt(fd, "Now reboot your device for settings to take effect\n"); |
Dan Albert | c6c0f88 | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 199 | } |
| 200 | } else { |
Elliott Hughes | 6a6a479 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 201 | WriteFdFmt(fd, "%s-verity only works for userdebug builds\n", |
| 202 | enable ? "enable" : "disable"); |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Bernhard Rosenkränzer | ca25bc5 | 2014-12-12 22:22:37 +0100 | [diff] [blame] | 205 | errout: |
Paul Lawrence | 51d7970 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 206 | adb_close(fd); |
| 207 | } |