1 /*
2  * Copyright (C) 2018 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 #include "switch_root.h"
18 
19 #include <fcntl.h>
20 #include <mntent.h>
21 #include <sys/mount.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 
25 #include <android-base/logging.h>
26 #include <android-base/strings.h>
27 
28 using android::base::StartsWith;
29 
30 using namespace std::literals;
31 
32 namespace android {
33 namespace init {
34 
35 namespace {
36 
GetMounts(const std::string & new_root)37 std::vector<std::string> GetMounts(const std::string& new_root) {
38     auto fp = std::unique_ptr<std::FILE, decltype(&endmntent)>{setmntent("/proc/mounts", "re"),
39                                                                endmntent};
40     if (fp == nullptr) {
41         PLOG(FATAL) << "Failed to open /proc/mounts";
42     }
43 
44     std::vector<std::string> result;
45     mntent* mentry;
46     while ((mentry = getmntent(fp.get())) != nullptr) {
47         // We won't try to move rootfs.
48         if (mentry->mnt_dir == "/"s) {
49             continue;
50         }
51 
52         // The new root mount is handled separately.
53         if (mentry->mnt_dir == new_root) {
54             continue;
55         }
56 
57         // Move operates on subtrees, so do not try to move children of other mounts.
58         if (std::find_if(result.begin(), result.end(), [&mentry](const auto& older_mount) {
59                 return StartsWith(mentry->mnt_dir, older_mount);
60             }) != result.end()) {
61             continue;
62         }
63 
64         result.emplace_back(mentry->mnt_dir);
65     }
66 
67     return result;
68 }
69 
70 }  // namespace
71 
SwitchRoot(const std::string & new_root)72 void SwitchRoot(const std::string& new_root) {
73     auto mounts = GetMounts(new_root);
74 
75     LOG(INFO) << "Switching root to '" << new_root << "'";
76 
77     for (const auto& mount_path : mounts) {
78         auto new_mount_path = new_root + mount_path;
79         mkdir(new_mount_path.c_str(), 0755);
80         if (mount(mount_path.c_str(), new_mount_path.c_str(), nullptr, MS_MOVE, nullptr) != 0) {
81             PLOG(FATAL) << "Unable to move mount at '" << mount_path << "'";
82         }
83     }
84 
85     if (chdir(new_root.c_str()) != 0) {
86         PLOG(FATAL) << "Could not chdir to new_root, '" << new_root << "'";
87     }
88 
89     if (mount(new_root.c_str(), "/", nullptr, MS_MOVE, nullptr) != 0) {
90         PLOG(FATAL) << "Unable to move root mount to new_root, '" << new_root << "'";
91     }
92 
93     if (chroot(".") != 0) {
94         PLOG(FATAL) << "Unable to chroot to new root";
95     }
96 }
97 
98 }  // namespace init
99 }  // namespace android
100