The Android Open Source Project | dd7bc33 | 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 | |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 17 | #define TRACE_TAG TRACE_ADB |
| 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include <errno.h> |
Mark Salyzyn | 60299df | 2014-04-30 09:10:31 -0700 | [diff] [blame] | 22 | #include <fcntl.h> |
Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame] | 23 | #include <mntent.h> |
Mark Salyzyn | 60299df | 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 | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 29 | |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 30 | #include <string> |
| 31 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | #include "adb.h" |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 33 | #include "adb_io.h" |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 34 | #include "adb_utils.h" |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 35 | #include "cutils/properties.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | |
| 37 | static int system_ro = 1; |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 38 | static int vendor_ro = 1; |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 39 | static int oem_ro = 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | |
Colin Cross | c880ee0 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 41 | /* Returns the device used to mount a directory in /proc/mounts */ |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 42 | static std::string find_mount(const char *dir) { |
Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame] | 43 | FILE* fp; |
| 44 | struct mntent* mentry; |
| 45 | char* device = NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 | |
Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame] | 47 | if ((fp = setmntent("/proc/mounts", "r")) == NULL) { |
Colin Cross | c880ee0 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 48 | return NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | } |
Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame] | 50 | while ((mentry = getmntent(fp)) != NULL) { |
| 51 | if (strcmp(dir, mentry->mnt_dir) == 0) { |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 52 | device = mentry->mnt_fsname; |
Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame] | 53 | break; |
| 54 | } |
| 55 | } |
| 56 | endmntent(fp); |
| 57 | return device; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 60 | int make_block_device_writable(const std::string& dev) { |
| 61 | int fd = unix_open(dev.c_str(), O_RDONLY | O_CLOEXEC); |
| 62 | if (fd == -1) { |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | int result = -1; |
Paul Lawrence | 982089d | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 67 | int OFF = 0; |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 68 | if (!ioctl(fd, BLKROSET, &OFF)) { |
| 69 | result = 0; |
Sami Tolvanen | 13449cd | 2015-01-02 13:30:50 +0000 | [diff] [blame] | 70 | } |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 71 | adb_close(fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 72 | |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 73 | return result; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 76 | // Init mounts /system as read only, remount to enable writes. |
| 77 | static int remount(const char* dir, int* dir_ro) { |
| 78 | std::string dev(find_mount(dir)); |
| 79 | if (dev.empty() || make_block_device_writable(dev)) { |
| 80 | return -1; |
Dan Albert | 6084a01 | 2015-03-16 21:35:53 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 83 | int rc = mount(dev.c_str(), dir, "none", MS_REMOUNT, NULL); |
Dan Albert | 6084a01 | 2015-03-16 21:35:53 +0000 | [diff] [blame] | 84 | *dir_ro = rc; |
Dan Albert | 6084a01 | 2015-03-16 21:35:53 +0000 | [diff] [blame] | 85 | return rc; |
MÃ¥rten Kongstad | 81416fd | 2014-11-03 13:52:57 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 88 | static bool remount_partition(int fd, const char* partition, int* ro) { |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 89 | if (!directory_exists(partition)) { |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 90 | return true; |
| 91 | } |
| 92 | if (remount(partition, ro)) { |
| 93 | char buf[200]; |
| 94 | snprintf(buf, sizeof(buf), "remount of %s failed: %s\n", partition, strerror(errno)); |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame^] | 95 | WriteFdExactly(fd, buf); |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | void remount_service(int fd, void* cookie) { |
Paul Lawrence | 3463755 | 2014-10-27 10:37:59 -0700 | [diff] [blame] | 102 | char prop_buf[PROPERTY_VALUE_MAX]; |
| 103 | |
Nick Kralevich | 268eb4f | 2015-02-25 15:48:06 -0800 | [diff] [blame] | 104 | if (getuid() != 0) { |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame^] | 105 | WriteFdExactly(fd, "Not running as root. Try \"adb root\" first.\n"); |
Nick Kralevich | 268eb4f | 2015-02-25 15:48:06 -0800 | [diff] [blame] | 106 | adb_close(fd); |
| 107 | return; |
| 108 | } |
| 109 | |
Paul Lawrence | 3463755 | 2014-10-27 10:37:59 -0700 | [diff] [blame] | 110 | bool system_verified = false, vendor_verified = false; |
Sami Tolvanen | 4547423 | 2015-03-30 11:38:38 +0100 | [diff] [blame] | 111 | property_get("partition.system.verified", prop_buf, ""); |
| 112 | if (strlen(prop_buf) > 0) { |
Paul Lawrence | 3463755 | 2014-10-27 10:37:59 -0700 | [diff] [blame] | 113 | system_verified = true; |
| 114 | } |
| 115 | |
Sami Tolvanen | 4547423 | 2015-03-30 11:38:38 +0100 | [diff] [blame] | 116 | property_get("partition.vendor.verified", prop_buf, ""); |
| 117 | if (strlen(prop_buf) > 0) { |
Paul Lawrence | 3463755 | 2014-10-27 10:37:59 -0700 | [diff] [blame] | 118 | vendor_verified = true; |
| 119 | } |
| 120 | |
| 121 | if (system_verified || vendor_verified) { |
| 122 | // Allow remount but warn of likely bad effects |
| 123 | bool both = system_verified && vendor_verified; |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 124 | char buffer[200]; |
Paul Lawrence | 3463755 | 2014-10-27 10:37:59 -0700 | [diff] [blame] | 125 | snprintf(buffer, sizeof(buffer), |
| 126 | "dm_verity is enabled on the %s%s%s partition%s.\n", |
| 127 | system_verified ? "system" : "", |
| 128 | both ? " and " : "", |
| 129 | vendor_verified ? "vendor" : "", |
| 130 | both ? "s" : ""); |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame^] | 131 | WriteFdExactly(fd, buffer); |
| 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 | 3463755 | 2014-10-27 10:37:59 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 138 | bool success = true; |
| 139 | success &= remount_partition(fd, "/system", &system_ro); |
| 140 | success &= remount_partition(fd, "/vendor", &vendor_ro); |
| 141 | success &= remount_partition(fd, "/oem", &oem_ro); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 142 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame^] | 143 | WriteFdExactly(fd, success ? "remount succeeded\n" : "remount failed\n"); |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 144 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 145 | adb_close(fd); |
| 146 | } |