blob: f645a7885ef5e1fbe3c86e18032fda23cf5c8be7 [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
Dan Albert33134262015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_ADB
18
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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080033#include "adb_io.h"
Elliott Hughes58305772015-04-17 13:57:15 -070034#include "adb_utils.h"
Dan Albert76649012015-02-24 15:51:19 -080035#include "cutils/properties.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036
37static int system_ro = 1;
Daniel Rosenberg686bce62014-06-30 20:29:40 -070038static int vendor_ro = 1;
Elliott Hughesec7a6672015-03-16 21:58:32 +000039static int oem_ro = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
Elliott Hughes5677c232015-05-07 23:37:40 -070041// Returns the device used to mount a directory in /proc/mounts.
42static std::string find_mount(const char* dir) {
43 std::unique_ptr<FILE, int(*)(FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent);
44 if (!fp) {
45 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046 }
Elliott Hughes5677c232015-05-07 23:37:40 -070047
48 mntent* e;
49 while ((e = getmntent(fp.get())) != nullptr) {
50 if (strcmp(dir, e->mnt_dir) == 0) {
51 return e->mnt_fsname;
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080052 }
53 }
Elliott Hughes5677c232015-05-07 23:37:40 -070054 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055}
56
Elliott Hughesec7a6672015-03-16 21:58:32 +000057int make_block_device_writable(const std::string& dev) {
58 int fd = unix_open(dev.c_str(), O_RDONLY | O_CLOEXEC);
59 if (fd == -1) {
60 return -1;
61 }
62
63 int result = -1;
Paul Lawrence982089d2014-12-03 15:31:57 -080064 int OFF = 0;
Elliott Hughesec7a6672015-03-16 21:58:32 +000065 if (!ioctl(fd, BLKROSET, &OFF)) {
66 result = 0;
Sami Tolvanen13449cd2015-01-02 13:30:50 +000067 }
Elliott Hughesec7a6672015-03-16 21:58:32 +000068 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
Elliott Hughesec7a6672015-03-16 21:58:32 +000070 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071}
72
Elliott Hughesec7a6672015-03-16 21:58:32 +000073// Init mounts /system as read only, remount to enable writes.
74static int remount(const char* dir, int* dir_ro) {
Elliott Hughes5677c232015-05-07 23:37:40 -070075 std::string dev = find_mount(dir);
Elliott Hughesec7a6672015-03-16 21:58:32 +000076 if (dev.empty() || make_block_device_writable(dev)) {
77 return -1;
Dan Albert6084a012015-03-16 21:35:53 +000078 }
79
Elliott Hughesec7a6672015-03-16 21:58:32 +000080 int rc = mount(dev.c_str(), dir, "none", MS_REMOUNT, NULL);
Dan Albert6084a012015-03-16 21:35:53 +000081 *dir_ro = rc;
Dan Albert6084a012015-03-16 21:35:53 +000082 return rc;
MÃ¥rten Kongstad81416fd2014-11-03 13:52:57 +010083}
84
Elliott Hughesec7a6672015-03-16 21:58:32 +000085static bool remount_partition(int fd, const char* partition, int* ro) {
Elliott Hughes5677c232015-05-07 23:37:40 -070086 if (!directory_exists(partition)) {
87 return true;
88 }
89 if (remount(partition, ro)) {
90 WriteFdFmt(fd, "remount of %s failed: %s\n", partition, strerror(errno));
91 return false;
92 }
Elliott Hughesec7a6672015-03-16 21:58:32 +000093 return true;
Elliott Hughesec7a6672015-03-16 21:58:32 +000094}
95
96void remount_service(int fd, void* cookie) {
Nick Kralevich268eb4f2015-02-25 15:48:06 -080097 if (getuid() != 0) {
Elliott Hughese67f1f82015-04-30 17:32:03 -070098 WriteFdExactly(fd, "Not running as root. Try \"adb root\" first.\n");
Nick Kralevich268eb4f2015-02-25 15:48:06 -080099 adb_close(fd);
100 return;
101 }
102
Elliott Hughes5677c232015-05-07 23:37:40 -0700103 char prop_buf[PROPERTY_VALUE_MAX];
Sami Tolvanen45474232015-03-30 11:38:38 +0100104 property_get("partition.system.verified", prop_buf, "");
Elliott Hughes5677c232015-05-07 23:37:40 -0700105 bool system_verified = (strlen(prop_buf) > 0);
Paul Lawrence34637552014-10-27 10:37:59 -0700106
Sami Tolvanen45474232015-03-30 11:38:38 +0100107 property_get("partition.vendor.verified", prop_buf, "");
Elliott Hughes5677c232015-05-07 23:37:40 -0700108 bool vendor_verified = (strlen(prop_buf) > 0);
Paul Lawrence34637552014-10-27 10:37:59 -0700109
110 if (system_verified || vendor_verified) {
111 // Allow remount but warn of likely bad effects
112 bool both = system_verified && vendor_verified;
Elliott Hughesab52c182015-05-01 17:04:38 -0700113 WriteFdFmt(fd,
114 "dm_verity is enabled on the %s%s%s partition%s.\n",
115 system_verified ? "system" : "",
116 both ? " and " : "",
117 vendor_verified ? "vendor" : "",
118 both ? "s" : "");
Elliott Hughese67f1f82015-04-30 17:32:03 -0700119 WriteFdExactly(fd,
120 "Use \"adb disable-verity\" to disable verity.\n"
121 "If you do not, remount may succeed, however, you will still "
122 "not be able to write to these volumes.\n");
Paul Lawrence34637552014-10-27 10:37:59 -0700123 }
124
Elliott Hughesec7a6672015-03-16 21:58:32 +0000125 bool success = true;
126 success &= remount_partition(fd, "/system", &system_ro);
127 success &= remount_partition(fd, "/vendor", &vendor_ro);
128 success &= remount_partition(fd, "/oem", &oem_ro);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129
Elliott Hughese67f1f82015-04-30 17:32:03 -0700130 WriteFdExactly(fd, success ? "remount succeeded\n" : "remount failed\n");
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700131
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 adb_close(fd);
133}