Debian Codegraph @

src/shared/nsresource.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sched.h>
4 #include <sys/prctl.h>
5
6 #include "sd-varlink.h"
7
8 #include "alloc-util.h"
9 #include "fd-util.h"
10 #include "format-util.h"
11 #include "json-util.h"
12 #include "log.h"
13 #include "namespace-util.h"
14 #include "nsresource.h"
15 #include "process-util.h"
16 #include "string-util.h"
17
18 /* Maximum namespace name length */
19 #define NAMESPACE_NAME_MAX 16U
20
21 /* So the namespace name should be 16 chars at max (because we want that it is usable in usernames, which
22 * have a limit of 31 chars effectively, and the nsresourced service wants to prefix/suffix some bits). But
23 * it also should be unique if we are called multiple times in a row. Hence we take the "comm" name (which is
24 * 15 chars), and suffix it with the PID and a counter, possibly overriding the end. */
25 assert_cc(TASK_COMM_LEN == NAMESPACE_NAME_MAX);
26
27 static int make_pid_name(char **ret) {
28 char comm[TASK_COMM_LEN];
29 static uint64_t counter = 0;
30
31 assert(ret);
32
33 if (prctl(PR_GET_NAME, comm) < 0)
34 return -errno;
35
36 char spid[DECIMAL_STR_MAX(pid_t)];
37 xsprintf(spid, PID_FMT, getpid_cached());
38
39 /* Include a counter in the name, so that we can allocate multiple namespaces per process, with
40 * unique names. For the first namespace we suppress the suffix */
41 char scounter[sizeof(counter) * 2 + 1];
42 if (counter == 0)
43 scounter[0] = 0;
44 else
45 xsprintf(scounter, "%" PRIx64, counter);
46 counter++;
47
48 strshorten(comm, LESS_BY(NAMESPACE_NAME_MAX, strlen(spid) + strlen(scounter)));
49
50 _cleanup_free_ char *s = strjoin(comm, spid, scounter);
51 if (!s)
52 return -ENOMEM;
53
54 strshorten(s, NAMESPACE_NAME_MAX);
55
56 *ret = TAKE_PTR(s);
57 return 0;
58 }
59
60 int nsresource_connect(sd_varlink **ret) {
61 int r;
62
63 assert(ret);
64
65 _cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL;
66 r = sd_varlink_connect_address(&vl, "/run/systemd/io.systemd.NamespaceResource");
67 if (r < 0)
68 return log_debug_errno(r, "Failed to connect to namespace resource manager: %m");
69
70 r = sd_varlink_set_allow_fd_passing_output(vl, true);
71 if (r < 0)
72 return log_debug_errno(r, "Failed to enable varlink fd passing for write: %m");
73
74 *ret = TAKE_PTR(vl);
75 return 0;
76 }
77
78 int nsresource_allocate_userns_full(sd_varlink *vl, const char *name, uint64_t size, uint64_t delegate_container_ranges) {
79 _cleanup_close_ int userns_fd = -EBADF;
80 _cleanup_free_ char *_name = NULL;
81 const char *error_id;
82 int r, userns_fd_idx;
83
84 /* Allocate a new dynamic user namespace via the userdb registry logic */
85
86 if (!name) {
87 r = make_pid_name(&_name);
88 if (r < 0)
89 return r;
90
91 name = _name;
92 }
93
94 if (size <= 0 || size > UINT64_C(0x100000000)) /* Note: the server actually only allows allocating 1 or 64K right now */
95 return -EINVAL;
96
97 _cleanup_(sd_varlink_unrefp) sd_varlink *_vl = NULL;
98 if (!vl) {
99 r = nsresource_connect(&_vl);
100 if (r < 0)
101 return r;
102
103 vl = _vl;
104 }
105
106 userns_fd = userns_acquire_empty();
107 if (userns_fd < 0)
108 return log_debug_errno(userns_fd, "Failed to acquire empty user namespace: %m");
109
110 userns_fd_idx = sd_varlink_push_dup_fd(vl, userns_fd);
111 if (userns_fd_idx < 0)
112 return log_debug_errno(userns_fd_idx, "Failed to push userns fd into varlink connection: %m");
113
114 sd_json_variant *reply = NULL;
115 r = sd_varlink_callbo(
116 vl,
117 "io.systemd.NamespaceResource.AllocateUserRange",
118 &reply,
119 &error_id,
120 SD_JSON_BUILD_PAIR_STRING("name", name),
121 SD_JSON_BUILD_PAIR_BOOLEAN("mangleName", true),
122 SD_JSON_BUILD_PAIR_UNSIGNED("size", size),
123 SD_JSON_BUILD_PAIR_UNSIGNED("userNamespaceFileDescriptor", userns_fd_idx),
124 JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("delegateContainerRanges", delegate_container_ranges));
125 if (r < 0)
126 return log_debug_errno(r, "Failed to call AllocateUserRange() varlink call: %m");
127 if (streq_ptr(error_id, "io.systemd.NamespaceResource.UserNamespaceInterfaceNotSupported"))
128 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Unprivileged user namespace delegation is not supported on this system.");
129 if (error_id)
130 return log_debug_errno(sd_varlink_error_to_errno(error_id, reply), "Failed to allocate user namespace with %" PRIu64 " users: %s", size, error_id);
131
132 return TAKE_FD(userns_fd);
133 }
134
135 int nsresource_register_userns(sd_varlink *vl, const char *name, int userns_fd) {
136 _cleanup_close_ int _userns_fd = -EBADF;
137 _cleanup_free_ char *_name = NULL;
138 const char *error_id;
139 int r, userns_fd_idx;
140
141 /* Register the specified user namespace with userbd. */
142
143 if (!name) {
144 r = make_pid_name(&_name);
145 if (r < 0)
146 return r;
147
148 name = _name;
149 }
150
151 if (userns_fd < 0) {
152 _userns_fd = namespace_open_by_type(NAMESPACE_USER);
153 if (_userns_fd < 0)
154 return _userns_fd;
155
156 userns_fd = _userns_fd;
157 }
158
159 _cleanup_(sd_varlink_unrefp) sd_varlink *_vl = NULL;
160 if (!vl) {
161 r = nsresource_connect(&_vl);
162 if (r < 0)
163 return r;
164
165 vl = _vl;
166 }
167
168 userns_fd_idx = sd_varlink_push_dup_fd(vl, userns_fd);
169 if (userns_fd_idx < 0)
170 return log_debug_errno(userns_fd_idx, "Failed to push userns fd into varlink connection: %m");
171
172 sd_json_variant *reply = NULL;
173 r = sd_varlink_callbo(
174 vl,
175 "io.systemd.NamespaceResource.RegisterUserNamespace",
176 &reply,
177 &error_id,
178 SD_JSON_BUILD_PAIR_STRING("name", name),
179 SD_JSON_BUILD_PAIR_BOOLEAN("mangleName", true),
180 SD_JSON_BUILD_PAIR_UNSIGNED("userNamespaceFileDescriptor", userns_fd_idx));
181 if (r < 0)
182 return log_debug_errno(r, "Failed to call RegisterUserNamespace() varlink call: %m");
183 if (streq_ptr(error_id, "io.systemd.NamespaceResource.UserNamespaceInterfaceNotSupported"))
184 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Unprivileged user namespace delegation is not supported on this system.");
185 if (error_id)
186 return log_debug_errno(sd_varlink_error_to_errno(error_id, reply), "Failed to register user namespace: %s", error_id);
187
188 return 0;
189 }
190
191 int nsresource_add_mount(sd_varlink *vl, int userns_fd, int mount_fd) {
192 _cleanup_close_ int _userns_fd = -EBADF;
193 int r, userns_fd_idx, mount_fd_idx;
194 const char *error_id;
195
196 assert(mount_fd >= 0);
197
198 if (userns_fd < 0) {
199 _userns_fd = namespace_open_by_type(NAMESPACE_USER);
200 if (_userns_fd < 0)
201 return _userns_fd;
202
203 userns_fd = _userns_fd;
204 }
205
206 _cleanup_(sd_varlink_unrefp) sd_varlink *_vl = NULL;
207 if (!vl) {
208 r = nsresource_connect(&_vl);
209 if (r < 0)
210 return r;
211
212 vl = _vl;
213 }
214
215 userns_fd_idx = sd_varlink_push_dup_fd(vl, userns_fd);
216 if (userns_fd_idx < 0)
217 return log_debug_errno(userns_fd_idx, "Failed to push userns fd into varlink connection: %m");
218
219 mount_fd_idx = sd_varlink_push_dup_fd(vl, mount_fd);
220 if (mount_fd_idx < 0)
221 return log_debug_errno(mount_fd_idx, "Failed to push mount fd into varlink connection: %m");
222
223 sd_json_variant *reply = NULL;
224 r = sd_varlink_callbo(
225 vl,
226 "io.systemd.NamespaceResource.AddMountToUserNamespace",
227 &reply,
228 &error_id,
229 SD_JSON_BUILD_PAIR_UNSIGNED("userNamespaceFileDescriptor", userns_fd_idx),
230 SD_JSON_BUILD_PAIR_UNSIGNED("mountFileDescriptor", mount_fd_idx));
231 if (r < 0)
232 return log_debug_errno(r, "Failed to call AddMountToUserNamespace() varlink call: %m");
233 if (streq_ptr(error_id, "io.systemd.NamespaceResource.UserNamespaceNotRegistered")) {
234 log_debug("User namespace has not been allocated via namespace resource registry, not adding mount to registration.");
235 return 0;
236 }
237 if (error_id)
238 return log_debug_errno(sd_varlink_error_to_errno(error_id, reply), "Failed to mount image: %s", error_id);
239
240 return 1;
241 }
242
243 int nsresource_add_cgroup(sd_varlink *vl, int userns_fd, int cgroup_fd) {
244 _cleanup_close_ int _userns_fd = -EBADF;
245 int r, userns_fd_idx, cgroup_fd_idx;
246 const char *error_id;
247
248 assert(cgroup_fd >= 0);
249
250 if (userns_fd < 0) {
251 _userns_fd = namespace_open_by_type(NAMESPACE_USER);
252 if (_userns_fd < 0)
253 return _userns_fd;
254
255 userns_fd = _userns_fd;
256 }
257
258 _cleanup_(sd_varlink_unrefp) sd_varlink *_vl = NULL;
259 if (!vl) {
260 r = nsresource_connect(&_vl);
261 if (r < 0)
262 return r;
263
264 vl = _vl;
265 }
266
267 userns_fd_idx = sd_varlink_push_dup_fd(vl, userns_fd);
268 if (userns_fd_idx < 0)
269 return log_debug_errno(userns_fd_idx, "Failed to push userns fd into varlink connection: %m");
270
271 cgroup_fd_idx = sd_varlink_push_dup_fd(vl, cgroup_fd);
272 if (cgroup_fd_idx < 0)
273 return log_debug_errno(cgroup_fd_idx, "Failed to push cgroup fd into varlink connection: %m");
274
275 sd_json_variant *reply = NULL;
276 r = sd_varlink_callbo(
277 vl,
278 "io.systemd.NamespaceResource.AddControlGroupToUserNamespace",
279 &reply,
280 &error_id,
281 SD_JSON_BUILD_PAIR_UNSIGNED("userNamespaceFileDescriptor", userns_fd_idx),
282 SD_JSON_BUILD_PAIR_UNSIGNED("controlGroupFileDescriptor", cgroup_fd_idx));
283 if (r < 0)
284 return log_debug_errno(r, "Failed to call AddControlGroupToUserNamespace() varlink call: %m");
285 if (streq_ptr(error_id, "io.systemd.NamespaceResource.UserNamespaceNotRegistered")) {
286 log_debug("User namespace has not been allocated via namespace resource registry, not adding cgroup to registration.");
287 return 0;
288 }
289 if (error_id)
290 return log_debug_errno(sd_varlink_error_to_errno(error_id, reply), "Failed to add cgroup to user namespace: %s", error_id);
291
292 return 1;
293 }
294
295 typedef struct InterfaceParams {
296 char *host_interface_name;
297 char *namespace_interface_name;
298 unsigned interface_fd_index;
299 } InterfaceParams;
300
301 static void interface_params_done(InterfaceParams *p) {
302 assert(p);
303
304 free(p->host_interface_name);
305 free(p->namespace_interface_name);
306 }
307
308 int nsresource_add_netif_veth(
309 sd_varlink *vl,
310 int userns_fd,
311 int netns_fd,
312 const char *namespace_ifname,
313 char **ret_host_ifname,
314 char **ret_namespace_ifname) {
315
316 _cleanup_close_ int _userns_fd = -EBADF, _netns_fd = -EBADF;
317 int r, userns_fd_idx, netns_fd_idx;
318 const char *error_id;
319
320 if (userns_fd < 0) {
321 _userns_fd = namespace_open_by_type(NAMESPACE_USER);
322 if (_userns_fd < 0)
323 return _userns_fd;
324
325 userns_fd = _userns_fd;
326 }
327
328 if (netns_fd < 0) {
329 _netns_fd = namespace_open_by_type(NAMESPACE_NET);
330 if (_netns_fd < 0)
331 return _netns_fd;
332
333 netns_fd = _netns_fd;
334 }
335
336 _cleanup_(sd_varlink_unrefp) sd_varlink *_vl = NULL;
337 if (!vl) {
338 r = nsresource_connect(&_vl);
339 if (r < 0)
340 return r;
341
342 vl = _vl;
343 }
344
345 userns_fd_idx = sd_varlink_push_dup_fd(vl, userns_fd);
346 if (userns_fd_idx < 0)
347 return log_debug_errno(userns_fd_idx, "Failed to push userns fd into varlink connection: %m");
348
349 netns_fd_idx = sd_varlink_push_dup_fd(vl, netns_fd);
350 if (netns_fd_idx < 0)
351 return log_debug_errno(netns_fd_idx, "Failed to push netns fd into varlink connection: %m");
352
353 sd_json_variant *reply = NULL;
354 r = sd_varlink_callbo(
355 vl,
356 "io.systemd.NamespaceResource.AddNetworkToUserNamespace",
357 &reply,
358 &error_id,
359 SD_JSON_BUILD_PAIR_UNSIGNED("userNamespaceFileDescriptor", userns_fd_idx),
360 SD_JSON_BUILD_PAIR_UNSIGNED("networkNamespaceFileDescriptor", netns_fd_idx),
361 SD_JSON_BUILD_PAIR("mode", JSON_BUILD_CONST_STRING("veth")),
362 SD_JSON_BUILD_PAIR_CONDITION(!!namespace_ifname, "namespaceInterfaceName", SD_JSON_BUILD_STRING(namespace_ifname)));
363 if (r < 0)
364 return log_debug_errno(r, "Failed to call AddNetworkToUserNamespace() varlink call: %m");
365 if (streq_ptr(error_id, "io.systemd.NamespaceResource.UserNamespaceNotRegistered")) {
366 log_debug("User namespace has not been allocated via namespace resource registry, not adding network to registration.");
367 return 0;
368 }
369 if (error_id)
370 return log_debug_errno(sd_varlink_error_to_errno(error_id, reply), "Failed to add network to user namespace: %s", error_id);
371
372 static const sd_json_dispatch_field dispatch_table[] = {
373 { "hostInterfaceName", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(InterfaceParams, host_interface_name), SD_JSON_MANDATORY },
374 { "namespaceInterfaceName", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(InterfaceParams, namespace_interface_name), SD_JSON_MANDATORY },
375 {}
376 };
377
378 _cleanup_(interface_params_done) InterfaceParams p = {};
379 r = sd_json_dispatch(reply, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &p);
380 if (r < 0)
381 return r;
382
383 if (ret_host_ifname)
384 *ret_host_ifname = TAKE_PTR(p.host_interface_name);
385 if (ret_namespace_ifname)
386 *ret_namespace_ifname = TAKE_PTR(p.namespace_interface_name);
387
388 return 1;
389 }
390
391 int nsresource_add_netif_tap(
392 sd_varlink *vl,
393 int userns_fd,
394 char **ret_host_ifname) {
395
396 _cleanup_close_ int _userns_fd = -EBADF;
397 int r, userns_fd_idx;
398 const char *error_id;
399
400 if (userns_fd < 0) {
401 _userns_fd = namespace_open_by_type(NAMESPACE_USER);
402 if (_userns_fd < 0)
403 return _userns_fd;
404
405 userns_fd = _userns_fd;
406 }
407
408 _cleanup_(sd_varlink_unrefp) sd_varlink *_vl = NULL;
409 if (!vl) {
410 r = nsresource_connect(&_vl);
411 if (r < 0)
412 return r;
413
414 vl = _vl;
415 }
416
417 r = sd_varlink_set_allow_fd_passing_input(vl, true);
418 if (r < 0)
419 return log_debug_errno(r, "Failed to enable varlink fd passing for read: %m");
420
421 userns_fd_idx = sd_varlink_push_dup_fd(vl, userns_fd);
422 if (userns_fd_idx < 0)
423 return log_debug_errno(userns_fd_idx, "Failed to push userns fd into varlink connection: %m");
424
425 sd_json_variant *reply = NULL;
426 r = sd_varlink_callbo(
427 vl,
428 "io.systemd.NamespaceResource.AddNetworkToUserNamespace",
429 &reply,
430 &error_id,
431 SD_JSON_BUILD_PAIR_UNSIGNED("userNamespaceFileDescriptor", userns_fd_idx),
432 SD_JSON_BUILD_PAIR("mode", JSON_BUILD_CONST_STRING("tap")));
433 if (r < 0)
434 return log_debug_errno(r, "Failed to call AddNetworkToUserNamespace() varlink call: %m");
435 if (error_id)
436 return log_debug_errno(sd_varlink_error_to_errno(error_id, reply), "Failed to add network to user namespace: %s", error_id);
437
438 static const sd_json_dispatch_field dispatch_table[] = {
439 { "hostInterfaceName", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(InterfaceParams, host_interface_name), SD_JSON_MANDATORY },
440 { "interfaceFileDescriptor", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint, offsetof(InterfaceParams, interface_fd_index), SD_JSON_MANDATORY },
441 {}
442 };
443
444 _cleanup_(interface_params_done) InterfaceParams p = {};
445 r = sd_json_dispatch(reply, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &p);
446 if (r < 0)
447 return r;
448
449 _cleanup_close_ int tap_fd = sd_varlink_take_fd(vl, p.interface_fd_index);
450 if (tap_fd < 0)
451 return tap_fd;
452
453 if (ret_host_ifname)
454 *ret_host_ifname = TAKE_PTR(p.host_interface_name);
455
456 return TAKE_FD(tap_fd);
457 }