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 | |
Elliott Hughes | 5677c23 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 37 | // Returns the device used to mount a directory in /proc/mounts. |
| 38 | static std::string find_mount(const char* dir) { |
| 39 | std::unique_ptr<FILE, int(*)(FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent); |
| 40 | if (!fp) { |
| 41 | return ""; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | } |
Elliott Hughes | 5677c23 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 43 | |
| 44 | mntent* e; |
| 45 | while ((e = getmntent(fp.get())) != nullptr) { |
| 46 | if (strcmp(dir, e->mnt_dir) == 0) { |
| 47 | return e->mnt_fsname; |
Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame] | 48 | } |
| 49 | } |
Elliott Hughes | 5677c23 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 50 | return ""; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Elliott Hughes | 9aa4fda | 2015-05-11 11:55:25 -0700 | [diff] [blame] | 53 | bool make_block_device_writable(const std::string& dev) { |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 54 | int fd = unix_open(dev.c_str(), O_RDONLY | O_CLOEXEC); |
| 55 | if (fd == -1) { |
Elliott Hughes | 9aa4fda | 2015-05-11 11:55:25 -0700 | [diff] [blame] | 56 | return false; |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Paul Lawrence | 982089d | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 59 | int OFF = 0; |
Elliott Hughes | 9aa4fda | 2015-05-11 11:55:25 -0700 | [diff] [blame] | 60 | bool result = (ioctl(fd, BLKROSET, &OFF) != -1); |
Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame^] | 61 | unix_close(fd); |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 62 | return result; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Elliott Hughes | 9aa4fda | 2015-05-11 11:55:25 -0700 | [diff] [blame] | 65 | static bool remount_partition(int fd, const char* dir) { |
| 66 | if (!directory_exists(dir)) { |
Elliott Hughes | 5677c23 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 67 | return true; |
| 68 | } |
Elliott Hughes | 9aa4fda | 2015-05-11 11:55:25 -0700 | [diff] [blame] | 69 | std::string dev = find_mount(dir); |
| 70 | if (dev.empty()) { |
| 71 | return true; |
| 72 | } |
| 73 | if (!make_block_device_writable(dev)) { |
| 74 | WriteFdFmt(fd, "remount of %s failed; couldn't make block device %s writable: %s\n", |
| 75 | dir, dev.c_str(), strerror(errno)); |
| 76 | return false; |
| 77 | } |
| 78 | if (mount(dev.c_str(), dir, "none", MS_REMOUNT, nullptr) == -1) { |
| 79 | WriteFdFmt(fd, "remount of %s failed: %s\n", dir, strerror(errno)); |
Elliott Hughes | 5677c23 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 80 | return false; |
| 81 | } |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 82 | return true; |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void remount_service(int fd, void* cookie) { |
Nick Kralevich | 268eb4f | 2015-02-25 15:48:06 -0800 | [diff] [blame] | 86 | if (getuid() != 0) { |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 87 | WriteFdExactly(fd, "Not running as root. Try \"adb root\" first.\n"); |
Nick Kralevich | 268eb4f | 2015-02-25 15:48:06 -0800 | [diff] [blame] | 88 | adb_close(fd); |
| 89 | return; |
| 90 | } |
| 91 | |
Elliott Hughes | 5677c23 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 92 | char prop_buf[PROPERTY_VALUE_MAX]; |
Sami Tolvanen | 4547423 | 2015-03-30 11:38:38 +0100 | [diff] [blame] | 93 | property_get("partition.system.verified", prop_buf, ""); |
Elliott Hughes | 5677c23 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 94 | bool system_verified = (strlen(prop_buf) > 0); |
Paul Lawrence | 3463755 | 2014-10-27 10:37:59 -0700 | [diff] [blame] | 95 | |
Sami Tolvanen | 4547423 | 2015-03-30 11:38:38 +0100 | [diff] [blame] | 96 | property_get("partition.vendor.verified", prop_buf, ""); |
Elliott Hughes | 5677c23 | 2015-05-07 23:37:40 -0700 | [diff] [blame] | 97 | bool vendor_verified = (strlen(prop_buf) > 0); |
Paul Lawrence | 3463755 | 2014-10-27 10:37:59 -0700 | [diff] [blame] | 98 | |
| 99 | if (system_verified || vendor_verified) { |
| 100 | // Allow remount but warn of likely bad effects |
| 101 | bool both = system_verified && vendor_verified; |
Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 102 | WriteFdFmt(fd, |
| 103 | "dm_verity is enabled on the %s%s%s partition%s.\n", |
| 104 | system_verified ? "system" : "", |
| 105 | both ? " and " : "", |
| 106 | vendor_verified ? "vendor" : "", |
| 107 | both ? "s" : ""); |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 108 | WriteFdExactly(fd, |
| 109 | "Use \"adb disable-verity\" to disable verity.\n" |
| 110 | "If you do not, remount may succeed, however, you will still " |
| 111 | "not be able to write to these volumes.\n"); |
Paul Lawrence | 3463755 | 2014-10-27 10:37:59 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 114 | bool success = true; |
Elliott Hughes | 9aa4fda | 2015-05-11 11:55:25 -0700 | [diff] [blame] | 115 | success &= remount_partition(fd, "/system"); |
| 116 | success &= remount_partition(fd, "/vendor"); |
| 117 | success &= remount_partition(fd, "/oem"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 118 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 119 | WriteFdExactly(fd, success ? "remount succeeded\n" : "remount failed\n"); |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 120 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 121 | adb_close(fd); |
| 122 | } |