summaryrefslogtreecommitdiffstats
path: root/adb/remount_service.cpp
blob: 1eaee732af13d8f55ce6e5fb09c303f408141201 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define TRACE_TAG TRACE_ADB

#include "sysdeps.h"

#include <errno.h>
#include <fcntl.h>
#include <mntent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <unistd.h>

#include <string>

#include "adb.h"
#include "adb_io.h"
#include "adb_utils.h"
#include "cutils/properties.h"

static int system_ro = 1;
static int vendor_ro = 1;
static int oem_ro = 1;

/* Returns the device used to mount a directory in /proc/mounts */
static std::string find_mount(const char *dir) {
    FILE* fp;
    struct mntent* mentry;
    char* device = NULL;

    if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
        return NULL;
    }
    while ((mentry = getmntent(fp)) != NULL) {
        if (strcmp(dir, mentry->mnt_dir) == 0) {
            device = mentry->mnt_fsname;
            break;
        }
    }
    endmntent(fp);
    return device;
}

int make_block_device_writable(const std::string& dev) {
    int fd = unix_open(dev.c_str(), O_RDONLY | O_CLOEXEC);
    if (fd == -1) {
        return -1;
    }

    int result = -1;
    int OFF = 0;
    if (!ioctl(fd, BLKROSET, &OFF)) {
        result = 0;
    }
    adb_close(fd);

    return result;
}

// Init mounts /system as read only, remount to enable writes.
static int remount(const char* dir, int* dir_ro) {
    std::string dev(find_mount(dir));
    if (dev.empty() || make_block_device_writable(dev)) {
        return -1;
    }

    int rc = mount(dev.c_str(), dir, "none", MS_REMOUNT, NULL);
    *dir_ro = rc;
    return rc;
}

static bool remount_partition(int fd, const char* partition, int* ro) {
  if (!directory_exists(partition)) {
    return true;
  }
  if (remount(partition, ro)) {
    char buf[200];
    snprintf(buf, sizeof(buf), "remount of %s failed: %s\n", partition, strerror(errno));
    WriteStringFully(fd, buf);
    return false;
  }
  return true;
}

void remount_service(int fd, void* cookie) {
    char prop_buf[PROPERTY_VALUE_MAX];

    if (getuid() != 0) {
        WriteStringFully(fd, "Not running as root. Try \"adb root\" first.\n");
        adb_close(fd);
        return;
    }

    bool system_verified = false, vendor_verified = false;
    property_get("partition.system.verified", prop_buf, "");
    if (strlen(prop_buf) > 0) {
        system_verified = true;
    }

    property_get("partition.vendor.verified", prop_buf, "");
    if (strlen(prop_buf) > 0) {
        vendor_verified = true;
    }

    if (system_verified || vendor_verified) {
        // Allow remount but warn of likely bad effects
        bool both = system_verified && vendor_verified;
        char buffer[200];
        snprintf(buffer, sizeof(buffer),
                 "dm_verity is enabled on the %s%s%s partition%s.\n",
                 system_verified ? "system" : "",
                 both ? " and " : "",
                 vendor_verified ? "vendor" : "",
                 both ? "s" : "");
        WriteStringFully(fd, buffer);
        snprintf(buffer, sizeof(buffer),
                 "Use \"adb disable-verity\" to disable verity.\n"
                 "If you do not, remount may succeed, however, you will still "
                 "not be able to write to these volumes.\n");
        WriteStringFully(fd, buffer);
    }

    bool success = true;
    success &= remount_partition(fd, "/system", &system_ro);
    success &= remount_partition(fd, "/vendor", &vendor_ro);
    success &= remount_partition(fd, "/oem", &oem_ro);

    WriteStringFully(fd, success ? "remount succeeded\n" : "remount failed\n");

    adb_close(fd);
}