Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 "service_utils.h" |
| 18 | |
| 19 | #include <grp.h> |
| 20 | #include <sys/mount.h> |
| 21 | #include <sys/prctl.h> |
| 22 | #include <sys/wait.h> |
| 23 | |
| 24 | #include <android-base/file.h> |
| 25 | #include <android-base/logging.h> |
| 26 | #include <android-base/properties.h> |
| 27 | #include <android-base/stringprintf.h> |
| 28 | #include <android-base/strings.h> |
| 29 | #include <android-base/unique_fd.h> |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame^] | 30 | #include <cutils/android_get_control_file.h> |
| 31 | #include <cutils/sockets.h> |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 32 | #include <processgroup/processgroup.h> |
| 33 | |
| 34 | #include "mount_namespace.h" |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame^] | 35 | #include "util.h" |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 36 | |
| 37 | using android::base::GetProperty; |
| 38 | using android::base::StartsWith; |
| 39 | using android::base::StringPrintf; |
| 40 | using android::base::unique_fd; |
| 41 | using android::base::WriteStringToFile; |
| 42 | |
| 43 | namespace android { |
| 44 | namespace init { |
| 45 | |
| 46 | namespace { |
| 47 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 48 | Result<void> EnterNamespace(int nstype, const char* path) { |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 49 | auto fd = unique_fd{open(path, O_RDONLY | O_CLOEXEC)}; |
| 50 | if (fd == -1) { |
| 51 | return ErrnoError() << "Could not open namespace at " << path; |
| 52 | } |
| 53 | if (setns(fd, nstype) == -1) { |
| 54 | return ErrnoError() << "Could not setns() namespace at " << path; |
| 55 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 56 | return {}; |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 59 | Result<void> SetUpMountNamespace(bool remount_proc, bool remount_sys) { |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 60 | constexpr unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID; |
| 61 | |
| 62 | // Recursively remount / as slave like zygote does so unmounting and mounting /proc |
| 63 | // doesn't interfere with the parent namespace's /proc mount. This will also |
| 64 | // prevent any other mounts/unmounts initiated by the service from interfering |
| 65 | // with the parent namespace but will still allow mount events from the parent |
| 66 | // namespace to propagate to the child. |
| 67 | if (mount("rootfs", "/", nullptr, (MS_SLAVE | MS_REC), nullptr) == -1) { |
| 68 | return ErrnoError() << "Could not remount(/) recursively as slave"; |
| 69 | } |
| 70 | |
| 71 | // umount() then mount() /proc and/or /sys |
| 72 | // Note that it is not sufficient to mount with MS_REMOUNT. |
| 73 | if (remount_proc) { |
| 74 | if (umount("/proc") == -1) { |
| 75 | return ErrnoError() << "Could not umount(/proc)"; |
| 76 | } |
| 77 | if (mount("", "/proc", "proc", kSafeFlags, "") == -1) { |
| 78 | return ErrnoError() << "Could not mount(/proc)"; |
| 79 | } |
| 80 | } |
| 81 | if (remount_sys) { |
| 82 | if (umount2("/sys", MNT_DETACH) == -1) { |
| 83 | return ErrnoError() << "Could not umount(/sys)"; |
| 84 | } |
| 85 | if (mount("", "/sys", "sysfs", kSafeFlags, "") == -1) { |
| 86 | return ErrnoError() << "Could not mount(/sys)"; |
| 87 | } |
| 88 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 89 | return {}; |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 92 | Result<void> SetUpPidNamespace(const char* name) { |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 93 | if (prctl(PR_SET_NAME, name) == -1) { |
| 94 | return ErrnoError() << "Could not set name"; |
| 95 | } |
| 96 | |
| 97 | pid_t child_pid = fork(); |
| 98 | if (child_pid == -1) { |
| 99 | return ErrnoError() << "Could not fork init inside the PID namespace"; |
| 100 | } |
| 101 | |
| 102 | if (child_pid > 0) { |
| 103 | // So that we exit with the right status. |
| 104 | static int init_exitstatus = 0; |
| 105 | signal(SIGTERM, [](int) { _exit(init_exitstatus); }); |
| 106 | |
| 107 | pid_t waited_pid; |
| 108 | int status; |
| 109 | while ((waited_pid = wait(&status)) > 0) { |
| 110 | // This loop will end when there are no processes left inside the |
| 111 | // PID namespace or when the init process inside the PID namespace |
| 112 | // gets a signal. |
| 113 | if (waited_pid == child_pid) { |
| 114 | init_exitstatus = status; |
| 115 | } |
| 116 | } |
| 117 | if (!WIFEXITED(init_exitstatus)) { |
| 118 | _exit(EXIT_FAILURE); |
| 119 | } |
| 120 | _exit(WEXITSTATUS(init_exitstatus)); |
| 121 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 122 | return {}; |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void ZapStdio() { |
Tom Cherry | 247ffbf | 2019-07-08 15:09:36 -0700 | [diff] [blame] | 126 | auto fd = unique_fd{open("/dev/null", O_RDWR | O_CLOEXEC)}; |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 127 | dup2(fd, 0); |
| 128 | dup2(fd, 1); |
| 129 | dup2(fd, 2); |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void OpenConsole(const std::string& console) { |
Tom Cherry | 247ffbf | 2019-07-08 15:09:36 -0700 | [diff] [blame] | 133 | auto fd = unique_fd{open(console.c_str(), O_RDWR | O_CLOEXEC)}; |
| 134 | if (fd == -1) fd.reset(open("/dev/null", O_RDWR | O_CLOEXEC)); |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 135 | ioctl(fd, TIOCSCTTY, 0); |
| 136 | dup2(fd, 0); |
| 137 | dup2(fd, 1); |
| 138 | dup2(fd, 2); |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame^] | 141 | void PublishDescriptor(const std::string& key, const std::string& name, int fd) { |
| 142 | std::string published_name = key + name; |
| 143 | for (auto& c : published_name) { |
| 144 | c = isalnum(c) ? c : '_'; |
| 145 | } |
| 146 | |
| 147 | std::string val = std::to_string(fd); |
| 148 | setenv(published_name.c_str(), val.c_str(), 1); |
| 149 | } |
| 150 | |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 151 | } // namespace |
| 152 | |
Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame^] | 153 | Result<void> SocketDescriptor::CreateAndPublish(const std::string& global_context) const { |
| 154 | const auto& socket_context = context.empty() ? global_context : context; |
| 155 | auto result = CreateSocket(name, type, passcred, perm, uid, gid, socket_context); |
| 156 | if (!result) { |
| 157 | return result.error(); |
| 158 | } |
| 159 | |
| 160 | PublishDescriptor(ANDROID_SOCKET_ENV_PREFIX, name, *result); |
| 161 | |
| 162 | return {}; |
| 163 | } |
| 164 | |
| 165 | Result<void> FileDescriptor::CreateAndPublish() const { |
| 166 | int flags = (type == "r") ? O_RDONLY : (type == "w") ? O_WRONLY : O_RDWR; |
| 167 | |
| 168 | // Make sure we do not block on open (eg: devices can chose to block on carrier detect). Our |
| 169 | // intention is never to delay launch of a service for such a condition. The service can |
| 170 | // perform its own blocking on carrier detect. |
| 171 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(name.c_str(), flags | O_NONBLOCK))); |
| 172 | |
| 173 | if (fd < 0) { |
| 174 | return ErrnoError() << "Failed to open file '" << name << "'"; |
| 175 | } |
| 176 | |
| 177 | // Fixup as we set O_NONBLOCK for open, the intent for fd is to block reads. |
| 178 | fcntl(fd, F_SETFL, flags); |
| 179 | |
| 180 | LOG(INFO) << "Opened file '" << name << "', flags " << flags; |
| 181 | |
| 182 | PublishDescriptor(ANDROID_FILE_ENV_PREFIX, name, fd.release()); |
| 183 | |
| 184 | return {}; |
| 185 | } |
| 186 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 187 | Result<void> EnterNamespaces(const NamespaceInfo& info, const std::string& name, bool pre_apexd) { |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 188 | for (const auto& [nstype, path] : info.namespaces_to_enter) { |
| 189 | if (auto result = EnterNamespace(nstype, path.c_str()); !result) { |
| 190 | return result; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | #if defined(__ANDROID__) |
| 195 | if (pre_apexd) { |
| 196 | if (!SwitchToBootstrapMountNamespaceIfNeeded()) { |
| 197 | return Error() << "could not enter into the bootstrap mount namespace"; |
| 198 | } |
| 199 | } |
| 200 | #endif |
| 201 | |
| 202 | if (info.flags & CLONE_NEWNS) { |
| 203 | bool remount_proc = info.flags & CLONE_NEWPID; |
| 204 | bool remount_sys = |
| 205 | std::any_of(info.namespaces_to_enter.begin(), info.namespaces_to_enter.end(), |
| 206 | [](const auto& entry) { return entry.first == CLONE_NEWNET; }); |
| 207 | if (auto result = SetUpMountNamespace(remount_proc, remount_sys); !result) { |
| 208 | return result; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | if (info.flags & CLONE_NEWPID) { |
| 213 | // This will fork again to run an init process inside the PID namespace. |
| 214 | if (auto result = SetUpPidNamespace(name.c_str()); !result) { |
| 215 | return result; |
| 216 | } |
| 217 | } |
| 218 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 219 | return {}; |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 222 | Result<void> SetProcessAttributes(const ProcessAttributes& attr) { |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 223 | if (attr.ioprio_class != IoSchedClass_NONE) { |
| 224 | if (android_set_ioprio(getpid(), attr.ioprio_class, attr.ioprio_pri)) { |
| 225 | PLOG(ERROR) << "failed to set pid " << getpid() << " ioprio=" << attr.ioprio_class |
| 226 | << "," << attr.ioprio_pri; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if (!attr.console.empty()) { |
| 231 | setsid(); |
| 232 | OpenConsole(attr.console); |
| 233 | } else { |
| 234 | if (setpgid(0, getpid()) == -1) { |
| 235 | return ErrnoError() << "setpgid failed"; |
| 236 | } |
| 237 | ZapStdio(); |
| 238 | } |
| 239 | |
| 240 | for (const auto& rlimit : attr.rlimits) { |
| 241 | if (setrlimit(rlimit.first, &rlimit.second) == -1) { |
| 242 | return ErrnoError() << StringPrintf( |
| 243 | "setrlimit(%d, {rlim_cur=%ld, rlim_max=%ld}) failed", rlimit.first, |
| 244 | rlimit.second.rlim_cur, rlimit.second.rlim_max); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if (attr.gid) { |
| 249 | if (setgid(attr.gid) != 0) { |
| 250 | return ErrnoError() << "setgid failed"; |
| 251 | } |
| 252 | } |
| 253 | if (setgroups(attr.supp_gids.size(), const_cast<gid_t*>(&attr.supp_gids[0])) != 0) { |
| 254 | return ErrnoError() << "setgroups failed"; |
| 255 | } |
| 256 | if (attr.uid) { |
| 257 | if (setuid(attr.uid) != 0) { |
| 258 | return ErrnoError() << "setuid failed"; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (attr.priority != 0) { |
| 263 | if (setpriority(PRIO_PROCESS, 0, attr.priority) != 0) { |
| 264 | return ErrnoError() << "setpriority failed"; |
| 265 | } |
| 266 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 267 | return {}; |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 270 | Result<void> WritePidToFiles(std::vector<std::string>* files) { |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 271 | // See if there were "writepid" instructions to write to files under cpuset path. |
| 272 | std::string cpuset_path; |
| 273 | if (CgroupGetControllerPath("cpuset", &cpuset_path)) { |
| 274 | auto cpuset_predicate = [&cpuset_path](const std::string& path) { |
| 275 | return StartsWith(path, cpuset_path + "/"); |
| 276 | }; |
| 277 | auto iter = std::find_if(files->begin(), files->end(), cpuset_predicate); |
| 278 | if (iter == files->end()) { |
| 279 | // There were no "writepid" instructions for cpusets, check if the system default |
| 280 | // cpuset is specified to be used for the process. |
| 281 | std::string default_cpuset = GetProperty("ro.cpuset.default", ""); |
| 282 | if (!default_cpuset.empty()) { |
| 283 | // Make sure the cpuset name starts and ends with '/'. |
| 284 | // A single '/' means the 'root' cpuset. |
| 285 | if (default_cpuset.front() != '/') { |
| 286 | default_cpuset.insert(0, 1, '/'); |
| 287 | } |
| 288 | if (default_cpuset.back() != '/') { |
| 289 | default_cpuset.push_back('/'); |
| 290 | } |
| 291 | files->push_back( |
| 292 | StringPrintf("%s%stasks", cpuset_path.c_str(), default_cpuset.c_str())); |
| 293 | } |
| 294 | } |
| 295 | } else { |
| 296 | LOG(ERROR) << "cpuset cgroup controller is not mounted!"; |
| 297 | } |
| 298 | std::string pid_str = std::to_string(getpid()); |
| 299 | for (const auto& file : *files) { |
| 300 | if (!WriteStringToFile(pid_str, file)) { |
| 301 | return ErrnoError() << "couldn't write " << pid_str << " to " << file; |
| 302 | } |
| 303 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 304 | return {}; |
Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | } // namespace init |
| 308 | } // namespace android |