The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include <errno.h> |
Mark Salyzyn | 63e39f2 | 2014-04-30 09:10:31 -0700 | [diff] [blame] | 22 | #include <fcntl.h> |
Yabin Cui | 685860d | 2015-01-02 14:02:14 -0800 | [diff] [blame] | 23 | #include <mntent.h> |
Mark Salyzyn | 63e39f2 | 2014-04-30 09:10:31 -0700 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/mount.h> |
| 28 | #include <unistd.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 29 | |
Elliott Hughes | dedf767 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 30 | #include <string> |
| 31 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | #include "adb.h" |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 33 | #include "adb_io.h" |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 34 | #include "adb_utils.h" |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 35 | #include "cutils/properties.h" |
Daniel Rosenberg | 0de6891 | 2015-06-29 17:30:28 -0700 | [diff] [blame] | 36 | #include "fs_mgr.h" |
| 37 | |
Elliott Hughes | dabb974 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 38 | // Returns the device used to mount a directory in /proc/mounts. |
Daniel Rosenberg | 0de6891 | 2015-06-29 17:30:28 -0700 | [diff] [blame] | 39 | static std::string find_proc_mount(const char* dir) { |
Elliott Hughes | dabb974 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 40 | std::unique_ptr<FILE, int(*)(FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent); |
| 41 | if (!fp) { |
| 42 | return ""; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | } |
Elliott Hughes | dabb974 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 44 | |
| 45 | mntent* e; |
| 46 | while ((e = getmntent(fp.get())) != nullptr) { |
| 47 | if (strcmp(dir, e->mnt_dir) == 0) { |
| 48 | return e->mnt_fsname; |
Yabin Cui | 685860d | 2015-01-02 14:02:14 -0800 | [diff] [blame] | 49 | } |
| 50 | } |
Elliott Hughes | dabb974 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 51 | return ""; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Daniel Rosenberg | 0de6891 | 2015-06-29 17:30:28 -0700 | [diff] [blame] | 54 | // Returns the device used to mount a directory in the fstab. |
| 55 | static std::string find_fstab_mount(const char* dir) { |
| 56 | char propbuf[PROPERTY_VALUE_MAX]; |
| 57 | |
| 58 | property_get("ro.hardware", propbuf, ""); |
Josh Gao | e3a87d0 | 2015-11-11 17:56:12 -0800 | [diff] [blame] | 59 | std::string fstab_filename = std::string("/fstab.") + propbuf; |
Daniel Rosenberg | 0de6891 | 2015-06-29 17:30:28 -0700 | [diff] [blame] | 60 | struct fstab* fstab = fs_mgr_read_fstab(fstab_filename.c_str()); |
| 61 | struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, dir); |
| 62 | std::string dev = rec ? std::string(rec->blk_device) : ""; |
| 63 | fs_mgr_free_fstab(fstab); |
| 64 | return dev; |
| 65 | } |
| 66 | |
| 67 | // The proc entry for / is full of lies, so check fstab instead. |
| 68 | // /proc/mounts lists rootfs and /dev/root, neither of which is what we want. |
| 69 | static std::string find_mount(const char* dir) { |
| 70 | if (strcmp(dir, "/") == 0) { |
| 71 | return find_fstab_mount(dir); |
| 72 | } else { |
| 73 | return find_proc_mount(dir); |
| 74 | } |
| 75 | } |
| 76 | |
Elliott Hughes | 689011f | 2015-05-11 11:55:25 -0700 | [diff] [blame] | 77 | bool make_block_device_writable(const std::string& dev) { |
Elliott Hughes | dedf767 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 78 | int fd = unix_open(dev.c_str(), O_RDONLY | O_CLOEXEC); |
| 79 | if (fd == -1) { |
Elliott Hughes | 689011f | 2015-05-11 11:55:25 -0700 | [diff] [blame] | 80 | return false; |
Elliott Hughes | dedf767 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Paul Lawrence | 8ba2b02 | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 83 | int OFF = 0; |
Elliott Hughes | 689011f | 2015-05-11 11:55:25 -0700 | [diff] [blame] | 84 | bool result = (ioctl(fd, BLKROSET, &OFF) != -1); |
Spencer Low | 3a2421b | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 85 | unix_close(fd); |
Elliott Hughes | dedf767 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 86 | return result; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Elliott Hughes | 689011f | 2015-05-11 11:55:25 -0700 | [diff] [blame] | 89 | static bool remount_partition(int fd, const char* dir) { |
| 90 | if (!directory_exists(dir)) { |
Elliott Hughes | dabb974 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 91 | return true; |
| 92 | } |
Elliott Hughes | 689011f | 2015-05-11 11:55:25 -0700 | [diff] [blame] | 93 | std::string dev = find_mount(dir); |
| 94 | if (dev.empty()) { |
| 95 | return true; |
| 96 | } |
| 97 | if (!make_block_device_writable(dev)) { |
| 98 | WriteFdFmt(fd, "remount of %s failed; couldn't make block device %s writable: %s\n", |
| 99 | dir, dev.c_str(), strerror(errno)); |
| 100 | return false; |
| 101 | } |
| 102 | if (mount(dev.c_str(), dir, "none", MS_REMOUNT, nullptr) == -1) { |
| 103 | WriteFdFmt(fd, "remount of %s failed: %s\n", dir, strerror(errno)); |
Elliott Hughes | dabb974 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 104 | return false; |
| 105 | } |
Elliott Hughes | dedf767 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 106 | return true; |
Elliott Hughes | dedf767 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void remount_service(int fd, void* cookie) { |
Nick Kralevich | fee8841 | 2015-02-25 15:48:06 -0800 | [diff] [blame] | 110 | if (getuid() != 0) { |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 111 | WriteFdExactly(fd, "Not running as root. Try \"adb root\" first.\n"); |
Nick Kralevich | fee8841 | 2015-02-25 15:48:06 -0800 | [diff] [blame] | 112 | adb_close(fd); |
| 113 | return; |
| 114 | } |
| 115 | |
Elliott Hughes | dabb974 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 116 | char prop_buf[PROPERTY_VALUE_MAX]; |
Sami Tolvanen | 5c54f58 | 2015-03-30 11:38:38 +0100 | [diff] [blame] | 117 | property_get("partition.system.verified", prop_buf, ""); |
Elliott Hughes | dabb974 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 118 | bool system_verified = (strlen(prop_buf) > 0); |
Paul Lawrence | a64f877 | 2014-10-27 10:37:59 -0700 | [diff] [blame] | 119 | |
Sami Tolvanen | 5c54f58 | 2015-03-30 11:38:38 +0100 | [diff] [blame] | 120 | property_get("partition.vendor.verified", prop_buf, ""); |
Elliott Hughes | dabb974 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 121 | bool vendor_verified = (strlen(prop_buf) > 0); |
Paul Lawrence | a64f877 | 2014-10-27 10:37:59 -0700 | [diff] [blame] | 122 | |
| 123 | if (system_verified || vendor_verified) { |
| 124 | // Allow remount but warn of likely bad effects |
| 125 | bool both = system_verified && vendor_verified; |
Elliott Hughes | fb59684 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 126 | WriteFdFmt(fd, |
| 127 | "dm_verity is enabled on the %s%s%s partition%s.\n", |
| 128 | system_verified ? "system" : "", |
| 129 | both ? " and " : "", |
| 130 | vendor_verified ? "vendor" : "", |
| 131 | both ? "s" : ""); |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 132 | WriteFdExactly(fd, |
| 133 | "Use \"adb disable-verity\" to disable verity.\n" |
| 134 | "If you do not, remount may succeed, however, you will still " |
| 135 | "not be able to write to these volumes.\n"); |
Paul Lawrence | a64f877 | 2014-10-27 10:37:59 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Elliott Hughes | dedf767 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 138 | bool success = true; |
Daniel Rosenberg | 0de6891 | 2015-06-29 17:30:28 -0700 | [diff] [blame] | 139 | property_get("ro.build.system_root_image", prop_buf, ""); |
| 140 | bool system_root = !strcmp(prop_buf, "true"); |
| 141 | if (system_root) { |
| 142 | success &= remount_partition(fd, "/"); |
| 143 | } else { |
| 144 | success &= remount_partition(fd, "/system"); |
| 145 | } |
Elliott Hughes | 689011f | 2015-05-11 11:55:25 -0700 | [diff] [blame] | 146 | success &= remount_partition(fd, "/vendor"); |
| 147 | success &= remount_partition(fd, "/oem"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 148 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 149 | WriteFdExactly(fd, success ? "remount succeeded\n" : "remount failed\n"); |
Daniel Rosenberg | e9a1c9c | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 150 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 151 | adb_close(fd); |
| 152 | } |