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> |
| 23 | #include <stdarg.h> |
Dan Albert | 59bcbe2 | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <sys/stat.h> |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 26 | |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame^] | 27 | #include "android-base/properties.h" |
| 28 | #include "android-base/stringprintf.h" |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 29 | |
| 30 | #include "adb.h" |
Elliott Hughes | fb59684 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 31 | #include "adb_io.h" |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame^] | 32 | #include "adb_unique_fd.h" |
Dan Albert | 59bcbe2 | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 33 | #include "fs_mgr.h" |
Elliott Hughes | dedf767 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 34 | #include "remount_service.h" |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 35 | |
Sami Tolvanen | 778f500 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 36 | #include "fec/io.h" |
| 37 | |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 38 | struct fstab *fstab; |
| 39 | |
Dan Albert | 59bcbe2 | 2015-01-26 17:49:17 -0800 | [diff] [blame] | 40 | #ifdef ALLOW_ADBD_DISABLE_VERITY |
| 41 | static const bool kAllowDisableVerity = true; |
| 42 | #else |
| 43 | static const bool kAllowDisableVerity = false; |
| 44 | #endif |
| 45 | |
Paul Lawrence | 8ba2b02 | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 46 | /* Turn verity on/off */ |
| 47 | static int set_verity_enabled_state(int fd, const char *block_device, |
| 48 | const char* mount_point, bool enable) |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 49 | { |
Elliott Hughes | 689011f | 2015-05-11 11:55:25 -0700 | [diff] [blame] | 50 | if (!make_block_device_writable(block_device)) { |
Elliott Hughes | fb59684 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 51 | WriteFdFmt(fd, "Could not make block device %s writable (%s).\n", |
| 52 | block_device, strerror(errno)); |
Sami Tolvanen | 778f500 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 53 | return -1; |
Sami Tolvanen | 5638a1a | 2015-01-02 13:30:50 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Sami Tolvanen | 778f500 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 56 | fec::io fh(block_device, O_RDWR); |
| 57 | |
| 58 | if (!fh) { |
Elliott Hughes | fb59684 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 59 | 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] | 60 | WriteFdFmt(fd, "Maybe run adb root?\n"); |
| 61 | return -1; |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Sami Tolvanen | 778f500 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 64 | fec_verity_metadata metadata; |
| 65 | |
| 66 | if (!fh.get_verity_metadata(metadata)) { |
| 67 | WriteFdFmt(fd, "Couldn't find verity metadata!\n"); |
| 68 | return -1; |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Sami Tolvanen | 778f500 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 71 | if (!enable && metadata.disabled) { |
Elliott Hughes | fb59684 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 72 | WriteFdFmt(fd, "Verity already disabled on %s\n", mount_point); |
Sami Tolvanen | 778f500 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 73 | return -1; |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Sami Tolvanen | 778f500 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 76 | if (enable && !metadata.disabled) { |
Elliott Hughes | fb59684 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 77 | WriteFdFmt(fd, "Verity already enabled on %s\n", mount_point); |
Sami Tolvanen | 778f500 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 78 | return -1; |
Paul Lawrence | 8ba2b02 | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Sami Tolvanen | 778f500 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 81 | if (!fh.set_verity_status(enable)) { |
Elliott Hughes | fb59684 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 82 | WriteFdFmt(fd, "Could not set verity %s flag on device %s with error %s\n", |
| 83 | enable ? "enabled" : "disabled", |
| 84 | block_device, strerror(errno)); |
Sami Tolvanen | 778f500 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 85 | return -1; |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Elliott Hughes | fb59684 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 88 | WriteFdFmt(fd, "Verity %s on %s\n", enable ? "enabled" : "disabled", mount_point); |
Sami Tolvanen | 778f500 | 2015-10-20 13:24:24 +0100 | [diff] [blame] | 89 | return 0; |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame^] | 92 | void set_verity_enabled_state_service(int fd, void* cookie) { |
| 93 | unique_fd closer(fd); |
| 94 | |
Paul Lawrence | 8ba2b02 | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 95 | bool enable = (cookie != NULL); |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame^] | 96 | if (!kAllowDisableVerity) { |
Elliott Hughes | fb59684 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 97 | WriteFdFmt(fd, "%s-verity only works for userdebug builds\n", |
| 98 | enable ? "enable" : "disable"); |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame^] | 101 | if (!android::base::GetBoolProperty("ro.secure", false)) { |
| 102 | WriteFdFmt(fd, "verity not enabled - ENG build\n"); |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | if (!android::base::GetBoolProperty("ro.debuggable", false)) { |
| 107 | WriteFdFmt(fd, "verity cannot be disabled/enabled - USER build\n"); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | std::string fstab_filename = "/fstab." + android::base::GetProperty("ro.hardware", ""); |
| 112 | |
| 113 | fstab = fs_mgr_read_fstab(fstab_filename.c_str()); |
| 114 | if (!fstab) { |
| 115 | WriteFdFmt(fd, "Failed to open %s\nMaybe run adb root?\n", fstab_filename.c_str()); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | // Loop through entries looking for ones that vold manages. |
| 120 | bool any_changed = false; |
| 121 | for (int i = 0; i < fstab->num_entries; i++) { |
| 122 | if (fs_mgr_is_verified(&fstab->recs[i])) { |
| 123 | if (!set_verity_enabled_state(fd, fstab->recs[i].blk_device, |
| 124 | fstab->recs[i].mount_point, |
| 125 | enable)) { |
| 126 | any_changed = true; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (any_changed) { |
| 132 | WriteFdFmt(fd, "Now reboot your device for settings to take effect\n"); |
| 133 | } |
Paul Lawrence | 5f35648 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 134 | } |