2009-03-04 03:32:55 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-09-22 22:52:57 +00:00
|
|
|
#define TRACE_TAG ADB
|
2015-03-19 22:21:08 +00:00
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
|
2014-04-30 16:10:31 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2015-01-02 22:02:14 +00:00
|
|
|
#include <mntent.h>
|
2018-05-11 23:34:55 +00:00
|
|
|
#include <spawn.h>
|
2009-03-04 03:32:55 +00:00
|
|
|
#include <stdio.h>
|
2014-04-30 16:10:31 +00:00
|
|
|
#include <stdlib.h>
|
2009-03-04 03:32:55 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mount.h>
|
2018-07-19 00:43:28 +00:00
|
|
|
#include <sys/statvfs.h>
|
2018-04-26 21:32:48 +00:00
|
|
|
#include <sys/vfs.h>
|
2014-04-30 16:10:31 +00:00
|
|
|
#include <unistd.h>
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2018-05-17 19:12:54 +00:00
|
|
|
#include <set>
|
2015-03-16 21:58:32 +00:00
|
|
|
#include <string>
|
2018-04-26 21:32:48 +00:00
|
|
|
#include <vector>
|
2015-03-16 21:58:32 +00:00
|
|
|
|
2016-09-23 22:40:03 +00:00
|
|
|
#include <android-base/properties.h>
|
2018-05-17 19:12:54 +00:00
|
|
|
#include <bootloader_message/bootloader_message.h>
|
|
|
|
#include <cutils/android_reboot.h>
|
2018-04-26 21:32:48 +00:00
|
|
|
#include <ext4_utils/ext4_utils.h>
|
2016-09-23 22:40:03 +00:00
|
|
|
|
2009-03-04 03:32:55 +00:00
|
|
|
#include "adb.h"
|
2015-02-25 05:26:58 +00:00
|
|
|
#include "adb_io.h"
|
2018-04-26 21:32:48 +00:00
|
|
|
#include "adb_unique_fd.h"
|
2015-04-17 20:57:15 +00:00
|
|
|
#include "adb_utils.h"
|
2015-06-30 00:30:28 +00:00
|
|
|
#include "fs_mgr.h"
|
2018-07-19 02:40:12 +00:00
|
|
|
#include "set_verity_enable_state_service.h"
|
2015-06-30 00:30:28 +00:00
|
|
|
|
2015-05-08 06:37:40 +00:00
|
|
|
// Returns the device used to mount a directory in /proc/mounts.
|
2015-06-30 00:30:28 +00:00
|
|
|
static std::string find_proc_mount(const char* dir) {
|
2015-05-08 06:37:40 +00:00
|
|
|
std::unique_ptr<FILE, int(*)(FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent);
|
|
|
|
if (!fp) {
|
|
|
|
return "";
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
2015-05-08 06:37:40 +00:00
|
|
|
|
|
|
|
mntent* e;
|
|
|
|
while ((e = getmntent(fp.get())) != nullptr) {
|
|
|
|
if (strcmp(dir, e->mnt_dir) == 0) {
|
|
|
|
return e->mnt_fsname;
|
2015-01-02 22:02:14 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-08 06:37:40 +00:00
|
|
|
return "";
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 00:30:28 +00:00
|
|
|
// Returns the device used to mount a directory in the fstab.
|
|
|
|
static std::string find_fstab_mount(const char* dir) {
|
2017-03-10 08:43:02 +00:00
|
|
|
std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
|
|
|
|
fs_mgr_free_fstab);
|
|
|
|
struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab.get(), dir);
|
|
|
|
return rec ? rec->blk_device : "";
|
2015-06-30 00:30:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The proc entry for / is full of lies, so check fstab instead.
|
|
|
|
// /proc/mounts lists rootfs and /dev/root, neither of which is what we want.
|
2018-02-22 05:19:07 +00:00
|
|
|
static std::string find_mount(const char* dir, bool is_root) {
|
|
|
|
if (is_root) {
|
|
|
|
return find_fstab_mount(dir);
|
2015-06-30 00:30:28 +00:00
|
|
|
} else {
|
|
|
|
return find_proc_mount(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 18:55:25 +00:00
|
|
|
bool make_block_device_writable(const std::string& dev) {
|
2015-03-16 21:58:32 +00:00
|
|
|
int fd = unix_open(dev.c_str(), O_RDONLY | O_CLOEXEC);
|
|
|
|
if (fd == -1) {
|
2015-05-11 18:55:25 +00:00
|
|
|
return false;
|
2014-12-03 23:31:57 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 21:58:32 +00:00
|
|
|
int OFF = 0;
|
2015-05-11 18:55:25 +00:00
|
|
|
bool result = (ioctl(fd, BLKROSET, &OFF) != -1);
|
2015-05-23 03:09:06 +00:00
|
|
|
unix_close(fd);
|
2015-03-16 21:58:32 +00:00
|
|
|
return result;
|
|
|
|
}
|
2015-03-16 21:35:53 +00:00
|
|
|
|
2018-07-30 23:06:01 +00:00
|
|
|
static bool fs_has_shared_blocks(const std::string& mount_point, const std::string& device) {
|
|
|
|
std::string path = mount_point + "/lost+found";
|
2018-04-26 21:32:48 +00:00
|
|
|
struct statfs fs;
|
2018-07-30 23:06:01 +00:00
|
|
|
if (statfs(path.c_str(), &fs) == -1 || fs.f_type != EXT4_SUPER_MAGIC) {
|
2018-04-26 21:32:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-07-30 23:06:01 +00:00
|
|
|
unique_fd fd(unix_open(device.c_str(), O_RDONLY));
|
2018-04-26 21:32:48 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
struct ext4_super_block sb;
|
|
|
|
if (lseek64(fd, 1024, SEEK_SET) < 0 || unix_read(fd, &sb, sizeof(sb)) < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
struct fs_info info;
|
|
|
|
if (ext4_parse_sb(&sb, &info) < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (info.feat_ro_compat & EXT4_FEATURE_RO_COMPAT_SHARED_BLOCKS) != 0;
|
|
|
|
}
|
|
|
|
|
2018-05-11 23:34:55 +00:00
|
|
|
static bool can_unshare_blocks(int fd, const char* dev) {
|
|
|
|
const char* E2FSCK_BIN = "/system/bin/e2fsck";
|
|
|
|
if (access(E2FSCK_BIN, X_OK)) {
|
|
|
|
WriteFdFmt(fd, "e2fsck is not available, cannot undo deduplication on %s\n", dev);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t child;
|
|
|
|
char* env[] = {nullptr};
|
|
|
|
const char* argv[] = {E2FSCK_BIN, "-n", "-E", "unshare_blocks", dev, nullptr};
|
|
|
|
if (posix_spawn(&child, E2FSCK_BIN, nullptr, nullptr, const_cast<char**>(argv), env)) {
|
|
|
|
WriteFdFmt(fd, "failed to e2fsck to check deduplication: %s\n", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int status = 0;
|
|
|
|
int ret = TEMP_FAILURE_RETRY(waitpid(child, &status, 0));
|
|
|
|
if (ret < 0) {
|
|
|
|
WriteFdFmt(fd, "failed to get e2fsck status: %s\n", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!WIFEXITED(status)) {
|
|
|
|
WriteFdFmt(fd, "e2fsck exited abnormally with status %d\n", status);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int rc = WEXITSTATUS(status);
|
|
|
|
if (rc != 0) {
|
|
|
|
WriteFdFmt(fd,
|
|
|
|
"%s is deduplicated, and an e2fsck check failed. It might not "
|
|
|
|
"have enough free-space to be remounted as writable.\n",
|
|
|
|
dev);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-19 00:43:28 +00:00
|
|
|
static unsigned long get_mount_flags(int fd, const char* dir) {
|
|
|
|
struct statvfs st_vfs;
|
|
|
|
if (statvfs(dir, &st_vfs) == -1) {
|
|
|
|
// Even though we could not get the original mount flags, assume that
|
|
|
|
// the mount was originally read-only.
|
|
|
|
WriteFdFmt(fd, "statvfs of the %s mount failed: %s.\n", dir, strerror(errno));
|
|
|
|
return MS_RDONLY;
|
|
|
|
}
|
|
|
|
return st_vfs.f_flag;
|
|
|
|
}
|
|
|
|
|
2018-05-17 19:12:54 +00:00
|
|
|
static bool remount_partition(int fd, const char* dir) {
|
2015-05-11 18:55:25 +00:00
|
|
|
if (!directory_exists(dir)) {
|
|
|
|
return true;
|
2015-01-02 13:30:50 +00:00
|
|
|
}
|
2018-02-22 05:19:07 +00:00
|
|
|
bool is_root = strcmp(dir, "/") == 0;
|
|
|
|
std::string dev = find_mount(dir, is_root);
|
|
|
|
// Even if the device for the root is not found, we still try to remount it
|
|
|
|
// as rw. This typically only happens when running Android in a container:
|
|
|
|
// the root will almost always be in a loop device, which is dynamic, so
|
|
|
|
// it's not convenient to put in the fstab.
|
|
|
|
if (dev.empty() && !is_root) {
|
2015-05-08 06:37:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-02-22 05:19:07 +00:00
|
|
|
if (!dev.empty() && !make_block_device_writable(dev)) {
|
2015-05-11 18:55:25 +00:00
|
|
|
WriteFdFmt(fd, "remount of %s failed; couldn't make block device %s writable: %s\n",
|
|
|
|
dir, dev.c_str(), strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-19 00:43:28 +00:00
|
|
|
|
|
|
|
unsigned long remount_flags = get_mount_flags(fd, dir);
|
|
|
|
remount_flags &= ~MS_RDONLY;
|
|
|
|
remount_flags |= MS_REMOUNT;
|
|
|
|
|
|
|
|
if (mount(dev.c_str(), dir, "none", remount_flags | MS_BIND, nullptr) == -1) {
|
2018-02-22 05:19:07 +00:00
|
|
|
// This is useful for cases where the superblock is already marked as
|
|
|
|
// read-write, but the mount itself is read-only, such as containers
|
|
|
|
// where the remount with just MS_REMOUNT is forbidden by the kernel.
|
|
|
|
WriteFdFmt(fd, "remount of the %s mount failed: %s.\n", dir, strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-22 05:49:14 +00:00
|
|
|
if (mount(dev.c_str(), dir, "none", MS_REMOUNT, nullptr) == -1) {
|
2018-02-22 05:19:07 +00:00
|
|
|
WriteFdFmt(fd, "remount of the %s superblock failed: %s\n", dir, strerror(errno));
|
2015-05-08 06:37:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-03-16 21:58:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-17 19:12:54 +00:00
|
|
|
static void reboot_for_remount(int fd, bool need_fsck) {
|
|
|
|
std::string reboot_cmd = "reboot";
|
|
|
|
if (need_fsck) {
|
|
|
|
const std::vector<std::string> options = {"--fsck_unshare_blocks"};
|
|
|
|
std::string err;
|
|
|
|
if (!write_bootloader_message(options, &err)) {
|
|
|
|
WriteFdFmt(fd, "Failed to set bootloader message: %s\n", err.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteFdExactly(fd,
|
|
|
|
"The device will now reboot to recovery and attempt "
|
|
|
|
"un-deduplication.\n");
|
|
|
|
reboot_cmd = "reboot,recovery";
|
|
|
|
}
|
|
|
|
|
|
|
|
sync();
|
|
|
|
android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd.c_str());
|
|
|
|
}
|
|
|
|
|
2018-07-25 23:51:59 +00:00
|
|
|
void remount_service(unique_fd fd, const std::string& cmd) {
|
2018-07-27 23:06:24 +00:00
|
|
|
bool user_requested_reboot = cmd == "-R";
|
2018-05-17 19:12:54 +00:00
|
|
|
|
2015-02-25 23:48:06 +00:00
|
|
|
if (getuid() != 0) {
|
2018-07-19 02:40:12 +00:00
|
|
|
WriteFdExactly(fd.get(), "Not running as root. Try \"adb root\" first.\n");
|
2015-02-25 23:48:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-23 22:40:03 +00:00
|
|
|
bool system_verified = !(android::base::GetProperty("partition.system.verified", "").empty());
|
|
|
|
bool vendor_verified = !(android::base::GetProperty("partition.vendor.verified", "").empty());
|
2014-10-27 17:37:59 +00:00
|
|
|
|
2018-05-17 19:12:54 +00:00
|
|
|
std::vector<std::string> partitions = {"/odm", "/oem", "/product", "/vendor"};
|
|
|
|
if (android::base::GetBoolProperty("ro.build.system_root_image", false)) {
|
|
|
|
partitions.push_back("/");
|
|
|
|
} else {
|
|
|
|
partitions.push_back("/system");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find partitions that are deduplicated, and can be un-deduplicated.
|
|
|
|
std::set<std::string> dedup;
|
|
|
|
for (const auto& partition : partitions) {
|
|
|
|
std::string dev = find_mount(partition.c_str(), partition == "/");
|
2018-07-30 23:06:01 +00:00
|
|
|
if (dev.empty() || !fs_has_shared_blocks(partition, dev)) {
|
2018-05-17 19:12:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-07-19 02:40:12 +00:00
|
|
|
if (can_unshare_blocks(fd.get(), dev.c_str())) {
|
2018-05-17 19:12:54 +00:00
|
|
|
dedup.emplace(partition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool verity_enabled = (system_verified || vendor_verified);
|
|
|
|
|
|
|
|
// Reboot now if the user requested it (and an operation needs a reboot).
|
|
|
|
if (user_requested_reboot) {
|
|
|
|
if (!dedup.empty() || verity_enabled) {
|
|
|
|
if (verity_enabled) {
|
2018-07-25 23:51:59 +00:00
|
|
|
set_verity_enabled_state_service(unique_fd(dup(fd.get())), false);
|
2018-05-17 19:12:54 +00:00
|
|
|
}
|
2018-07-19 02:40:12 +00:00
|
|
|
reboot_for_remount(fd.get(), !dedup.empty());
|
2018-05-17 19:12:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-07-19 02:40:12 +00:00
|
|
|
WriteFdExactly(fd.get(), "No reboot needed, skipping -R.\n");
|
2018-05-17 19:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we need to disable-verity, but we also need to perform a recovery
|
|
|
|
// fsck for deduplicated partitions, hold off on warning about verity. We
|
|
|
|
// can handle both verity and the recovery fsck in the same reboot cycle.
|
|
|
|
if (verity_enabled && dedup.empty()) {
|
2014-10-27 17:37:59 +00:00
|
|
|
// Allow remount but warn of likely bad effects
|
|
|
|
bool both = system_verified && vendor_verified;
|
2018-07-19 02:40:12 +00:00
|
|
|
WriteFdFmt(fd.get(), "dm_verity is enabled on the %s%s%s partition%s.\n",
|
|
|
|
system_verified ? "system" : "", both ? " and " : "",
|
|
|
|
vendor_verified ? "vendor" : "", both ? "s" : "");
|
|
|
|
WriteFdExactly(fd.get(),
|
2015-05-01 00:32:03 +00:00
|
|
|
"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");
|
2018-07-19 02:40:12 +00:00
|
|
|
WriteFdExactly(fd.get(),
|
2018-05-17 19:12:54 +00:00
|
|
|
"Alternately, use \"adb remount -R\" to disable verity "
|
|
|
|
"and automatically reboot.\n");
|
2014-10-27 17:37:59 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 21:58:32 +00:00
|
|
|
bool success = true;
|
2018-05-17 19:12:54 +00:00
|
|
|
for (const auto& partition : partitions) {
|
|
|
|
// Don't try to remount partitions that need an fsck in recovery.
|
|
|
|
if (dedup.count(partition)) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-19 02:40:12 +00:00
|
|
|
success &= remount_partition(fd.get(), partition.c_str());
|
2018-04-26 21:32:48 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 19:12:54 +00:00
|
|
|
if (!dedup.empty()) {
|
2018-07-19 02:40:12 +00:00
|
|
|
WriteFdExactly(fd.get(),
|
2018-05-17 19:12:54 +00:00
|
|
|
"The following partitions are deduplicated and cannot "
|
|
|
|
"yet be remounted:\n");
|
2018-04-26 21:32:48 +00:00
|
|
|
for (const std::string& name : dedup) {
|
2018-07-19 02:40:12 +00:00
|
|
|
WriteFdFmt(fd.get(), " %s\n", name.c_str());
|
2018-04-26 21:32:48 +00:00
|
|
|
}
|
2018-05-17 19:12:54 +00:00
|
|
|
|
2018-07-19 02:40:12 +00:00
|
|
|
WriteFdExactly(fd.get(),
|
2018-05-17 19:12:54 +00:00
|
|
|
"To reboot and un-deduplicate the listed partitions, "
|
|
|
|
"please retry with adb remount -R.\n");
|
|
|
|
if (system_verified || vendor_verified) {
|
2018-07-19 02:40:12 +00:00
|
|
|
WriteFdExactly(fd.get(), "Note: verity will be automatically disabled after reboot.\n");
|
2018-05-17 19:12:54 +00:00
|
|
|
}
|
|
|
|
return;
|
2015-06-30 00:30:28 +00:00
|
|
|
}
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2018-05-17 19:12:54 +00:00
|
|
|
if (!success) {
|
2018-07-19 02:40:12 +00:00
|
|
|
WriteFdExactly(fd.get(), "remount failed\n");
|
2018-05-17 19:12:54 +00:00
|
|
|
} else {
|
2018-07-19 02:40:12 +00:00
|
|
|
WriteFdExactly(fd.get(), "remount succeeded\n");
|
2018-05-17 19:12:54 +00:00
|
|
|
}
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|