2019-05-30 00:58:32 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "service_utils.h"
|
|
|
|
|
2019-09-24 01:16:54 +02:00
|
|
|
#include <fcntl.h>
|
2019-05-30 00:58:32 +02:00
|
|
|
#include <grp.h>
|
2022-03-31 23:15:11 +02:00
|
|
|
#include <map>
|
2019-05-30 00:58:32 +02:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#include <sys/wait.h>
|
2019-09-24 01:16:54 +02:00
|
|
|
#include <unistd.h>
|
2019-05-30 00:58:32 +02:00
|
|
|
|
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/properties.h>
|
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
#include <android-base/strings.h>
|
2019-07-09 22:33:36 +02:00
|
|
|
#include <cutils/android_get_control_file.h>
|
|
|
|
#include <cutils/sockets.h>
|
2019-05-30 00:58:32 +02:00
|
|
|
#include <processgroup/processgroup.h>
|
|
|
|
|
|
|
|
#include "mount_namespace.h"
|
2019-07-09 22:33:36 +02:00
|
|
|
#include "util.h"
|
2019-05-30 00:58:32 +02:00
|
|
|
|
|
|
|
using android::base::GetProperty;
|
|
|
|
using android::base::StartsWith;
|
|
|
|
using android::base::StringPrintf;
|
|
|
|
using android::base::unique_fd;
|
|
|
|
using android::base::WriteStringToFile;
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace init {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> EnterNamespace(int nstype, const char* path) {
|
2019-05-30 00:58:32 +02:00
|
|
|
auto fd = unique_fd{open(path, O_RDONLY | O_CLOEXEC)};
|
|
|
|
if (fd == -1) {
|
|
|
|
return ErrnoError() << "Could not open namespace at " << path;
|
|
|
|
}
|
2022-12-03 03:48:15 +01:00
|
|
|
if (setns(fd.get(), nstype) == -1) {
|
2019-05-30 00:58:32 +02:00
|
|
|
return ErrnoError() << "Could not setns() namespace at " << path;
|
|
|
|
}
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2019-05-30 00:58:32 +02:00
|
|
|
}
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> SetUpMountNamespace(bool remount_proc, bool remount_sys) {
|
2019-05-30 00:58:32 +02:00
|
|
|
constexpr unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
|
|
|
|
|
2020-07-28 20:09:03 +02:00
|
|
|
// Recursively remount / as MS_SLAVE like zygote does so that
|
|
|
|
// unmounting and mounting /proc doesn't interfere with the parent
|
|
|
|
// namespace's /proc mount. This will also prevent any other
|
|
|
|
// mounts/unmounts initiated by the service from interfering with the
|
|
|
|
// parent namespace but will still allow mount events from the parent
|
2019-05-30 00:58:32 +02:00
|
|
|
// namespace to propagate to the child.
|
|
|
|
if (mount("rootfs", "/", nullptr, (MS_SLAVE | MS_REC), nullptr) == -1) {
|
2020-07-28 20:09:03 +02:00
|
|
|
return ErrnoError() << "Could not remount(/) recursively as MS_SLAVE";
|
2019-05-30 00:58:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// umount() then mount() /proc and/or /sys
|
|
|
|
// Note that it is not sufficient to mount with MS_REMOUNT.
|
|
|
|
if (remount_proc) {
|
|
|
|
if (umount("/proc") == -1) {
|
|
|
|
return ErrnoError() << "Could not umount(/proc)";
|
|
|
|
}
|
|
|
|
if (mount("", "/proc", "proc", kSafeFlags, "") == -1) {
|
|
|
|
return ErrnoError() << "Could not mount(/proc)";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (remount_sys) {
|
|
|
|
if (umount2("/sys", MNT_DETACH) == -1) {
|
|
|
|
return ErrnoError() << "Could not umount(/sys)";
|
|
|
|
}
|
|
|
|
if (mount("", "/sys", "sysfs", kSafeFlags, "") == -1) {
|
|
|
|
return ErrnoError() << "Could not mount(/sys)";
|
|
|
|
}
|
|
|
|
}
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2019-05-30 00:58:32 +02:00
|
|
|
}
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> SetUpPidNamespace(const char* name) {
|
2019-05-30 00:58:32 +02:00
|
|
|
if (prctl(PR_SET_NAME, name) == -1) {
|
|
|
|
return ErrnoError() << "Could not set name";
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t child_pid = fork();
|
|
|
|
if (child_pid == -1) {
|
|
|
|
return ErrnoError() << "Could not fork init inside the PID namespace";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child_pid > 0) {
|
|
|
|
// So that we exit with the right status.
|
|
|
|
static int init_exitstatus = 0;
|
|
|
|
signal(SIGTERM, [](int) { _exit(init_exitstatus); });
|
|
|
|
|
|
|
|
pid_t waited_pid;
|
|
|
|
int status;
|
|
|
|
while ((waited_pid = wait(&status)) > 0) {
|
|
|
|
// This loop will end when there are no processes left inside the
|
|
|
|
// PID namespace or when the init process inside the PID namespace
|
|
|
|
// gets a signal.
|
|
|
|
if (waited_pid == child_pid) {
|
|
|
|
init_exitstatus = status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!WIFEXITED(init_exitstatus)) {
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
_exit(WEXITSTATUS(init_exitstatus));
|
|
|
|
}
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2019-05-30 00:58:32 +02:00
|
|
|
}
|
|
|
|
|
2019-09-24 01:16:54 +02:00
|
|
|
void SetupStdio(bool stdio_to_kmsg) {
|
2019-07-09 00:09:36 +02:00
|
|
|
auto fd = unique_fd{open("/dev/null", O_RDWR | O_CLOEXEC)};
|
2022-12-03 03:48:15 +01:00
|
|
|
dup2(fd.get(), STDIN_FILENO);
|
2019-09-24 01:16:54 +02:00
|
|
|
if (stdio_to_kmsg) {
|
|
|
|
fd.reset(open("/dev/kmsg_debug", O_WRONLY | O_CLOEXEC));
|
|
|
|
if (fd == -1) fd.reset(open("/dev/null", O_WRONLY | O_CLOEXEC));
|
|
|
|
}
|
2022-12-03 03:48:15 +01:00
|
|
|
dup2(fd.get(), STDOUT_FILENO);
|
|
|
|
dup2(fd.get(), STDERR_FILENO);
|
2019-05-30 00:58:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenConsole(const std::string& console) {
|
2019-07-09 00:09:36 +02:00
|
|
|
auto fd = unique_fd{open(console.c_str(), O_RDWR | O_CLOEXEC)};
|
|
|
|
if (fd == -1) fd.reset(open("/dev/null", O_RDWR | O_CLOEXEC));
|
2022-12-03 03:48:15 +01:00
|
|
|
ioctl(fd.get(), TIOCSCTTY, 0);
|
|
|
|
dup2(fd.get(), 0);
|
|
|
|
dup2(fd.get(), 1);
|
|
|
|
dup2(fd.get(), 2);
|
2019-05-30 00:58:32 +02:00
|
|
|
}
|
|
|
|
|
2019-09-10 23:20:35 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void Descriptor::Publish() const {
|
|
|
|
auto published_name = name_;
|
|
|
|
|
2019-07-09 22:33:36 +02:00
|
|
|
for (auto& c : published_name) {
|
|
|
|
c = isalnum(c) ? c : '_';
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:20:35 +02:00
|
|
|
int fd = fd_.get();
|
|
|
|
// For safety, the FD is created as CLOEXEC, so that must be removed before publishing.
|
|
|
|
auto fd_flags = fcntl(fd, F_GETFD);
|
|
|
|
fd_flags &= ~FD_CLOEXEC;
|
|
|
|
if (fcntl(fd, F_SETFD, fd_flags) != 0) {
|
|
|
|
PLOG(ERROR) << "Failed to remove CLOEXEC from '" << published_name << "'";
|
|
|
|
}
|
|
|
|
|
2019-07-09 22:33:36 +02:00
|
|
|
std::string val = std::to_string(fd);
|
|
|
|
setenv(published_name.c_str(), val.c_str(), 1);
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:20:35 +02:00
|
|
|
Result<Descriptor> SocketDescriptor::Create(const std::string& global_context) const {
|
2019-07-09 22:33:36 +02:00
|
|
|
const auto& socket_context = context.empty() ? global_context : context;
|
init: Add option to listen on sockets before starting service.
Review note: Original change was a p-o-c by agl in
https://r.android.com/2094350 which I think is actually
production quality. I'm just taking it over so that he doesn't
get spammed by any review comments as that's not a good use
of his time.
Needed for the hardware entropy daemon (see bug).
Original commit message:
If one needs to create a service that synchronously starts listening on
a socket then there are currently no good options.
The traditional UNIX solution is to have the service create the socket
and then daemonise. In this situation, init could start the service with
`exec_start` and yet not block forever because the service forks and
exits. However, when the initial child process exits, init kills the
daemon process:
> init: Killed 1 additional processes from a oneshot process group for
> service 'foo'. This is new behavior, previously child processes
> would not be killed in this case.
Next, there is a `socket` option for services and (although the
documentation didn't nail this down), the socket is created
synchronously by `start`. However, init doesn't call `listen` on the
socket so, until the service starts listening on the socket itself,
clients will get ECONNREFUSED.
This this change adds a `+listen` option, similar to `+passcred` which
allows a socket service to reliably handle connections.
Bug: 243933553
Test: Started prng_seeder from init using the new listen flag
Change-Id: I91b3b2b1fd38cc3d96e19e92b76c8e95788191d5
2022-05-12 00:32:47 +02:00
|
|
|
auto result = CreateSocket(name, type | SOCK_CLOEXEC, passcred, listen, perm, uid, gid,
|
|
|
|
socket_context);
|
2020-02-05 19:49:33 +01:00
|
|
|
if (!result.ok()) {
|
2019-07-09 22:33:36 +02:00
|
|
|
return result.error();
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:20:35 +02:00
|
|
|
return Descriptor(ANDROID_SOCKET_ENV_PREFIX + name, unique_fd(*result));
|
2019-07-09 22:33:36 +02:00
|
|
|
}
|
|
|
|
|
2019-09-10 23:20:35 +02:00
|
|
|
Result<Descriptor> FileDescriptor::Create() const {
|
2019-07-09 22:33:36 +02:00
|
|
|
int flags = (type == "r") ? O_RDONLY : (type == "w") ? O_WRONLY : O_RDWR;
|
|
|
|
|
|
|
|
// Make sure we do not block on open (eg: devices can chose to block on carrier detect). Our
|
|
|
|
// intention is never to delay launch of a service for such a condition. The service can
|
|
|
|
// perform its own blocking on carrier detect.
|
2019-09-10 23:20:35 +02:00
|
|
|
unique_fd fd(TEMP_FAILURE_RETRY(open(name.c_str(), flags | O_NONBLOCK | O_CLOEXEC)));
|
2019-07-09 22:33:36 +02:00
|
|
|
|
|
|
|
if (fd < 0) {
|
|
|
|
return ErrnoError() << "Failed to open file '" << name << "'";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fixup as we set O_NONBLOCK for open, the intent for fd is to block reads.
|
2022-12-03 03:48:15 +01:00
|
|
|
fcntl(fd.get(), F_SETFL, flags);
|
2019-07-09 22:33:36 +02:00
|
|
|
|
2019-09-10 23:20:35 +02:00
|
|
|
return Descriptor(ANDROID_FILE_ENV_PREFIX + name, std::move(fd));
|
2019-07-09 22:33:36 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 06:44:17 +02:00
|
|
|
Result<void> EnterNamespaces(const NamespaceInfo& info, const std::string& name,
|
|
|
|
std::optional<MountNamespace> override_mount_namespace) {
|
2019-05-30 00:58:32 +02:00
|
|
|
for (const auto& [nstype, path] : info.namespaces_to_enter) {
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = EnterNamespace(nstype, path.c_str()); !result.ok()) {
|
2019-05-30 00:58:32 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__ANDROID__)
|
2020-06-09 06:44:17 +02:00
|
|
|
if (override_mount_namespace.has_value()) {
|
|
|
|
if (auto result = SwitchToMountNamespaceIfNeeded(override_mount_namespace.value());
|
|
|
|
!result.ok()) {
|
|
|
|
return result;
|
2019-05-30 00:58:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (info.flags & CLONE_NEWNS) {
|
|
|
|
bool remount_proc = info.flags & CLONE_NEWPID;
|
|
|
|
bool remount_sys =
|
|
|
|
std::any_of(info.namespaces_to_enter.begin(), info.namespaces_to_enter.end(),
|
|
|
|
[](const auto& entry) { return entry.first == CLONE_NEWNET; });
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = SetUpMountNamespace(remount_proc, remount_sys); !result.ok()) {
|
2019-05-30 00:58:32 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.flags & CLONE_NEWPID) {
|
|
|
|
// This will fork again to run an init process inside the PID namespace.
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = SetUpPidNamespace(name.c_str()); !result.ok()) {
|
2019-05-30 00:58:32 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2019-05-30 00:58:32 +02:00
|
|
|
}
|
|
|
|
|
2022-11-15 01:45:47 +01:00
|
|
|
Result<void> SetProcessAttributes(const ProcessAttributes& attr, InterprocessFifo setsid_finished) {
|
2019-05-30 00:58:32 +02:00
|
|
|
if (attr.ioprio_class != IoSchedClass_NONE) {
|
|
|
|
if (android_set_ioprio(getpid(), attr.ioprio_class, attr.ioprio_pri)) {
|
|
|
|
PLOG(ERROR) << "failed to set pid " << getpid() << " ioprio=" << attr.ioprio_class
|
|
|
|
<< "," << attr.ioprio_pri;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-15 01:54:03 +01:00
|
|
|
if (RequiresConsole(attr)) {
|
2019-05-30 00:58:32 +02:00
|
|
|
setsid();
|
2022-11-15 01:45:47 +01:00
|
|
|
setsid_finished.Write(kSetSidFinished);
|
|
|
|
setsid_finished.Close();
|
2019-05-30 00:58:32 +02:00
|
|
|
OpenConsole(attr.console);
|
|
|
|
} else {
|
2022-11-15 01:45:47 +01:00
|
|
|
// Without PID namespaces, this call duplicates the setpgid() call from
|
|
|
|
// the parent process. With PID namespaces, this setpgid() call sets the
|
|
|
|
// process group ID for a child of the init process in the PID
|
|
|
|
// namespace.
|
2022-11-18 18:45:33 +01:00
|
|
|
if (setpgid(0, 0) == -1) {
|
2019-05-30 00:58:32 +02:00
|
|
|
return ErrnoError() << "setpgid failed";
|
|
|
|
}
|
2019-09-24 01:16:54 +02:00
|
|
|
SetupStdio(attr.stdio_to_kmsg);
|
2019-05-30 00:58:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& rlimit : attr.rlimits) {
|
|
|
|
if (setrlimit(rlimit.first, &rlimit.second) == -1) {
|
2020-02-05 00:20:27 +01:00
|
|
|
return ErrnoErrorf("setrlimit({}, {{rlim_cur={}, rlim_max={}}}) failed", rlimit.first,
|
|
|
|
rlimit.second.rlim_cur, rlimit.second.rlim_max);
|
2019-05-30 00:58:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attr.gid) {
|
|
|
|
if (setgid(attr.gid) != 0) {
|
|
|
|
return ErrnoError() << "setgid failed";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (setgroups(attr.supp_gids.size(), const_cast<gid_t*>(&attr.supp_gids[0])) != 0) {
|
|
|
|
return ErrnoError() << "setgroups failed";
|
|
|
|
}
|
|
|
|
if (attr.uid) {
|
|
|
|
if (setuid(attr.uid) != 0) {
|
|
|
|
return ErrnoError() << "setuid failed";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attr.priority != 0) {
|
|
|
|
if (setpriority(PRIO_PROCESS, 0, attr.priority) != 0) {
|
|
|
|
return ErrnoError() << "setpriority failed";
|
|
|
|
}
|
|
|
|
}
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2019-05-30 00:58:32 +02:00
|
|
|
}
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> WritePidToFiles(std::vector<std::string>* files) {
|
2022-10-20 15:14:39 +02:00
|
|
|
if (files->empty()) {
|
|
|
|
// No files to write pid to, exit early.
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CgroupsAvailable()) {
|
|
|
|
return Error() << "cgroups are not available";
|
|
|
|
}
|
|
|
|
|
2019-05-30 00:58:32 +02:00
|
|
|
// See if there were "writepid" instructions to write to files under cpuset path.
|
|
|
|
std::string cpuset_path;
|
|
|
|
if (CgroupGetControllerPath("cpuset", &cpuset_path)) {
|
|
|
|
auto cpuset_predicate = [&cpuset_path](const std::string& path) {
|
|
|
|
return StartsWith(path, cpuset_path + "/");
|
|
|
|
};
|
|
|
|
auto iter = std::find_if(files->begin(), files->end(), cpuset_predicate);
|
|
|
|
if (iter == files->end()) {
|
|
|
|
// There were no "writepid" instructions for cpusets, check if the system default
|
|
|
|
// cpuset is specified to be used for the process.
|
|
|
|
std::string default_cpuset = GetProperty("ro.cpuset.default", "");
|
|
|
|
if (!default_cpuset.empty()) {
|
|
|
|
// Make sure the cpuset name starts and ends with '/'.
|
|
|
|
// A single '/' means the 'root' cpuset.
|
|
|
|
if (default_cpuset.front() != '/') {
|
|
|
|
default_cpuset.insert(0, 1, '/');
|
|
|
|
}
|
|
|
|
if (default_cpuset.back() != '/') {
|
|
|
|
default_cpuset.push_back('/');
|
|
|
|
}
|
|
|
|
files->push_back(
|
|
|
|
StringPrintf("%s%stasks", cpuset_path.c_str(), default_cpuset.c_str()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "cpuset cgroup controller is not mounted!";
|
|
|
|
}
|
2022-03-31 23:15:11 +02:00
|
|
|
|
|
|
|
// Issue a warning whenever writepid is being used with a cgroup. This can't be done during
|
|
|
|
// command parsing because cgroups might not be configured at the time or parsing.
|
|
|
|
for (const auto& file : *files) {
|
|
|
|
if (CgroupGetControllerFromPath(file, nullptr)) {
|
|
|
|
LOG(WARNING) << "writepid usage with cgroups path '" << file
|
|
|
|
<< "' is obsolete, please use task_profiles!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 00:58:32 +02:00
|
|
|
std::string pid_str = std::to_string(getpid());
|
|
|
|
for (const auto& file : *files) {
|
|
|
|
if (!WriteStringToFile(pid_str, file)) {
|
|
|
|
return ErrnoError() << "couldn't write " << pid_str << " to " << file;
|
|
|
|
}
|
|
|
|
}
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2019-05-30 00:58:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|