blob: d679a6dff40ac420d26556e7b34e9ecd6551c229 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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 Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG ADB
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <errno.h>
Mark Salyzyn60299df2014-04-30 09:10:31 -070022#include <fcntl.h>
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080023#include <mntent.h>
Mark Salyzyn60299df2014-04-30 09:10:31 -070024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/mount.h>
28#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
Elliott Hughesec7a6672015-03-16 21:58:32 +000030#include <string>
31
Elliott Hughesffdec182016-09-23 15:40:03 -070032#include <android-base/properties.h>
33
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080035#include "adb_io.h"
Elliott Hughes58305772015-04-17 13:57:15 -070036#include "adb_utils.h"
Daniel Rosenbergd6eba892015-06-29 17:30:28 -070037#include "fs_mgr.h"
38
Elliott Hughes5677c232015-05-07 23:37:40 -070039// Returns the device used to mount a directory in /proc/mounts.
Daniel Rosenbergd6eba892015-06-29 17:30:28 -070040static std::string find_proc_mount(const char* dir) {
Elliott Hughes5677c232015-05-07 23:37:40 -070041 std::unique_ptr<FILE, int(*)(FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent);
42 if (!fp) {
43 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044 }
Elliott Hughes5677c232015-05-07 23:37:40 -070045
46 mntent* e;
47 while ((e = getmntent(fp.get())) != nullptr) {
48 if (strcmp(dir, e->mnt_dir) == 0) {
49 return e->mnt_fsname;
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080050 }
51 }
Elliott Hughes5677c232015-05-07 23:37:40 -070052 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053}
54
Daniel Rosenbergd6eba892015-06-29 17:30:28 -070055// Returns the device used to mount a directory in the fstab.
56static std::string find_fstab_mount(const char* dir) {
Bowgo Tsai66ee2772017-03-10 16:43:02 +080057 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
58 fs_mgr_free_fstab);
59 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab.get(), dir);
60 return rec ? rec->blk_device : "";
Daniel Rosenbergd6eba892015-06-29 17:30:28 -070061}
62
63// The proc entry for / is full of lies, so check fstab instead.
64// /proc/mounts lists rootfs and /dev/root, neither of which is what we want.
Luis Hector Chavez0db4b7a2018-02-21 21:19:07 -080065static std::string find_mount(const char* dir, bool is_root) {
66 if (is_root) {
67 return find_fstab_mount(dir);
Daniel Rosenbergd6eba892015-06-29 17:30:28 -070068 } else {
69 return find_proc_mount(dir);
70 }
71}
72
Elliott Hughes9aa4fda2015-05-11 11:55:25 -070073bool make_block_device_writable(const std::string& dev) {
Elliott Hughesec7a6672015-03-16 21:58:32 +000074 int fd = unix_open(dev.c_str(), O_RDONLY | O_CLOEXEC);
75 if (fd == -1) {
Elliott Hughes9aa4fda2015-05-11 11:55:25 -070076 return false;
Elliott Hughesec7a6672015-03-16 21:58:32 +000077 }
78
Paul Lawrence982089d2014-12-03 15:31:57 -080079 int OFF = 0;
Elliott Hughes9aa4fda2015-05-11 11:55:25 -070080 bool result = (ioctl(fd, BLKROSET, &OFF) != -1);
Spencer Low6ac5d7d2015-05-22 20:09:06 -070081 unix_close(fd);
Elliott Hughesec7a6672015-03-16 21:58:32 +000082 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083}
84
Elliott Hughes9aa4fda2015-05-11 11:55:25 -070085static bool remount_partition(int fd, const char* dir) {
86 if (!directory_exists(dir)) {
Elliott Hughes5677c232015-05-07 23:37:40 -070087 return true;
88 }
Luis Hector Chavez0db4b7a2018-02-21 21:19:07 -080089 bool is_root = strcmp(dir, "/") == 0;
90 std::string dev = find_mount(dir, is_root);
91 // Even if the device for the root is not found, we still try to remount it
92 // as rw. This typically only happens when running Android in a container:
93 // the root will almost always be in a loop device, which is dynamic, so
94 // it's not convenient to put in the fstab.
95 if (dev.empty() && !is_root) {
Elliott Hughes9aa4fda2015-05-11 11:55:25 -070096 return true;
97 }
Luis Hector Chavez0db4b7a2018-02-21 21:19:07 -080098 if (!dev.empty() && !make_block_device_writable(dev)) {
Elliott Hughes9aa4fda2015-05-11 11:55:25 -070099 WriteFdFmt(fd, "remount of %s failed; couldn't make block device %s writable: %s\n",
100 dir, dev.c_str(), strerror(errno));
101 return false;
102 }
Luis Hector Chavez0db4b7a2018-02-21 21:19:07 -0800103 if (mount(dev.c_str(), dir, "none", MS_REMOUNT | MS_BIND, nullptr) == -1) {
104 // This is useful for cases where the superblock is already marked as
105 // read-write, but the mount itself is read-only, such as containers
106 // where the remount with just MS_REMOUNT is forbidden by the kernel.
107 WriteFdFmt(fd, "remount of the %s mount failed: %s.\n", dir, strerror(errno));
108 return false;
109 }
Elliott Hughes9aa4fda2015-05-11 11:55:25 -0700110 if (mount(dev.c_str(), dir, "none", MS_REMOUNT, nullptr) == -1) {
Luis Hector Chavez0db4b7a2018-02-21 21:19:07 -0800111 WriteFdFmt(fd, "remount of the %s superblock failed: %s\n", dir, strerror(errno));
Elliott Hughes5677c232015-05-07 23:37:40 -0700112 return false;
113 }
Elliott Hughesec7a6672015-03-16 21:58:32 +0000114 return true;
Elliott Hughesec7a6672015-03-16 21:58:32 +0000115}
116
117void remount_service(int fd, void* cookie) {
Nick Kralevich268eb4f2015-02-25 15:48:06 -0800118 if (getuid() != 0) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700119 WriteFdExactly(fd, "Not running as root. Try \"adb root\" first.\n");
Nick Kralevich268eb4f2015-02-25 15:48:06 -0800120 adb_close(fd);
121 return;
122 }
123
Elliott Hughesffdec182016-09-23 15:40:03 -0700124 bool system_verified = !(android::base::GetProperty("partition.system.verified", "").empty());
125 bool vendor_verified = !(android::base::GetProperty("partition.vendor.verified", "").empty());
Paul Lawrence34637552014-10-27 10:37:59 -0700126
127 if (system_verified || vendor_verified) {
128 // Allow remount but warn of likely bad effects
129 bool both = system_verified && vendor_verified;
Elliott Hughesab52c182015-05-01 17:04:38 -0700130 WriteFdFmt(fd,
131 "dm_verity is enabled on the %s%s%s partition%s.\n",
132 system_verified ? "system" : "",
133 both ? " and " : "",
134 vendor_verified ? "vendor" : "",
135 both ? "s" : "");
Elliott Hughese67f1f82015-04-30 17:32:03 -0700136 WriteFdExactly(fd,
137 "Use \"adb disable-verity\" to disable verity.\n"
138 "If you do not, remount may succeed, however, you will still "
139 "not be able to write to these volumes.\n");
Paul Lawrence34637552014-10-27 10:37:59 -0700140 }
141
Elliott Hughesec7a6672015-03-16 21:58:32 +0000142 bool success = true;
Elliott Hughesffdec182016-09-23 15:40:03 -0700143 if (android::base::GetBoolProperty("ro.build.system_root_image", false)) {
Daniel Rosenbergd6eba892015-06-29 17:30:28 -0700144 success &= remount_partition(fd, "/");
145 } else {
146 success &= remount_partition(fd, "/system");
147 }
Elliott Hughes792581f2018-04-03 10:54:39 -0700148 success &= remount_partition(fd, "/odm");
Elliott Hughes9aa4fda2015-05-11 11:55:25 -0700149 success &= remount_partition(fd, "/oem");
Elliott Hughes792581f2018-04-03 10:54:39 -0700150 success &= remount_partition(fd, "/product");
151 success &= remount_partition(fd, "/vendor");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152
Elliott Hughese67f1f82015-04-30 17:32:03 -0700153 WriteFdExactly(fd, success ? "remount succeeded\n" : "remount failed\n");
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700154
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155 adb_close(fd);
156}