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
17#define TRACE_TAG ADB
18
19#include "sysdeps.h"
20
21#include <errno.h>
22#include <fcntl.h>
23#include <mntent.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/mount.h>
28#include <unistd.h>
29
30#include <string>
31
32#include <android-base/properties.h>
33
34#include "adb.h"
35#include "adb_io.h"
36#include "adb_utils.h"
37#include "fs_mgr.h"
38
39// Returns the device used to mount a directory in /proc/mounts.
40static std::string find_proc_mount(const char* dir) {
41    std::unique_ptr<FILE, int(*)(FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent);
42    if (!fp) {
43        return "";
44    }
45
46    mntent* e;
47    while ((e = getmntent(fp.get())) != nullptr) {
48        if (strcmp(dir, e->mnt_dir) == 0) {
49            return e->mnt_fsname;
50        }
51    }
52    return "";
53}
54
55// Returns the device used to mount a directory in the fstab.
56static std::string find_fstab_mount(const char* dir) {
57    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 : "";
61}
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.
65static std::string find_mount(const char* dir) {
66    if (strcmp(dir, "/") == 0) {
67       return find_fstab_mount(dir);
68    } else {
69       return find_proc_mount(dir);
70    }
71}
72
73bool make_block_device_writable(const std::string& dev) {
74    int fd = unix_open(dev.c_str(), O_RDONLY | O_CLOEXEC);
75    if (fd == -1) {
76        return false;
77    }
78
79    int OFF = 0;
80    bool result = (ioctl(fd, BLKROSET, &OFF) != -1);
81    unix_close(fd);
82    return result;
83}
84
85static bool remount_partition(int fd, const char* dir) {
86    if (!directory_exists(dir)) {
87        return true;
88    }
89    std::string dev = find_mount(dir);
90    if (dev.empty()) {
91        return true;
92    }
93    if (!make_block_device_writable(dev)) {
94        WriteFdFmt(fd, "remount of %s failed; couldn't make block device %s writable: %s\n",
95                   dir, dev.c_str(), strerror(errno));
96        return false;
97    }
98    if (mount(dev.c_str(), dir, "none", MS_REMOUNT, nullptr) == -1) {
99        WriteFdFmt(fd, "remount of %s failed: %s\n", dir, strerror(errno));
100        return false;
101    }
102    return true;
103}
104
105void remount_service(int fd, void* cookie) {
106    if (getuid() != 0) {
107        WriteFdExactly(fd, "Not running as root. Try \"adb root\" first.\n");
108        adb_close(fd);
109        return;
110    }
111
112    bool system_verified = !(android::base::GetProperty("partition.system.verified", "").empty());
113    bool vendor_verified = !(android::base::GetProperty("partition.vendor.verified", "").empty());
114
115    if (system_verified || vendor_verified) {
116        // Allow remount but warn of likely bad effects
117        bool both = system_verified && vendor_verified;
118        WriteFdFmt(fd,
119                   "dm_verity is enabled on the %s%s%s partition%s.\n",
120                   system_verified ? "system" : "",
121                   both ? " and " : "",
122                   vendor_verified ? "vendor" : "",
123                   both ? "s" : "");
124        WriteFdExactly(fd,
125                       "Use \"adb disable-verity\" to disable verity.\n"
126                       "If you do not, remount may succeed, however, you will still "
127                       "not be able to write to these volumes.\n");
128    }
129
130    bool success = true;
131    if (android::base::GetBoolProperty("ro.build.system_root_image", false)) {
132        success &= remount_partition(fd, "/");
133    } else {
134        success &= remount_partition(fd, "/system");
135    }
136    success &= remount_partition(fd, "/vendor");
137    success &= remount_partition(fd, "/oem");
138
139    WriteFdExactly(fd, success ? "remount succeeded\n" : "remount failed\n");
140
141    adb_close(fd);
142}
143