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