| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/mount.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "cgroup-setup.h" |
| 8 |
#include "chase.h" |
| 9 |
#include "fd-util.h" |
| 10 |
#include "format-util.h" |
| 11 |
#include "log.h" |
| 12 |
#include "mount-setup.h" |
| 13 |
#include "mount-util.h" |
| 14 |
#include "mountpoint-util.h" |
| 15 |
#include "nspawn-cgroup.h" |
| 16 |
#include "nsresource.h" |
| 17 |
#include "path-util.h" |
| 18 |
#include "pidref.h" |
| 19 |
#include "string-util.h" |
| 20 |
#include "strv.h" |
| 21 |
|
| 22 |
static int chown_cgroup_path(const char *path, uid_t uid_shift) { |
| 23 |
_cleanup_close_ int fd = -EBADF; |
| 24 |
|
| 25 |
assert(path); |
| 26 |
|
| 27 |
fd = open(path, O_PATH|O_CLOEXEC|O_DIRECTORY); |
| 28 |
if (fd < 0) |
| 29 |
return -errno; |
| 30 |
|
| 31 |
FOREACH_STRING(fn, |
| 32 |
".", |
| 33 |
"cgroup.controllers", |
| 34 |
"cgroup.events", |
| 35 |
"cgroup.procs", |
| 36 |
"cgroup.stat", |
| 37 |
"cgroup.subtree_control", |
| 38 |
"cgroup.threads", |
| 39 |
"memory.oom.group", |
| 40 |
"memory.reclaim") |
| 41 |
if (fchownat(fd, fn, uid_shift, uid_shift, 0) < 0) |
| 42 |
log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno, |
| 43 |
"Failed to chown \"%s/%s\", ignoring: %m", path, fn); |
| 44 |
|
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
int create_subcgroup( |
| 49 |
const PidRef *pid, |
| 50 |
bool keep_unit, |
| 51 |
uid_t uid_shift, |
| 52 |
int userns_fd, |
| 53 |
UserNamespaceMode userns_mode) { |
| 54 |
|
| 55 |
_cleanup_free_ char *cgroup = NULL, *payload = NULL; |
| 56 |
CGroupMask supported; |
| 57 |
int r; |
| 58 |
|
| 59 |
assert(pidref_is_set(pid)); |
| 60 |
assert(pid->pid > 1); |
| 61 |
assert((userns_fd >= 0) == (userns_mode == USER_NAMESPACE_MANAGED)); |
| 62 |
|
| 63 |
/* In the unified hierarchy inner nodes may only contain subgroups, but not processes. Hence, if we running in |
| 64 |
* the unified hierarchy and the container does the same, and we did not create a scope unit for the container |
| 65 |
* move us and the container into two separate subcgroups. |
| 66 |
* |
| 67 |
* Moreover, container payloads such as systemd try to manage the cgroup they run in full (i.e. including |
| 68 |
* its attributes), while the host systemd will only delegate cgroups for children of the cgroup created for a |
| 69 |
* delegation unit, instead of the cgroup itself. This means, if we'd pass on the cgroup allocated from the |
| 70 |
* host systemd directly to the payload, the host and payload systemd might fight for the cgroup |
| 71 |
* attributes. Hence, let's insert an intermediary cgroup to cover that case too. */ |
| 72 |
|
| 73 |
r = cg_mask_supported(&supported); |
| 74 |
if (r < 0) |
| 75 |
return log_error_errno(r, "Failed to determine supported controllers: %m"); |
| 76 |
|
| 77 |
if (keep_unit) |
| 78 |
r = cg_pid_get_path(0, &cgroup); |
| 79 |
else |
| 80 |
r = cg_pidref_get_path(pid, &cgroup); |
| 81 |
if (r < 0) |
| 82 |
return log_error_errno(r, "Failed to get our control group: %m"); |
| 83 |
|
| 84 |
/* If the service manager already placed us in the supervisor cgroup, let's handle that. */ |
| 85 |
char *e = endswith(cgroup, "/supervisor"); |
| 86 |
if (e) |
| 87 |
*e = 0; /* chop off, we want the main path delegated to us */ |
| 88 |
|
| 89 |
payload = path_join(cgroup, "payload"); |
| 90 |
if (!payload) |
| 91 |
return log_oom(); |
| 92 |
|
| 93 |
if (userns_mode != USER_NAMESPACE_MANAGED) |
| 94 |
r = cg_create_and_attach(payload, pid->pid); |
| 95 |
else |
| 96 |
r = cg_create(payload); |
| 97 |
if (r < 0) |
| 98 |
return log_error_errno(r, "Failed to create %s subcgroup: %m", payload); |
| 99 |
|
| 100 |
if (userns_mode == USER_NAMESPACE_MANAGED) { |
| 101 |
_cleanup_close_ int cgroup_fd = -EBADF; |
| 102 |
|
| 103 |
cgroup_fd = cg_path_open(payload); |
| 104 |
if (cgroup_fd < 0) |
| 105 |
return log_error_errno(cgroup_fd, "Failed to open cgroup %s: %m", payload); |
| 106 |
|
| 107 |
r = cg_fd_attach(cgroup_fd, pid->pid); |
| 108 |
if (r < 0) |
| 109 |
return log_error_errno(r, "Failed to add process " PID_FMT " to cgroup %s: %m", pid->pid, payload); |
| 110 |
|
| 111 |
r = nsresource_add_cgroup( |
| 112 |
/* vl= */ NULL, |
| 113 |
userns_fd, |
| 114 |
cgroup_fd); |
| 115 |
if (r < 0) |
| 116 |
return log_error_errno(r, "Failed to add cgroup %s to userns: %m", payload); |
| 117 |
} else { |
| 118 |
_cleanup_free_ char *fs = NULL; |
| 119 |
r = cg_get_path(payload, /* suffix= */ NULL, &fs); |
| 120 |
if (r < 0) |
| 121 |
return log_error_errno(r, "Failed to get file system path for container cgroup: %m"); |
| 122 |
|
| 123 |
r = chown_cgroup_path(fs, uid_shift); |
| 124 |
if (r < 0) |
| 125 |
return log_error_errno(r, "Failed to chown() cgroup %s: %m", fs); |
| 126 |
} |
| 127 |
|
| 128 |
if (keep_unit) { |
| 129 |
_cleanup_free_ char *supervisor = NULL; |
| 130 |
supervisor = path_join(cgroup, "supervisor"); |
| 131 |
if (!supervisor) |
| 132 |
return log_oom(); |
| 133 |
|
| 134 |
r = cg_create_and_attach(supervisor, 0); |
| 135 |
if (r < 0) |
| 136 |
return log_error_errno(r, "Failed to create %s subcgroup: %m", supervisor); |
| 137 |
} |
| 138 |
|
| 139 |
/* Try to enable as many controllers as possible for the new payload. */ |
| 140 |
(void) cg_enable(supported, supported, cgroup, NULL); |
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
int mount_cgroups(const char *dest, bool accept_existing) { |
| 145 |
_cleanup_free_ char *p = NULL; |
| 146 |
_cleanup_close_ int fd = -EBADF; |
| 147 |
int r; |
| 148 |
|
| 149 |
r = chase("/sys/fs/cgroup", dest, CHASE_PREFIX_ROOT | CHASE_MKDIR_0755, &p, &fd); |
| 150 |
if (r < 0) |
| 151 |
return log_error_errno(r, "Failed to chase %s/sys/fs/cgroup: %m", strempty(dest)); |
| 152 |
|
| 153 |
r = is_mount_point_at(fd, /* path= */ NULL, /* flags= */ 0); |
| 154 |
if (r < 0) |
| 155 |
return log_error_errno(r, "Failed to determine if %s is mounted already: %m", p); |
| 156 |
if (r > 0) { |
| 157 |
if (!accept_existing) |
| 158 |
return log_error_errno(SYNTHETIC_ERRNO(EEXIST), "Refusing existing cgroupfs mount: %s", p); |
| 159 |
|
| 160 |
if (faccessat(fd, "cgroup.procs", F_OK, /* flags= */ 0) >= 0) |
| 161 |
return 0; |
| 162 |
if (errno != ENOENT) |
| 163 |
return log_error_errno(errno, "Failed to determine if mount point %s contains the unified cgroup hierarchy: %m", p); |
| 164 |
|
| 165 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 166 |
"%s is already mounted but not a unified cgroup hierarchy. Refusing.", p); |
| 167 |
} |
| 168 |
|
| 169 |
return mount_cgroupfs(p); |
| 170 |
} |
| 171 |
|
| 172 |
int bind_mount_cgroup_hierarchy(void) { |
| 173 |
_cleanup_free_ char *own_cgroup_path = NULL; |
| 174 |
int r; |
| 175 |
|
| 176 |
/* NB: This must be called from the inner child, with /sys/fs/cgroup/ being a bind mount in mountns! */ |
| 177 |
|
| 178 |
r = cg_pid_get_path(0, &own_cgroup_path); |
| 179 |
if (r < 0) |
| 180 |
return log_error_errno(r, "Failed to determine our own cgroup path: %m"); |
| 181 |
|
| 182 |
/* If we are living in the top-level, then there's nothing to do... */ |
| 183 |
if (path_equal(own_cgroup_path, "/")) |
| 184 |
return 0; |
| 185 |
|
| 186 |
const char *p = strjoina("/sys/fs/cgroup", own_cgroup_path); |
| 187 |
|
| 188 |
/* Make our own cgroup a (writable) bind mount */ |
| 189 |
r = mount_nofollow_verbose(LOG_ERR, p, p, NULL, MS_BIND, NULL); |
| 190 |
if (r < 0) |
| 191 |
return r; |
| 192 |
|
| 193 |
/* And then remount the systemd cgroup root read-only */ |
| 194 |
return mount_nofollow_verbose(LOG_ERR, NULL, "/sys/fs/cgroup", NULL, |
| 195 |
MS_BIND|MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY, NULL); |
| 196 |
} |