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 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 17 | #include <errno.h> |
Mark Salyzyn | 60299df | 2014-04-30 09:10:31 -0700 | [diff] [blame] | 18 | #include <fcntl.h> |
Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame] | 19 | #include <mntent.h> |
Mark Salyzyn | 60299df | 2014-04-30 09:10:31 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/mount.h> |
| 24 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 26 | #include "sysdeps.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | |
| 28 | #define TRACE_TAG TRACE_ADB |
| 29 | #include "adb.h" |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 30 | #include "adb_io.h" |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 31 | #include "cutils/properties.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | |
| 33 | static int system_ro = 1; |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 34 | static int vendor_ro = 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 35 | |
Colin Cross | c880ee0 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 36 | /* Returns the device used to mount a directory in /proc/mounts */ |
Dan Albert | 6084a01 | 2015-03-16 21:35:53 +0000 | [diff] [blame] | 37 | static char *find_mount(const char *dir) |
| 38 | { |
Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame] | 39 | FILE* fp; |
| 40 | struct mntent* mentry; |
| 41 | char* device = NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | |
Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame] | 43 | if ((fp = setmntent("/proc/mounts", "r")) == NULL) { |
Colin Cross | c880ee0 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 44 | return NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 45 | } |
Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame] | 46 | while ((mentry = getmntent(fp)) != NULL) { |
| 47 | if (strcmp(dir, mentry->mnt_dir) == 0) { |
Dan Albert | 6084a01 | 2015-03-16 21:35:53 +0000 | [diff] [blame] | 48 | device = strdup(mentry->mnt_fsname); |
Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame] | 49 | break; |
| 50 | } |
| 51 | } |
| 52 | endmntent(fp); |
| 53 | return device; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Dan Albert | 6084a01 | 2015-03-16 21:35:53 +0000 | [diff] [blame] | 56 | static int hasVendorPartition() |
| 57 | { |
| 58 | struct stat info; |
| 59 | if (!lstat("/vendor", &info)) |
| 60 | if ((info.st_mode & S_IFMT) == S_IFDIR) |
| 61 | return true; |
| 62 | return false; |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Dan Albert | 6084a01 | 2015-03-16 21:35:53 +0000 | [diff] [blame] | 65 | int make_block_device_writable(const char* dev) |
| 66 | { |
| 67 | int fd = -1; |
Paul Lawrence | 982089d | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 68 | int OFF = 0; |
Dan Albert | 6084a01 | 2015-03-16 21:35:53 +0000 | [diff] [blame] | 69 | int rc = -1; |
Paul Lawrence | 982089d | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 70 | |
Dan Albert | 6084a01 | 2015-03-16 21:35:53 +0000 | [diff] [blame] | 71 | if (!dev) |
| 72 | goto errout; |
Paul Lawrence | 982089d | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 73 | |
Dan Albert | 6084a01 | 2015-03-16 21:35:53 +0000 | [diff] [blame] | 74 | fd = unix_open(dev, O_RDONLY | O_CLOEXEC); |
| 75 | if (fd < 0) |
| 76 | goto errout; |
| 77 | |
| 78 | if (ioctl(fd, BLKROSET, &OFF)) { |
| 79 | goto errout; |
Sami Tolvanen | 13449cd | 2015-01-02 13:30:50 +0000 | [diff] [blame] | 80 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 81 | |
Dan Albert | 6084a01 | 2015-03-16 21:35:53 +0000 | [diff] [blame] | 82 | rc = 0; |
| 83 | |
| 84 | errout: |
| 85 | if (fd >= 0) { |
| 86 | adb_close(fd); |
| 87 | } |
Sami Tolvanen | 13449cd | 2015-01-02 13:30:50 +0000 | [diff] [blame] | 88 | return rc; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Dan Albert | 6084a01 | 2015-03-16 21:35:53 +0000 | [diff] [blame] | 91 | /* Init mounts /system as read only, remount to enable writes. */ |
| 92 | static int remount(const char* dir, int* dir_ro) |
| 93 | { |
| 94 | char *dev = 0; |
| 95 | int rc = -1; |
| 96 | |
| 97 | dev = find_mount(dir); |
| 98 | |
| 99 | if (!dev || make_block_device_writable(dev)) { |
| 100 | goto errout; |
| 101 | } |
| 102 | |
| 103 | rc = mount(dev, dir, "none", MS_REMOUNT, NULL); |
| 104 | *dir_ro = rc; |
| 105 | |
| 106 | errout: |
| 107 | free(dev); |
| 108 | return rc; |
MÃ¥rten Kongstad | 81416fd | 2014-11-03 13:52:57 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Dan Albert | 6084a01 | 2015-03-16 21:35:53 +0000 | [diff] [blame] | 111 | void remount_service(int fd, void *cookie) |
| 112 | { |
| 113 | char buffer[200]; |
Paul Lawrence | 3463755 | 2014-10-27 10:37:59 -0700 | [diff] [blame] | 114 | char prop_buf[PROPERTY_VALUE_MAX]; |
| 115 | |
Nick Kralevich | 268eb4f | 2015-02-25 15:48:06 -0800 | [diff] [blame] | 116 | if (getuid() != 0) { |
| 117 | WriteStringFully(fd, "Not running as root. Try \"adb root\" first.\n"); |
| 118 | adb_close(fd); |
| 119 | return; |
| 120 | } |
| 121 | |
Paul Lawrence | 3463755 | 2014-10-27 10:37:59 -0700 | [diff] [blame] | 122 | bool system_verified = false, vendor_verified = false; |
| 123 | property_get("partition.system.verified", prop_buf, "0"); |
| 124 | if (!strcmp(prop_buf, "1")) { |
| 125 | system_verified = true; |
| 126 | } |
| 127 | |
| 128 | property_get("partition.vendor.verified", prop_buf, "0"); |
| 129 | if (!strcmp(prop_buf, "1")) { |
| 130 | vendor_verified = true; |
| 131 | } |
| 132 | |
| 133 | if (system_verified || vendor_verified) { |
| 134 | // Allow remount but warn of likely bad effects |
| 135 | bool both = system_verified && vendor_verified; |
| 136 | snprintf(buffer, sizeof(buffer), |
| 137 | "dm_verity is enabled on the %s%s%s partition%s.\n", |
| 138 | system_verified ? "system" : "", |
| 139 | both ? " and " : "", |
| 140 | vendor_verified ? "vendor" : "", |
| 141 | both ? "s" : ""); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 142 | WriteStringFully(fd, buffer); |
Paul Lawrence | 3463755 | 2014-10-27 10:37:59 -0700 | [diff] [blame] | 143 | snprintf(buffer, sizeof(buffer), |
| 144 | "Use \"adb disable-verity\" to disable verity.\n" |
| 145 | "If you do not, remount may succeed, however, you will still " |
| 146 | "not be able to write to these volumes.\n"); |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 147 | WriteStringFully(fd, buffer); |
Paul Lawrence | 3463755 | 2014-10-27 10:37:59 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Dan Albert | 6084a01 | 2015-03-16 21:35:53 +0000 | [diff] [blame] | 150 | if (remount("/system", &system_ro)) { |
| 151 | snprintf(buffer, sizeof(buffer), "remount of system failed: %s\n",strerror(errno)); |
| 152 | WriteStringFully(fd, buffer); |
| 153 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 154 | |
Dan Albert | 6084a01 | 2015-03-16 21:35:53 +0000 | [diff] [blame] | 155 | if (hasVendorPartition()) { |
| 156 | if (remount("/vendor", &vendor_ro)) { |
| 157 | snprintf(buffer, sizeof(buffer), "remount of vendor failed: %s\n",strerror(errno)); |
| 158 | WriteStringFully(fd, buffer); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if (!system_ro && (!vendor_ro || !hasVendorPartition())) |
| 163 | WriteStringFully(fd, "remount succeeded\n"); |
| 164 | else { |
| 165 | WriteStringFully(fd, "remount failed\n"); |
| 166 | } |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 167 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 168 | adb_close(fd); |
| 169 | } |