]>
Commit | Line | Data |
---|---|---|
cc73685d | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
1d52bdf7 | 2 | |
d38dd64a CB |
3 | #ifndef _GNU_SOURCE |
4 | #define _GNU_SOURCE 1 | |
5 | #endif | |
9d257a2a | 6 | #include <arpa/inet.h> |
8f3e280e CB |
7 | #include <dirent.h> |
8 | #include <errno.h> | |
9 | #include <fcntl.h> | |
10 | #include <grp.h> | |
11 | #include <inttypes.h> | |
12 | #include <libgen.h> | |
9d257a2a CB |
13 | #include <linux/loop.h> |
14 | #include <net/if.h> | |
15 | #include <netinet/in.h> | |
8f3e280e CB |
16 | #include <pwd.h> |
17 | #include <stdarg.h> | |
1b82d721 | 18 | #include <stdbool.h> |
0ad19a3f | 19 | #include <stdio.h> |
0ad19a3f | 20 | #include <stdlib.h> |
0ad19a3f | 21 | #include <string.h> |
8f3e280e CB |
22 | #include <sys/mman.h> |
23 | #include <sys/mount.h> | |
24 | #include <sys/param.h> | |
25 | #include <sys/prctl.h> | |
6a49f05e | 26 | #include <sys/sendfile.h> |
8f3e280e | 27 | #include <sys/socket.h> |
9d257a2a | 28 | #include <sys/stat.h> |
2d76d1d7 | 29 | #include <sys/syscall.h> |
9d257a2a | 30 | #include <sys/sysmacros.h> |
97e9cfa0 | 31 | #include <sys/types.h> |
8f3e280e CB |
32 | #include <sys/utsname.h> |
33 | #include <sys/wait.h> | |
9d257a2a CB |
34 | #include <time.h> |
35 | #include <unistd.h> | |
1d52bdf7 | 36 | |
d38dd64a CB |
37 | #include "af_unix.h" |
38 | #include "caps.h" | |
5f126977 | 39 | #include "cgroups/cgroup.h" |
52ce8504 | 40 | #include "compiler.h" |
d38dd64a CB |
41 | #include "conf.h" |
42 | #include "config.h" | |
43 | #include "confile.h" | |
44 | #include "confile_utils.h" | |
45 | #include "error.h" | |
46 | #include "log.h" | |
47 | #include "lsm/lsm.h" | |
48 | #include "lxclock.h" | |
49 | #include "lxcseccomp.h" | |
50 | #include "macro.h" | |
2f443e88 | 51 | #include "memory_utils.h" |
7f88a1a2 | 52 | #include "mount_utils.h" |
d38dd64a CB |
53 | #include "namespace.h" |
54 | #include "network.h" | |
55 | #include "parse.h" | |
f40988c7 | 56 | #include "process_utils.h" |
d38dd64a CB |
57 | #include "ringbuf.h" |
58 | #include "start.h" | |
5f126977 | 59 | #include "storage/storage.h" |
d38dd64a | 60 | #include "storage/overlay.h" |
1b82d721 | 61 | #include "sync.h" |
6b3d24d7 | 62 | #include "syscall_wrappers.h" |
d38dd64a CB |
63 | #include "terminal.h" |
64 | #include "utils.h" | |
20502652 | 65 | #include "uuid.h" |
d38dd64a | 66 | |
af6824fc | 67 | #ifdef MAJOR_IN_MKDEV |
9d257a2a | 68 | #include <sys/mkdev.h> |
af6824fc | 69 | #endif |
af6824fc | 70 | |
614305f3 | 71 | #ifdef HAVE_STATVFS |
2938f7c8 | 72 | #include <sys/statvfs.h> |
614305f3 | 73 | #endif |
e827ff7e | 74 | |
35eb5cdc | 75 | #if HAVE_OPENPTY |
b0a33c1e | 76 | #include <pty.h> |
e827ff7e SG |
77 | #else |
78 | #include <../include/openpty.h> | |
79 | #endif | |
0ad19a3f | 80 | |
9d257a2a CB |
81 | #if HAVE_LIBCAP |
82 | #include <sys/capability.h> | |
83 | #endif | |
84 | ||
f1e05b90 DJ |
85 | #ifndef HAVE_STRLCAT |
86 | #include "include/strlcat.h" | |
87 | #endif | |
88 | ||
9d257a2a CB |
89 | #if IS_BIONIC |
90 | #include <../include/lxcmntent.h> | |
91 | #else | |
92 | #include <mntent.h> | |
93 | #endif | |
94 | ||
95 | #if !defined(HAVE_PRLIMIT) && defined(HAVE_PRLIMIT64) | |
96 | #include <../include/prlimit.h> | |
97 | #endif | |
98 | ||
f6815906 CB |
99 | #ifndef HAVE_STRLCPY |
100 | #include "include/strlcpy.h" | |
101 | #endif | |
102 | ||
60933dae CB |
103 | #ifndef HAVE_STRCHRNUL |
104 | #include "include/strchrnul.h" | |
105 | #endif | |
106 | ||
ac2cecc4 | 107 | lxc_log_define(conf, lxc); |
e5bda9ee | 108 | |
52ce8504 CB |
109 | /* |
110 | * The lxc_conf of the container currently being worked on in an API call. | |
0fd73091 CB |
111 | * This is used in the error calls. |
112 | */ | |
d7f19646 | 113 | thread_local struct lxc_conf *current_config; |
8912711c | 114 | |
0fd73091 CB |
115 | char *lxchook_names[NUM_LXC_HOOKS] = { |
116 | "pre-start", | |
117 | "pre-mount", | |
118 | "mount", | |
119 | "autodev", | |
120 | "start", | |
121 | "stop", | |
122 | "post-stop", | |
123 | "clone", | |
124 | "destroy", | |
125 | "start-host" | |
126 | }; | |
72d0e1cb | 127 | |
998ac676 RT |
128 | struct mount_opt { |
129 | char *name; | |
130 | int clear; | |
1b82d721 CB |
131 | bool recursive; |
132 | __u64 flag; | |
1e4bce2c | 133 | int legacy_flag; |
998ac676 RT |
134 | }; |
135 | ||
81810dd1 DL |
136 | struct caps_opt { |
137 | char *name; | |
138 | int value; | |
139 | }; | |
140 | ||
c6d09e15 WB |
141 | struct limit_opt { |
142 | char *name; | |
143 | int value; | |
144 | }; | |
145 | ||
998ac676 | 146 | static struct mount_opt mount_opt[] = { |
1b82d721 CB |
147 | { "atime", 1, false, MOUNT_ATTR_NOATIME, MS_NOATIME }, |
148 | { "dev", 1, false, MOUNT_ATTR_NODEV, MS_NODEV }, | |
149 | { "diratime", 1, false, MOUNT_ATTR_NODIRATIME, MS_NODIRATIME }, | |
150 | { "exec", 1, false, MOUNT_ATTR_NOEXEC, MS_NOEXEC }, | |
151 | { "noatime", 0, false, MOUNT_ATTR_NOATIME, MS_NOATIME }, | |
152 | { "nodev", 0, false, MOUNT_ATTR_NODEV, MS_NODEV }, | |
153 | { "nodiratime", 0, false, MOUNT_ATTR_NODIRATIME, MS_NODIRATIME }, | |
154 | { "noexec", 0, false, MOUNT_ATTR_NOEXEC, MS_NOEXEC }, | |
155 | { "norelatime", 1, false, MOUNT_ATTR_RELATIME, MS_RELATIME }, | |
156 | { "nostrictatime", 1, false, MOUNT_ATTR_STRICTATIME, MS_STRICTATIME }, | |
157 | { "nosuid", 0, false, MOUNT_ATTR_NOSUID, MS_NOSUID }, | |
158 | { "relatime", 0, false, MOUNT_ATTR_RELATIME, MS_RELATIME }, | |
159 | { "ro", 0, false, MOUNT_ATTR_RDONLY, MS_RDONLY }, | |
160 | { "rw", 1, false, MOUNT_ATTR_RDONLY, MS_RDONLY }, | |
161 | { "strictatime", 0, false, MOUNT_ATTR_STRICTATIME, MS_STRICTATIME }, | |
162 | { "suid", 1, false, MOUNT_ATTR_NOSUID, MS_NOSUID }, | |
163 | ||
164 | { "bind", 0, false, 0, MS_BIND }, | |
165 | { "defaults", 0, false, 0, 0 }, | |
166 | { "rbind", 0, true, 0, MS_BIND | MS_REC }, | |
167 | ||
168 | { "sync", 0, false, ~0, MS_SYNCHRONOUS }, | |
169 | { "async", 1, false, ~0, MS_SYNCHRONOUS }, | |
170 | { "dirsync", 0, false, ~0, MS_DIRSYNC }, | |
171 | { "lazytime", 0, false, ~0, MS_LAZYTIME }, | |
172 | { "mand", 0, false, ~0, MS_MANDLOCK }, | |
173 | { "nomand", 1, false, ~0, MS_MANDLOCK }, | |
174 | { "remount", 0, false, ~0, MS_REMOUNT }, | |
175 | ||
176 | { NULL, 0, false, ~0, ~0 }, | |
998ac676 RT |
177 | }; |
178 | ||
d840039e | 179 | static struct mount_opt propagation_opt[] = { |
1b82d721 CB |
180 | { "private", 0, false, MS_PRIVATE, MS_PRIVATE }, |
181 | { "shared", 0, false, MS_SHARED, MS_SHARED }, | |
182 | { "slave", 0, false, MS_SLAVE, MS_SLAVE }, | |
183 | { "unbindable", 0, false, MS_UNBINDABLE, MS_UNBINDABLE }, | |
184 | { "rprivate", 0, true, MS_PRIVATE, MS_PRIVATE | MS_REC }, | |
185 | { "rshared", 0, true, MS_SHARED, MS_SHARED | MS_REC }, | |
186 | { "rslave", 0, true, MS_SLAVE, MS_SLAVE | MS_REC }, | |
187 | { "runbindable", 0, true, MS_UNBINDABLE, MS_UNBINDABLE | MS_REC }, | |
188 | { NULL, 0, 0 }, | |
d840039e YT |
189 | }; |
190 | ||
81810dd1 | 191 | static struct caps_opt caps_opt[] = { |
8560cd36 | 192 | #if HAVE_LIBCAP |
7b4cd468 CB |
193 | { "chown", CAP_CHOWN }, |
194 | { "dac_override", CAP_DAC_OVERRIDE }, | |
195 | { "dac_read_search", CAP_DAC_READ_SEARCH }, | |
196 | { "fowner", CAP_FOWNER }, | |
197 | { "fsetid", CAP_FSETID }, | |
198 | { "kill", CAP_KILL }, | |
199 | { "setgid", CAP_SETGID }, | |
200 | { "setuid", CAP_SETUID }, | |
201 | { "setpcap", CAP_SETPCAP }, | |
202 | { "linux_immutable", CAP_LINUX_IMMUTABLE }, | |
203 | { "net_bind_service", CAP_NET_BIND_SERVICE }, | |
204 | { "net_broadcast", CAP_NET_BROADCAST }, | |
205 | { "net_admin", CAP_NET_ADMIN }, | |
206 | { "net_raw", CAP_NET_RAW }, | |
207 | { "ipc_lock", CAP_IPC_LOCK }, | |
208 | { "ipc_owner", CAP_IPC_OWNER }, | |
209 | { "sys_module", CAP_SYS_MODULE }, | |
210 | { "sys_rawio", CAP_SYS_RAWIO }, | |
211 | { "sys_chroot", CAP_SYS_CHROOT }, | |
212 | { "sys_ptrace", CAP_SYS_PTRACE }, | |
213 | { "sys_pacct", CAP_SYS_PACCT }, | |
214 | { "sys_admin", CAP_SYS_ADMIN }, | |
215 | { "sys_boot", CAP_SYS_BOOT }, | |
216 | { "sys_nice", CAP_SYS_NICE }, | |
217 | { "sys_resource", CAP_SYS_RESOURCE }, | |
218 | { "sys_time", CAP_SYS_TIME }, | |
219 | { "sys_tty_config", CAP_SYS_TTY_CONFIG }, | |
220 | { "mknod", CAP_MKNOD }, | |
221 | { "lease", CAP_LEASE }, | |
222 | { "audit_write", CAP_AUDIT_WRITE }, | |
223 | { "audit_control", CAP_AUDIT_CONTROL }, | |
224 | { "setfcap", CAP_SETFCAP }, | |
225 | { "mac_override", CAP_MAC_OVERRIDE }, | |
226 | { "mac_admin", CAP_MAC_ADMIN }, | |
227 | { "syslog", CAP_SYSLOG }, | |
228 | { "wake_alarm", CAP_WAKE_ALARM }, | |
229 | { "block_suspend", CAP_BLOCK_SUSPEND }, | |
230 | { "audit_read", CAP_AUDIT_READ }, | |
231 | { "perfmon", CAP_PERFMON }, | |
232 | { "bpf", CAP_BPF }, | |
233 | { "checkpoint_restore", CAP_CHECKPOINT_RESTORE }, | |
2b54359b | 234 | #endif |
8560cd36 | 235 | }; |
81810dd1 | 236 | |
c6d09e15 WB |
237 | static struct limit_opt limit_opt[] = { |
238 | #ifdef RLIMIT_AS | |
239 | { "as", RLIMIT_AS }, | |
240 | #endif | |
241 | #ifdef RLIMIT_CORE | |
242 | { "core", RLIMIT_CORE }, | |
243 | #endif | |
244 | #ifdef RLIMIT_CPU | |
245 | { "cpu", RLIMIT_CPU }, | |
246 | #endif | |
247 | #ifdef RLIMIT_DATA | |
248 | { "data", RLIMIT_DATA }, | |
249 | #endif | |
250 | #ifdef RLIMIT_FSIZE | |
251 | { "fsize", RLIMIT_FSIZE }, | |
252 | #endif | |
253 | #ifdef RLIMIT_LOCKS | |
254 | { "locks", RLIMIT_LOCKS }, | |
255 | #endif | |
256 | #ifdef RLIMIT_MEMLOCK | |
257 | { "memlock", RLIMIT_MEMLOCK }, | |
258 | #endif | |
259 | #ifdef RLIMIT_MSGQUEUE | |
260 | { "msgqueue", RLIMIT_MSGQUEUE }, | |
261 | #endif | |
262 | #ifdef RLIMIT_NICE | |
263 | { "nice", RLIMIT_NICE }, | |
264 | #endif | |
265 | #ifdef RLIMIT_NOFILE | |
266 | { "nofile", RLIMIT_NOFILE }, | |
267 | #endif | |
268 | #ifdef RLIMIT_NPROC | |
269 | { "nproc", RLIMIT_NPROC }, | |
270 | #endif | |
271 | #ifdef RLIMIT_RSS | |
272 | { "rss", RLIMIT_RSS }, | |
273 | #endif | |
274 | #ifdef RLIMIT_RTPRIO | |
275 | { "rtprio", RLIMIT_RTPRIO }, | |
276 | #endif | |
277 | #ifdef RLIMIT_RTTIME | |
278 | { "rttime", RLIMIT_RTTIME }, | |
279 | #endif | |
280 | #ifdef RLIMIT_SIGPENDING | |
281 | { "sigpending", RLIMIT_SIGPENDING }, | |
282 | #endif | |
283 | #ifdef RLIMIT_STACK | |
284 | { "stack", RLIMIT_STACK }, | |
285 | #endif | |
286 | }; | |
287 | ||
91c3830e SH |
288 | static int run_buffer(char *buffer) |
289 | { | |
cc6a0e78 | 290 | __do_free char *output = NULL; |
55022530 | 291 | __do_lxc_pclose struct lxc_popen_FILE *f = NULL; |
ebf3a6af | 292 | int fd, ret; |
91c3830e | 293 | |
ebec9176 | 294 | f = lxc_popen(buffer); |
55022530 CB |
295 | if (!f) |
296 | return log_error_errno(-1, errno, "Failed to popen() %s", buffer); | |
91c3830e | 297 | |
b8e43ef0 | 298 | output = zalloc(LXC_LOG_BUFFER_SIZE); |
55022530 CB |
299 | if (!output) |
300 | return log_error_errno(-1, ENOMEM, "Failed to allocate memory for %s", buffer); | |
91c3830e | 301 | |
ebf3a6af | 302 | fd = fileno(f->f); |
55022530 CB |
303 | if (fd < 0) |
304 | return log_error_errno(-1, errno, "Failed to retrieve underlying file descriptor"); | |
ebf3a6af CB |
305 | |
306 | for (int i = 0; i < 10; i++) { | |
307 | ssize_t bytes_read; | |
308 | ||
309 | bytes_read = lxc_read_nointr(fd, output, LXC_LOG_BUFFER_SIZE - 1); | |
310 | if (bytes_read > 0) { | |
311 | output[bytes_read] = '\0'; | |
312 | DEBUG("Script %s produced output: %s", buffer, output); | |
313 | continue; | |
314 | } | |
315 | ||
316 | break; | |
317 | } | |
91c3830e | 318 | |
55022530 CB |
319 | ret = lxc_pclose(move_ptr(f)); |
320 | if (ret == -1) | |
321 | return log_error_errno(-1, errno, "Script exited with error"); | |
322 | else if (WIFEXITED(ret) && WEXITSTATUS(ret) != 0) | |
323 | return log_error(-1, "Script exited with status %d", WEXITSTATUS(ret)); | |
324 | else if (WIFSIGNALED(ret)) | |
325 | return log_error(-1, "Script terminated by signal %d", WTERMSIG(ret)); | |
91c3830e SH |
326 | |
327 | return 0; | |
328 | } | |
329 | ||
14a7b0f9 CB |
330 | int run_script_argv(const char *name, unsigned int hook_version, |
331 | const char *section, const char *script, | |
586b1ce7 | 332 | const char *hookname, char **argv) |
148e91f5 | 333 | { |
e1a94937 | 334 | __do_free char *buffer = NULL; |
3f60c2f7 | 335 | int buf_pos, i, ret; |
d08e5708 | 336 | size_t size = 0; |
148e91f5 | 337 | |
3f60c2f7 | 338 | if (hook_version == 0) |
55022530 CB |
339 | INFO("Executing script \"%s\" for container \"%s\", config section \"%s\"", |
340 | script, name, section); | |
3f60c2f7 CB |
341 | else |
342 | INFO("Executing script \"%s\" for container \"%s\"", script, name); | |
148e91f5 | 343 | |
586b1ce7 CB |
344 | for (i = 0; argv && argv[i]; i++) |
345 | size += strlen(argv[i]) + 1; | |
148e91f5 | 346 | |
6333c915 CB |
347 | size += STRLITERALLEN("exec"); |
348 | size++; | |
148e91f5 | 349 | size += strlen(script); |
3f60c2f7 CB |
350 | size++; |
351 | ||
148e91f5 | 352 | if (size > INT_MAX) |
3f60c2f7 | 353 | return -EFBIG; |
148e91f5 | 354 | |
3f60c2f7 | 355 | if (hook_version == 0) { |
d08e5708 CB |
356 | size += strlen(hookname); |
357 | size++; | |
358 | ||
359 | size += strlen(name); | |
360 | size++; | |
361 | ||
362 | size += strlen(section); | |
363 | size++; | |
364 | ||
365 | if (size > INT_MAX) | |
366 | return -EFBIG; | |
327cce76 | 367 | } |
3f60c2f7 | 368 | |
b8e43ef0 | 369 | buffer = zalloc(size); |
6f8d00d2 CB |
370 | if (!buffer) |
371 | return -ENOMEM; | |
372 | ||
327cce76 | 373 | if (hook_version == 0) |
9bcde680 | 374 | buf_pos = strnprintf(buffer, size, "exec %s %s %s %s", script, name, section, hookname); |
327cce76 | 375 | else |
9bcde680 CB |
376 | buf_pos = strnprintf(buffer, size, "exec %s", script); |
377 | if (buf_pos < 0) | |
55022530 | 378 | return log_error_errno(-1, errno, "Failed to create command line for script \"%s\"", script); |
3f60c2f7 | 379 | |
327cce76 | 380 | if (hook_version == 1) { |
3f60c2f7 CB |
381 | ret = setenv("LXC_HOOK_TYPE", hookname, 1); |
382 | if (ret < 0) { | |
55022530 | 383 | return log_error_errno(-1, errno, "Failed to set environment variable: LXC_HOOK_TYPE=%s", hookname); |
3f60c2f7 | 384 | } |
90f20466 | 385 | TRACE("Set environment variable: LXC_HOOK_TYPE=%s", hookname); |
3f60c2f7 CB |
386 | |
387 | ret = setenv("LXC_HOOK_SECTION", section, 1); | |
55022530 CB |
388 | if (ret < 0) |
389 | return log_error_errno(-1, errno, "Failed to set environment variable: LXC_HOOK_SECTION=%s", section); | |
3f60c2f7 | 390 | TRACE("Set environment variable: LXC_HOOK_SECTION=%s", section); |
14a7b0f9 | 391 | |
71528742 | 392 | if (strequal(section, "net")) { |
14a7b0f9 CB |
393 | char *parent; |
394 | ||
586b1ce7 | 395 | if (!argv || !argv[0]) |
e1a94937 | 396 | return -1; |
14a7b0f9 | 397 | |
586b1ce7 | 398 | ret = setenv("LXC_NET_TYPE", argv[0], 1); |
55022530 CB |
399 | if (ret < 0) |
400 | return log_error_errno(-1, errno, "Failed to set environment variable: LXC_NET_TYPE=%s", argv[0]); | |
586b1ce7 | 401 | TRACE("Set environment variable: LXC_NET_TYPE=%s", argv[0]); |
14a7b0f9 | 402 | |
586b1ce7 | 403 | parent = argv[1] ? argv[1] : ""; |
14a7b0f9 | 404 | |
71528742 | 405 | if (strequal(argv[0], "macvlan")) { |
14a7b0f9 | 406 | ret = setenv("LXC_NET_PARENT", parent, 1); |
55022530 CB |
407 | if (ret < 0) |
408 | return log_error_errno(-1, errno, "Failed to set environment variable: LXC_NET_PARENT=%s", parent); | |
14a7b0f9 | 409 | TRACE("Set environment variable: LXC_NET_PARENT=%s", parent); |
71528742 | 410 | } else if (strequal(argv[0], "phys")) { |
14a7b0f9 | 411 | ret = setenv("LXC_NET_PARENT", parent, 1); |
55022530 CB |
412 | if (ret < 0) |
413 | return log_error_errno(-1, errno, "Failed to set environment variable: LXC_NET_PARENT=%s", parent); | |
14a7b0f9 | 414 | TRACE("Set environment variable: LXC_NET_PARENT=%s", parent); |
71528742 | 415 | } else if (strequal(argv[0], "veth")) { |
586b1ce7 | 416 | char *peer = argv[2] ? argv[2] : ""; |
14a7b0f9 CB |
417 | |
418 | ret = setenv("LXC_NET_PEER", peer, 1); | |
55022530 CB |
419 | if (ret < 0) |
420 | return log_error_errno(-1, errno, "Failed to set environment variable: LXC_NET_PEER=%s", peer); | |
14a7b0f9 CB |
421 | TRACE("Set environment variable: LXC_NET_PEER=%s", peer); |
422 | ||
423 | ret = setenv("LXC_NET_PARENT", parent, 1); | |
55022530 CB |
424 | if (ret < 0) |
425 | return log_error_errno(-1, errno, "Failed to set environment variable: LXC_NET_PARENT=%s", parent); | |
14a7b0f9 CB |
426 | TRACE("Set environment variable: LXC_NET_PARENT=%s", parent); |
427 | } | |
428 | } | |
148e91f5 SH |
429 | } |
430 | ||
586b1ce7 | 431 | for (i = 0; argv && argv[i]; i++) { |
3f60c2f7 CB |
432 | size_t len = size - buf_pos; |
433 | ||
9bcde680 CB |
434 | ret = strnprintf(buffer + buf_pos, len, " %s", argv[i]); |
435 | if (ret < 0) | |
55022530 | 436 | return log_error_errno(-1, errno, "Failed to create command line for script \"%s\"", script); |
3f60c2f7 | 437 | buf_pos += ret; |
148e91f5 SH |
438 | } |
439 | ||
e1a94937 | 440 | return run_buffer(buffer); |
148e91f5 SH |
441 | } |
442 | ||
811ef482 | 443 | int run_script(const char *name, const char *section, const char *script, ...) |
e3b4c4c4 | 444 | { |
2f443e88 | 445 | __do_free char *buffer = NULL; |
abbfd20b | 446 | int ret; |
2f443e88 | 447 | char *p; |
abbfd20b | 448 | va_list ap; |
0fd73091 | 449 | size_t size = 0; |
751d9dcd | 450 | |
0fd73091 | 451 | INFO("Executing script \"%s\" for container \"%s\", config section \"%s\"", |
751d9dcd | 452 | script, name, section); |
e3b4c4c4 | 453 | |
abbfd20b DL |
454 | va_start(ap, script); |
455 | while ((p = va_arg(ap, char *))) | |
95642a10 | 456 | size += strlen(p) + 1; |
abbfd20b DL |
457 | va_end(ap); |
458 | ||
6333c915 | 459 | size += STRLITERALLEN("exec"); |
abbfd20b DL |
460 | size += strlen(script); |
461 | size += strlen(name); | |
462 | size += strlen(section); | |
6d1a5f93 | 463 | size += 4; |
abbfd20b | 464 | |
95642a10 MS |
465 | if (size > INT_MAX) |
466 | return -1; | |
467 | ||
2f443e88 | 468 | buffer = must_realloc(NULL, size); |
9bcde680 CB |
469 | ret = strnprintf(buffer, size, "exec %s %s %s", script, name, section); |
470 | if (ret < 0) | |
9ba8130c | 471 | return -1; |
751d9dcd | 472 | |
abbfd20b | 473 | va_start(ap, script); |
9ba8130c | 474 | while ((p = va_arg(ap, char *))) { |
062b72c6 | 475 | int len = size - ret; |
9ba8130c | 476 | int rc; |
9bcde680 CB |
477 | rc = strnprintf(buffer + ret, len, " %s", p); |
478 | if (rc < 0) { | |
7b5a2435 | 479 | va_end(ap); |
9ba8130c | 480 | return -1; |
7b5a2435 | 481 | } |
9ba8130c SH |
482 | ret += rc; |
483 | } | |
abbfd20b | 484 | va_end(ap); |
751d9dcd | 485 | |
91c3830e | 486 | return run_buffer(buffer); |
e3b4c4c4 ST |
487 | } |
488 | ||
4e86cad3 CB |
489 | int lxc_storage_prepare(struct lxc_conf *conf) |
490 | { | |
491 | int ret; | |
492 | struct lxc_rootfs *rootfs = &conf->rootfs; | |
493 | ||
494 | if (!rootfs->path) { | |
495 | ret = mount("", "/", NULL, MS_SLAVE | MS_REC, 0); | |
496 | if (ret < 0) | |
497 | return log_error_errno(-1, errno, "Failed to recursively turn root mount tree into dependent mount"); | |
498 | ||
499 | rootfs->dfd_mnt = open_at(-EBADF, "/", PROTECT_OPATH_DIRECTORY, PROTECT_LOOKUP_ABSOLUTE, 0); | |
500 | if (rootfs->dfd_mnt < 0) | |
501 | return -errno; | |
502 | ||
503 | return 0; | |
504 | } | |
505 | ||
506 | ret = access(rootfs->mount, F_OK); | |
507 | if (ret != 0) | |
508 | return log_error_errno(-1, errno, "Failed to access to \"%s\". Check it is present", | |
509 | rootfs->mount); | |
510 | ||
511 | rootfs->storage = storage_init(conf); | |
512 | if (!rootfs->storage) | |
513 | return log_error(-1, "Failed to mount rootfs \"%s\" onto \"%s\" with options \"%s\"", | |
514 | rootfs->path, rootfs->mount, | |
515 | rootfs->options ? rootfs->options : "(null)"); | |
516 | ||
517 | return 0; | |
518 | } | |
519 | ||
520 | void lxc_storage_put(struct lxc_conf *conf) | |
521 | { | |
522 | storage_put(conf->rootfs.storage); | |
523 | conf->rootfs.storage = NULL; | |
524 | } | |
525 | ||
79ff643d | 526 | /* lxc_rootfs_prepare |
63fc76c3 | 527 | * if rootfs is a directory, then open ${rootfs}/.lxc-keep for writing for |
b7ed4bf0 CS |
528 | * the duration of the container run, to prevent the container from marking |
529 | * the underlying fs readonly on shutdown. unlink the file immediately so | |
63fc76c3 GJ |
530 | * no name pollution is happens. |
531 | * don't unlink on NFS to avoid random named stale handles. | |
0c547523 | 532 | */ |
239f29c9 | 533 | int lxc_rootfs_init(struct lxc_conf *conf, bool userns) |
0c547523 | 534 | { |
1b82d721 | 535 | __do_close int dfd_path = -EBADF, fd_pin = -EBADF; |
79ff643d CB |
536 | int ret; |
537 | struct stat st; | |
538 | struct statfs stfs; | |
4e86cad3 CB |
539 | struct lxc_rootfs *rootfs = &conf->rootfs; |
540 | ||
541 | ret = lxc_storage_prepare(conf); | |
542 | if (ret) | |
543 | return syserror_set(-EINVAL, "Failed to prepare rootfs storage"); | |
0c547523 | 544 | |
e26cf563 | 545 | if (!is_empty_string(rootfs->mnt_opts.userns_path)) { |
657ed14a CB |
546 | if (!rootfs->path) |
547 | return syserror_set(-EINVAL, "Idmapped rootfs currently only supported with separate rootfs for container"); | |
548 | ||
549 | if (rootfs->bdev_type && !strequal(rootfs->bdev_type, "dir")) | |
550 | return syserror_set(-EINVAL, "Idmapped rootfs currently only supports the \"dir\" storage driver"); | |
e26cf563 CB |
551 | } |
552 | ||
c119f018 CB |
553 | if (!rootfs->path) |
554 | return log_trace(0, "Not pinning because container does not have a rootfs"); | |
e99ee0de | 555 | |
c119f018 CB |
556 | if (userns) |
557 | return log_trace(0, "Not pinning because container runs in user namespace"); | |
79ff643d | 558 | |
c119f018 CB |
559 | if (rootfs->bdev_type) { |
560 | if (strequal(rootfs->bdev_type, "overlay") || | |
561 | strequal(rootfs->bdev_type, "overlayfs")) | |
562 | return log_trace_errno(0, EINVAL, "Not pinning on stacking filesystem"); | |
0c547523 | 563 | |
c119f018 CB |
564 | if (strequal(rootfs->bdev_type, "zfs")) |
565 | return log_trace_errno(0, EINVAL, "Not pinning on ZFS filesystem"); | |
e26cf563 | 566 | } |
79ff643d | 567 | |
c119f018 CB |
568 | dfd_path = open_at(-EBADF, rootfs->path, PROTECT_OPATH_FILE, 0, 0); |
569 | if (dfd_path < 0) | |
570 | return syserror("Failed to open \"%s\"", rootfs->path); | |
571 | ||
79ff643d | 572 | ret = fstat(dfd_path, &st); |
957c4704 | 573 | if (ret < 0) |
79ff643d | 574 | return log_trace_errno(-errno, errno, "Failed to retrieve file status"); |
0c547523 | 575 | |
79ff643d CB |
576 | if (!S_ISDIR(st.st_mode)) |
577 | return log_trace_errno(0, ENOTDIR, "Not pinning because file descriptor is not a directory"); | |
0c547523 | 578 | |
79ff643d CB |
579 | fd_pin = open_at(dfd_path, ".lxc_keep", |
580 | PROTECT_OPEN | O_CREAT, | |
581 | PROTECT_LOOKUP_BENEATH, | |
582 | S_IWUSR | S_IRUSR); | |
e859a5ee WM |
583 | if (fd_pin < 0) { |
584 | if (errno == EROFS) { | |
585 | return log_trace_errno(0, EROFS, "Not pinning on read-only filesystem"); | |
586 | } | |
e26cf563 | 587 | return syserror("Failed to pin rootfs"); |
e859a5ee | 588 | } |
0c547523 | 589 | |
79ff643d | 590 | TRACE("Pinned rootfs %d(.lxc_keep)", fd_pin); |
0fd73091 | 591 | |
79ff643d CB |
592 | ret = fstatfs(fd_pin, &stfs); |
593 | if (ret < 0) { | |
594 | SYSWARN("Failed to retrieve filesystem status"); | |
595 | goto out; | |
596 | } | |
63fc76c3 | 597 | |
79ff643d CB |
598 | if (stfs.f_type == NFS_SUPER_MAGIC) { |
599 | DEBUG("Not unlinking pinned file on NFS"); | |
600 | goto out; | |
601 | } | |
63fc76c3 | 602 | |
79ff643d CB |
603 | if (unlinkat(dfd_path, ".lxc_keep", 0)) |
604 | SYSTRACE("Failed to unlink rootfs pinning file %d(.lxc_keep)", dfd_path); | |
605 | else | |
606 | TRACE("Unlinked pinned file %d(.lxc_keep)", dfd_path); | |
0fd73091 | 607 | |
79ff643d CB |
608 | out: |
609 | rootfs->fd_path_pin = move_fd(fd_pin); | |
610 | return 0; | |
0c547523 SH |
611 | } |
612 | ||
4b875ef9 CB |
613 | int lxc_rootfs_prepare_parent(struct lxc_handler *handler) |
614 | { | |
615 | __do_close int dfd_idmapped = -EBADF, fd_userns = -EBADF; | |
616 | struct lxc_rootfs *rootfs = &handler->conf->rootfs; | |
617 | struct lxc_storage *storage = rootfs->storage; | |
618 | int ret; | |
619 | const char *path_source; | |
620 | ||
621 | if (lxc_list_empty(&handler->conf->id_map)) | |
622 | return 0; | |
623 | ||
624 | if (is_empty_string(rootfs->mnt_opts.userns_path)) | |
625 | return 0; | |
626 | ||
627 | if (handler->conf->rootfs_setup) | |
628 | return 0; | |
629 | ||
630 | if (rootfs_is_blockdev(handler->conf)) | |
631 | return syserror_set(-EOPNOTSUPP, "Idmapped mounts on block-backed storage not yet supported"); | |
632 | ||
633 | if (!can_use_bind_mounts()) | |
634 | return syserror_set(-EOPNOTSUPP, "Kernel does not support the new mount api"); | |
635 | ||
1b82d721 | 636 | if (strequal(rootfs->mnt_opts.userns_path, "container")) |
4b875ef9 CB |
637 | fd_userns = dup_cloexec(handler->nsfd[LXC_NS_USER]); |
638 | else | |
639 | fd_userns = open_at(-EBADF, rootfs->mnt_opts.userns_path, | |
640 | PROTECT_OPEN_WITH_TRAILING_SYMLINKS, 0, 0); | |
641 | if (fd_userns < 0) | |
642 | return syserror("Failed to open user namespace"); | |
643 | ||
644 | path_source = lxc_storage_get_path(storage->src, storage->type); | |
645 | ||
646 | dfd_idmapped = create_detached_idmapped_mount(path_source, fd_userns, true); | |
647 | if (dfd_idmapped < 0) | |
648 | return syserror("Failed to create detached idmapped mount"); | |
649 | ||
650 | ret = lxc_abstract_unix_send_fds(handler->data_sock[0], &dfd_idmapped, 1, NULL, 0); | |
651 | if (ret < 0) | |
652 | return syserror("Failed to send detached idmapped mount fd"); | |
653 | ||
654 | TRACE("Created detached idmapped mount %d", dfd_idmapped); | |
655 | return 0; | |
656 | } | |
657 | ||
6b741397 CB |
658 | static int add_shmount_to_list(struct lxc_conf *conf) |
659 | { | |
6b5a54cd | 660 | char new_mount[PATH_MAX]; |
0d190408 | 661 | /* Offset for the leading '/' since the path_cont |
6b741397 CB |
662 | * is absolute inside the container. |
663 | */ | |
664 | int offset = 1, ret = -1; | |
0d190408 | 665 | |
9bcde680 | 666 | ret = strnprintf(new_mount, sizeof(new_mount), |
6b741397 CB |
667 | "%s %s none bind,create=dir 0 0", conf->shmount.path_host, |
668 | conf->shmount.path_cont + offset); | |
9bcde680 | 669 | if (ret < 0) |
0d190408 LT |
670 | return -1; |
671 | ||
6b741397 | 672 | return add_elem_to_mount_list(new_mount, conf); |
0d190408 LT |
673 | } |
674 | ||
6d25a524 | 675 | static int lxc_mount_auto_mounts(struct lxc_handler *handler, int flags) |
368bbc02 | 676 | { |
7b371c1e | 677 | int i, ret; |
b06b8511 CS |
678 | static struct { |
679 | int match_mask; | |
680 | int match_flag; | |
681 | const char *source; | |
682 | const char *destination; | |
683 | const char *fstype; | |
684 | unsigned long flags; | |
685 | const char *options; | |
e8b9c9ec | 686 | bool requires_cap_net_admin; |
b06b8511 | 687 | } default_mounts[] = { |
0fd73091 CB |
688 | /* Read-only bind-mounting... In older kernels, doing that |
689 | * required to do one MS_BIND mount and then | |
690 | * MS_REMOUNT|MS_RDONLY the same one. According to mount(2) | |
691 | * manpage, MS_BIND honors MS_RDONLY from kernel 2.6.26 | |
692 | * onwards. However, this apparently does not work on kernel | |
693 | * 3.8. Unfortunately, on that very same kernel, doing the same | |
694 | * trick as above doesn't seem to work either, there one needs | |
695 | * to ALSO specify MS_BIND for the remount, otherwise the | |
696 | * entire fs is remounted read-only or the mount fails because | |
697 | * it's busy... MS_REMOUNT|MS_BIND|MS_RDONLY seems to work for | |
698 | * kernels as low as 2.6.32... | |
368bbc02 | 699 | */ |
5d1bf4c4 | 700 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "proc", "%r/proc", "proc", MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL, false }, |
592fd47a | 701 | /* proc/tty is used as a temporary placeholder for proc/sys/net which we'll move back in a few steps */ |
cb4889ab | 702 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sys/net", "%r/proc/tty", NULL, MS_BIND, NULL, true, }, |
5d1bf4c4 CB |
703 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sys", "%r/proc/sys", NULL, MS_BIND, NULL, false }, |
704 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, NULL, "%r/proc/sys", NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL, false }, | |
705 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/tty", "%r/proc/sys/net", NULL, MS_MOVE, NULL, true }, | |
706 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sysrq-trigger", "%r/proc/sysrq-trigger", NULL, MS_BIND, NULL, false }, | |
707 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, NULL, "%r/proc/sysrq-trigger", NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL, false }, | |
708 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW, "proc", "%r/proc", "proc", MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL, false }, | |
709 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RW, "sysfs", "%r/sys", "sysfs", 0, NULL, false }, | |
710 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RO, "sysfs", "%r/sys", "sysfs", MS_RDONLY, NULL, false }, | |
cb4889ab CB |
711 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "sysfs", "%r/sys", "sysfs", MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC, NULL, false }, |
712 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "%r/sys/devices/virtual/net", "%r/sys/devices/virtual/net", NULL, MS_BIND, NULL, false }, | |
713 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, NULL, "%r/sys/devices/virtual/net", NULL, MS_REMOUNT|MS_NOSUID|MS_NODEV|MS_NOEXEC, NULL, false }, | |
5d1bf4c4 | 714 | { 0, 0, NULL, NULL, NULL, 0, NULL, false } |
b06b8511 | 715 | }; |
6d25a524 | 716 | struct lxc_conf *conf = handler->conf; |
e25af1bc CB |
717 | struct lxc_rootfs *rootfs = &conf->rootfs; |
718 | bool has_cap_net_admin; | |
368bbc02 | 719 | |
f4bea7cc | 720 | if (flags & LXC_AUTO_PROC_MASK) { |
f6c5aab0 CB |
721 | if (rootfs->path) { |
722 | /* | |
723 | * Only unmount procfs if we have a separate rootfs so | |
724 | * we can still access it in safe_mount() below. | |
725 | */ | |
726 | ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/proc", | |
727 | rootfs->path ? rootfs->mount : ""); | |
728 | if (ret < 0) | |
729 | return ret_errno(EIO); | |
95258e34 | 730 | |
f6c5aab0 CB |
731 | ret = umount2(rootfs->buf, MNT_DETACH); |
732 | if (ret) | |
733 | SYSDEBUG("Tried to ensure procfs is unmounted"); | |
734 | } | |
95258e34 | 735 | |
ea57e424 | 736 | ret = mkdirat(rootfs->dfd_mnt, "proc" , S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
f4bea7cc | 737 | if (ret < 0 && errno != EEXIST) |
0d33a382 | 738 | return syserror("Failed to create procfs mountpoint under %d", rootfs->dfd_mnt); |
c119f018 CB |
739 | |
740 | TRACE("Created procfs mountpoint under %d", rootfs->dfd_mnt); | |
f4bea7cc CB |
741 | } |
742 | ||
743 | if (flags & LXC_AUTO_SYS_MASK) { | |
f6c5aab0 CB |
744 | if (rootfs->path) { |
745 | /* | |
746 | * Only unmount sysfs if we have a separate rootfs so | |
747 | * we can still access it in safe_mount() below. | |
748 | */ | |
749 | ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/sys", | |
750 | rootfs->path ? rootfs->mount : ""); | |
751 | if (ret < 0) | |
752 | return ret_errno(EIO); | |
95258e34 | 753 | |
f6c5aab0 CB |
754 | ret = umount2(rootfs->buf, MNT_DETACH); |
755 | if (ret) | |
756 | SYSDEBUG("Tried to ensure sysfs is unmounted"); | |
757 | } | |
95258e34 | 758 | |
ea57e424 | 759 | ret = mkdirat(rootfs->dfd_mnt, "sys" , S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
f4bea7cc | 760 | if (ret < 0 && errno != EEXIST) |
0d33a382 | 761 | return syserror("Failed to create sysfs mountpoint under %d", rootfs->dfd_mnt); |
c119f018 CB |
762 | |
763 | TRACE("Created sysfs mountpoint under %d", rootfs->dfd_mnt); | |
f4bea7cc CB |
764 | } |
765 | ||
e25af1bc | 766 | has_cap_net_admin = lxc_wants_cap(CAP_NET_ADMIN, conf); |
d84b26bc | 767 | for (i = 0; default_mounts[i].match_mask; i++) { |
8db92302 | 768 | __do_free char *destination = NULL, *source = NULL; |
cb4889ab | 769 | unsigned long mflags = default_mounts[i].flags; |
96f306e6 | 770 | |
0fd73091 CB |
771 | if ((flags & default_mounts[i].match_mask) != default_mounts[i].match_flag) |
772 | continue; | |
773 | ||
774 | if (default_mounts[i].source) { | |
cc4fd506 | 775 | /* will act like strdup if %r is not present */ |
e25af1bc | 776 | source = lxc_string_replace("%r", rootfs->path ? rootfs->mount : "", default_mounts[i].source); |
0fd73091 | 777 | if (!source) |
75fca1ac | 778 | return syserror_set(-ENOMEM, "Failed to create source path"); |
0fd73091 | 779 | } |
f24a52d5 | 780 | |
55022530 | 781 | if (!default_mounts[i].destination) |
75fca1ac | 782 | return syserror_set(-EINVAL, "BUG: auto mounts destination %d was NULL", i); |
0fd73091 | 783 | |
e8b9c9ec | 784 | if (!has_cap_net_admin && default_mounts[i].requires_cap_net_admin) { |
785 | TRACE("Container does not have CAP_NET_ADMIN. Skipping \"%s\" mount", default_mounts[i].source ?: "(null)"); | |
786 | continue; | |
787 | } | |
788 | ||
0fd73091 | 789 | /* will act like strdup if %r is not present */ |
e25af1bc | 790 | destination = lxc_string_replace("%r", rootfs->path ? rootfs->mount : "", default_mounts[i].destination); |
55022530 | 791 | if (!destination) |
75fca1ac | 792 | return syserror_set(-ENOMEM, "Failed to create target path"); |
0fd73091 | 793 | |
cb4889ab CB |
794 | ret = safe_mount(source, destination, |
795 | default_mounts[i].fstype, | |
796 | mflags, | |
797 | default_mounts[i].options, | |
75fca1ac | 798 | rootfs->path ? rootfs->mount : NULL); |
7b371c1e | 799 | if (ret < 0) { |
75fca1ac CB |
800 | if (errno != ENOENT) |
801 | return syserror("Failed to mount \"%s\" on \"%s\" with flags %lu", source, destination, mflags); | |
802 | ||
803 | INFO("Mount source or target for \"%s\" on \"%s\" does not exist. Skipping", source, destination); | |
804 | continue; | |
368bbc02 | 805 | } |
cb4889ab CB |
806 | |
807 | if (mflags & MS_REMOUNT) | |
808 | TRACE("Remounted automount \"%s\" on \"%s\" %s with flags %lu", source, destination, (mflags & MS_RDONLY) ? "read-only" : "read-write", mflags); | |
809 | else | |
810 | TRACE("Mounted automount \"%s\" on \"%s\" %s with flags %lu", source, destination, (mflags & MS_RDONLY) ? "read-only" : "read-write", mflags); | |
368bbc02 CS |
811 | } |
812 | ||
b06b8511 | 813 | if (flags & LXC_AUTO_CGROUP_MASK) { |
0769b82a CS |
814 | int cg_flags; |
815 | ||
3f69fb12 | 816 | cg_flags = flags & (LXC_AUTO_CGROUP_MASK & ~LXC_AUTO_CGROUP_FORCE); |
0fd73091 CB |
817 | /* If the type of cgroup mount was not specified, it depends on |
818 | * the container's capabilities as to what makes sense: if we | |
819 | * have CAP_SYS_ADMIN, the read-only part can be remounted | |
820 | * read-write anyway, so we may as well default to read-write; | |
821 | * then the admin will not be given a false sense of security. | |
822 | * (And if they really want mixed r/o r/w, then they can | |
823 | * explicitly specify :mixed.) OTOH, if the container lacks | |
824 | * CAP_SYS_ADMIN, do only default to :mixed, because then the | |
825 | * container can't remount it read-write. | |
826 | */ | |
9394b6dc | 827 | if ((cg_flags == LXC_AUTO_CGROUP_NOSPEC) || (cg_flags == LXC_AUTO_CGROUP_FULL_NOSPEC)) { |
0769b82a | 828 | int has_sys_admin = 0; |
b0ee5983 CB |
829 | |
830 | if (!lxc_list_empty(&conf->keepcaps)) | |
0769b82a | 831 | has_sys_admin = in_caplist(CAP_SYS_ADMIN, &conf->keepcaps); |
b0ee5983 | 832 | else |
0769b82a | 833 | has_sys_admin = !in_caplist(CAP_SYS_ADMIN, &conf->caps); |
b0ee5983 CB |
834 | |
835 | if (cg_flags == LXC_AUTO_CGROUP_NOSPEC) | |
0769b82a | 836 | cg_flags = has_sys_admin ? LXC_AUTO_CGROUP_RW : LXC_AUTO_CGROUP_MIXED; |
b0ee5983 | 837 | else |
0769b82a | 838 | cg_flags = has_sys_admin ? LXC_AUTO_CGROUP_FULL_RW : LXC_AUTO_CGROUP_FULL_MIXED; |
0769b82a | 839 | } |
0fd73091 | 840 | |
3f69fb12 | 841 | if (flags & LXC_AUTO_CGROUP_FORCE) |
0fd73091 CB |
842 | cg_flags |= LXC_AUTO_CGROUP_FORCE; |
843 | ||
ab8cd5d9 | 844 | if (!handler->cgroup_ops->mount(handler->cgroup_ops, handler, cg_flags)) |
55022530 | 845 | return log_error_errno(-1, errno, "Failed to mount \"/sys/fs/cgroup\""); |
368bbc02 CS |
846 | } |
847 | ||
0d190408 | 848 | if (flags & LXC_AUTO_SHMOUNTS_MASK) { |
7b371c1e | 849 | ret = add_shmount_to_list(conf); |
55022530 CB |
850 | if (ret < 0) |
851 | return log_error(-1, "Failed to add shmount entry to container config"); | |
0d190408 LT |
852 | } |
853 | ||
368bbc02 | 854 | return 0; |
368bbc02 CS |
855 | } |
856 | ||
4e5440c6 | 857 | static int setup_utsname(struct utsname *utsname) |
0ad19a3f | 858 | { |
0fd73091 CB |
859 | int ret; |
860 | ||
4e5440c6 DL |
861 | if (!utsname) |
862 | return 0; | |
0ad19a3f | 863 | |
0fd73091 | 864 | ret = sethostname(utsname->nodename, strlen(utsname->nodename)); |
55022530 CB |
865 | if (ret < 0) |
866 | return log_error_errno(-1, errno, "Failed to set the hostname to \"%s\"", | |
867 | utsname->nodename); | |
0ad19a3f | 868 | |
0fd73091 | 869 | INFO("Set hostname to \"%s\"", utsname->nodename); |
cd54d859 | 870 | |
0ad19a3f | 871 | return 0; |
872 | } | |
873 | ||
69aa6655 DE |
874 | struct dev_symlinks { |
875 | const char *oldpath; | |
876 | const char *name; | |
877 | }; | |
878 | ||
879 | static const struct dev_symlinks dev_symlinks[] = { | |
0fd73091 CB |
880 | { "/proc/self/fd", "fd" }, |
881 | { "/proc/self/fd/0", "stdin" }, | |
882 | { "/proc/self/fd/1", "stdout" }, | |
883 | { "/proc/self/fd/2", "stderr" }, | |
69aa6655 DE |
884 | }; |
885 | ||
ed8704d0 | 886 | static int lxc_setup_dev_symlinks(const struct lxc_rootfs *rootfs) |
69aa6655 | 887 | { |
79019997 CB |
888 | for (int i = 0; i < sizeof(dev_symlinks) / sizeof(dev_symlinks[0]); i++) { |
889 | int ret; | |
890 | struct stat s; | |
69aa6655 | 891 | const struct dev_symlinks *d = &dev_symlinks[i]; |
0fd73091 | 892 | |
79019997 CB |
893 | /* |
894 | * Stat the path first. If we don't get an error accept it as | |
0fd73091 | 895 | * is and don't try to create it |
09227be2 | 896 | */ |
a5a08920 | 897 | ret = fstatat(rootfs->dfd_dev, d->name, &s, 0); |
0fd73091 | 898 | if (ret == 0) |
09227be2 | 899 | continue; |
09227be2 | 900 | |
a5a08920 | 901 | ret = symlinkat(d->oldpath, rootfs->dfd_dev, d->name); |
79019997 CB |
902 | if (ret) { |
903 | switch (errno) { | |
904 | case EROFS: | |
905 | WARN("Failed to create \"%s\" on read-only filesystem", d->name); | |
906 | __fallthrough; | |
907 | case EEXIST: | |
908 | break; | |
909 | default: | |
910 | return log_error_errno(-errno, errno, "Failed to create \"%s\"", d->name); | |
911 | } | |
69aa6655 DE |
912 | } |
913 | } | |
0fd73091 | 914 | |
69aa6655 DE |
915 | return 0; |
916 | } | |
917 | ||
2187efd3 | 918 | /* Build a space-separate list of ptys to pass to systemd. */ |
885766f5 | 919 | static bool append_ttyname(char **pp, char *name) |
b0a33c1e | 920 | { |
393903d1 | 921 | char *p; |
f1e05b90 | 922 | size_t size; |
393903d1 SH |
923 | |
924 | if (!*pp) { | |
b8e43ef0 | 925 | *pp = zalloc(strlen(name) + strlen("container_ttys=") + 1); |
393903d1 SH |
926 | if (!*pp) |
927 | return false; | |
0fd73091 | 928 | |
393903d1 SH |
929 | sprintf(*pp, "container_ttys=%s", name); |
930 | return true; | |
931 | } | |
0fd73091 | 932 | |
f1e05b90 DJ |
933 | size = strlen(*pp) + strlen(name) + 2; |
934 | p = realloc(*pp, size); | |
393903d1 SH |
935 | if (!p) |
936 | return false; | |
0fd73091 | 937 | |
393903d1 | 938 | *pp = p; |
f1e05b90 DJ |
939 | (void)strlcat(p, " ", size); |
940 | (void)strlcat(p, name, size); | |
0fd73091 | 941 | |
393903d1 SH |
942 | return true; |
943 | } | |
944 | ||
128655e7 PM |
945 | static int open_ttymnt_at(int dfd, const char *path) |
946 | { | |
947 | int fd; | |
948 | ||
949 | fd = open_at(dfd, path, PROTECT_OPEN | O_CREAT | O_EXCL, | |
950 | PROTECT_LOOKUP_BENEATH, 0); | |
951 | if (fd < 0 && (errno == ENXIO || errno == EEXIST)) | |
952 | fd = open_at(dfd, path, PROTECT_OPATH_FILE, | |
953 | PROTECT_LOOKUP_BENEATH, 0); | |
954 | ||
955 | return fd; | |
956 | } | |
957 | ||
2187efd3 | 958 | static int lxc_setup_ttys(struct lxc_conf *conf) |
393903d1 | 959 | { |
7369e6bf CB |
960 | int ret; |
961 | struct lxc_rootfs *rootfs = &conf->rootfs; | |
0e4be3cf | 962 | const struct lxc_tty_info *ttys = &conf->ttys; |
885766f5 | 963 | char *ttydir = ttys->dir; |
6a2ca1b4 | 964 | char tty_source[LXC_PROC_PID_FD_LEN], tty_target[LXC_PROC_PID_FD_LEN]; |
b0a33c1e | 965 | |
e8bd4e43 | 966 | if (!conf->rootfs.path) |
bc9bd0e3 DL |
967 | return 0; |
968 | ||
7369e6bf CB |
969 | for (int i = 0; i < ttys->max; i++) { |
970 | __do_close int fd_to = -EBADF; | |
0e4be3cf | 971 | struct lxc_terminal_info *tty = &ttys->tty[i]; |
b0a33c1e | 972 | |
7c6ef2a2 | 973 | if (ttydir) { |
7369e6bf | 974 | char *tty_name, *tty_path; |
9e1045e3 | 975 | |
9bcde680 | 976 | ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), |
7369e6bf | 977 | "/dev/%s/tty%d", ttydir, i + 1); |
9bcde680 | 978 | if (ret < 0) |
7369e6bf CB |
979 | return ret_errno(-EIO); |
980 | ||
981 | tty_path = &rootfs->buf[STRLITERALLEN("/dev/")]; | |
982 | tty_name = tty_path + strlen(ttydir) + 1; | |
983 | ||
984 | /* create bind-mount target */ | |
128655e7 | 985 | fd_to = open_ttymnt_at(rootfs->dfd_dev, tty_path); |
7369e6bf CB |
986 | if (fd_to < 0) |
987 | return log_error_errno(-errno, errno, | |
988 | "Failed to create tty mount target %d(%s)", | |
989 | rootfs->dfd_dev, tty_path); | |
990 | ||
991 | ret = unlinkat(rootfs->dfd_dev, tty_name, 0); | |
992 | if (ret < 0 && errno != ENOENT) | |
993 | return log_error_errno(-errno, errno, | |
994 | "Failed to unlink %d(%s)", | |
995 | rootfs->dfd_dev, tty_name); | |
996 | ||
de7f9f33 | 997 | if (can_use_mount_api()) { |
7369e6bf CB |
998 | ret = fd_bind_mount(tty->pty, "", |
999 | PROTECT_OPATH_FILE, | |
1000 | PROTECT_LOOKUP_BENEATH_XDEV, | |
1001 | fd_to, "", | |
1002 | PROTECT_OPATH_FILE, | |
1003 | PROTECT_LOOKUP_BENEATH_XDEV, 0, | |
1004 | false); | |
1005 | } else { | |
6a2ca1b4 CB |
1006 | ret = strnprintf(tty_source, sizeof(tty_source), "/proc/self/fd/%d", tty->pty); |
1007 | if (ret < 0) | |
1008 | return ret; | |
1009 | ||
1010 | ret = strnprintf(tty_target, sizeof(tty_target), "/proc/self/fd/%d", fd_to); | |
1011 | if (ret < 0) | |
1012 | return ret; | |
1013 | ||
1014 | ret = mount(tty_source, tty_target, "none", MS_BIND, 0); | |
7c6ef2a2 | 1015 | } |
7369e6bf CB |
1016 | if (ret < 0) |
1017 | return log_error_errno(-errno, errno, | |
1018 | "Failed to bind mount \"%s\" onto \"%s\"", | |
1019 | tty->name, rootfs->buf); | |
1020 | DEBUG("Bind mounted \"%s\" onto \"%s\"", tty->name, rootfs->buf); | |
9e1045e3 | 1021 | |
7369e6bf | 1022 | ret = symlinkat(tty_path, rootfs->dfd_dev, tty_name); |
55022530 | 1023 | if (ret < 0) |
7369e6bf CB |
1024 | return log_error_errno(-errno, errno, |
1025 | "Failed to create symlink \"%d(%s)\" -> \"%d(%s)\"", | |
1026 | rootfs->dfd_dev, tty_name, | |
1027 | rootfs->dfd_dev, tty_path); | |
7c6ef2a2 | 1028 | } else { |
9bcde680 CB |
1029 | ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "tty%d", i + 1); |
1030 | if (ret < 0) | |
7369e6bf CB |
1031 | return ret_errno(-EIO); |
1032 | ||
1033 | /* If we populated /dev, then we need to create /dev/tty<idx>. */ | |
128655e7 | 1034 | fd_to = open_ttymnt_at(rootfs->dfd_dev, rootfs->buf); |
7369e6bf CB |
1035 | if (fd_to < 0) |
1036 | return log_error_errno(-errno, errno, | |
1037 | "Failed to create tty mount target %d(%s)", | |
1038 | rootfs->dfd_dev, rootfs->buf); | |
1039 | ||
de7f9f33 | 1040 | if (can_use_mount_api()) { |
7369e6bf CB |
1041 | ret = fd_bind_mount(tty->pty, "", |
1042 | PROTECT_OPATH_FILE, | |
1043 | PROTECT_LOOKUP_BENEATH_XDEV, | |
1044 | fd_to, "", | |
1045 | PROTECT_OPATH_FILE, | |
1046 | PROTECT_LOOKUP_BENEATH, 0, | |
1047 | false); | |
1048 | } else { | |
6a2ca1b4 CB |
1049 | ret = strnprintf(tty_source, sizeof(tty_source), "/proc/self/fd/%d", tty->pty); |
1050 | if (ret < 0) | |
1051 | return ret; | |
1052 | ||
1053 | ret = strnprintf(tty_target, sizeof(tty_target), "/proc/self/fd/%d", fd_to); | |
9bcde680 | 1054 | if (ret < 0) |
6a2ca1b4 | 1055 | return ret; |
7369e6bf | 1056 | |
6a2ca1b4 | 1057 | ret = mount(tty_source, tty_target, "none", MS_BIND, 0); |
7c6ef2a2 | 1058 | } |
7369e6bf CB |
1059 | if (ret < 0) |
1060 | return log_error_errno(-errno, errno, | |
1061 | "Failed to bind mount \"%s\" onto \"%s\"", | |
1062 | tty->name, rootfs->buf); | |
1063 | DEBUG("Bind mounted \"%s\" onto \"%s\"", tty->name, rootfs->buf); | |
393903d1 | 1064 | } |
9e1045e3 | 1065 | |
55022530 CB |
1066 | if (!append_ttyname(&conf->ttys.tty_names, tty->name)) |
1067 | return log_error(-1, "Error setting up container_ttys string"); | |
b0a33c1e | 1068 | } |
1069 | ||
885766f5 | 1070 | INFO("Finished setting up %zu /dev/tty<N> device(s)", ttys->max); |
b0a33c1e | 1071 | return 0; |
1072 | } | |
1073 | ||
9d0e129b CB |
1074 | define_cleanup_function(struct lxc_tty_info *, lxc_delete_tty); |
1075 | ||
59eac805 | 1076 | static int lxc_allocate_ttys(struct lxc_conf *conf) |
2187efd3 | 1077 | { |
9d0e129b | 1078 | call_cleaner(lxc_delete_tty) struct lxc_tty_info *ttys = &conf->ttys; |
fca23691 | 1079 | int ret; |
2187efd3 CB |
1080 | |
1081 | /* no tty in the configuration */ | |
885766f5 | 1082 | if (ttys->max == 0) |
2187efd3 CB |
1083 | return 0; |
1084 | ||
9d0e129b CB |
1085 | ttys->tty = zalloc(sizeof(struct lxc_terminal_info) * ttys->max); |
1086 | if (!ttys->tty) | |
2187efd3 | 1087 | return -ENOMEM; |
2187efd3 | 1088 | |
7369e6bf | 1089 | for (size_t i = 0; i < conf->ttys.max; i++) { |
d926c261 | 1090 | int pty_nr = -1; |
9d0e129b | 1091 | struct lxc_terminal_info *tty = &ttys->tty[i]; |
2187efd3 | 1092 | |
d926c261 CB |
1093 | ret = lxc_devpts_terminal(conf->devpts_fd, conf, &tty->ptx, |
1094 | &tty->pty, &pty_nr); | |
77a39805 | 1095 | if (ret < 0) { |
7369e6bf | 1096 | conf->ttys.max = i; |
6a2ca1b4 | 1097 | return syserror_set(-ENOTTY, "Failed to create tty %zu", i); |
2187efd3 | 1098 | } |
d926c261 CB |
1099 | DEBUG("Created tty with ptx fd %d and pty fd %d and index %d", |
1100 | tty->ptx, tty->pty, pty_nr); | |
7581d645 | 1101 | tty->busy = -1; |
2187efd3 CB |
1102 | } |
1103 | ||
885766f5 | 1104 | INFO("Finished creating %zu tty devices", ttys->max); |
9d0e129b | 1105 | move_ptr(ttys); |
2187efd3 CB |
1106 | return 0; |
1107 | } | |
1108 | ||
0e4be3cf | 1109 | void lxc_delete_tty(struct lxc_tty_info *ttys) |
2187efd3 | 1110 | { |
386e6768 CB |
1111 | if (!ttys->tty) |
1112 | return; | |
1113 | ||
55022530 | 1114 | for (int i = 0; i < ttys->max; i++) { |
0e4be3cf | 1115 | struct lxc_terminal_info *tty = &ttys->tty[i]; |
36a94ce8 | 1116 | close_prot_errno_disarm(tty->ptx); |
41808e20 | 1117 | close_prot_errno_disarm(tty->pty); |
2187efd3 CB |
1118 | } |
1119 | ||
55022530 | 1120 | free_disarm(ttys->tty); |
2187efd3 CB |
1121 | } |
1122 | ||
1b82d721 | 1123 | static int __lxc_send_ttys_to_parent(struct lxc_handler *handler) |
2187efd3 CB |
1124 | { |
1125 | int i; | |
0fd73091 | 1126 | int ret = -1; |
2187efd3 | 1127 | struct lxc_conf *conf = handler->conf; |
0e4be3cf | 1128 | struct lxc_tty_info *ttys = &conf->ttys; |
2187efd3 | 1129 | int sock = handler->data_sock[0]; |
2187efd3 | 1130 | |
885766f5 | 1131 | if (ttys->max == 0) |
2187efd3 CB |
1132 | return 0; |
1133 | ||
885766f5 | 1134 | for (i = 0; i < ttys->max; i++) { |
2187efd3 | 1135 | int ttyfds[2]; |
0e4be3cf | 1136 | struct lxc_terminal_info *tty = &ttys->tty[i]; |
2187efd3 | 1137 | |
36a94ce8 | 1138 | ttyfds[0] = tty->ptx; |
41808e20 | 1139 | ttyfds[1] = tty->pty; |
2187efd3 CB |
1140 | |
1141 | ret = lxc_abstract_unix_send_fds(sock, ttyfds, 2, NULL, 0); | |
1142 | if (ret < 0) | |
1143 | break; | |
1144 | ||
41808e20 CB |
1145 | TRACE("Sent tty \"%s\" with ptx fd %d and pty fd %d to parent", |
1146 | tty->name, tty->ptx, tty->pty); | |
2187efd3 CB |
1147 | } |
1148 | ||
1149 | if (ret < 0) | |
6d1400b5 | 1150 | SYSERROR("Failed to send %zu ttys to parent", ttys->max); |
2187efd3 | 1151 | else |
885766f5 | 1152 | TRACE("Sent %zu ttys to parent", ttys->max); |
2187efd3 CB |
1153 | |
1154 | return ret; | |
1155 | } | |
1156 | ||
1157 | static int lxc_create_ttys(struct lxc_handler *handler) | |
1158 | { | |
1159 | int ret = -1; | |
1160 | struct lxc_conf *conf = handler->conf; | |
1161 | ||
663014ee | 1162 | ret = lxc_allocate_ttys(conf); |
2187efd3 CB |
1163 | if (ret < 0) { |
1164 | ERROR("Failed to allocate ttys"); | |
1165 | goto on_error; | |
1166 | } | |
1167 | ||
2187efd3 CB |
1168 | if (!conf->is_execute) { |
1169 | ret = lxc_setup_ttys(conf); | |
1170 | if (ret < 0) { | |
1171 | ERROR("Failed to setup ttys"); | |
1172 | goto on_error; | |
1173 | } | |
1174 | } | |
1175 | ||
885766f5 CB |
1176 | if (conf->ttys.tty_names) { |
1177 | ret = setenv("container_ttys", conf->ttys.tty_names, 1); | |
1b82d721 | 1178 | if (ret < 0) { |
885766f5 | 1179 | SYSERROR("Failed to set \"container_ttys=%s\"", conf->ttys.tty_names); |
1b82d721 CB |
1180 | goto on_error; |
1181 | } | |
2187efd3 CB |
1182 | } |
1183 | ||
1b82d721 | 1184 | return 0; |
2187efd3 CB |
1185 | |
1186 | on_error: | |
0e4be3cf | 1187 | lxc_delete_tty(&conf->ttys); |
2187efd3 | 1188 | |
1b82d721 CB |
1189 | return -1; |
1190 | } | |
1191 | ||
111ed96e | 1192 | static int lxc_send_ttys_to_parent(struct lxc_handler *handler) |
1b82d721 CB |
1193 | { |
1194 | int ret = -1; | |
1195 | ||
1196 | ret = __lxc_send_ttys_to_parent(handler); | |
1197 | lxc_delete_tty(&handler->conf->ttys); | |
2187efd3 CB |
1198 | return ret; |
1199 | } | |
1200 | ||
7133b912 CB |
1201 | /* Just create a path for /dev under $lxcpath/$name and in rootfs If we hit an |
1202 | * error, log it but don't fail yet. | |
91c3830e | 1203 | */ |
7133b912 | 1204 | static int mount_autodev(const char *name, const struct lxc_rootfs *rootfs, |
63012bdd | 1205 | int autodevtmpfssize, const char *lxcpath) |
91c3830e | 1206 | { |
bfbfeedf | 1207 | __do_close int fd_fs = -EBADF; |
ee8eeba8 | 1208 | const char *path = rootfs->path ? rootfs->mount : NULL; |
bfbfeedf | 1209 | size_t tmpfs_size = (autodevtmpfssize != 0) ? autodevtmpfssize : 500000; |
91c3830e | 1210 | int ret; |
87e0e273 | 1211 | mode_t cur_mask; |
63012bdd | 1212 | char mount_options[128]; |
91c3830e | 1213 | |
7133b912 | 1214 | INFO("Preparing \"/dev\""); |
bc6928ff | 1215 | |
87e0e273 | 1216 | cur_mask = umask(S_IXUSR | S_IXGRP | S_IXOTH); |
ea57e424 | 1217 | ret = mkdirat(rootfs->dfd_mnt, "dev" , S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
87e0e273 CB |
1218 | if (ret < 0 && errno != EEXIST) { |
1219 | SYSERROR("Failed to create \"/dev\" directory"); | |
1220 | ret = -errno; | |
1221 | goto reset_umask; | |
bc6928ff | 1222 | } |
87da4ec3 | 1223 | |
de7f9f33 | 1224 | if (can_use_mount_api()) { |
635e7bac CB |
1225 | fd_fs = fs_prepare("tmpfs", -EBADF, "", 0, 0); |
1226 | if (fd_fs < 0) | |
1227 | return log_error_errno(-errno, errno, "Failed to prepare filesystem context for tmpfs"); | |
ee8eeba8 | 1228 | |
bfbfeedf CB |
1229 | sprintf(mount_options, "%zu", tmpfs_size); |
1230 | ||
1231 | ret = fs_set_property(fd_fs, "mode", "0755"); | |
1232 | if (ret < 0) | |
1233 | return log_error_errno(-errno, errno, "Failed to mount tmpfs onto %d(dev)", fd_fs); | |
1234 | ||
1235 | ret = fs_set_property(fd_fs, "size", mount_options); | |
1236 | if (ret < 0) | |
1237 | return log_error_errno(-errno, errno, "Failed to mount tmpfs onto %d(dev)", fd_fs); | |
1238 | ||
89606dfb CB |
1239 | ret = fs_attach(fd_fs, rootfs->dfd_mnt, "dev", |
1240 | PROTECT_OPATH_DIRECTORY, | |
1241 | PROTECT_LOOKUP_BENEATH_XDEV, 0); | |
635e7bac CB |
1242 | } else { |
1243 | __do_free char *fallback_path = NULL; | |
1244 | ||
1245 | sprintf(mount_options, "size=%zu,mode=755", tmpfs_size); | |
1246 | DEBUG("Using mount options: %s", mount_options); | |
1247 | ||
1248 | if (path) { | |
1249 | fallback_path = must_make_path(path, "/dev", NULL); | |
1250 | ret = safe_mount("none", fallback_path, "tmpfs", 0, mount_options, path); | |
1251 | } else { | |
1252 | ret = safe_mount("none", "dev", "tmpfs", 0, mount_options, NULL); | |
1253 | } | |
87e0e273 | 1254 | } |
bfbfeedf CB |
1255 | if (ret < 0) { |
1256 | SYSERROR("Failed to mount tmpfs on \"%s\"", path); | |
1257 | goto reset_umask; | |
1258 | } | |
1259 | ||
7133b912 | 1260 | /* If we are running on a devtmpfs mapping, dev/pts may already exist. |
bc6928ff MW |
1261 | * If not, then create it and exit if that fails... |
1262 | */ | |
ea57e424 | 1263 | ret = mkdirat(rootfs->dfd_mnt, "dev/pts", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
87e0e273 | 1264 | if (ret < 0 && errno != EEXIST) { |
bfbfeedf | 1265 | SYSERROR("Failed to create directory \"dev/pts\""); |
87e0e273 CB |
1266 | ret = -errno; |
1267 | goto reset_umask; | |
91c3830e SH |
1268 | } |
1269 | ||
87e0e273 CB |
1270 | ret = 0; |
1271 | ||
1272 | reset_umask: | |
1273 | (void)umask(cur_mask); | |
1274 | ||
7133b912 | 1275 | INFO("Prepared \"/dev\""); |
87e0e273 | 1276 | return ret; |
91c3830e SH |
1277 | } |
1278 | ||
5e73416f | 1279 | struct lxc_device_node { |
74a3920a | 1280 | const char *name; |
5e73416f CB |
1281 | const mode_t mode; |
1282 | const int maj; | |
1283 | const int min; | |
c6883f38 SH |
1284 | }; |
1285 | ||
5e73416f | 1286 | static const struct lxc_device_node lxc_devices[] = { |
06749971 | 1287 | { "full", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 7 }, |
5e73416f | 1288 | { "null", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 3 }, |
06749971 CB |
1289 | { "random", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 8 }, |
1290 | { "tty", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 5, 0 }, | |
5e73416f CB |
1291 | { "urandom", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 9 }, |
1292 | { "zero", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 5 }, | |
c6883f38 SH |
1293 | }; |
1294 | ||
5067e4dd CB |
1295 | |
1296 | enum { | |
1297 | LXC_DEVNODE_BIND, | |
1298 | LXC_DEVNODE_MKNOD, | |
1299 | LXC_DEVNODE_PARTIAL, | |
1300 | LXC_DEVNODE_OPEN, | |
1301 | }; | |
1302 | ||
887ae844 | 1303 | static int lxc_fill_autodev(struct lxc_rootfs *rootfs) |
c6883f38 | 1304 | { |
5e73416f | 1305 | int i, ret; |
3a32201c | 1306 | mode_t cmask; |
5067e4dd | 1307 | int use_mknod = LXC_DEVNODE_MKNOD; |
c6883f38 | 1308 | |
a5a08920 | 1309 | if (rootfs->dfd_dev < 0) |
81498328 | 1310 | return log_info(0, "No /dev directory found, skipping setup"); |
d43d5191 | 1311 | |
3999be0a CB |
1312 | INFO("Populating \"/dev\""); |
1313 | ||
3a32201c | 1314 | cmask = umask(S_IXUSR | S_IXGRP | S_IXOTH); |
5e73416f | 1315 | for (i = 0; i < sizeof(lxc_devices) / sizeof(lxc_devices[0]); i++) { |
5e73416f | 1316 | const struct lxc_device_node *device = &lxc_devices[i]; |
0728ebf4 | 1317 | |
5067e4dd | 1318 | if (use_mknod >= LXC_DEVNODE_MKNOD) { |
a5a08920 | 1319 | ret = mknodat(rootfs->dfd_dev, device->name, device->mode, makedev(device->maj, device->min)); |
5e73416f | 1320 | if (ret == 0 || (ret < 0 && errno == EEXIST)) { |
d43d5191 | 1321 | DEBUG("Created device node \"%s\"", device->name); |
5067e4dd | 1322 | } else if (ret < 0) { |
55022530 | 1323 | if (errno != EPERM) |
d43d5191 | 1324 | return log_error_errno(-1, errno, "Failed to create device node \"%s\"", device->name); |
0bbf8572 | 1325 | |
5067e4dd | 1326 | use_mknod = LXC_DEVNODE_BIND; |
9cb4d183 | 1327 | } |
3999be0a | 1328 | |
5067e4dd CB |
1329 | /* Device nodes are fully useable. */ |
1330 | if (use_mknod == LXC_DEVNODE_OPEN) | |
1331 | continue; | |
1332 | ||
1333 | if (use_mknod == LXC_DEVNODE_MKNOD) { | |
d43d5191 | 1334 | __do_close int fd = -EBADF; |
5067e4dd CB |
1335 | /* See |
1336 | * - https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=55956b59df336f6738da916dbb520b6e37df9fbd | |
1337 | * - https://lists.linuxfoundation.org/pipermail/containers/2018-June/039176.html | |
1338 | */ | |
a5a08920 | 1339 | fd = open_at(rootfs->dfd_dev, device->name, PROTECT_OPEN, PROTECT_LOOKUP_BENEATH, 0); |
d43d5191 | 1340 | if (fd >= 0) { |
5067e4dd CB |
1341 | /* Device nodes are fully useable. */ |
1342 | use_mknod = LXC_DEVNODE_OPEN; | |
1343 | continue; | |
1344 | } | |
1345 | ||
d43d5191 | 1346 | SYSTRACE("Failed to open \"%s\" device", device->name); |
5067e4dd CB |
1347 | /* Device nodes are only partially useable. */ |
1348 | use_mknod = LXC_DEVNODE_PARTIAL; | |
1349 | } | |
5e73416f CB |
1350 | } |
1351 | ||
5067e4dd CB |
1352 | if (use_mknod != LXC_DEVNODE_PARTIAL) { |
1353 | /* If we are dealing with partially functional device | |
1354 | * nodes the prio mknod() call will have created the | |
1355 | * device node so we can use it as a bind-mount target. | |
1356 | */ | |
a5a08920 | 1357 | ret = mknodat(rootfs->dfd_dev, device->name, S_IFREG | 0000, 0); |
55022530 | 1358 | if (ret < 0 && errno != EEXIST) |
d43d5191 | 1359 | return log_error_errno(-1, errno, "Failed to create file \"%s\"", device->name); |
5e73416f CB |
1360 | } |
1361 | ||
1362 | /* Fallback to bind-mounting the device from the host. */ | |
9bcde680 CB |
1363 | ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "dev/%s", device->name); |
1364 | if (ret < 0) | |
b41ff502 | 1365 | return ret_errno(EIO); |
5e73416f | 1366 | |
de7f9f33 | 1367 | if (can_use_mount_api()) { |
887ae844 | 1368 | ret = fd_bind_mount(rootfs->dfd_host, rootfs->buf, |
635e7bac CB |
1369 | PROTECT_OPATH_FILE, |
1370 | PROTECT_LOOKUP_BENEATH_XDEV, | |
1371 | rootfs->dfd_dev, device->name, | |
1372 | PROTECT_OPATH_FILE, | |
1373 | PROTECT_LOOKUP_BENEATH, 0, false); | |
1374 | } else { | |
927ea337 CB |
1375 | char path[PATH_MAX]; |
1376 | ||
9bcde680 CB |
1377 | ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "/dev/%s", device->name); |
1378 | if (ret < 0) | |
927ea337 CB |
1379 | return ret_errno(EIO); |
1380 | ||
9bcde680 CB |
1381 | ret = strnprintf(path, sizeof(path), "%s/dev/%s", get_rootfs_mnt(rootfs), device->name); |
1382 | if (ret < 0) | |
927ea337 CB |
1383 | return log_error(-1, "Failed to create device path for %s", device->name); |
1384 | ||
887ae844 | 1385 | ret = safe_mount(rootfs->buf, path, 0, MS_BIND, NULL, get_rootfs_mnt(rootfs)); |
927ea337 | 1386 | if (ret < 0) |
887ae844 | 1387 | return log_error_errno(-1, errno, "Failed to bind mount host device node \"%s\" to \"%s\"", rootfs->buf, path); |
927ea337 | 1388 | |
887ae844 | 1389 | DEBUG("Bind mounted host device node \"%s\" to \"%s\"", rootfs->buf, path); |
927ea337 | 1390 | continue; |
d43d5191 | 1391 | } |
887ae844 | 1392 | DEBUG("Bind mounted host device %d(%s) to %d(%s)", rootfs->dfd_host, rootfs->buf, rootfs->dfd_dev, device->name); |
c6883f38 | 1393 | } |
5e73416f | 1394 | (void)umask(cmask); |
c6883f38 | 1395 | |
3999be0a | 1396 | INFO("Populated \"/dev\""); |
c6883f38 SH |
1397 | return 0; |
1398 | } | |
1399 | ||
4e86cad3 | 1400 | static int lxc_mount_rootfs(struct lxc_rootfs *rootfs) |
0ad19a3f | 1401 | { |
9aa76a17 | 1402 | int ret; |
cc28d0b0 | 1403 | |
a0f379bf | 1404 | if (!rootfs->path) { |
0fd73091 | 1405 | ret = mount("", "/", NULL, MS_SLAVE | MS_REC, 0); |
55022530 | 1406 | if (ret < 0) |
9e61fb1f | 1407 | return log_error_errno(-1, errno, "Failed to recursively turn root mount tree into dependent mount"); |
0fd73091 | 1408 | |
ea57e424 CB |
1409 | rootfs->dfd_mnt = open_at(-EBADF, "/", PROTECT_OPATH_DIRECTORY, PROTECT_LOOKUP_ABSOLUTE, 0); |
1410 | if (rootfs->dfd_mnt < 0) | |
31f8b2fd CB |
1411 | return -errno; |
1412 | ||
c119f018 | 1413 | return log_trace(0, "Container doesn't use separate rootfs. Opened host's rootfs"); |
a0f379bf | 1414 | } |
0ad19a3f | 1415 | |
0fd73091 | 1416 | ret = access(rootfs->mount, F_OK); |
55022530 CB |
1417 | if (ret != 0) |
1418 | return log_error_errno(-1, errno, "Failed to access to \"%s\". Check it is present", | |
1419 | rootfs->mount); | |
b1789442 | 1420 | |
4e86cad3 | 1421 | ret = rootfs->storage->ops->mount(rootfs->storage); |
55022530 CB |
1422 | if (ret < 0) |
1423 | return log_error(-1, "Failed to mount rootfs \"%s\" onto \"%s\" with options \"%s\"", | |
1424 | rootfs->path, rootfs->mount, | |
1425 | rootfs->options ? rootfs->options : "(null)"); | |
0ad19a3f | 1426 | |
0fd73091 | 1427 | DEBUG("Mounted rootfs \"%s\" onto \"%s\" with options \"%s\"", |
91c3e281 CB |
1428 | rootfs->path, rootfs->mount, |
1429 | rootfs->options ? rootfs->options : "(null)"); | |
9aa76a17 | 1430 | |
ea57e424 CB |
1431 | rootfs->dfd_mnt = open_at(-EBADF, rootfs->mount, PROTECT_OPATH_DIRECTORY, PROTECT_LOOKUP_ABSOLUTE_XDEV, 0); |
1432 | if (rootfs->dfd_mnt < 0) | |
31f8b2fd CB |
1433 | return -errno; |
1434 | ||
c119f018 | 1435 | return log_trace(0, "Container uses separate rootfs. Opened container's rootfs"); |
ac778708 DL |
1436 | } |
1437 | ||
59eac805 | 1438 | static int lxc_chroot(const struct lxc_rootfs *rootfs) |
91e93c71 | 1439 | { |
b8d88764 | 1440 | __do_free char *nroot = NULL; |
0fd73091 | 1441 | int i, ret; |
8ce1abc2 | 1442 | char *root = rootfs->mount; |
91e93c71 | 1443 | |
74e7b662 | 1444 | nroot = realpath(root, NULL); |
55022530 CB |
1445 | if (!nroot) |
1446 | return log_error_errno(-1, errno, "Failed to resolve \"%s\"", root); | |
91e93c71 | 1447 | |
0fd73091 | 1448 | ret = chdir("/"); |
b8d88764 | 1449 | if (ret < 0) |
0fd73091 | 1450 | return -1; |
91e93c71 | 1451 | |
0fd73091 CB |
1452 | /* We could use here MS_MOVE, but in userns this mount is locked and |
1453 | * can't be moved. | |
91e93c71 | 1454 | */ |
8ce1abc2 | 1455 | ret = mount(nroot, "/", NULL, MS_REC | MS_BIND, NULL); |
55022530 CB |
1456 | if (ret < 0) |
1457 | return log_error_errno(-1, errno, "Failed to mount \"%s\" onto \"/\" as MS_REC | MS_BIND", nroot); | |
91e93c71 | 1458 | |
0fd73091 | 1459 | ret = mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL); |
55022530 CB |
1460 | if (ret < 0) |
1461 | return log_error_errno(-1, errno, "Failed to remount \"/\""); | |
91e93c71 | 1462 | |
aa899945 | 1463 | /* The following code cleans up inherited mounts which are not required |
0fd73091 | 1464 | * for CT. |
91e93c71 AV |
1465 | * |
1466 | * The mountinfo file shows not all mounts, if a few points have been | |
1467 | * unmounted between read operations from the mountinfo. So we need to | |
1468 | * read mountinfo a few times. | |
1469 | * | |
7ded5fa7 | 1470 | * This loop can be skipped if a container uses userns, because all |
91e93c71 AV |
1471 | * inherited mounts are locked and we should live with all this trash. |
1472 | */ | |
0fd73091 | 1473 | for (;;) { |
4fdd1f72 | 1474 | __do_fclose FILE *f = NULL; |
f3d38164 CB |
1475 | __do_free char *line = NULL; |
1476 | char *slider1, *slider2; | |
91e93c71 | 1477 | int progress = 0; |
f3d38164 | 1478 | size_t len = 0; |
91e93c71 | 1479 | |
4110345b | 1480 | f = fopen("./proc/self/mountinfo", "re"); |
55022530 CB |
1481 | if (!f) |
1482 | return log_error_errno(-1, errno, "Failed to open \"/proc/self/mountinfo\""); | |
0fd73091 | 1483 | |
f3d38164 CB |
1484 | while (getline(&line, &len, f) > 0) { |
1485 | for (slider1 = line, i = 0; slider1 && i < 4; i++) | |
1486 | slider1 = strchr(slider1 + 1, ' '); | |
0fd73091 | 1487 | |
f3d38164 | 1488 | if (!slider1) |
91e93c71 | 1489 | continue; |
0fd73091 | 1490 | |
f3d38164 CB |
1491 | slider2 = strchr(slider1 + 1, ' '); |
1492 | if (!slider2) | |
91e93c71 AV |
1493 | continue; |
1494 | ||
f3d38164 CB |
1495 | *slider2 = '\0'; |
1496 | *slider1 = '.'; | |
91e93c71 | 1497 | |
71528742 | 1498 | if (strequal(slider1 + 1, "/")) |
91e93c71 | 1499 | continue; |
0fd73091 | 1500 | |
71528742 | 1501 | if (strequal(slider1 + 1, "/proc")) |
91e93c71 AV |
1502 | continue; |
1503 | ||
f3d38164 | 1504 | ret = umount2(slider1, MNT_DETACH); |
0fd73091 | 1505 | if (ret == 0) |
91e93c71 AV |
1506 | progress++; |
1507 | } | |
0fd73091 | 1508 | |
91e93c71 AV |
1509 | if (!progress) |
1510 | break; | |
1511 | } | |
1512 | ||
7ded5fa7 | 1513 | /* This also can be skipped if a container uses userns. */ |
0fd73091 | 1514 | (void)umount2("./proc", MNT_DETACH); |
91e93c71 AV |
1515 | |
1516 | /* It is weird, but chdir("..") moves us in a new root */ | |
0fd73091 | 1517 | ret = chdir(".."); |
55022530 CB |
1518 | if (ret < 0) |
1519 | return log_error_errno(-1, errno, "Failed to chdir(\"..\")"); | |
91e93c71 | 1520 | |
0fd73091 | 1521 | ret = chroot("."); |
55022530 CB |
1522 | if (ret < 0) |
1523 | return log_error_errno(-1, errno, "Failed to chroot(\".\")"); | |
91e93c71 AV |
1524 | |
1525 | return 0; | |
1526 | } | |
1527 | ||
8ce1abc2 CB |
1528 | /* (The following explanation is copied verbatim from the kernel.) |
1529 | * | |
1530 | * pivot_root Semantics: | |
1531 | * Moves the root file system of the current process to the directory put_old, | |
1532 | * makes new_root as the new root file system of the current process, and sets | |
1533 | * root/cwd of all processes which had them on the current root to new_root. | |
1534 | * | |
1535 | * Restrictions: | |
1536 | * The new_root and put_old must be directories, and must not be on the | |
1537 | * same file system as the current process root. The put_old must be | |
1538 | * underneath new_root, i.e. adding a non-zero number of /.. to the string | |
1539 | * pointed to by put_old must yield the same directory as new_root. No other | |
1540 | * file system may be mounted on put_old. After all, new_root is a mountpoint. | |
1541 | * | |
1542 | * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. | |
1543 | * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives | |
1544 | * in this situation. | |
1545 | * | |
1546 | * Notes: | |
1547 | * - we don't move root/cwd if they are not at the root (reason: if something | |
1548 | * cared enough to change them, it's probably wrong to force them elsewhere) | |
1549 | * - it's okay to pick a root that isn't the root of a file system, e.g. | |
1550 | * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, | |
1551 | * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root | |
1552 | * first. | |
1553 | */ | |
7f50ec8b | 1554 | static int lxc_pivot_root(const struct lxc_rootfs *rootfs) |
ac778708 | 1555 | { |
7f50ec8b | 1556 | __do_close int fd_oldroot = -EBADF; |
b0d7aac4 | 1557 | int ret; |
0fd73091 | 1558 | |
7f50ec8b CB |
1559 | fd_oldroot = open_at(-EBADF, "/", PROTECT_OPATH_DIRECTORY, PROTECT_LOOKUP_ABSOLUTE, 0); |
1560 | if (fd_oldroot < 0) | |
55022530 | 1561 | return log_error_errno(-1, errno, "Failed to open old root directory"); |
ac778708 | 1562 | |
8ce1abc2 | 1563 | /* change into new root fs */ |
ea57e424 | 1564 | ret = fchdir(rootfs->dfd_mnt); |
55022530 | 1565 | if (ret < 0) |
7f50ec8b | 1566 | return log_error_errno(-errno, errno, "Failed to change into new root directory \"%s\"", rootfs->mount); |
39c7b795 | 1567 | |
8ce1abc2 CB |
1568 | /* pivot_root into our new root fs */ |
1569 | ret = pivot_root(".", "."); | |
55022530 | 1570 | if (ret < 0) |
7f50ec8b | 1571 | return log_error_errno(-errno, errno, "Failed to pivot into new root directory \"%s\"", rootfs->mount); |
39c7b795 | 1572 | |
8ce1abc2 CB |
1573 | /* At this point the old-root is mounted on top of our new-root. To |
1574 | * unmounted it we must not be chdir'd into it, so escape back to | |
1575 | * old-root. | |
1576 | */ | |
7f50ec8b | 1577 | ret = fchdir(fd_oldroot); |
55022530 | 1578 | if (ret < 0) |
7f50ec8b | 1579 | return log_error_errno(-errno, errno, "Failed to enter old root directory"); |
c69bd12f | 1580 | |
7f50ec8b CB |
1581 | /* |
1582 | * Make fd_oldroot a depedent mount to make sure our umounts don't | |
1583 | * propagate to the host. | |
8ce1abc2 CB |
1584 | */ |
1585 | ret = mount("", ".", "", MS_SLAVE | MS_REC, NULL); | |
55022530 | 1586 | if (ret < 0) |
7f50ec8b | 1587 | return log_error_errno(-errno, errno, "Failed to recursively turn old root mount tree into dependent mount"); |
8ce1abc2 CB |
1588 | |
1589 | ret = umount2(".", MNT_DETACH); | |
55022530 | 1590 | if (ret < 0) |
7f50ec8b | 1591 | return log_error_errno(-errno, errno, "Failed to detach old root directory"); |
8ce1abc2 | 1592 | |
ea57e424 | 1593 | ret = fchdir(rootfs->dfd_mnt); |
55022530 | 1594 | if (ret < 0) |
7f50ec8b | 1595 | return log_error_errno(-errno, errno, "Failed to re-enter new root directory \"%s\"", rootfs->mount); |
8ce1abc2 | 1596 | |
7f50ec8b | 1597 | TRACE("Changed into new rootfs \"%s\"", rootfs->mount); |
b0d7aac4 | 1598 | return 0; |
0ad19a3f | 1599 | } |
1600 | ||
8ce1abc2 CB |
1601 | static int lxc_setup_rootfs_switch_root(const struct lxc_rootfs *rootfs) |
1602 | { | |
55022530 CB |
1603 | if (!rootfs->path) |
1604 | return log_debug(0, "Container does not have a rootfs"); | |
8ce1abc2 CB |
1605 | |
1606 | if (detect_ramfs_rootfs()) | |
1607 | return lxc_chroot(rootfs); | |
1608 | ||
7f50ec8b | 1609 | return lxc_pivot_root(rootfs); |
0ad19a3f | 1610 | } |
1611 | ||
7581a82f | 1612 | static const struct id_map *find_mapped_nsid_entry(const struct lxc_conf *conf, |
8ce1abc2 CB |
1613 | unsigned id, |
1614 | enum idtype idtype) | |
f4900711 CB |
1615 | { |
1616 | struct lxc_list *it; | |
1617 | struct id_map *map; | |
1618 | struct id_map *retmap = NULL; | |
1619 | ||
dcf0ffdf CB |
1620 | /* Shortcut for container's root mappings. */ |
1621 | if (id == 0) { | |
1622 | if (idtype == ID_TYPE_UID) | |
1623 | return conf->root_nsuid_map; | |
1624 | ||
1625 | if (idtype == ID_TYPE_GID) | |
1626 | return conf->root_nsgid_map; | |
1627 | } | |
1628 | ||
f4900711 CB |
1629 | lxc_list_for_each(it, &conf->id_map) { |
1630 | map = it->elem; | |
1631 | if (map->idtype != idtype) | |
1632 | continue; | |
1633 | ||
1634 | if (id >= map->nsid && id < map->nsid + map->range) { | |
1635 | retmap = map; | |
1636 | break; | |
1637 | } | |
1638 | } | |
1639 | ||
1640 | return retmap; | |
1641 | } | |
1642 | ||
42c0d056 | 1643 | static int lxc_recv_devpts_from_child(struct lxc_handler *handler) |
68f3899e CB |
1644 | { |
1645 | int ret; | |
1646 | ||
1647 | if (handler->conf->pty_max <= 0) | |
1648 | return 0; | |
1649 | ||
d17c815d CB |
1650 | ret = lxc_abstract_unix_recv_one_fd(handler->data_sock[1], |
1651 | &handler->conf->devpts_fd, | |
1652 | &handler->conf->devpts_fd, | |
1653 | sizeof(handler->conf->devpts_fd)); | |
68f3899e CB |
1654 | if (ret < 0) |
1655 | return log_error_errno(-1, errno, "Failed to receive devpts fd from child"); | |
1656 | ||
1657 | TRACE("Received devpts file descriptor %d from child", handler->conf->devpts_fd); | |
1658 | return 0; | |
1659 | } | |
1660 | ||
96a980e1 | 1661 | static int lxc_setup_devpts_child(struct lxc_handler *handler) |
289b707b | 1662 | { |
96a980e1 | 1663 | __do_close int devpts_fd = -EBADF, fd_fs = -EBADF; |
289b707b | 1664 | struct lxc_conf *conf = handler->conf; |
f30fc74d | 1665 | struct lxc_rootfs *rootfs = &conf->rootfs; |
289b707b CB |
1666 | int ret; |
1667 | ||
289b707b CB |
1668 | if (conf->pty_max <= 0) |
1669 | return log_debug(0, "No new devpts instance will be mounted since no pts devices are requested"); | |
1670 | ||
f30fc74d CB |
1671 | ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), |
1672 | "/proc/self/fd/%d/pts", rootfs->dfd_dev); | |
1673 | if (ret < 0) | |
1674 | return syserror("Failed to create path"); | |
1675 | ||
1676 | (void)umount2(rootfs->buf, MNT_DETACH); | |
1677 | ||
1678 | /* Create mountpoint for devpts instance. */ | |
1679 | ret = mkdirat(rootfs->dfd_dev, "pts", 0755); | |
1680 | if (ret < 0 && errno != EEXIST) | |
1681 | return log_error_errno(-1, errno, "Failed to create \"/dev/pts\" directory"); | |
1682 | ||
96a980e1 CB |
1683 | if (can_use_mount_api()) { |
1684 | fd_fs = fs_prepare("devpts", -EBADF, "", 0, 0); | |
1685 | if (fd_fs < 0) | |
1686 | return syserror("Failed to prepare filesystem context for devpts"); | |
289b707b | 1687 | |
96a980e1 CB |
1688 | ret = fs_set_property(fd_fs, "source", "devpts"); |
1689 | if (ret < 0) | |
1690 | SYSTRACE("Failed to set \"source=devpts\" on devpts filesystem context %d", fd_fs); | |
289b707b | 1691 | |
96a980e1 CB |
1692 | ret = fs_set_property(fd_fs, "gid", "5"); |
1693 | if (ret < 0) | |
1694 | SYSTRACE("Failed to set \"gid=5\" on devpts filesystem context %d", fd_fs); | |
289b707b | 1695 | |
96a980e1 CB |
1696 | ret = fs_set_flag(fd_fs, "newinstance"); |
1697 | if (ret < 0) | |
1698 | return syserror("Failed to set \"newinstance\" property on devpts filesystem context %d", fd_fs); | |
289b707b | 1699 | |
96a980e1 CB |
1700 | ret = fs_set_property(fd_fs, "ptmxmode", "0666"); |
1701 | if (ret < 0) | |
1702 | return syserror("Failed to set \"ptmxmode=0666\" property on devpts filesystem context %d", fd_fs); | |
289b707b | 1703 | |
96a980e1 CB |
1704 | ret = fs_set_property(fd_fs, "mode", "0620"); |
1705 | if (ret < 0) | |
1706 | return syserror("Failed to set \"mode=0620\" property on devpts filesystem context %d", fd_fs); | |
289b707b | 1707 | |
96a980e1 CB |
1708 | ret = fs_set_property(fd_fs, "max", fdstr(conf->pty_max)); |
1709 | if (ret < 0) | |
1710 | return syserror("Failed to set \"max=%zu\" property on devpts filesystem context %d", conf->pty_max, fd_fs); | |
f30fc74d | 1711 | |
96a980e1 CB |
1712 | ret = fsconfig(fd_fs, FSCONFIG_CMD_CREATE, NULL, NULL, 0); |
1713 | if (ret < 0) | |
1714 | return syserror("Failed to finalize filesystem context %d", fd_fs); | |
289b707b | 1715 | |
96a980e1 CB |
1716 | devpts_fd = fsmount(fd_fs, FSMOUNT_CLOEXEC, MOUNT_ATTR_NOSUID | MOUNT_ATTR_NOEXEC); |
1717 | if (devpts_fd < 0) | |
1718 | return syserror("Failed to create new mount for filesystem context %d", fd_fs); | |
1719 | TRACE("Created detached devpts mount %d", devpts_fd); | |
77890c6d | 1720 | |
96a980e1 CB |
1721 | ret = move_mount(devpts_fd, "", rootfs->dfd_dev, "pts", MOVE_MOUNT_F_EMPTY_PATH); |
1722 | if (ret) | |
1723 | return syserror("Failed to attach devpts mount %d to %d/pts", conf->devpts_fd, rootfs->dfd_dev); | |
3c26f34e | 1724 | |
96a980e1 CB |
1725 | DEBUG("Attached detached devpts mount %d to %d/pts", devpts_fd, rootfs->dfd_dev); |
1726 | } else { | |
1727 | char **opts; | |
1728 | char devpts_mntopts[256]; | |
1729 | char *mntopt_sets[5]; | |
1730 | char default_devpts_mntopts[256] = "gid=5,newinstance,ptmxmode=0666,mode=0620"; | |
7e40254a | 1731 | |
f30fc74d CB |
1732 | /* |
1733 | * Fallback codepath in case the new mount API can't be used to | |
1734 | * create detached mounts. | |
1735 | */ | |
1736 | ||
289b707b CB |
1737 | ret = strnprintf(devpts_mntopts, sizeof(devpts_mntopts), "%s,max=%zu", |
1738 | default_devpts_mntopts, conf->pty_max); | |
1739 | if (ret < 0) | |
1740 | return -1; | |
3c26f34e | 1741 | |
289b707b CB |
1742 | /* Create mountpoint for devpts instance. */ |
1743 | ret = mkdirat(rootfs->dfd_dev, "pts", 0755); | |
1744 | if (ret < 0 && errno != EEXIST) | |
1745 | return log_error_errno(-1, errno, "Failed to create \"/dev/pts\" directory"); | |
ce155c60 | 1746 | |
289b707b CB |
1747 | /* gid=5 && max= */ |
1748 | mntopt_sets[0] = devpts_mntopts; | |
ce155c60 | 1749 | |
289b707b CB |
1750 | /* !gid=5 && max= */ |
1751 | mntopt_sets[1] = devpts_mntopts + STRLITERALLEN("gid=5") + 1; | |
ce155c60 | 1752 | |
289b707b CB |
1753 | /* gid=5 && !max= */ |
1754 | mntopt_sets[2] = default_devpts_mntopts; | |
ce155c60 | 1755 | |
289b707b CB |
1756 | /* !gid=5 && !max= */ |
1757 | mntopt_sets[3] = default_devpts_mntopts + STRLITERALLEN("gid=5") + 1; | |
ce155c60 | 1758 | |
289b707b CB |
1759 | /* end */ |
1760 | mntopt_sets[4] = NULL; | |
70761e5e | 1761 | |
289b707b CB |
1762 | for (ret = -1, opts = mntopt_sets; opts && *opts; opts++) { |
1763 | /* mount new devpts instance */ | |
c146c54e | 1764 | ret = mount_beneath_fd(rootfs->dfd_dev, "", "pts", "devpts", MS_NOSUID | MS_NOEXEC, *opts); |
289b707b CB |
1765 | if (ret == 0) |
1766 | break; | |
1767 | } | |
289b707b CB |
1768 | if (ret < 0) |
1769 | return log_error_errno(-1, errno, "Failed to mount new devpts instance"); | |
1770 | ||
1771 | devpts_fd = open_at(rootfs->dfd_dev, "pts", PROTECT_OPATH_DIRECTORY, PROTECT_LOOKUP_BENEATH_XDEV, 0); | |
1772 | if (devpts_fd < 0) { | |
1773 | devpts_fd = -EBADF; | |
1774 | TRACE("Failed to create detached devpts mount"); | |
1775 | } | |
1776 | ||
289b707b CB |
1777 | DEBUG("Mounted new devpts instance with options \"%s\"", *opts); |
1778 | } | |
96a980e1 | 1779 | handler->conf->devpts_fd = move_fd(devpts_fd); |
f797f05e | 1780 | |
d5cb35d6 | 1781 | /* Remove any pre-existing /dev/ptmx file. */ |
a5a08920 | 1782 | ret = unlinkat(rootfs->dfd_dev, "ptmx", 0); |
b29e05d6 | 1783 | if (ret < 0) { |
55022530 CB |
1784 | if (errno != ENOENT) |
1785 | return log_error_errno(-1, errno, "Failed to remove existing \"/dev/ptmx\" file"); | |
b29e05d6 | 1786 | } else { |
0fd73091 | 1787 | DEBUG("Removed existing \"/dev/ptmx\" file"); |
3c26f34e | 1788 | } |
1789 | ||
8de0119d | 1790 | /* Create placeholder /dev/ptmx file as bind mountpoint for /dev/pts/ptmx. */ |
a5a08920 | 1791 | ret = mknodat(rootfs->dfd_dev, "ptmx", S_IFREG | 0000, 0); |
55022530 | 1792 | if (ret < 0 && errno != EEXIST) |
8de0119d CB |
1793 | return log_error_errno(-1, errno, "Failed to create \"/dev/ptmx\" file as bind mount target"); |
1794 | DEBUG("Created \"/dev/ptmx\" file as bind mount target"); | |
77890c6d | 1795 | |
d27ae999 | 1796 | /* Main option: use a bind-mount to please AppArmor */ |
c146c54e | 1797 | ret = mount_beneath_fd(rootfs->dfd_dev, "pts/ptmx", "ptmx", NULL, MS_BIND, NULL); |
55022530 CB |
1798 | if (!ret) |
1799 | return log_debug(0, "Bind mounted \"/dev/pts/ptmx\" to \"/dev/ptmx\""); | |
1800 | else | |
d5cb35d6 | 1801 | /* Fallthrough and try to create a symlink. */ |
0fd73091 | 1802 | ERROR("Failed to bind mount \"/dev/pts/ptmx\" to \"/dev/ptmx\""); |
d5cb35d6 | 1803 | |
8de0119d | 1804 | /* Remove the placeholder /dev/ptmx file we created above. */ |
a5a08920 | 1805 | ret = unlinkat(rootfs->dfd_dev, "ptmx", 0); |
55022530 CB |
1806 | if (ret < 0) |
1807 | return log_error_errno(-1, errno, "Failed to remove existing \"/dev/ptmx\""); | |
d5cb35d6 CB |
1808 | |
1809 | /* Fallback option: Create symlink /dev/ptmx -> /dev/pts/ptmx. */ | |
06853427 | 1810 | ret = symlinkat("/dev/pts/ptmx", rootfs->dfd_dev, "dev/ptmx"); |
55022530 CB |
1811 | if (ret < 0) |
1812 | return log_error_errno(-1, errno, "Failed to create symlink from \"/dev/ptmx\" to \"/dev/pts/ptmx\""); | |
cd54d859 | 1813 | |
185b9ee9 | 1814 | DEBUG("Created symlink from \"/dev/ptmx\" to \"/dev/pts/ptmx\""); |
3c26f34e | 1815 | return 0; |
1816 | } | |
1817 | ||
111ed96e | 1818 | static int lxc_send_devpts_to_parent(struct lxc_handler *handler) |
1b82d721 CB |
1819 | { |
1820 | int ret; | |
1821 | ||
1822 | if (handler->conf->pty_max <= 0) | |
1823 | return log_debug(0, "No devpts file descriptor will be sent since no pts devices are requested"); | |
1824 | ||
1825 | ret = lxc_abstract_unix_send_fds(handler->data_sock[0], &handler->conf->devpts_fd, 1, NULL, 0); | |
1826 | if (ret < 0) | |
1827 | SYSERROR("Failed to send devpts file descriptor %d to parent", handler->conf->devpts_fd); | |
1828 | else | |
1829 | TRACE("Sent devpts file descriptor %d to parent", handler->conf->devpts_fd); | |
1830 | ||
1831 | close_prot_errno_disarm(handler->conf->devpts_fd); | |
1832 | ||
1833 | return 0; | |
1834 | } | |
1835 | ||
64a04c84 | 1836 | static int setup_personality(personality_t persona) |
cccc74b5 | 1837 | { |
0fd73091 CB |
1838 | int ret; |
1839 | ||
9c601e1f CB |
1840 | if (persona == LXC_ARCH_UNCHANGED) |
1841 | return log_debug(0, "Retaining original personality"); | |
cccc74b5 | 1842 | |
64a04c84 | 1843 | ret = lxc_personality(persona); |
55022530 | 1844 | if (ret < 0) |
9c601e1f | 1845 | return syserror("Failed to set personality to \"0lx%lx\"", persona); |
cccc74b5 | 1846 | |
38608992 | 1847 | INFO("Set personality to \"0lx%lx\"", persona); |
cccc74b5 DL |
1848 | return 0; |
1849 | } | |
1850 | ||
efbfe93f CB |
1851 | static inline bool wants_console(const struct lxc_terminal *terminal) |
1852 | { | |
71528742 | 1853 | return !terminal->path || !strequal(terminal->path, "none"); |
efbfe93f CB |
1854 | } |
1855 | ||
37c74fd1 CB |
1856 | static int lxc_bind_mount_console(const struct lxc_terminal *console, |
1857 | int dfd_to, const char *path_to) | |
1858 | { | |
1859 | __do_close int fd_pty = -EBADF; | |
1860 | ||
1861 | if (is_empty_string(console->name)) | |
1862 | return ret_errno(EINVAL); | |
1863 | ||
1864 | /* | |
1865 | * When the pty fd stashed in console->pty has been retrieved via the | |
1866 | * TIOCGPTPEER ioctl() to avoid dangerous path-based lookups when | |
1867 | * allocating new pty devices we can't reopen it through openat2() or | |
1868 | * created a detached mount through open_tree() from it. This means we | |
1869 | * would need to mount using the path stased in console->name which is | |
1870 | * unsafe. We could be mounting a device that isn't identical to the | |
1871 | * one we've already safely opened and stashed in console->pty. | |
1872 | * So, what we do is we open an O_PATH file descriptor for | |
1873 | * console->name and verify that the opened fd and the fd we stashed in | |
1874 | * console->pty refer to the same device. If they do we can go on and | |
1875 | * created a detached mount based on the newly opened O_PATH file | |
1876 | * descriptor and then safely mount. | |
1877 | */ | |
1878 | fd_pty = open_at(-EBADF, console->name, PROTECT_OPATH_FILE, | |
1879 | PROTECT_LOOKUP_ABSOLUTE_XDEV, 0); | |
1880 | if (fd_pty < 0) | |
1881 | return log_error_errno(-errno, errno, "Failed to open \"%s\"", console->name); | |
1882 | ||
1883 | if (!same_file_lax(console->pty, fd_pty)) | |
1884 | return log_error_errno(-EINVAL, EINVAL, "Console file descriptor changed"); | |
1885 | ||
1886 | /* | |
1887 | * Note, there are intentionally no open or lookup restrictions since | |
1888 | * we're operating directly on the fd. | |
1889 | */ | |
1890 | return fd_bind_mount(fd_pty, "", 0, 0, | |
1891 | dfd_to, path_to, PROTECT_OPATH_FILE, PROTECT_LOOKUP_BENEATH, | |
1892 | 0, false); | |
1893 | } | |
1894 | ||
58b38111 | 1895 | static int lxc_setup_dev_console(struct lxc_rootfs *rootfs, |
37c74fd1 | 1896 | const struct lxc_terminal *console) |
6e590161 | 1897 | { |
882671aa | 1898 | int ret; |
86530b0a | 1899 | char *rootfs_path = rootfs->path ? rootfs->mount : ""; |
52e35957 | 1900 | |
cf68ffd9 CB |
1901 | /* |
1902 | * When we are asked to setup a console we remove any previous | |
8b1b1210 CB |
1903 | * /dev/console bind-mounts. |
1904 | */ | |
a5a08920 | 1905 | if (exists_file_at(rootfs->dfd_dev, "console")) { |
9bcde680 CB |
1906 | ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/dev/console", rootfs_path); |
1907 | if (ret < 0) | |
953db219 CB |
1908 | return -1; |
1909 | ||
58b38111 | 1910 | ret = lxc_unstack_mountpoint(rootfs->buf, false); |
55022530 | 1911 | if (ret < 0) |
58b38111 | 1912 | return log_error_errno(-ret, errno, "Failed to unmount \"%s\"", rootfs->buf); |
55022530 | 1913 | else |
58b38111 | 1914 | DEBUG("Cleared all (%d) mounts from \"%s\"", ret, rootfs->buf); |
8b1b1210 CB |
1915 | } |
1916 | ||
cf68ffd9 CB |
1917 | /* |
1918 | * For unprivileged containers autodev or automounts will already have | |
8b1b1210 CB |
1919 | * taken care of creating /dev/console. |
1920 | */ | |
a5a08920 | 1921 | ret = mknodat(rootfs->dfd_dev, "console", S_IFREG | 0000, 0); |
55022530 CB |
1922 | if (ret < 0 && errno != EEXIST) |
1923 | return log_error_errno(-errno, errno, "Failed to create console"); | |
52e35957 | 1924 | |
1dd71c90 | 1925 | ret = fchmod(console->pty, 0620); |
55022530 | 1926 | if (ret < 0) |
07020e48 | 1927 | return log_error_errno(-errno, errno, "Failed to set mode \"0%o\" to \"%s\"", 0620, console->name); |
13954cce | 1928 | |
de7f9f33 | 1929 | if (can_use_mount_api()) { |
37c74fd1 CB |
1930 | ret = lxc_bind_mount_console(console, rootfs->dfd_dev, "console"); |
1931 | } else { | |
1932 | ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/dev/console", rootfs_path); | |
1933 | if (ret < 0) | |
1934 | return ret; | |
953db219 | 1935 | |
37c74fd1 | 1936 | ret = safe_mount(console->name, rootfs->buf, "none", MS_BIND, NULL, rootfs_path); |
953db219 | 1937 | } |
37c74fd1 CB |
1938 | if (ret < 0) |
1939 | return log_error_errno(ret, errno, "Failed to mount %d(%s) on \"%s\"", console->pty, console->name, rootfs->buf); | |
6e590161 | 1940 | |
f640c818 | 1941 | DEBUG("Mounted pty device %d(%s) onto \"/dev/console\"", console->pty, console->name); |
7c6ef2a2 SH |
1942 | return 0; |
1943 | } | |
1944 | ||
37c74fd1 | 1945 | static int lxc_setup_ttydir_console(struct lxc_rootfs *rootfs, |
dcad02f8 | 1946 | const struct lxc_terminal *console, |
37c74fd1 | 1947 | char *ttydir) |
7c6ef2a2 | 1948 | { |
3b7e332f | 1949 | int ret; |
6b5a54cd | 1950 | char path[PATH_MAX], lxcpath[PATH_MAX]; |
86530b0a | 1951 | char *rootfs_path = rootfs->path ? rootfs->mount : ""; |
7c6ef2a2 SH |
1952 | |
1953 | /* create rootfs/dev/<ttydir> directory */ | |
9bcde680 CB |
1954 | ret = strnprintf(path, sizeof(path), "%s/dev/%s", rootfs_path, ttydir); |
1955 | if (ret < 0) | |
7c6ef2a2 | 1956 | return -1; |
3d7d929a | 1957 | |
7c6ef2a2 | 1958 | ret = mkdir(path, 0755); |
55022530 CB |
1959 | if (ret && errno != EEXIST) |
1960 | return log_error_errno(-errno, errno, "Failed to create \"%s\"", path); | |
4742cd9a | 1961 | DEBUG("Created directory for console and tty devices at \"%s\"", path); |
7c6ef2a2 | 1962 | |
9bcde680 CB |
1963 | ret = strnprintf(lxcpath, sizeof(lxcpath), "%s/dev/%s/console", rootfs_path, ttydir); |
1964 | if (ret < 0) | |
3d7d929a CB |
1965 | return -1; |
1966 | ||
3b7e332f | 1967 | ret = mknod(lxcpath, S_IFREG | 0000, 0); |
55022530 CB |
1968 | if (ret < 0 && errno != EEXIST) |
1969 | return log_error_errno(-errno, errno, "Failed to create \"%s\"", lxcpath); | |
7c6ef2a2 | 1970 | |
9bcde680 CB |
1971 | ret = strnprintf(path, sizeof(path), "%s/dev/console", rootfs_path); |
1972 | if (ret < 0) | |
7c6ef2a2 | 1973 | return -1; |
2a12fefd | 1974 | |
3dc035f1 | 1975 | if (file_exists(path)) { |
a7ba3c7f | 1976 | ret = lxc_unstack_mountpoint(path, false); |
55022530 CB |
1977 | if (ret < 0) |
1978 | return log_error_errno(-ret, errno, "Failed to unmount \"%s\"", path); | |
1979 | else | |
86530b0a | 1980 | DEBUG("Cleared all (%d) mounts from \"%s\"", ret, path); |
3dc035f1 | 1981 | } |
2a12fefd | 1982 | |
3b7e332f | 1983 | ret = mknod(path, S_IFREG | 0000, 0); |
55022530 CB |
1984 | if (ret < 0 && errno != EEXIST) |
1985 | return log_error_errno(-errno, errno, "Failed to create console"); | |
7c6ef2a2 | 1986 | |
1dd71c90 | 1987 | ret = fchmod(console->pty, 0620); |
55022530 | 1988 | if (ret < 0) |
07020e48 | 1989 | return log_error_errno(-errno, errno, "Failed to set mode \"0%o\" to \"%s\"", 0620, console->name); |
2a12fefd | 1990 | |
3dc035f1 | 1991 | /* bind mount console->name to '/dev/<ttydir>/console' */ |
de7f9f33 | 1992 | if (can_use_mount_api()) { |
37c74fd1 CB |
1993 | ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/console", ttydir); |
1994 | if (ret < 0) | |
1995 | return ret; | |
efbfe93f | 1996 | |
37c74fd1 CB |
1997 | ret = lxc_bind_mount_console(console, rootfs->dfd_dev, rootfs->buf); |
1998 | } else { | |
1999 | ret = safe_mount(console->name, lxcpath, "none", MS_BIND, 0, rootfs_path); | |
efbfe93f | 2000 | } |
55022530 | 2001 | if (ret < 0) |
37c74fd1 | 2002 | return log_error_errno(ret, errno, "Failed to mount %d(%s) on \"%s\"", console->pty, console->name, lxcpath); |
86530b0a | 2003 | DEBUG("Mounted \"%s\" onto \"%s\"", console->name, lxcpath); |
3dc035f1 L |
2004 | |
2005 | /* bind mount '/dev/<ttydir>/console' to '/dev/console' */ | |
de7f9f33 | 2006 | if (can_use_mount_api()) { |
37c74fd1 CB |
2007 | ret = fd_bind_mount(rootfs->dfd_dev, rootfs->buf, |
2008 | PROTECT_OPATH_FILE, PROTECT_LOOKUP_BENEATH_XDEV, | |
2009 | rootfs->dfd_dev, "console", | |
2010 | PROTECT_OPATH_FILE, PROTECT_LOOKUP_BENEATH, | |
2011 | 0, false); | |
2012 | } else { | |
2013 | ret = safe_mount(lxcpath, path, "none", MS_BIND, 0, rootfs_path); | |
2014 | } | |
55022530 CB |
2015 | if (ret < 0) |
2016 | return log_error_errno(-1, errno, "Failed to mount \"%s\" on \"%s\"", console->name, lxcpath); | |
37c74fd1 | 2017 | DEBUG("Mounted \"%s\" onto \"%s\"", lxcpath, path); |
3dc035f1 | 2018 | |
86530b0a | 2019 | DEBUG("Console has been setup under \"%s\" and mounted to \"%s\"", lxcpath, path); |
6e590161 | 2020 | return 0; |
2021 | } | |
2022 | ||
f3dff080 CB |
2023 | static int lxc_setup_console(const struct lxc_handler *handler, |
2024 | struct lxc_rootfs *rootfs, | |
37c74fd1 | 2025 | struct lxc_terminal *console, char *ttydir) |
7c6ef2a2 | 2026 | { |
f3dff080 | 2027 | __do_close int fd_pty = -EBADF; |
37c74fd1 | 2028 | int ret; |
3d7d929a | 2029 | |
37c74fd1 CB |
2030 | if (!wants_console(console)) |
2031 | return log_trace(0, "Skipping console setup"); | |
7c6ef2a2 | 2032 | |
37c74fd1 CB |
2033 | if (ttydir) |
2034 | ret = lxc_setup_ttydir_console(rootfs, console, ttydir); | |
2035 | else | |
2036 | ret = lxc_setup_dev_console(rootfs, console); | |
87dfb724 CB |
2037 | if (ret < 0) |
2038 | return syserror("Failed to setup console"); | |
2039 | ||
f3dff080 CB |
2040 | fd_pty = move_fd(console->pty); |
2041 | ||
2042 | /* | |
2043 | * Some init's such as busybox will set sane tty settings on stdin, | |
2044 | * stdout, stderr which it thinks is the console. We already set them | |
2045 | * the way we wanted on the real terminal, and we want init to do its | |
2046 | * setup on its console ie. the pty allocated in lxc_terminal_setup() so | |
2047 | * make sure that that pty is stdin,stdout,stderr. | |
2048 | */ | |
2049 | if (fd_pty >= 0) { | |
2050 | if (handler->daemonize || !handler->conf->is_execute) | |
2051 | ret = set_stdfds(fd_pty); | |
2052 | else | |
2053 | ret = lxc_terminal_set_stdfds(fd_pty); | |
2054 | if (ret < 0) | |
2d7b0895 | 2055 | return syserror("Failed to redirect std{in,out,err} to pty file descriptor %d", fd_pty); |
f3dff080 CB |
2056 | } |
2057 | ||
37c74fd1 | 2058 | return ret; |
7c6ef2a2 SH |
2059 | } |
2060 | ||
a08bfbe3 | 2061 | static int parse_mntopt(char *opt, unsigned long *flags, char **data, size_t size) |
998ac676 | 2062 | { |
a08bfbe3 | 2063 | ssize_t ret; |
998ac676 | 2064 | |
85c2de39 MB |
2065 | /* If '=' is contained in opt, the option must go into data. */ |
2066 | if (!strchr(opt, '=')) { | |
a08bfbe3 CB |
2067 | /* |
2068 | * If opt is found in mount_opt, set or clear flags. | |
2069 | * Otherwise append it to data. | |
2070 | */ | |
85c2de39 | 2071 | size_t opt_len = strlen(opt); |
a08bfbe3 | 2072 | for (struct mount_opt *mo = &mount_opt[0]; mo->name != NULL; mo++) { |
85c2de39 | 2073 | size_t mo_name_len = strlen(mo->name); |
a08bfbe3 | 2074 | |
eed95eb0 | 2075 | if (opt_len == mo_name_len && strnequal(opt, mo->name, mo_name_len)) { |
85c2de39 | 2076 | if (mo->clear) |
1e4bce2c | 2077 | *flags &= ~mo->legacy_flag; |
85c2de39 | 2078 | else |
1e4bce2c | 2079 | *flags |= mo->legacy_flag; |
a08bfbe3 | 2080 | return 0; |
85c2de39 | 2081 | } |
998ac676 RT |
2082 | } |
2083 | } | |
2084 | ||
a08bfbe3 CB |
2085 | if (strlen(*data)) { |
2086 | ret = strlcat(*data, ",", size); | |
2087 | if (ret < 0) | |
2088 | return log_error_errno(ret, errno, "Failed to append \",\" to %s", *data); | |
2089 | } | |
2090 | ||
2091 | ret = strlcat(*data, opt, size); | |
2092 | if (ret < 0) | |
2093 | return log_error_errno(ret, errno, "Failed to append \"%s\" to %s", opt, *data); | |
efed99a4 | 2094 | |
a08bfbe3 | 2095 | return 0; |
998ac676 RT |
2096 | } |
2097 | ||
d94eb390 | 2098 | int parse_mntopts_legacy(const char *mntopts, unsigned long *mntflags, char **mntdata) |
998ac676 | 2099 | { |
a08bfbe3 CB |
2100 | __do_free char *mntopts_new = NULL, *mntopts_dup = NULL; |
2101 | char *mntopt_cur = NULL; | |
efed99a4 | 2102 | size_t size; |
998ac676 | 2103 | |
a08bfbe3 CB |
2104 | if (*mntdata || *mntflags) |
2105 | return ret_errno(EINVAL); | |
911324ef DL |
2106 | |
2107 | if (!mntopts) | |
998ac676 RT |
2108 | return 0; |
2109 | ||
a08bfbe3 CB |
2110 | mntopts_dup = strdup(mntopts); |
2111 | if (!mntopts_dup) | |
2112 | return ret_errno(ENOMEM); | |
998ac676 | 2113 | |
a08bfbe3 CB |
2114 | size = strlen(mntopts_dup) + 1; |
2115 | mntopts_new = zalloc(size); | |
2116 | if (!mntopts_new) | |
2117 | return ret_errno(ENOMEM); | |
998ac676 | 2118 | |
a08bfbe3 CB |
2119 | lxc_iterate_parts(mntopt_cur, mntopts_dup, ",") |
2120 | if (parse_mntopt(mntopt_cur, mntflags, &mntopts_new, size) < 0) | |
2121 | return ret_errno(EINVAL); | |
998ac676 | 2122 | |
a08bfbe3 CB |
2123 | if (*mntopts_new) |
2124 | *mntdata = move_ptr(mntopts_new); | |
998ac676 RT |
2125 | |
2126 | return 0; | |
2127 | } | |
2128 | ||
1b82d721 CB |
2129 | static int parse_vfs_attr(struct lxc_mount_options *opts, char *opt, size_t size) |
2130 | { | |
2131 | /* | |
2132 | * If opt is found in mount_opt, set or clear flags. | |
2133 | * Otherwise append it to data. | |
2134 | */ | |
2135 | for (struct mount_opt *mo = &mount_opt[0]; mo->name != NULL; mo++) { | |
2136 | if (!strnequal(opt, mo->name, strlen(mo->name))) | |
2137 | continue; | |
2138 | ||
2139 | /* This is a recursive bind-mount. */ | |
2140 | if (strequal(mo->name, "rbind")) { | |
2141 | opts->recursive = 1; | |
2142 | opts->bind = 1; | |
2143 | return 0; | |
2144 | } | |
2145 | ||
2146 | /* This is a bind-mount. */ | |
2147 | if (strequal(mo->name, "bind")) { | |
2148 | opts->bind = 1; | |
2149 | return 0; | |
2150 | } | |
2151 | ||
2152 | if (mo->flag == ~0) | |
2153 | return log_info(0, "Ignoring %s mount option", mo->name); | |
2154 | ||
2155 | if (mo->clear) { | |
2156 | opts->attr.attr_clr |= mo->flag; | |
2157 | TRACE("Lowering %s", mo->name); | |
2158 | } else { | |
2159 | opts->attr.attr_set |= mo->flag; | |
2160 | TRACE("Raising %s", mo->name); | |
2161 | } | |
2162 | ||
2163 | return 0; | |
2164 | } | |
2165 | ||
2166 | for (struct mount_opt *mo = &mount_opt[0]; mo->name != NULL; mo++) { | |
2167 | if (!strnequal(opt, mo->name, strlen(mo->name))) | |
2168 | continue; | |
2169 | ||
2170 | /* TODO: Handle recursive propagation requests. */ | |
2171 | opts->attr.propagation = mo->flag; | |
2172 | return 0; | |
2173 | } | |
2174 | ||
2175 | return 0; | |
2176 | } | |
2177 | ||
2178 | static int parse_mount_attrs(struct lxc_mount_options *opts, const char *mntopts) | |
2179 | { | |
2180 | __do_free char *mntopts_new = NULL, *mntopts_dup = NULL; | |
380fcc08 | 2181 | char *end = NULL, *mntopt_cur = NULL; |
1b82d721 CB |
2182 | int ret; |
2183 | size_t size; | |
2184 | ||
2185 | if (!opts) | |
2186 | return ret_errno(EINVAL); | |
2187 | ||
2188 | if (!mntopts) | |
2189 | return 0; | |
2190 | ||
2191 | mntopts_dup = strdup(mntopts); | |
2192 | if (!mntopts_dup) | |
2193 | return ret_errno(ENOMEM); | |
2194 | ||
2195 | size = strlen(mntopts_dup) + 1; | |
2196 | mntopts_new = zalloc(size); | |
2197 | if (!mntopts_new) | |
2198 | return ret_errno(ENOMEM); | |
2199 | ||
2200 | lxc_iterate_parts(mntopt_cur, mntopts_dup, ",") { | |
1b82d721 CB |
2201 | /* This is a filesystem specific option. */ |
2202 | if (strchr(mntopt_cur, '=')) { | |
2203 | if (!end) { | |
2204 | end = stpcpy(mntopts_new, mntopt_cur); | |
2205 | } else { | |
2206 | end = stpcpy(end, ","); | |
2207 | end = stpcpy(end, mntopt_cur); | |
2208 | } | |
2209 | ||
2210 | continue; | |
2211 | } | |
2212 | ||
2213 | /* This is a generic vfs option. */ | |
2214 | ret = parse_vfs_attr(opts, mntopt_cur, size); | |
2215 | if (ret < 0) | |
2216 | return syserror("Failed to parse mount attributes: \"%s\"", mntopt_cur); | |
2217 | } | |
2218 | ||
2219 | if (*mntopts_new) | |
2220 | opts->data = move_ptr(mntopts_new); | |
2221 | ||
2222 | return 0; | |
2223 | } | |
2224 | ||
d840039e YT |
2225 | static void parse_propagationopt(char *opt, unsigned long *flags) |
2226 | { | |
2227 | struct mount_opt *mo; | |
2228 | ||
2229 | /* If opt is found in propagation_opt, set or clear flags. */ | |
d840039e | 2230 | for (mo = &propagation_opt[0]; mo->name != NULL; mo++) { |
eed95eb0 | 2231 | if (!strnequal(opt, mo->name, strlen(mo->name))) |
0fd73091 CB |
2232 | continue; |
2233 | ||
2234 | if (mo->clear) | |
1e4bce2c | 2235 | *flags &= ~mo->legacy_flag; |
0fd73091 | 2236 | else |
1e4bce2c | 2237 | *flags |= mo->legacy_flag; |
0fd73091 CB |
2238 | |
2239 | return; | |
d840039e YT |
2240 | } |
2241 | } | |
2242 | ||
8ce1abc2 | 2243 | int parse_propagationopts(const char *mntopts, unsigned long *pflags) |
d840039e | 2244 | { |
dfd2e059 CB |
2245 | __do_free char *s = NULL; |
2246 | char *p; | |
d840039e YT |
2247 | |
2248 | if (!mntopts) | |
2249 | return 0; | |
2250 | ||
2251 | s = strdup(mntopts); | |
55022530 CB |
2252 | if (!s) |
2253 | return log_error_errno(-ENOMEM, errno, "Failed to allocate memory"); | |
d840039e | 2254 | |
0fd73091 | 2255 | *pflags = 0L; |
8db9d26f | 2256 | lxc_iterate_parts(p, s, ",") |
d840039e | 2257 | parse_propagationopt(p, pflags); |
0fd73091 | 2258 | |
d840039e YT |
2259 | return 0; |
2260 | } | |
2261 | ||
6fd5e769 SH |
2262 | static void null_endofword(char *word) |
2263 | { | |
2264 | while (*word && *word != ' ' && *word != '\t') | |
2265 | word++; | |
2266 | *word = '\0'; | |
2267 | } | |
2268 | ||
0fd73091 | 2269 | /* skip @nfields spaces in @src */ |
6fd5e769 SH |
2270 | static char *get_field(char *src, int nfields) |
2271 | { | |
6fd5e769 | 2272 | int i; |
0fd73091 | 2273 | char *p = src; |
6fd5e769 SH |
2274 | |
2275 | for (i = 0; i < nfields; i++) { | |
2276 | while (*p && *p != ' ' && *p != '\t') | |
2277 | p++; | |
0fd73091 | 2278 | |
6fd5e769 SH |
2279 | if (!*p) |
2280 | break; | |
0fd73091 | 2281 | |
6fd5e769 SH |
2282 | p++; |
2283 | } | |
0fd73091 | 2284 | |
6fd5e769 SH |
2285 | return p; |
2286 | } | |
2287 | ||
911324ef DL |
2288 | static int mount_entry(const char *fsname, const char *target, |
2289 | const char *fstype, unsigned long mountflags, | |
d840039e YT |
2290 | unsigned long pflags, const char *data, bool optional, |
2291 | bool dev, bool relative, const char *rootfs) | |
911324ef | 2292 | { |
0ac4b28a | 2293 | int ret; |
6b5a54cd | 2294 | char srcbuf[PATH_MAX]; |
181437fd | 2295 | const char *srcpath = fsname; |
614305f3 | 2296 | #ifdef HAVE_STATVFS |
2938f7c8 | 2297 | struct statvfs sb; |
614305f3 | 2298 | #endif |
2938f7c8 | 2299 | |
181437fd | 2300 | if (relative) { |
9bcde680 CB |
2301 | ret = strnprintf(srcbuf, sizeof(srcbuf), "%s/%s", rootfs ? rootfs : "/", fsname ? fsname : ""); |
2302 | if (ret < 0) | |
55022530 | 2303 | return log_error_errno(-1, errno, "source path is too long"); |
181437fd YT |
2304 | srcpath = srcbuf; |
2305 | } | |
2306 | ||
2307 | ret = safe_mount(srcpath, target, fstype, mountflags & ~MS_REMOUNT, data, | |
0ac4b28a CB |
2308 | rootfs); |
2309 | if (ret < 0) { | |
55022530 CB |
2310 | if (optional) |
2311 | return log_info_errno(0, errno, "Failed to mount \"%s\" on \"%s\" (optional)", | |
2312 | srcpath ? srcpath : "(null)", target); | |
0ac4b28a | 2313 | |
55022530 CB |
2314 | return log_error_errno(-1, errno, "Failed to mount \"%s\" on \"%s\"", |
2315 | srcpath ? srcpath : "(null)", target); | |
911324ef DL |
2316 | } |
2317 | ||
2318 | if ((mountflags & MS_REMOUNT) || (mountflags & MS_BIND)) { | |
0ac4b28a | 2319 | |
55022530 CB |
2320 | DEBUG("Remounting \"%s\" on \"%s\" to respect bind or remount options", |
2321 | srcpath ? srcpath : "(none)", target ? target : "(none)"); | |
0ac4b28a | 2322 | |
614305f3 | 2323 | #ifdef HAVE_STATVFS |
181437fd | 2324 | if (srcpath && statvfs(srcpath, &sb) == 0) { |
94bef7e4 TA |
2325 | unsigned long required_flags = 0; |
2326 | ||
2938f7c8 SH |
2327 | if (sb.f_flag & MS_NOSUID) |
2328 | required_flags |= MS_NOSUID; | |
0ac4b28a | 2329 | |
ae7a770e | 2330 | if (sb.f_flag & MS_NODEV && !dev) |
2938f7c8 | 2331 | required_flags |= MS_NODEV; |
0ac4b28a | 2332 | |
2938f7c8 SH |
2333 | if (sb.f_flag & MS_RDONLY) |
2334 | required_flags |= MS_RDONLY; | |
0ac4b28a | 2335 | |
2938f7c8 SH |
2336 | if (sb.f_flag & MS_NOEXEC) |
2337 | required_flags |= MS_NOEXEC; | |
0ac4b28a | 2338 | |
55022530 CB |
2339 | DEBUG("Flags for \"%s\" were %lu, required extra flags are %lu", |
2340 | srcpath, sb.f_flag, required_flags); | |
0ac4b28a CB |
2341 | |
2342 | /* If this was a bind mount request, and required_flags | |
2938f7c8 | 2343 | * does not have any flags which are not already in |
0ac4b28a | 2344 | * mountflags, then skip the remount. |
2938f7c8 | 2345 | */ |
94bef7e4 TA |
2346 | if (!(mountflags & MS_REMOUNT) && |
2347 | (!(required_flags & ~mountflags) && !(mountflags & MS_RDONLY))) { | |
15f3e22b CB |
2348 | DEBUG("Mountflags already were %lu, skipping remount", mountflags); |
2349 | goto skipremount; | |
2938f7c8 | 2350 | } |
0ac4b28a | 2351 | |
2938f7c8 | 2352 | mountflags |= required_flags; |
6fd5e769 | 2353 | } |
614305f3 | 2354 | #endif |
911324ef | 2355 | |
181437fd | 2356 | ret = mount(srcpath, target, fstype, mountflags | MS_REMOUNT, data); |
0ac4b28a | 2357 | if (ret < 0) { |
55022530 CB |
2358 | if (optional) |
2359 | return log_info_errno(0, errno, "Failed to mount \"%s\" on \"%s\" (optional)", | |
2360 | srcpath ? srcpath : "(null)", | |
2361 | target); | |
2362 | ||
2363 | return log_error_errno(-1, errno, "Failed to mount \"%s\" on \"%s\"", | |
2364 | srcpath ? srcpath : "(null)", | |
2365 | target); | |
911324ef DL |
2366 | } |
2367 | } | |
2368 | ||
a3ed9b81 | 2369 | #ifdef HAVE_STATVFS |
2370 | skipremount: | |
2371 | #endif | |
d840039e YT |
2372 | if (pflags) { |
2373 | ret = mount(NULL, target, NULL, pflags, NULL); | |
2374 | if (ret < 0) { | |
55022530 CB |
2375 | if (optional) |
2376 | return log_info_errno(0, errno, "Failed to change mount propagation for \"%s\" (optional)", target); | |
2377 | else | |
2378 | return log_error_errno(-1, errno, "Failed to change mount propagation for \"%s\" (optional)", target); | |
d840039e YT |
2379 | } |
2380 | DEBUG("Changed mount propagation for \"%s\"", target); | |
2381 | } | |
2382 | ||
0103eb53 | 2383 | DEBUG("Mounted \"%s\" on \"%s\" with filesystem type \"%s\"", |
181437fd | 2384 | srcpath ? srcpath : "(null)", target, fstype); |
911324ef DL |
2385 | |
2386 | return 0; | |
2387 | } | |
2388 | ||
0b932f9d CB |
2389 | const char *lxc_mount_options_info[LXC_MOUNT_MAX] = { |
2390 | "create=dir", | |
2391 | "create=file", | |
2392 | "optional", | |
2393 | "relative", | |
f6815906 | 2394 | "idmap=", |
0b932f9d CB |
2395 | }; |
2396 | ||
c5e30de4 | 2397 | /* Remove "optional", "create=dir", and "create=file" from mntopt */ |
1b82d721 | 2398 | int parse_lxc_mount_attrs(struct lxc_mount_options *opts, char *mnt_opts) |
4e4ca161 | 2399 | { |
0b932f9d | 2400 | for (size_t i = LXC_MOUNT_CREATE_DIR; i < LXC_MOUNT_MAX; i++) { |
8e05f350 | 2401 | __do_close int fd_userns = -EBADF; |
0b932f9d | 2402 | const char *opt_name = lxc_mount_options_info[i]; |
f6815906 | 2403 | size_t len; |
d97d9e9f | 2404 | char *idmap_path, *opt, *opt_next; |
c5e30de4 | 2405 | |
d97d9e9f CB |
2406 | opt = strstr(mnt_opts, opt_name); |
2407 | if (!opt) | |
4e4ca161 | 2408 | continue; |
c5e30de4 | 2409 | |
0b932f9d CB |
2410 | switch (i) { |
2411 | case LXC_MOUNT_CREATE_DIR: | |
2412 | opts->create_dir = 1; | |
2413 | break; | |
2414 | case LXC_MOUNT_CREATE_FILE: | |
2415 | opts->create_file = 1; | |
2416 | break; | |
2417 | case LXC_MOUNT_OPTIONAL: | |
2418 | opts->optional = 1; | |
2419 | break; | |
2420 | case LXC_MOUNT_RELATIVE: | |
2421 | opts->relative = 1; | |
2422 | break; | |
f6815906 | 2423 | case LXC_MOUNT_IDMAP: |
d97d9e9f CB |
2424 | opt_next = opt; |
2425 | opt_next += STRLITERALLEN("idmap="); | |
2426 | idmap_path = strchrnul(opt_next, ','); | |
2e5c468a | 2427 | len = idmap_path - opt_next + 1; |
f6815906 | 2428 | |
f6815906 | 2429 | if (len >= sizeof(opts->userns_path)) |
16fcdacc CB |
2430 | return syserror_set(-EIO, "Excessive idmap path length for \"idmap=<path>\" LXC specific mount option"); |
2431 | ||
1b82d721 | 2432 | strlcpy(opts->userns_path, opt_next, len); |
2e5c468a | 2433 | |
16fcdacc CB |
2434 | if (is_empty_string(opts->userns_path)) |
2435 | return syserror_set(-EINVAL, "Missing idmap path for \"idmap=<path>\" LXC specific mount option"); | |
2436 | ||
1b82d721 | 2437 | if (!strequal(opts->userns_path, "container")) { |
4b875ef9 CB |
2438 | fd_userns = open(opts->userns_path, O_RDONLY | O_NOCTTY | O_CLOEXEC); |
2439 | if (fd_userns < 0) | |
1b82d721 | 2440 | return syserror("Failed to open user namespace %s", opts->userns_path); |
4b875ef9 | 2441 | } |
e26cf563 CB |
2442 | |
2443 | TRACE("Parse LXC specific mount option %d->\"idmap=%s\"", fd_userns, opts->userns_path); | |
f6815906 | 2444 | break; |
0b932f9d | 2445 | default: |
16fcdacc | 2446 | return syserror_set(-EINVAL, "Unknown LXC specific mount option"); |
4e4ca161 | 2447 | } |
c5e30de4 | 2448 | |
d97d9e9f CB |
2449 | opt_next = strchr(opt, ','); |
2450 | if (!opt_next) | |
2451 | *opt = '\0'; /* no more mntopts, so just chop it here */ | |
0b932f9d | 2452 | else |
d97d9e9f | 2453 | memmove(opt, opt_next + 1, strlen(opt_next + 1) + 1); |
4e4ca161 | 2454 | } |
16fcdacc CB |
2455 | |
2456 | return 0; | |
4e4ca161 SH |
2457 | } |
2458 | ||
4d5b72a1 | 2459 | static int mount_entry_create_dir_file(const struct mntent *mntent, |
749f98d9 CB |
2460 | const char *path, |
2461 | const struct lxc_rootfs *rootfs, | |
0fd73091 | 2462 | const char *lxc_name, const char *lxc_path) |
0ad19a3f | 2463 | { |
7a76eeaa | 2464 | __do_free char *p1 = NULL; |
3b7e332f | 2465 | int ret; |
7a76eeaa | 2466 | char *p2; |
911324ef | 2467 | |
eed95eb0 | 2468 | if (strnequal(mntent->mnt_type, "overlay", 7)) { |
749f98d9 | 2469 | ret = ovl_mkdir(mntent, rootfs, lxc_name, lxc_path); |
12e6ab5d CB |
2470 | if (ret < 0) |
2471 | return -1; | |
2472 | } | |
6e46cc0d | 2473 | |
34cfffb3 | 2474 | if (hasmntopt(mntent, "create=dir")) { |
749f98d9 | 2475 | ret = mkdir_p(path, 0755); |
55022530 CB |
2476 | if (ret < 0 && errno != EEXIST) |
2477 | return log_error_errno(-1, errno, "Failed to create directory \"%s\"", path); | |
34cfffb3 SG |
2478 | } |
2479 | ||
0fd73091 CB |
2480 | if (!hasmntopt(mntent, "create=file")) |
2481 | return 0; | |
749f98d9 | 2482 | |
0fd73091 CB |
2483 | ret = access(path, F_OK); |
2484 | if (ret == 0) | |
2485 | return 0; | |
749f98d9 | 2486 | |
0fd73091 CB |
2487 | p1 = strdup(path); |
2488 | if (!p1) | |
2489 | return -1; | |
749f98d9 | 2490 | |
0fd73091 | 2491 | p2 = dirname(p1); |
749f98d9 | 2492 | |
0fd73091 | 2493 | ret = mkdir_p(p2, 0755); |
55022530 CB |
2494 | if (ret < 0 && errno != EEXIST) |
2495 | return log_error_errno(-1, errno, "Failed to create directory \"%s\"", path); | |
749f98d9 | 2496 | |
3b7e332f CB |
2497 | ret = mknod(path, S_IFREG | 0000, 0); |
2498 | if (ret < 0 && errno != EEXIST) | |
2499 | return -errno; | |
0fd73091 | 2500 | |
749f98d9 | 2501 | return 0; |
4d5b72a1 NC |
2502 | } |
2503 | ||
ec50007f CB |
2504 | /* rootfs, lxc_name, and lxc_path can be NULL when the container is created |
2505 | * without a rootfs. */ | |
db4aba38 | 2506 | static inline int mount_entry_on_generic(struct mntent *mntent, |
d8b712bc CB |
2507 | const char *path, |
2508 | const struct lxc_rootfs *rootfs, | |
2509 | const char *lxc_name, | |
2510 | const char *lxc_path) | |
4d5b72a1 | 2511 | { |
fd214f37 | 2512 | __do_free char *mntdata = NULL; |
a08bfbe3 CB |
2513 | unsigned long mntflags = 0, pflags = 0; |
2514 | char *rootfs_path = NULL; | |
d8b712bc | 2515 | int ret; |
181437fd | 2516 | bool dev, optional, relative; |
0b932f9d | 2517 | struct lxc_mount_options opts = {}; |
d8b712bc CB |
2518 | |
2519 | optional = hasmntopt(mntent, "optional") != NULL; | |
2520 | dev = hasmntopt(mntent, "dev") != NULL; | |
181437fd | 2521 | relative = hasmntopt(mntent, "relative") != NULL; |
d8b712bc | 2522 | |
ec50007f CB |
2523 | if (rootfs && rootfs->path) |
2524 | rootfs_path = rootfs->mount; | |
2525 | ||
d8b712bc CB |
2526 | ret = mount_entry_create_dir_file(mntent, path, rootfs, lxc_name, |
2527 | lxc_path); | |
2528 | if (ret < 0) { | |
2529 | if (optional) | |
2530 | return 0; | |
608e3567 | 2531 | |
d8b712bc CB |
2532 | return -1; |
2533 | } | |
16fcdacc | 2534 | |
1b82d721 | 2535 | ret = parse_lxc_mount_attrs(&opts, mntent->mnt_opts); |
16fcdacc CB |
2536 | if (ret < 0) |
2537 | return ret; | |
4e4ca161 | 2538 | |
1b82d721 CB |
2539 | /* |
2540 | * Idmapped mount entries will be setup by the parent for us. Note that | |
2541 | * we rely on mount_entry_create_dir_file() above to have already | |
2542 | * created the target path for us. So the parent can just open the | |
2543 | * target and send us the target fd. | |
2544 | */ | |
2545 | errno = EOPNOTSUPP; | |
fa8e75f0 | 2546 | if (!is_empty_string(opts.userns_path)) |
1b82d721 | 2547 | return systrace_ret(0, "Skipping idmapped mount entry"); |
fa8e75f0 | 2548 | |
d840039e YT |
2549 | ret = parse_propagationopts(mntent->mnt_opts, &pflags); |
2550 | if (ret < 0) | |
2551 | return -1; | |
2552 | ||
d94eb390 | 2553 | ret = parse_mntopts_legacy(mntent->mnt_opts, &mntflags, &mntdata); |
d8b712bc | 2554 | if (ret < 0) |
a08bfbe3 | 2555 | return ret; |
a17b1e65 | 2556 | |
6e46cc0d | 2557 | ret = mount_entry(mntent->mnt_fsname, path, mntent->mnt_type, mntflags, |
d840039e | 2558 | pflags, mntdata, optional, dev, relative, rootfs_path); |
68c152ef | 2559 | |
911324ef DL |
2560 | return ret; |
2561 | } | |
2562 | ||
8183f09e CB |
2563 | static inline int mount_entry_on_systemfs(struct lxc_rootfs *rootfs, |
2564 | struct mntent *mntent) | |
db4aba38 | 2565 | { |
1433c9f9 CB |
2566 | int ret; |
2567 | ||
2568 | /* For containers created without a rootfs all mounts are treated as | |
07667a6a CB |
2569 | * absolute paths starting at / on the host. |
2570 | */ | |
1433c9f9 | 2571 | if (mntent->mnt_dir[0] != '/') |
9bcde680 | 2572 | ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "/%s", mntent->mnt_dir); |
1433c9f9 | 2573 | else |
9bcde680 CB |
2574 | ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s", mntent->mnt_dir); |
2575 | if (ret < 0) | |
1433c9f9 | 2576 | return -1; |
1433c9f9 | 2577 | |
8183f09e | 2578 | return mount_entry_on_generic(mntent, rootfs->buf, NULL, NULL, NULL); |
db4aba38 NC |
2579 | } |
2580 | ||
4e4ca161 | 2581 | static int mount_entry_on_absolute_rootfs(struct mntent *mntent, |
9c0fd29a | 2582 | struct lxc_rootfs *rootfs, |
0a2dddd4 CB |
2583 | const char *lxc_name, |
2584 | const char *lxc_path) | |
911324ef | 2585 | { |
bdd2b34c | 2586 | int offset; |
013bd428 | 2587 | char *aux; |
67e571de | 2588 | const char *lxcpath; |
bdd2b34c | 2589 | int ret = 0; |
0ad19a3f | 2590 | |
593e8478 | 2591 | lxcpath = lxc_global_config_value("lxc.lxcpath"); |
bdd2b34c | 2592 | if (!lxcpath) |
2a59a681 | 2593 | return -1; |
2a59a681 | 2594 | |
bdd2b34c CB |
2595 | /* If rootfs->path is a blockdev path, allow container fstab to use |
2596 | * <lxcpath>/<name>/rootfs" as the target prefix. | |
2597 | */ | |
9bcde680 CB |
2598 | ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/%s/rootfs", lxcpath, lxc_name); |
2599 | if (ret < 0) | |
80a881b2 SH |
2600 | goto skipvarlib; |
2601 | ||
9c0fd29a | 2602 | aux = strstr(mntent->mnt_dir, rootfs->buf); |
80a881b2 | 2603 | if (aux) { |
9c0fd29a | 2604 | offset = strlen(rootfs->buf); |
80a881b2 SH |
2605 | goto skipabs; |
2606 | } | |
2607 | ||
2608 | skipvarlib: | |
013bd428 | 2609 | aux = strstr(mntent->mnt_dir, rootfs->path); |
55022530 CB |
2610 | if (!aux) |
2611 | return log_warn(ret, "Ignoring mount point \"%s\"", mntent->mnt_dir); | |
80a881b2 SH |
2612 | offset = strlen(rootfs->path); |
2613 | ||
2614 | skipabs: | |
9bcde680 CB |
2615 | ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/%s", rootfs->mount, aux + offset); |
2616 | if (ret < 0) | |
a17b1e65 | 2617 | return -1; |
a17b1e65 | 2618 | |
9c0fd29a | 2619 | return mount_entry_on_generic(mntent, rootfs->buf, rootfs, lxc_name, lxc_path); |
911324ef | 2620 | } |
d330fe7b | 2621 | |
4e4ca161 | 2622 | static int mount_entry_on_relative_rootfs(struct mntent *mntent, |
4806d3b9 | 2623 | struct lxc_rootfs *rootfs, |
0a2dddd4 CB |
2624 | const char *lxc_name, |
2625 | const char *lxc_path) | |
911324ef | 2626 | { |
911324ef | 2627 | int ret; |
d330fe7b | 2628 | |
34cfffb3 | 2629 | /* relative to root mount point */ |
9bcde680 CB |
2630 | ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/%s", rootfs->mount, mntent->mnt_dir); |
2631 | if (ret < 0) | |
9ba8130c | 2632 | return -1; |
911324ef | 2633 | |
4806d3b9 | 2634 | return mount_entry_on_generic(mntent, rootfs->buf, rootfs, lxc_name, lxc_path); |
911324ef DL |
2635 | } |
2636 | ||
8183f09e | 2637 | static int mount_file_entries(struct lxc_rootfs *rootfs, FILE *file, |
1ae3c19f | 2638 | const char *lxc_name, const char *lxc_path) |
911324ef | 2639 | { |
9d03d857 | 2640 | char buf[PATH_MAX]; |
0fd73091 | 2641 | struct mntent mntent; |
e76b8764 | 2642 | |
aaf901be | 2643 | while (getmntent_r(file, &mntent, buf, sizeof(buf))) { |
9d03d857 CB |
2644 | int ret; |
2645 | ||
1ae3c19f | 2646 | if (!rootfs->path) |
8183f09e | 2647 | ret = mount_entry_on_systemfs(rootfs, &mntent); |
1ae3c19f CB |
2648 | else if (mntent.mnt_dir[0] != '/') |
2649 | ret = mount_entry_on_relative_rootfs(&mntent, rootfs, | |
2650 | lxc_name, lxc_path); | |
2651 | else | |
2652 | ret = mount_entry_on_absolute_rootfs(&mntent, rootfs, | |
9d03d857 | 2653 | lxc_name, lxc_path); |
1ae3c19f CB |
2654 | if (ret < 0) |
2655 | return -1; | |
0ad19a3f | 2656 | } |
cd54d859 | 2657 | |
55022530 CB |
2658 | if (!feof(file) || ferror(file)) |
2659 | return log_error(-1, "Failed to parse mount entries"); | |
9d03d857 CB |
2660 | |
2661 | return 0; | |
e7938e9e MN |
2662 | } |
2663 | ||
55022530 CB |
2664 | static inline void __auto_endmntent__(FILE **f) |
2665 | { | |
2666 | if (*f) | |
2667 | endmntent(*f); | |
2668 | } | |
2669 | ||
2670 | #define __do_endmntent __attribute__((__cleanup__(__auto_endmntent__))) | |
2671 | ||
48e5dcc8 | 2672 | static int setup_mount_fstab(struct lxc_rootfs *rootfs, const char *fstab, |
8183f09e | 2673 | const char *lxc_name, const char *lxc_path) |
e7938e9e | 2674 | { |
55022530 | 2675 | __do_endmntent FILE *f = NULL; |
e7938e9e MN |
2676 | int ret; |
2677 | ||
2678 | if (!fstab) | |
2679 | return 0; | |
2680 | ||
55022530 CB |
2681 | f = setmntent(fstab, "re"); |
2682 | if (!f) | |
2683 | return log_error_errno(-1, errno, "Failed to open \"%s\"", fstab); | |
e7938e9e | 2684 | |
a7c6e830 | 2685 | ret = mount_file_entries(rootfs, f, lxc_name, lxc_path); |
42dff448 CB |
2686 | if (ret < 0) |
2687 | ERROR("Failed to set up mount entries"); | |
e7938e9e | 2688 | |
0ad19a3f | 2689 | return ret; |
2690 | } | |
2691 | ||
1800f924 WB |
2692 | /* |
2693 | * In order for nested containers to be able to mount /proc and /sys they need | |
2694 | * to see a "pure" proc and sysfs mount points with nothing mounted on top | |
2695 | * (like lxcfs). | |
2696 | * For this we provide proc and sysfs in /dev/.lxc/{proc,sys} while using an | |
2697 | * apparmor rule to deny access to them. This is mostly for convenience: The | |
2698 | * container's root user can mount them anyway and thus has access to the two | |
2699 | * file systems. But a non-root user in the container should not be allowed to | |
2700 | * access them as a side effect without explicitly allowing it. | |
2701 | */ | |
2702 | static const char nesting_helpers[] = | |
dc691e34 CB |
2703 | "proc dev/.lxc/proc proc create=dir,optional 0 0\n" |
2704 | "sys dev/.lxc/sys sysfs create=dir,optional 0 0\n"; | |
1800f924 WB |
2705 | |
2706 | FILE *make_anonymous_mount_file(struct lxc_list *mount, | |
2707 | bool include_nesting_helpers) | |
e7938e9e | 2708 | { |
f62cf1d4 | 2709 | __do_close int fd = -EBADF; |
4110345b | 2710 | FILE *f; |
5ef5c9a3 | 2711 | int ret; |
e7938e9e | 2712 | char *mount_entry; |
5ef5c9a3 | 2713 | struct lxc_list *iterator; |
5ef5c9a3 | 2714 | |
0fd73091 | 2715 | fd = memfd_create(".lxc_mount_file", MFD_CLOEXEC); |
5ef5c9a3 | 2716 | if (fd < 0) { |
a324e7eb CB |
2717 | char template[] = P_tmpdir "/.lxc_mount_file_XXXXXX"; |
2718 | ||
5ef5c9a3 CB |
2719 | if (errno != ENOSYS) |
2720 | return NULL; | |
a324e7eb CB |
2721 | |
2722 | fd = lxc_make_tmpfile(template, true); | |
55022530 CB |
2723 | if (fd < 0) |
2724 | return log_error_errno(NULL, errno, "Could not create temporary mount file"); | |
0fd73091 | 2725 | |
6bd04140 | 2726 | TRACE("Created temporary mount file"); |
5ef5c9a3 | 2727 | } |
e7938e9e | 2728 | |
0fd73091 CB |
2729 | lxc_list_for_each (iterator, mount) { |
2730 | size_t len; | |
2731 | ||
e7938e9e | 2732 | mount_entry = iterator->elem; |
0fd73091 | 2733 | len = strlen(mount_entry); |
5ef5c9a3 | 2734 | |
489f39be | 2735 | ret = lxc_write_nointr(fd, mount_entry, len); |
0fd73091 | 2736 | if (ret != len) |
79bcf5ee | 2737 | return NULL; |
0fd73091 | 2738 | |
489f39be | 2739 | ret = lxc_write_nointr(fd, "\n", 1); |
0fd73091 | 2740 | if (ret != 1) |
79bcf5ee | 2741 | return NULL; |
e7938e9e MN |
2742 | } |
2743 | ||
1800f924 WB |
2744 | if (include_nesting_helpers) { |
2745 | ret = lxc_write_nointr(fd, nesting_helpers, | |
6333c915 CB |
2746 | STRARRAYLEN(nesting_helpers)); |
2747 | if (ret != STRARRAYLEN(nesting_helpers)) | |
79bcf5ee | 2748 | return NULL; |
1800f924 WB |
2749 | } |
2750 | ||
0fd73091 CB |
2751 | ret = lseek(fd, 0, SEEK_SET); |
2752 | if (ret < 0) | |
79bcf5ee | 2753 | return NULL; |
0fd73091 | 2754 | |
4110345b CB |
2755 | f = fdopen(fd, "re+"); |
2756 | if (f) | |
2757 | move_fd(fd); /* Transfer ownership of fd. */ | |
2758 | return f; | |
9fc7f8c0 TA |
2759 | } |
2760 | ||
06749971 | 2761 | static int setup_mount_entries(const struct lxc_conf *conf, |
48e5dcc8 CB |
2762 | struct lxc_rootfs *rootfs, struct lxc_list *mount, |
2763 | const char *lxc_name, const char *lxc_path) | |
9fc7f8c0 | 2764 | { |
c85ced65 | 2765 | __do_fclose FILE *f = NULL; |
9fc7f8c0 | 2766 | |
1800f924 | 2767 | f = make_anonymous_mount_file(mount, conf->lsm_aa_allow_nesting); |
19b5d755 | 2768 | if (!f) |
9fc7f8c0 | 2769 | return -1; |
e7938e9e | 2770 | |
a7c6e830 | 2771 | return mount_file_entries(rootfs, f, lxc_name, lxc_path); |
e7938e9e MN |
2772 | } |
2773 | ||
1b82d721 CB |
2774 | static int __lxc_idmapped_mounts_child(struct lxc_handler *handler, FILE *f) |
2775 | { | |
2776 | struct lxc_conf *conf = handler->conf; | |
2777 | struct lxc_rootfs *rootfs = &conf->rootfs; | |
5a782dca | 2778 | int mnt_seq = 0; |
1b82d721 CB |
2779 | int ret; |
2780 | char buf[PATH_MAX]; | |
2781 | struct mntent mntent; | |
2782 | ||
2783 | while (getmntent_r(f, &mntent, buf, sizeof(buf))) { | |
2784 | __do_close int fd_from = -EBADF, fd_to = -EBADF, | |
2785 | fd_userns = -EBADF; | |
2786 | __do_free char *__data = NULL; | |
5a782dca | 2787 | int cur_mnt_seq = -1; |
1b82d721 CB |
2788 | struct lxc_mount_options opts = {}; |
2789 | int dfd_from; | |
2790 | const char *source_relative, *target_relative; | |
2791 | ||
2792 | ret = parse_lxc_mount_attrs(&opts, mntent.mnt_opts); | |
2793 | if (ret < 0) | |
2794 | return syserror("Failed to parse LXC specific mount options"); | |
2795 | __data = opts.data; | |
2796 | ||
2797 | ret = parse_mount_attrs(&opts, mntent.mnt_opts); | |
2798 | if (ret < 0) | |
2799 | return syserror("Failed to parse mount options"); | |
2800 | ||
2801 | /* No idmapped mount entry so skip it. */ | |
2802 | if (is_empty_string(opts.userns_path)) | |
2803 | continue; | |
2804 | ||
2805 | if (!can_use_bind_mounts()) | |
2806 | return syserror_set(-EINVAL, "Kernel does not support idmapped mounts"); | |
2807 | ||
2808 | if (!opts.bind) | |
2809 | return syserror_set(-EINVAL, "Only bind mounts can currently be idmapped"); | |
2810 | ||
2811 | /* We don't support new filesystem mounts yet. */ | |
2812 | if (!is_empty_string(mntent.mnt_type) && | |
2813 | !strequal(mntent.mnt_type, "none")) | |
2814 | return syserror_set(-EINVAL, "Only bind mounts can currently be idmapped"); | |
2815 | ||
2816 | /* Someone specified additional mount options for a bind-mount. */ | |
2817 | if (!is_empty_string(opts.data)) | |
2818 | return syserror_set(-EINVAL, "Bind mounts don't support non-generic mount options"); | |
2819 | ||
2820 | /* | |
2821 | * The source path is supposed to be taken relative to the | |
2822 | * container's rootfs mount or - if the container does not have | |
2823 | * a separate rootfs - to the host's /. | |
2824 | */ | |
2825 | source_relative = deabs(mntent.mnt_fsname); | |
2826 | if (opts.relative || !rootfs->path) | |
2827 | dfd_from = rootfs->dfd_mnt; | |
2828 | else | |
2829 | dfd_from = rootfs->dfd_host; | |
2830 | fd_from = open_tree(dfd_from, source_relative, | |
2831 | OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC | | |
2832 | (opts.recursive ? AT_RECURSIVE : 0)); | |
2833 | if (fd_from < 0) | |
2834 | return syserror("Failed to create detached %smount of %d/%s", | |
2835 | opts.recursive ? "recursive " : "", | |
2836 | dfd_from, source_relative); | |
2837 | ||
2838 | if (strequal(opts.userns_path, "container")) | |
2839 | fd_userns = openat(dfd_from, "proc/self/ns/user", O_RDONLY | O_CLOEXEC); | |
2840 | else | |
2841 | fd_userns = open_at(-EBADF, opts.userns_path, | |
2842 | PROTECT_OPEN_WITH_TRAILING_SYMLINKS, 0, 0); | |
2843 | if (fd_userns < 0) { | |
2844 | if (opts.optional) { | |
2845 | TRACE("Skipping optional idmapped mount"); | |
2846 | continue; | |
2847 | } | |
2848 | ||
2849 | return syserror("Failed to open user namespace \"%s\" for detached %smount of %d/%s", | |
2850 | opts.userns_path, opts.recursive ? "recursive " : "", | |
2851 | dfd_from, source_relative); | |
2852 | } | |
2853 | ||
2854 | ret = __lxc_abstract_unix_send_two_fds(handler->data_sock[0], | |
2855 | fd_from, fd_userns, | |
2856 | &opts, sizeof(opts)); | |
2857 | if (ret <= 0) { | |
2858 | if (opts.optional) { | |
2859 | TRACE("Skipping optional idmapped mount"); | |
2860 | continue; | |
2861 | } | |
2862 | ||
2863 | return syserror("Failed to send file descriptor %d for detached %smount of %d/%s and file descriptor %d of user namespace \"%s\" to parent", | |
2864 | fd_from, opts.recursive ? "recursive " : "", | |
2865 | dfd_from, source_relative, fd_userns, | |
2866 | opts.userns_path); | |
2867 | } | |
2868 | ||
5a782dca CB |
2869 | ret = lxc_abstract_unix_rcv_credential(handler->data_sock[0], |
2870 | &cur_mnt_seq, | |
2871 | sizeof(cur_mnt_seq)); | |
1b82d721 CB |
2872 | if (ret <= 0) { |
2873 | if (opts.optional) { | |
2874 | TRACE("Skipping optional idmapped mount"); | |
2875 | continue; | |
2876 | } | |
2877 | ||
2878 | return syserror("Failed to receive notification that parent idmapped detached %smount %d/%s to user namespace %d", | |
2879 | opts.recursive ? "recursive " : "", | |
2880 | dfd_from, source_relative, fd_userns); | |
2881 | } | |
2882 | ||
5a782dca CB |
2883 | if (mnt_seq != cur_mnt_seq) |
2884 | return syserror("Expected mount sequence number and mount sequence number from parent mismatch: %d != %d", | |
2885 | mnt_seq, cur_mnt_seq); | |
2886 | mnt_seq++; | |
2887 | ||
1b82d721 CB |
2888 | /* Set remaining mount options. */ |
2889 | ret = mount_setattr(fd_from, "", AT_EMPTY_PATH | | |
2890 | (opts.recursive ? AT_RECURSIVE : 0), | |
2891 | &opts.attr, sizeof(opts.attr)); | |
2892 | if (ret < 0) { | |
2893 | if (opts.optional) { | |
2894 | TRACE("Skipping optional idmapped mount"); | |
2895 | continue; | |
2896 | } | |
2897 | ||
2898 | return syserror("Failed to receive notification that parent idmapped detached %smount %d/%s to user namespace %d", | |
2899 | opts.recursive ? "recursive " : "", | |
2900 | dfd_from, source_relative, fd_userns); | |
2901 | } | |
2902 | ||
2903 | /* | |
2904 | * In contrast to the legacy mount codepath we will simplify | |
2905 | * our lifes and just always treat the target mountpoint to be | |
2906 | * relative to the container's rootfs mountpoint or - if the | |
2907 | * container does not have a separate rootfs - to the host's /. | |
2908 | */ | |
2909 | ||
2910 | target_relative = deabs(mntent.mnt_dir); | |
2911 | if (rootfs->path) | |
2912 | dfd_from = rootfs->dfd_mnt; | |
2913 | else | |
2914 | dfd_from = rootfs->dfd_host; | |
2915 | fd_to = open_at(dfd_from, target_relative, PROTECT_OPATH_FILE, PROTECT_LOOKUP_BENEATH_WITH_SYMLINKS, 0); | |
2916 | if (fd_to < 0) { | |
2917 | if (opts.optional) { | |
2918 | TRACE("Skipping optional idmapped mount"); | |
2919 | continue; | |
2920 | } | |
2921 | ||
2922 | return syserror("Failed to open target mountpoint %d/%s for detached idmapped %smount %d:%d/%s", | |
2923 | dfd_from, target_relative, | |
2924 | opts.recursive ? "recursive " : "", | |
2925 | fd_userns, dfd_from, source_relative); | |
2926 | } | |
2927 | ||
2928 | ret = move_detached_mount(fd_from, fd_to, "", 0, 0); | |
2929 | if (ret) { | |
2930 | if (opts.optional) { | |
2931 | TRACE("Skipping optional idmapped mount"); | |
2932 | continue; | |
2933 | } | |
2934 | ||
2935 | return syserror("Failed to attach detached idmapped %smount %d:%d/%s to target mountpoint %d/%s", | |
2936 | opts.recursive ? "recursive " : "", | |
2937 | fd_userns, dfd_from, source_relative, dfd_from, target_relative); | |
2938 | } | |
2939 | ||
2940 | TRACE("Attached detached idmapped %smount %d:%d/%s to target mountpoint %d/%s", | |
2941 | opts.recursive ? "recursive " : "", fd_userns, dfd_from, | |
2942 | source_relative, dfd_from, target_relative); | |
2943 | } | |
2944 | ||
2945 | if (!feof(f) || ferror(f)) | |
2946 | return syserror_set(-EINVAL, "Failed to parse mount entries"); | |
2947 | ||
2948 | return 0; | |
2949 | } | |
2950 | ||
2951 | static int lxc_idmapped_mounts_child(struct lxc_handler *handler) | |
2952 | { | |
2953 | __do_fclose FILE *f_entries = NULL; | |
2954 | int fret = -1; | |
2955 | struct lxc_conf *conf = handler->conf; | |
2956 | const char *fstab = conf->fstab; | |
2957 | struct lxc_list *mount = &conf->mount_list; | |
2958 | int ret; | |
2959 | ||
2960 | f_entries = make_anonymous_mount_file(mount, conf->lsm_aa_allow_nesting); | |
2961 | if (!f_entries) { | |
2962 | SYSERROR("Failed to create anonymous mount file"); | |
2963 | goto out; | |
2964 | } | |
2965 | ||
2966 | ret = __lxc_idmapped_mounts_child(handler, f_entries); | |
2967 | if (ret) { | |
2968 | SYSERROR("Failed to setup idmapped mount entries"); | |
2969 | goto out; | |
2970 | } | |
2971 | ||
2972 | TRACE("Finished setting up idmapped mounts"); | |
2973 | ||
2974 | if (fstab) { | |
2975 | __do_endmntent FILE *f_fstab = NULL; | |
2976 | ||
2977 | f_fstab = setmntent(fstab, "re"); | |
2978 | if (!f_fstab) { | |
2979 | SYSERROR("Failed to open fstab format file \"%s\"", fstab); | |
2980 | goto out; | |
2981 | } | |
2982 | ||
2983 | ret = __lxc_idmapped_mounts_child(handler, f_fstab); | |
2984 | if (ret) { | |
2985 | SYSERROR("Failed to setup idmapped mount entries specified in fstab"); | |
2986 | goto out; | |
2987 | } | |
2988 | ||
2989 | TRACE("Finished setting up idmapped mounts specified in fstab"); | |
2990 | } | |
2991 | ||
2992 | fret = 0; | |
2993 | ||
2994 | out: | |
2995 | ret = lxc_abstract_unix_send_credential(handler->data_sock[0], NULL, 0); | |
2996 | if (ret < 0) | |
2997 | return syserror("Failed to inform child that we are done setting up mounts"); | |
1b82d721 CB |
2998 | |
2999 | return fret; | |
3000 | } | |
3001 | ||
bab88e68 CS |
3002 | static int parse_cap(const char *cap) |
3003 | { | |
84760c11 | 3004 | size_t i; |
3005 | int capid = -1; | |
0fd73091 CB |
3006 | size_t end = sizeof(caps_opt) / sizeof(caps_opt[0]); |
3007 | char *ptr = NULL; | |
bab88e68 | 3008 | |
71528742 | 3009 | if (strequal(cap, "none")) |
7035407c DE |
3010 | return -2; |
3011 | ||
8560cd36 | 3012 | for (i = 0; i < end; i++) { |
71528742 | 3013 | if (!strequal(cap, caps_opt[i].name)) |
bab88e68 CS |
3014 | continue; |
3015 | ||
3016 | capid = caps_opt[i].value; | |
3017 | break; | |
3018 | } | |
3019 | ||
3020 | if (capid < 0) { | |
0fd73091 CB |
3021 | /* Try to see if it's numeric, so the user may specify |
3022 | * capabilities that the running kernel knows about but we | |
3023 | * don't | |
3024 | */ | |
bab88e68 CS |
3025 | errno = 0; |
3026 | capid = strtol(cap, &ptr, 10); | |
3027 | if (!ptr || *ptr != '\0' || errno != 0) | |
3028 | /* not a valid number */ | |
3029 | capid = -1; | |
3030 | else if (capid > lxc_caps_last_cap()) | |
3031 | /* we have a number but it's not a valid | |
3032 | * capability */ | |
3033 | capid = -1; | |
3034 | } | |
3035 | ||
3036 | return capid; | |
3037 | } | |
3038 | ||
0769b82a CS |
3039 | int in_caplist(int cap, struct lxc_list *caps) |
3040 | { | |
0769b82a | 3041 | int capid; |
0fd73091 | 3042 | struct lxc_list *iterator; |
0769b82a | 3043 | |
0fd73091 | 3044 | lxc_list_for_each (iterator, caps) { |
0769b82a CS |
3045 | capid = parse_cap(iterator->elem); |
3046 | if (capid == cap) | |
3047 | return 1; | |
3048 | } | |
3049 | ||
3050 | return 0; | |
3051 | } | |
3052 | ||
81810dd1 DL |
3053 | static int setup_caps(struct lxc_list *caps) |
3054 | { | |
bab88e68 | 3055 | int capid; |
0fd73091 CB |
3056 | char *drop_entry; |
3057 | struct lxc_list *iterator; | |
81810dd1 | 3058 | |
0fd73091 CB |
3059 | lxc_list_for_each (iterator, caps) { |
3060 | int ret; | |
81810dd1 DL |
3061 | |
3062 | drop_entry = iterator->elem; | |
3063 | ||
bab88e68 | 3064 | capid = parse_cap(drop_entry); |
55022530 CB |
3065 | if (capid < 0) |
3066 | return log_error(-1, "unknown capability %s", drop_entry); | |
81810dd1 | 3067 | |
b81689a1 CB |
3068 | ret = prctl(PR_CAPBSET_DROP, prctl_arg(capid), prctl_arg(0), |
3069 | prctl_arg(0), prctl_arg(0)); | |
55022530 CB |
3070 | if (ret < 0) |
3071 | return log_error_errno(-1, errno, "Failed to remove %s capability", drop_entry); | |
0fd73091 | 3072 | DEBUG("Dropped %s (%d) capability", drop_entry, capid); |
81810dd1 DL |
3073 | } |
3074 | ||
0fd73091 | 3075 | DEBUG("Capabilities have been setup"); |
1fb86a7c SH |
3076 | return 0; |
3077 | } | |
3078 | ||
3079 | static int dropcaps_except(struct lxc_list *caps) | |
3080 | { | |
2f443e88 | 3081 | __do_free int *caplist = NULL; |
0fd73091 | 3082 | int i, capid, numcaps; |
1fb86a7c | 3083 | char *keep_entry; |
0fd73091 | 3084 | struct lxc_list *iterator; |
1fb86a7c | 3085 | |
0fd73091 | 3086 | numcaps = lxc_caps_last_cap() + 1; |
2caf9a97 SH |
3087 | if (numcaps <= 0 || numcaps > 200) |
3088 | return -1; | |
0fd73091 | 3089 | TRACE("Found %d capabilities", numcaps); |
2caf9a97 | 3090 | |
1a0e70ac | 3091 | /* caplist[i] is 1 if we keep capability i */ |
2f443e88 | 3092 | caplist = must_realloc(NULL, numcaps * sizeof(int)); |
1fb86a7c SH |
3093 | memset(caplist, 0, numcaps * sizeof(int)); |
3094 | ||
0fd73091 | 3095 | lxc_list_for_each (iterator, caps) { |
1fb86a7c SH |
3096 | keep_entry = iterator->elem; |
3097 | ||
bab88e68 | 3098 | capid = parse_cap(keep_entry); |
7035407c DE |
3099 | if (capid == -2) |
3100 | continue; | |
3101 | ||
55022530 CB |
3102 | if (capid < 0) |
3103 | return log_error(-1, "Unknown capability %s", keep_entry); | |
1fb86a7c | 3104 | |
0fd73091 | 3105 | DEBUG("Keep capability %s (%d)", keep_entry, capid); |
1fb86a7c SH |
3106 | caplist[capid] = 1; |
3107 | } | |
0fd73091 CB |
3108 | |
3109 | for (i = 0; i < numcaps; i++) { | |
3110 | int ret; | |
3111 | ||
1fb86a7c SH |
3112 | if (caplist[i]) |
3113 | continue; | |
0fd73091 | 3114 | |
b81689a1 CB |
3115 | ret = prctl(PR_CAPBSET_DROP, prctl_arg(i), prctl_arg(0), |
3116 | prctl_arg(0), prctl_arg(0)); | |
55022530 CB |
3117 | if (ret < 0) |
3118 | return log_error_errno(-1, errno, "Failed to remove capability %d", i); | |
1fb86a7c SH |
3119 | } |
3120 | ||
0fd73091 | 3121 | DEBUG("Capabilities have been setup"); |
81810dd1 DL |
3122 | return 0; |
3123 | } | |
3124 | ||
0fd73091 CB |
3125 | static int parse_resource(const char *res) |
3126 | { | |
3127 | int ret; | |
c6d09e15 WB |
3128 | size_t i; |
3129 | int resid = -1; | |
3130 | ||
0fd73091 | 3131 | for (i = 0; i < sizeof(limit_opt) / sizeof(limit_opt[0]); ++i) |
71528742 | 3132 | if (strequal(res, limit_opt[i].name)) |
c6d09e15 | 3133 | return limit_opt[i].value; |
c6d09e15 | 3134 | |
0fd73091 | 3135 | /* Try to see if it's numeric, so the user may specify |
c6d09e15 | 3136 | * resources that the running kernel knows about but |
0fd73091 CB |
3137 | * we don't. |
3138 | */ | |
3139 | ret = lxc_safe_int(res, &resid); | |
3140 | if (ret < 0) | |
3141 | return -1; | |
3142 | ||
3143 | return resid; | |
c6d09e15 WB |
3144 | } |
3145 | ||
0fd73091 CB |
3146 | int setup_resource_limits(struct lxc_list *limits, pid_t pid) |
3147 | { | |
3148 | int resid; | |
c6d09e15 WB |
3149 | struct lxc_list *it; |
3150 | struct lxc_limit *lim; | |
c6d09e15 | 3151 | |
0fd73091 | 3152 | lxc_list_for_each (it, limits) { |
c6d09e15 WB |
3153 | lim = it->elem; |
3154 | ||
3155 | resid = parse_resource(lim->resource); | |
55022530 CB |
3156 | if (resid < 0) |
3157 | return log_error(-1, "Unknown resource %s", lim->resource); | |
c6d09e15 | 3158 | |
f48b5fd8 | 3159 | #if HAVE_PRLIMIT || HAVE_PRLIMIT64 |
55022530 CB |
3160 | if (prlimit(pid, resid, &lim->limit, NULL) != 0) |
3161 | return log_error_errno(-1, errno, "Failed to set limit %s", lim->resource); | |
2de12765 CB |
3162 | |
3163 | TRACE("Setup \"%s\" limit", lim->resource); | |
f48b5fd8 | 3164 | #else |
55022530 | 3165 | return log_error(-1, "Cannot set limit \"%s\" as prlimit is missing", lim->resource); |
f48b5fd8 | 3166 | #endif |
c6d09e15 | 3167 | } |
0fd73091 | 3168 | |
c6d09e15 WB |
3169 | return 0; |
3170 | } | |
3171 | ||
7edd0540 L |
3172 | int setup_sysctl_parameters(struct lxc_list *sysctls) |
3173 | { | |
e6f76452 | 3174 | __do_free char *tmp = NULL; |
7edd0540 L |
3175 | struct lxc_list *it; |
3176 | struct lxc_sysctl *elem; | |
0fd73091 | 3177 | int ret = 0; |
6b5a54cd | 3178 | char filename[PATH_MAX] = {0}; |
7edd0540 | 3179 | |
0fd73091 | 3180 | lxc_list_for_each (it, sysctls) { |
7edd0540 L |
3181 | elem = it->elem; |
3182 | tmp = lxc_string_replace(".", "/", elem->key); | |
55022530 CB |
3183 | if (!tmp) |
3184 | return log_error(-1, "Failed to replace key %s", elem->key); | |
7edd0540 | 3185 | |
9bcde680 CB |
3186 | ret = strnprintf(filename, sizeof(filename), "/proc/sys/%s", tmp); |
3187 | if (ret < 0) | |
55022530 | 3188 | return log_error(-1, "Error setting up sysctl parameters path"); |
7edd0540 | 3189 | |
0fd73091 | 3190 | ret = lxc_write_to_file(filename, elem->value, |
7cea5905 | 3191 | strlen(elem->value), false, 0666); |
55022530 CB |
3192 | if (ret < 0) |
3193 | return log_error_errno(-1, errno, "Failed to setup sysctl parameters %s to %s", | |
3194 | elem->key, elem->value); | |
7edd0540 | 3195 | } |
0fd73091 | 3196 | |
7edd0540 L |
3197 | return 0; |
3198 | } | |
3199 | ||
61d7a733 YT |
3200 | int setup_proc_filesystem(struct lxc_list *procs, pid_t pid) |
3201 | { | |
0c669152 | 3202 | __do_free char *tmp = NULL; |
61d7a733 YT |
3203 | struct lxc_list *it; |
3204 | struct lxc_proc *elem; | |
0fd73091 | 3205 | int ret = 0; |
6b5a54cd | 3206 | char filename[PATH_MAX] = {0}; |
61d7a733 | 3207 | |
0fd73091 | 3208 | lxc_list_for_each (it, procs) { |
61d7a733 YT |
3209 | elem = it->elem; |
3210 | tmp = lxc_string_replace(".", "/", elem->filename); | |
55022530 CB |
3211 | if (!tmp) |
3212 | return log_error(-1, "Failed to replace key %s", elem->filename); | |
61d7a733 | 3213 | |
9bcde680 CB |
3214 | ret = strnprintf(filename, sizeof(filename), "/proc/%d/%s", pid, tmp); |
3215 | if (ret < 0) | |
55022530 | 3216 | return log_error(-1, "Error setting up proc filesystem path"); |
61d7a733 | 3217 | |
0fd73091 | 3218 | ret = lxc_write_to_file(filename, elem->value, |
7cea5905 | 3219 | strlen(elem->value), false, 0666); |
55022530 CB |
3220 | if (ret < 0) |
3221 | return log_error_errno(-1, errno, "Failed to setup proc filesystem %s to %s", elem->filename, elem->value); | |
61d7a733 | 3222 | } |
0fd73091 | 3223 | |
61d7a733 YT |
3224 | return 0; |
3225 | } | |
3226 | ||
ae9242c8 SH |
3227 | static char *default_rootfs_mount = LXCROOTFSMOUNT; |
3228 | ||
7b379ab3 | 3229 | struct lxc_conf *lxc_conf_init(void) |
089cd8b8 | 3230 | { |
26ddeedd | 3231 | int i; |
0fd73091 | 3232 | struct lxc_conf *new; |
7b379ab3 | 3233 | |
b8e43ef0 | 3234 | new = zalloc(sizeof(*new)); |
0fd73091 | 3235 | if (!new) |
7b379ab3 | 3236 | return NULL; |
7b379ab3 | 3237 | |
4b73005c | 3238 | new->loglevel = LXC_LOG_LEVEL_NOTSET; |
9c601e1f | 3239 | new->personality = LXC_ARCH_UNCHANGED; |
124fa0a8 | 3240 | new->autodev = 1; |
3a784510 | 3241 | new->console.buffer_size = 0; |
596a818d DE |
3242 | new->console.log_path = NULL; |
3243 | new->console.log_fd = -1; | |
861813e5 | 3244 | new->console.log_size = 0; |
28a4b0e5 | 3245 | new->console.path = NULL; |
63376d7d | 3246 | new->console.peer = -1; |
fb87aa6a | 3247 | new->console.proxy.busy = -1; |
36a94ce8 | 3248 | new->console.proxy.ptx = -1; |
41808e20 | 3249 | new->console.proxy.pty = -1; |
f3dff080 CB |
3250 | new->console.ptx = -EBADF; |
3251 | new->console.pty = -EBADF; | |
d926c261 | 3252 | new->console.pty_nr = -1; |
63376d7d | 3253 | new->console.name[0] = '\0'; |
7294a26d | 3254 | new->devpts_fd = -EBADF; |
732375f5 | 3255 | memset(&new->console.ringbuf, 0, sizeof(struct lxc_ringbuf)); |
d2e30e99 | 3256 | new->maincmd_fd = -1; |
258f8051 | 3257 | new->monitor_signal_pdeath = SIGKILL; |
76a26f55 | 3258 | new->nbd_idx = -1; |
54c30e29 | 3259 | new->rootfs.mount = strdup(default_rootfs_mount); |
53f3f048 | 3260 | if (!new->rootfs.mount) { |
53f3f048 SH |
3261 | free(new); |
3262 | return NULL; | |
3263 | } | |
6e54330c | 3264 | new->rootfs.managed = true; |
ea57e424 | 3265 | new->rootfs.dfd_mnt = -EBADF; |
a5a08920 | 3266 | new->rootfs.dfd_dev = -EBADF; |
ea11a215 | 3267 | new->rootfs.dfd_host = -EBADF; |
79ff643d | 3268 | new->rootfs.fd_path_pin = -EBADF; |
4b875ef9 | 3269 | new->rootfs.dfd_idmapped = -EBADF; |
858377e4 | 3270 | new->logfd = -1; |
7b379ab3 | 3271 | lxc_list_init(&new->cgroup); |
54860ed0 | 3272 | lxc_list_init(&new->cgroup2); |
a134099d CB |
3273 | /* Block ("allowlist") all devices by default. */ |
3274 | new->bpf_devices.list_type = LXC_BPF_DEVICE_CGROUP_ALLOWLIST; | |
3275 | lxc_list_init(&(new->bpf_devices).device_item); | |
7b379ab3 MN |
3276 | lxc_list_init(&new->network); |
3277 | lxc_list_init(&new->mount_list); | |
81810dd1 | 3278 | lxc_list_init(&new->caps); |
1fb86a7c | 3279 | lxc_list_init(&new->keepcaps); |
f6d3e3e4 | 3280 | lxc_list_init(&new->id_map); |
46ad64ab CB |
3281 | new->root_nsuid_map = NULL; |
3282 | new->root_nsgid_map = NULL; | |
f979ac15 | 3283 | lxc_list_init(&new->includes); |
4184c3e1 | 3284 | lxc_list_init(&new->aliens); |
7c661726 | 3285 | lxc_list_init(&new->environment); |
c6d09e15 | 3286 | lxc_list_init(&new->limits); |
7edd0540 | 3287 | lxc_list_init(&new->sysctls); |
61d7a733 | 3288 | lxc_list_init(&new->procs); |
44ae0fb6 | 3289 | new->hooks_version = 0; |
28d9e29e | 3290 | for (i = 0; i < NUM_LXC_HOOKS; i++) |
26ddeedd | 3291 | lxc_list_init(&new->hooks[i]); |
ee1e7aa0 | 3292 | lxc_list_init(&new->groups); |
d39b10eb | 3293 | lxc_list_init(&new->state_clients); |
fe4de9a6 | 3294 | new->lsm_aa_profile = NULL; |
1800f924 | 3295 | lxc_list_init(&new->lsm_aa_raw); |
fe4de9a6 | 3296 | new->lsm_se_context = NULL; |
4fef78bc | 3297 | new->lsm_se_keyring_context = NULL; |
8f818a84 | 3298 | new->keyring_disable_session = false; |
952b5031 | 3299 | new->transient_procfs_mnt = false; |
7a41e857 LT |
3300 | new->shmount.path_host = NULL; |
3301 | new->shmount.path_cont = NULL; | |
7b379ab3 | 3302 | |
72bb04e4 PT |
3303 | /* if running in a new user namespace, init and COMMAND |
3304 | * default to running as UID/GID 0 when using lxc-execute */ | |
3305 | new->init_uid = 0; | |
3306 | new->init_gid = 0; | |
c71f64cb | 3307 | memset(&new->init_groups, 0, sizeof(lxc_groups_t)); |
43654d34 | 3308 | memset(&new->cgroup_meta, 0, sizeof(struct lxc_cgroup)); |
b074bbf1 | 3309 | memset(&new->ns_share, 0, sizeof(char *) * LXC_NS_MAX); |
70fd7fc9 | 3310 | memset(&new->timens, 0, sizeof(struct timens_offsets)); |
c3e3c21a | 3311 | seccomp_conf_init(new); |
72bb04e4 | 3312 | |
7b379ab3 | 3313 | return new; |
089cd8b8 DL |
3314 | } |
3315 | ||
344c9d81 | 3316 | int write_id_mapping(enum idtype idtype, pid_t pid, const char *buf, |
a19b974f | 3317 | size_t buf_size) |
f6d3e3e4 | 3318 | { |
f62cf1d4 | 3319 | __do_close int fd = -EBADF; |
76bcd422 | 3320 | int ret; |
6b5a54cd | 3321 | char path[PATH_MAX]; |
f6d3e3e4 | 3322 | |
a19b974f | 3323 | if (geteuid() != 0 && idtype == ID_TYPE_GID) { |
f62cf1d4 | 3324 | __do_close int setgroups_fd = -EBADF; |
a19b974f | 3325 | |
9bcde680 CB |
3326 | ret = strnprintf(path, sizeof(path), "/proc/%d/setgroups", pid); |
3327 | if (ret < 0) | |
a19b974f | 3328 | return -E2BIG; |
a19b974f | 3329 | |
76bcd422 | 3330 | setgroups_fd = open(path, O_WRONLY); |
55022530 CB |
3331 | if (setgroups_fd < 0 && errno != ENOENT) |
3332 | return log_error_errno(-1, errno, "Failed to open \"%s\"", path); | |
a19b974f | 3333 | |
76bcd422 CB |
3334 | if (setgroups_fd >= 0) { |
3335 | ret = lxc_write_nointr(setgroups_fd, "deny\n", | |
3336 | STRLITERALLEN("deny\n")); | |
55022530 CB |
3337 | if (ret != STRLITERALLEN("deny\n")) |
3338 | return log_error_errno(-1, errno, "Failed to write \"deny\" to \"/proc/%d/setgroups\"", pid); | |
395b1a3e | 3339 | TRACE("Wrote \"deny\" to \"/proc/%d/setgroups\"", pid); |
a19b974f | 3340 | } |
a19b974f CB |
3341 | } |
3342 | ||
9bcde680 | 3343 | ret = strnprintf(path, sizeof(path), "/proc/%d/%cid_map", pid, |
29053180 | 3344 | idtype == ID_TYPE_UID ? 'u' : 'g'); |
9bcde680 | 3345 | if (ret < 0) |
f6d3e3e4 | 3346 | return -E2BIG; |
29053180 | 3347 | |
55022530 CB |
3348 | fd = open(path, O_WRONLY | O_CLOEXEC); |
3349 | if (fd < 0) | |
3350 | return log_error_errno(-1, errno, "Failed to open \"%s\"", path); | |
29053180 | 3351 | |
29053180 | 3352 | ret = lxc_write_nointr(fd, buf, buf_size); |
55022530 CB |
3353 | if (ret != buf_size) |
3354 | return log_error_errno(-1, errno, "Failed to write %cid mapping to \"%s\"", | |
3355 | idtype == ID_TYPE_UID ? 'u' : 'g', path); | |
29053180 CB |
3356 | |
3357 | return 0; | |
f6d3e3e4 SH |
3358 | } |
3359 | ||
6e50e704 CB |
3360 | /* Check whether a binary exist and has either CAP_SETUID, CAP_SETGID or both. |
3361 | * | |
3362 | * @return 1 if functional binary was found | |
3363 | * @return 0 if binary exists but is lacking privilege | |
3364 | * @return -ENOENT if binary does not exist | |
3365 | * @return -EINVAL if cap to check is neither CAP_SETUID nor CAP_SETGID | |
6e50e704 | 3366 | */ |
df6a2945 CB |
3367 | static int idmaptool_on_path_and_privileged(const char *binary, cap_value_t cap) |
3368 | { | |
48411df2 | 3369 | __do_free char *path = NULL; |
df6a2945 CB |
3370 | int ret; |
3371 | struct stat st; | |
df6a2945 | 3372 | |
6e50e704 | 3373 | if (cap != CAP_SETUID && cap != CAP_SETGID) |
83cb7362 | 3374 | return ret_errno(EINVAL); |
6e50e704 | 3375 | |
df6a2945 CB |
3376 | path = on_path(binary, NULL); |
3377 | if (!path) | |
83cb7362 | 3378 | return ret_errno(ENOENT); |
df6a2945 CB |
3379 | |
3380 | ret = stat(path, &st); | |
3275932b | 3381 | if (ret < 0) |
83cb7362 | 3382 | return -errno; |
df6a2945 CB |
3383 | |
3384 | /* Check if the binary is setuid. */ | |
55022530 CB |
3385 | if (st.st_mode & S_ISUID) |
3386 | return log_debug(1, "The binary \"%s\" does have the setuid bit set", path); | |
df6a2945 | 3387 | |
0fd73091 | 3388 | #if HAVE_LIBCAP && LIBCAP_SUPPORTS_FILE_CAPABILITIES |
df6a2945 CB |
3389 | /* Check if it has the CAP_SETUID capability. */ |
3390 | if ((cap & CAP_SETUID) && | |
3391 | lxc_file_cap_is_set(path, CAP_SETUID, CAP_EFFECTIVE) && | |
55022530 CB |
3392 | lxc_file_cap_is_set(path, CAP_SETUID, CAP_PERMITTED)) |
3393 | return log_debug(1, "The binary \"%s\" has CAP_SETUID in its CAP_EFFECTIVE and CAP_PERMITTED sets", path); | |
df6a2945 CB |
3394 | |
3395 | /* Check if it has the CAP_SETGID capability. */ | |
3396 | if ((cap & CAP_SETGID) && | |
3397 | lxc_file_cap_is_set(path, CAP_SETGID, CAP_EFFECTIVE) && | |
55022530 CB |
3398 | lxc_file_cap_is_set(path, CAP_SETGID, CAP_PERMITTED)) |
3399 | return log_debug(1, "The binary \"%s\" has CAP_SETGID in its CAP_EFFECTIVE and CAP_PERMITTED sets", path); | |
a864a2e1 CB |
3400 | |
3401 | return 0; | |
0fd73091 | 3402 | #else |
83cb7362 CB |
3403 | /* |
3404 | * If we cannot check for file capabilities we need to give the benefit | |
69924fff CB |
3405 | * of the doubt. Otherwise we might fail even though all the necessary |
3406 | * file capabilities are set. | |
3407 | */ | |
55022530 | 3408 | DEBUG("Cannot check for file capabilities as full capability support is missing. Manual intervention needed"); |
3275932b | 3409 | return 1; |
a864a2e1 | 3410 | #endif |
df6a2945 CB |
3411 | } |
3412 | ||
59eac805 | 3413 | static int lxc_map_ids_exec_wrapper(void *args) |
986ef930 CB |
3414 | { |
3415 | execl("/bin/sh", "sh", "-c", (char *)args, (char *)NULL); | |
3416 | return -1; | |
3417 | } | |
3418 | ||
86c78011 CB |
3419 | static struct id_map *find_mapped_hostid_entry(const struct lxc_list *idmap, |
3420 | unsigned id, enum idtype idtype); | |
3421 | ||
f6d3e3e4 SH |
3422 | int lxc_map_ids(struct lxc_list *idmap, pid_t pid) |
3423 | { | |
0fd73091 | 3424 | int fill, left; |
986ef930 | 3425 | char u_or_g; |
4bc3b759 | 3426 | char *pos; |
6b5a54cd | 3427 | char cmd_output[PATH_MAX]; |
0fd73091 CB |
3428 | struct id_map *map; |
3429 | struct lxc_list *iterator; | |
3430 | enum idtype type; | |
0fd73091 | 3431 | int ret = 0, gidmap = 0, uidmap = 0; |
c6ba8981 CB |
3432 | char mapbuf[STRLITERALLEN("new@idmap") + STRLITERALLEN(" ") + |
3433 | INTTYPE_TO_STRLEN(pid_t) + STRLITERALLEN(" ") + | |
3434 | LXC_IDMAPLEN] = {0}; | |
86c78011 | 3435 | bool had_entry = false, maps_host_root = false, use_shadow = false; |
c724025c JC |
3436 | int hostuid, hostgid; |
3437 | ||
3438 | hostuid = geteuid(); | |
3439 | hostgid = getegid(); | |
df6a2945 | 3440 | |
86c78011 CB |
3441 | /* |
3442 | * Check whether caller wants to map host root. | |
3443 | * Due to a security fix newer kernels require CAP_SETFCAP when mapping | |
3444 | * host root into the child userns as you would be able to write fscaps | |
3445 | * that would be valid in the ancestor userns. Mapping host root should | |
3446 | * rarely be the case but LXC is being clever in a bunch of cases. | |
3447 | */ | |
3448 | if (find_mapped_hostid_entry(idmap, 0, ID_TYPE_UID)) | |
3449 | maps_host_root = true; | |
3450 | ||
df6a2945 CB |
3451 | /* If new{g,u}idmap exists, that is, if shadow is handing out subuid |
3452 | * ranges, then insist that root also reserve ranges in subuid. This | |
22038de5 SH |
3453 | * will protected it by preventing another user from being handed the |
3454 | * range by shadow. | |
3455 | */ | |
df6a2945 | 3456 | uidmap = idmaptool_on_path_and_privileged("newuidmap", CAP_SETUID); |
6e50e704 CB |
3457 | if (uidmap == -ENOENT) |
3458 | WARN("newuidmap binary is missing"); | |
3459 | else if (!uidmap) | |
3460 | WARN("newuidmap is lacking necessary privileges"); | |
3461 | ||
df6a2945 | 3462 | gidmap = idmaptool_on_path_and_privileged("newgidmap", CAP_SETGID); |
6e50e704 CB |
3463 | if (gidmap == -ENOENT) |
3464 | WARN("newgidmap binary is missing"); | |
3465 | else if (!gidmap) | |
3466 | WARN("newgidmap is lacking necessary privileges"); | |
3467 | ||
86c78011 CB |
3468 | if (maps_host_root) { |
3469 | INFO("Caller maps host root. Writing mapping directly"); | |
3470 | } else if (uidmap > 0 && gidmap > 0) { | |
0fd73091 | 3471 | DEBUG("Functional newuidmap and newgidmap binary found"); |
4bc3b759 | 3472 | use_shadow = true; |
df6a2945 | 3473 | } else { |
99d43365 CB |
3474 | /* In case unprivileged users run application containers via |
3475 | * execute() or a start*() there are valid cases where they may | |
3476 | * only want to map their own {g,u}id. Let's not block them from | |
3477 | * doing so by requiring geteuid() == 0. | |
3478 | */ | |
3479 | DEBUG("No newuidmap and newgidmap binary found. Trying to " | |
c724025c JC |
3480 | "write directly with euid %d", hostuid); |
3481 | } | |
3482 | ||
3483 | /* Check if we really need to use newuidmap and newgidmap. | |
f48e8071 | 3484 | * If the user is only remapping their own {g,u}id, we don't need it. |
c724025c JC |
3485 | */ |
3486 | if (use_shadow && lxc_list_len(idmap) == 2) { | |
3487 | use_shadow = false; | |
3488 | lxc_list_for_each(iterator, idmap) { | |
3489 | map = iterator->elem; | |
3490 | if (map->idtype == ID_TYPE_UID && map->range == 1 && | |
3491 | map->nsid == hostuid && map->hostid == hostuid) | |
3492 | continue; | |
3493 | if (map->idtype == ID_TYPE_GID && map->range == 1 && | |
3494 | map->nsid == hostgid && map->hostid == hostgid) | |
3495 | continue; | |
3496 | use_shadow = true; | |
3497 | break; | |
3498 | } | |
0e6e3a41 | 3499 | } |
251d0d2a | 3500 | |
986ef930 CB |
3501 | for (type = ID_TYPE_UID, u_or_g = 'u'; type <= ID_TYPE_GID; |
3502 | type++, u_or_g = 'g') { | |
3503 | pos = mapbuf; | |
3504 | ||
0e6e3a41 | 3505 | if (use_shadow) |
986ef930 | 3506 | pos += sprintf(mapbuf, "new%cidmap %d", u_or_g, pid); |
4f7521b4 | 3507 | |
cf3ef16d | 3508 | lxc_list_for_each(iterator, idmap) { |
251d0d2a | 3509 | map = iterator->elem; |
cf3ef16d SH |
3510 | if (map->idtype != type) |
3511 | continue; | |
3512 | ||
4bc3b759 CB |
3513 | had_entry = true; |
3514 | ||
986ef930 | 3515 | left = LXC_IDMAPLEN - (pos - mapbuf); |
9bcde680 | 3516 | fill = strnprintf(pos, left, "%s%lu %lu %lu%s", |
4bc3b759 CB |
3517 | use_shadow ? " " : "", map->nsid, |
3518 | map->hostid, map->range, | |
0e6e3a41 | 3519 | use_shadow ? "" : "\n"); |
55022530 CB |
3520 | /* |
3521 | * The kernel only takes <= 4k for writes to | |
3522 | * /proc/<pid>/{g,u}id_map | |
3523 | */ | |
9bcde680 | 3524 | if (fill <= 0) |
55022530 | 3525 | return log_error_errno(-1, errno, "Too many %cid mappings defined", u_or_g); |
4bc3b759 | 3526 | |
cf3ef16d | 3527 | pos += fill; |
251d0d2a | 3528 | } |
cf3ef16d | 3529 | if (!had_entry) |
4f7521b4 | 3530 | continue; |
cf3ef16d | 3531 | |
d85813cd | 3532 | /* Try to catch the output of new{g,u}idmap to make debugging |
986ef930 CB |
3533 | * easier. |
3534 | */ | |
3535 | if (use_shadow) { | |
3536 | ret = run_command(cmd_output, sizeof(cmd_output), | |
3537 | lxc_map_ids_exec_wrapper, | |
3538 | (void *)mapbuf); | |
55022530 CB |
3539 | if (ret < 0) |
3540 | return log_error(-1, "new%cidmap failed to write mapping \"%s\": %s", u_or_g, cmd_output, mapbuf); | |
54fbbeb5 | 3541 | TRACE("new%cidmap wrote mapping \"%s\"", u_or_g, mapbuf); |
d1838f34 | 3542 | } else { |
986ef930 | 3543 | ret = write_id_mapping(type, pid, mapbuf, pos - mapbuf); |
55022530 CB |
3544 | if (ret < 0) |
3545 | return log_error(-1, "Failed to write mapping: %s", mapbuf); | |
54fbbeb5 | 3546 | TRACE("Wrote mapping \"%s\"", mapbuf); |
d1838f34 | 3547 | } |
986ef930 CB |
3548 | |
3549 | memset(mapbuf, 0, sizeof(mapbuf)); | |
f6d3e3e4 | 3550 | } |
251d0d2a | 3551 | |
986ef930 | 3552 | return 0; |
f6d3e3e4 SH |
3553 | } |
3554 | ||
234998b4 CB |
3555 | /* |
3556 | * Return the host uid/gid to which the container root is mapped in val. | |
0b3a6504 | 3557 | * Return true if id was found, false otherwise. |
cf3ef16d | 3558 | */ |
234998b4 | 3559 | static id_t get_mapped_rootid(const struct lxc_conf *conf, enum idtype idtype) |
cf3ef16d | 3560 | { |
4160c3a0 | 3561 | unsigned nsid; |
0fd73091 CB |
3562 | struct id_map *map; |
3563 | struct lxc_list *it; | |
4160c3a0 CB |
3564 | |
3565 | if (idtype == ID_TYPE_UID) | |
3566 | nsid = (conf->root_nsuid_map != NULL) ? 0 : conf->init_uid; | |
3567 | else | |
3568 | nsid = (conf->root_nsgid_map != NULL) ? 0 : conf->init_gid; | |
cf3ef16d | 3569 | |
0fd73091 | 3570 | lxc_list_for_each (it, &conf->id_map) { |
cf3ef16d | 3571 | map = it->elem; |
7b50c609 | 3572 | if (map->idtype != idtype) |
cf3ef16d | 3573 | continue; |
4160c3a0 | 3574 | if (map->nsid != nsid) |
cf3ef16d | 3575 | continue; |
234998b4 | 3576 | return map->hostid; |
cf3ef16d | 3577 | } |
4160c3a0 | 3578 | |
234998b4 CB |
3579 | if (idtype == ID_TYPE_UID) |
3580 | return LXC_INVALID_UID; | |
3581 | ||
3582 | return LXC_INVALID_GID; | |
cf3ef16d SH |
3583 | } |
3584 | ||
facdf925 | 3585 | int mapped_hostid(unsigned id, const struct lxc_conf *conf, enum idtype idtype) |
cf3ef16d | 3586 | { |
cf3ef16d | 3587 | struct id_map *map; |
0fd73091 CB |
3588 | struct lxc_list *it; |
3589 | ||
3590 | lxc_list_for_each (it, &conf->id_map) { | |
cf3ef16d | 3591 | map = it->elem; |
2133f58c | 3592 | if (map->idtype != idtype) |
cf3ef16d | 3593 | continue; |
0fd73091 | 3594 | |
cf3ef16d | 3595 | if (id >= map->hostid && id < map->hostid + map->range) |
57d116ab | 3596 | return (id - map->hostid) + map->nsid; |
cf3ef16d | 3597 | } |
0fd73091 | 3598 | |
57d116ab | 3599 | return -1; |
cf3ef16d SH |
3600 | } |
3601 | ||
7581a82f | 3602 | int find_unmapped_nsid(const struct lxc_conf *conf, enum idtype idtype) |
cf3ef16d | 3603 | { |
cf3ef16d | 3604 | struct id_map *map; |
0fd73091 | 3605 | struct lxc_list *it; |
2133f58c | 3606 | unsigned int freeid = 0; |
0fd73091 | 3607 | |
cf3ef16d | 3608 | again: |
0fd73091 | 3609 | lxc_list_for_each (it, &conf->id_map) { |
cf3ef16d | 3610 | map = it->elem; |
2133f58c | 3611 | if (map->idtype != idtype) |
cf3ef16d | 3612 | continue; |
0fd73091 | 3613 | |
cf3ef16d SH |
3614 | if (freeid >= map->nsid && freeid < map->nsid + map->range) { |
3615 | freeid = map->nsid + map->range; | |
3616 | goto again; | |
3617 | } | |
3618 | } | |
0fd73091 | 3619 | |
cf3ef16d SH |
3620 | return freeid; |
3621 | } | |
3622 | ||
e1b9d6af CB |
3623 | /* |
3624 | * Mount a proc under @rootfs if proc self points to a pid other than | |
3625 | * my own. This is needed to have a known-good proc mount for setting | |
3626 | * up LSMs both at container startup and attach. | |
3627 | * | |
e1b9d6af CB |
3628 | * NOTE: not to be called from inside the container namespace! |
3629 | */ | |
952b5031 | 3630 | static int lxc_transient_proc(struct lxc_rootfs *rootfs) |
e1b9d6af | 3631 | { |
952b5031 CB |
3632 | __do_close int fd_proc = -EBADF; |
3633 | int link_to_pid, link_len, pid_self, ret; | |
3634 | char link[INTTYPE_TO_STRLEN(pid_t) + 1]; | |
e1b9d6af | 3635 | |
ea57e424 | 3636 | link_len = readlinkat(rootfs->dfd_mnt, "proc/self", link, sizeof(link)); |
952b5031 | 3637 | if (link_len < 0) { |
ea57e424 | 3638 | ret = mkdirat(rootfs->dfd_mnt, "proc", 0000); |
952b5031 | 3639 | if (ret < 0 && errno != EEXIST) |
ea57e424 | 3640 | return log_error_errno(-errno, errno, "Failed to create %d(proc)", rootfs->dfd_mnt); |
e1b9d6af | 3641 | |
952b5031 CB |
3642 | goto domount; |
3643 | } else if (link_len >= sizeof(link)) { | |
3644 | return log_error_errno(-EIO, EIO, "Truncated link target"); | |
e1b9d6af | 3645 | } |
952b5031 | 3646 | link[link_len] = '\0'; |
e1b9d6af | 3647 | |
952b5031 CB |
3648 | pid_self = lxc_raw_getpid(); |
3649 | INFO("Caller's PID is %d; /proc/self points to %s", pid_self, link); | |
e1b9d6af | 3650 | |
952b5031 CB |
3651 | ret = lxc_safe_int(link, &link_to_pid); |
3652 | if (ret) | |
3653 | return log_error_errno(-ret, ret, "Failed to parse %s", link); | |
e1b9d6af | 3654 | |
952b5031 CB |
3655 | /* Correct procfs is already mounted. */ |
3656 | if (link_to_pid == pid_self) | |
3657 | return log_trace(0, "Correct procfs instance mounted"); | |
e1b9d6af | 3658 | |
ea57e424 | 3659 | fd_proc = open_at(rootfs->dfd_mnt, "proc", PROTECT_OPATH_DIRECTORY, |
952b5031 CB |
3660 | PROTECT_LOOKUP_BENEATH_XDEV, 0); |
3661 | if (fd_proc < 0) | |
3662 | return log_error_errno(-errno, errno, "Failed to open transient procfs mountpoint"); | |
e1b9d6af | 3663 | |
9bcde680 CB |
3664 | ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "/proc/self/fd/%d", fd_proc); |
3665 | if (ret < 0) | |
952b5031 | 3666 | return ret_errno(EIO); |
e1b9d6af | 3667 | |
952b5031 | 3668 | ret = umount2(rootfs->buf, MNT_DETACH); |
e1b9d6af | 3669 | if (ret < 0) |
952b5031 | 3670 | SYSWARN("Failed to umount \"%s\" with MNT_DETACH", rootfs->buf); |
e1b9d6af CB |
3671 | |
3672 | domount: | |
3673 | /* rootfs is NULL */ | |
952b5031 CB |
3674 | if (!rootfs->path) { |
3675 | ret = mount("proc", rootfs->buf, "proc", 0, NULL); | |
3676 | } else { | |
ea57e424 | 3677 | ret = safe_mount_beneath_at(rootfs->dfd_mnt, "none", "proc", "proc", 0, NULL); |
952b5031 | 3678 | if (ret < 0) { |
9bcde680 CB |
3679 | ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/proc", rootfs->path ? rootfs->mount : ""); |
3680 | if (ret < 0) | |
952b5031 CB |
3681 | return ret_errno(EIO); |
3682 | ||
3683 | ret = safe_mount("proc", rootfs->buf, "proc", 0, NULL, rootfs->mount); | |
3684 | } | |
3685 | } | |
e1b9d6af | 3686 | if (ret < 0) |
952b5031 | 3687 | return log_error_errno(-1, errno, "Failed to mount temporary procfs"); |
e1b9d6af | 3688 | |
952b5031 | 3689 | INFO("Created transient procfs mount"); |
e1b9d6af CB |
3690 | return 1; |
3691 | } | |
3692 | ||
943144d9 | 3693 | /* NOTE: Must not be called from inside the container namespace! */ |
59eac805 | 3694 | static int lxc_create_tmp_proc_mount(struct lxc_conf *conf) |
5112cd70 SH |
3695 | { |
3696 | int mounted; | |
3697 | ||
952b5031 | 3698 | mounted = lxc_transient_proc(&conf->rootfs); |
5112cd70 | 3699 | if (mounted == -1) { |
01958b1f | 3700 | /* continue only if there is no rootfs */ |
943144d9 | 3701 | if (conf->rootfs.path) |
952b5031 | 3702 | return log_error_errno(-EPERM, EPERM, "Failed to create transient procfs mount"); |
5112cd70 | 3703 | } else if (mounted == 1) { |
952b5031 | 3704 | conf->transient_procfs_mnt = true; |
5112cd70 | 3705 | } |
943144d9 | 3706 | |
5112cd70 SH |
3707 | return 0; |
3708 | } | |
3709 | ||
3710 | void tmp_proc_unmount(struct lxc_conf *lxc_conf) | |
3711 | { | |
952b5031 CB |
3712 | if (lxc_conf->transient_procfs_mnt) { |
3713 | (void)umount2("/proc", MNT_DETACH); | |
3714 | lxc_conf->transient_procfs_mnt = false; | |
3715 | } | |
5112cd70 SH |
3716 | } |
3717 | ||
9e61fb1f | 3718 | /* Walk /proc/mounts and change any shared entries to dependent mounts. */ |
ed41e764 | 3719 | static void turn_into_dependent_mounts(const struct lxc_rootfs *rootfs) |
e995d7a2 | 3720 | { |
7969675f | 3721 | __do_free char *line = NULL; |
003be47b | 3722 | __do_fclose FILE *f = NULL; |
f62cf1d4 | 3723 | __do_close int memfd = -EBADF, mntinfo_fd = -EBADF; |
e995d7a2 | 3724 | size_t len = 0; |
a39fc34b CB |
3725 | ssize_t copied; |
3726 | int ret; | |
e995d7a2 | 3727 | |
ed41e764 CB |
3728 | mntinfo_fd = open_at(rootfs->dfd_host, "proc/self/mountinfo", PROTECT_OPEN, |
3729 | (PROTECT_LOOKUP_BENEATH_XDEV & ~RESOLVE_NO_SYMLINKS), 0); | |
fea3b91d | 3730 | if (mntinfo_fd < 0) { |
ed41e764 | 3731 | SYSERROR("Failed to open %d/proc/self/mountinfo", rootfs->dfd_host); |
6a49f05e | 3732 | return; |
fea3b91d | 3733 | } |
6a49f05e CB |
3734 | |
3735 | memfd = memfd_create(".lxc_mountinfo", MFD_CLOEXEC); | |
3736 | if (memfd < 0) { | |
3737 | char template[] = P_tmpdir "/.lxc_mountinfo_XXXXXX"; | |
3738 | ||
3739 | if (errno != ENOSYS) { | |
fea3b91d | 3740 | SYSERROR("Failed to create temporary in-memory file"); |
6a49f05e CB |
3741 | return; |
3742 | } | |
3743 | ||
3744 | memfd = lxc_make_tmpfile(template, true); | |
fea3b91d | 3745 | if (memfd < 0) { |
fea3b91d DJ |
3746 | WARN("Failed to create temporary file"); |
3747 | return; | |
3748 | } | |
6a49f05e CB |
3749 | } |
3750 | ||
a39fc34b | 3751 | copied = fd_to_fd(mntinfo_fd, memfd); |
6a49f05e | 3752 | if (copied < 0) { |
fea3b91d | 3753 | SYSERROR("Failed to copy \"/proc/self/mountinfo\""); |
6a49f05e CB |
3754 | return; |
3755 | } | |
6a49f05e | 3756 | |
6a49f05e CB |
3757 | ret = lseek(memfd, 0, SEEK_SET); |
3758 | if (ret < 0) { | |
fea3b91d | 3759 | SYSERROR("Failed to reset file descriptor offset"); |
6a49f05e CB |
3760 | return; |
3761 | } | |
3762 | ||
4110345b | 3763 | f = fdopen(memfd, "re"); |
e995d7a2 | 3764 | if (!f) { |
003be47b | 3765 | SYSERROR("Failed to open copy of \"/proc/self/mountinfo\" to mark all shared. Continuing"); |
e995d7a2 SH |
3766 | return; |
3767 | } | |
3768 | ||
003be47b CB |
3769 | /* |
3770 | * After a successful fdopen() memfd will be closed when calling | |
3771 | * fclose(f). Calling close(memfd) afterwards is undefined. | |
3772 | */ | |
3773 | move_fd(memfd); | |
3774 | ||
e995d7a2 | 3775 | while (getline(&line, &len, f) != -1) { |
0fd73091 CB |
3776 | char *opts, *target; |
3777 | ||
e995d7a2 SH |
3778 | target = get_field(line, 4); |
3779 | if (!target) | |
3780 | continue; | |
0fd73091 | 3781 | |
e995d7a2 SH |
3782 | opts = get_field(target, 2); |
3783 | if (!opts) | |
3784 | continue; | |
0fd73091 | 3785 | |
e995d7a2 SH |
3786 | null_endofword(opts); |
3787 | if (!strstr(opts, "shared")) | |
3788 | continue; | |
0fd73091 | 3789 | |
e995d7a2 | 3790 | null_endofword(target); |
0fd73091 CB |
3791 | ret = mount(NULL, target, NULL, MS_SLAVE, NULL); |
3792 | if (ret < 0) { | |
9e61fb1f | 3793 | SYSERROR("Failed to recursively turn old root mount tree into dependent mount. Continuing..."); |
6a49f05e | 3794 | continue; |
e995d7a2 SH |
3795 | } |
3796 | } | |
9e61fb1f | 3797 | TRACE("Turned all mount table entries into dependent mount"); |
e995d7a2 SH |
3798 | } |
3799 | ||
0fd73091 CB |
3800 | /* This does the work of remounting / if it is shared, calling the container |
3801 | * pre-mount hooks, and mounting the rootfs. | |
35120d9c | 3802 | */ |
8ce1abc2 CB |
3803 | int lxc_setup_rootfs_prepare_root(struct lxc_conf *conf, const char *name, |
3804 | const char *lxcpath) | |
0ad19a3f | 3805 | { |
0fd73091 CB |
3806 | int ret; |
3807 | ||
ea11a215 CB |
3808 | conf->rootfs.dfd_host = open_at(-EBADF, "/", PROTECT_OPATH_DIRECTORY, PROTECT_LOOKUP_ABSOLUTE, 0); |
3809 | if (conf->rootfs.dfd_host < 0) | |
a370f16b CB |
3810 | return log_error_errno(-errno, errno, "Failed to open \"/\""); |
3811 | ||
ed41e764 CB |
3812 | turn_into_dependent_mounts(&conf->rootfs); |
3813 | ||
35120d9c | 3814 | if (conf->rootfs_setup) { |
35120d9c | 3815 | const char *path = conf->rootfs.mount; |
0fd73091 | 3816 | |
ed41e764 CB |
3817 | /* |
3818 | * The rootfs was set up in another namespace. bind-mount it to | |
0fd73091 CB |
3819 | * give us a mount in our own ns so we can pivot_root to it |
3820 | */ | |
3821 | ret = mount(path, path, "rootfs", MS_BIND, NULL); | |
55022530 CB |
3822 | if (ret < 0) |
3823 | return log_error(-1, "Failed to bind mount container / onto itself"); | |
0fd73091 | 3824 | |
ea57e424 CB |
3825 | conf->rootfs.dfd_mnt = openat(-EBADF, path, O_RDONLY | O_CLOEXEC | O_DIRECTORY | O_PATH | O_NOCTTY); |
3826 | if (conf->rootfs.dfd_mnt < 0) | |
26ea5533 CB |
3827 | return log_error_errno(-errno, errno, "Failed to open file descriptor for container rootfs"); |
3828 | ||
55022530 | 3829 | return log_trace(0, "Bind mounted container / onto itself"); |
35120d9c | 3830 | } |
d4ef7c50 | 3831 | |
0fd73091 | 3832 | ret = run_lxc_hooks(name, "pre-mount", conf, NULL); |
55022530 CB |
3833 | if (ret < 0) |
3834 | return log_error(-1, "Failed to run pre-mount hooks"); | |
35120d9c | 3835 | |
4e86cad3 | 3836 | ret = lxc_mount_rootfs(&conf->rootfs); |
55022530 CB |
3837 | if (ret < 0) |
3838 | return log_error(-1, "Failed to setup rootfs for"); | |
35120d9c SH |
3839 | |
3840 | conf->rootfs_setup = true; | |
3841 | return 0; | |
3842 | } | |
3843 | ||
1c1c7051 SH |
3844 | static bool verify_start_hooks(struct lxc_conf *conf) |
3845 | { | |
6b5a54cd | 3846 | char path[PATH_MAX]; |
0fd73091 CB |
3847 | struct lxc_list *it; |
3848 | ||
3849 | lxc_list_for_each (it, &conf->hooks[LXCHOOK_START]) { | |
1c1c7051 | 3850 | int ret; |
0fd73091 | 3851 | char *hookname = it->elem; |
1c1c7051 | 3852 | |
9bcde680 | 3853 | ret = strnprintf(path, sizeof(path), "%s%s", |
0fd73091 CB |
3854 | conf->rootfs.path ? conf->rootfs.mount : "", |
3855 | hookname); | |
9bcde680 | 3856 | if (ret < 0) |
1c1c7051 | 3857 | return false; |
0fd73091 | 3858 | |
75193660 | 3859 | ret = access(path, X_OK); |
55022530 CB |
3860 | if (ret < 0) |
3861 | return log_error_errno(false, errno, "Start hook \"%s\" not found in container", hookname); | |
0fd73091 | 3862 | |
6a0c909a | 3863 | return true; |
1c1c7051 SH |
3864 | } |
3865 | ||
3866 | return true; | |
3867 | } | |
3868 | ||
20502652 CB |
3869 | static int lxc_setup_boot_id(void) |
3870 | { | |
3871 | int ret; | |
3872 | const char *boot_id_path = "/proc/sys/kernel/random/boot_id"; | |
3873 | const char *mock_boot_id_path = "/dev/.lxc-boot-id"; | |
3874 | lxc_id128_t n; | |
3875 | ||
3876 | if (access(boot_id_path, F_OK)) | |
3877 | return 0; | |
3878 | ||
3879 | memset(&n, 0, sizeof(n)); | |
3880 | if (lxc_id128_randomize(&n)) { | |
3881 | SYSERROR("Failed to generate random data for uuid"); | |
3882 | return -1; | |
3883 | } | |
3884 | ||
3885 | ret = lxc_id128_write(mock_boot_id_path, n); | |
3886 | if (ret < 0) { | |
3887 | SYSERROR("Failed to write uuid to %s", mock_boot_id_path); | |
3888 | return -1; | |
3889 | } | |
3890 | ||
3891 | ret = chmod(mock_boot_id_path, 0444); | |
3892 | if (ret < 0) { | |
3893 | SYSERROR("Failed to chown %s", mock_boot_id_path); | |
3894 | (void)unlink(mock_boot_id_path); | |
3895 | return -1; | |
3896 | } | |
3897 | ||
3898 | ret = mount(mock_boot_id_path, boot_id_path, NULL, MS_BIND, NULL); | |
3899 | if (ret < 0) { | |
3900 | SYSERROR("Failed to mount %s to %s", mock_boot_id_path, | |
3901 | boot_id_path); | |
3902 | (void)unlink(mock_boot_id_path); | |
3903 | return -1; | |
3904 | } | |
3905 | ||
3906 | ret = mount(NULL, boot_id_path, NULL, | |
3907 | (MS_BIND | MS_REMOUNT | MS_RDONLY | MS_NOSUID | MS_NOEXEC | | |
3908 | MS_NODEV), | |
3909 | NULL); | |
3910 | if (ret < 0) { | |
3911 | SYSERROR("Failed to remount %s read-only", boot_id_path); | |
3912 | (void)unlink(mock_boot_id_path); | |
3913 | return -1; | |
3914 | } | |
3915 | ||
3916 | return 0; | |
3917 | } | |
3918 | ||
af04d847 | 3919 | static int lxc_setup_keyring(struct lsm_ops *lsm_ops, const struct lxc_conf *conf) |
d701d729 CB |
3920 | { |
3921 | key_serial_t keyring; | |
3922 | int ret = 0; | |
3923 | ||
3924 | if (conf->lsm_se_keyring_context) | |
af04d847 | 3925 | ret = lsm_ops->keyring_label_set(lsm_ops, conf->lsm_se_keyring_context); |
d701d729 | 3926 | else if (conf->lsm_se_context) |
af04d847 | 3927 | ret = lsm_ops->keyring_label_set(lsm_ops, conf->lsm_se_context); |
d701d729 CB |
3928 | if (ret < 0) |
3929 | return log_error_errno(-1, errno, "Failed to set keyring context"); | |
3930 | ||
3931 | /* | |
3932 | * Try to allocate a new session keyring for the container to prevent | |
3933 | * information leaks. | |
3934 | */ | |
3935 | keyring = keyctl(KEYCTL_JOIN_SESSION_KEYRING, prctl_arg(0), | |
3936 | prctl_arg(0), prctl_arg(0), prctl_arg(0)); | |
3937 | if (keyring < 0) { | |
3938 | switch (errno) { | |
3939 | case ENOSYS: | |
3940 | DEBUG("The keyctl() syscall is not supported or blocked"); | |
3941 | break; | |
3942 | case EACCES: | |
3943 | __fallthrough; | |
3944 | case EPERM: | |
3945 | DEBUG("Failed to access kernel keyring. Continuing..."); | |
3946 | break; | |
3947 | default: | |
3948 | SYSERROR("Failed to create kernel keyring"); | |
3949 | break; | |
3950 | } | |
3951 | } | |
3952 | ||
3953 | return ret; | |
3954 | } | |
3955 | ||
4b875ef9 CB |
3956 | static int lxc_rootfs_prepare_child(struct lxc_handler *handler) |
3957 | { | |
3958 | struct lxc_rootfs *rootfs = &handler->conf->rootfs; | |
3959 | int dfd_idmapped = -EBADF; | |
3960 | int ret; | |
3961 | ||
3962 | if (lxc_list_empty(&handler->conf->id_map)) | |
3963 | return 0; | |
3964 | ||
3965 | if (is_empty_string(rootfs->mnt_opts.userns_path)) | |
3966 | return 0; | |
3967 | ||
3968 | if (handler->conf->rootfs_setup) | |
3969 | return 0; | |
3970 | ||
3971 | ret = lxc_abstract_unix_recv_one_fd(handler->data_sock[1], &dfd_idmapped, NULL, 0); | |
3972 | if (ret < 0) | |
3973 | return syserror("Failed to receive idmapped mount fd"); | |
3974 | ||
3975 | rootfs->dfd_idmapped = dfd_idmapped; | |
3976 | TRACE("Received detached idmapped mount %d", rootfs->dfd_idmapped); | |
3977 | return 0; | |
3978 | } | |
3979 | ||
1b82d721 CB |
3980 | int lxc_idmapped_mounts_parent(struct lxc_handler *handler) |
3981 | { | |
5a782dca CB |
3982 | int mnt_seq = 0; |
3983 | ||
1b82d721 CB |
3984 | for (;;) { |
3985 | __do_close int fd_from = -EBADF, fd_userns = -EBADF; | |
3986 | struct lxc_mount_attr attr = {}; | |
3987 | struct lxc_mount_options opts = {}; | |
3988 | ssize_t ret; | |
3989 | ||
3990 | ret = __lxc_abstract_unix_recv_two_fds(handler->data_sock[1], | |
3991 | &fd_from, &fd_userns, | |
3992 | &opts, sizeof(opts)); | |
3993 | if (ret < 0) | |
3994 | return syserror("Failed to receive idmapped mount file descriptors from child"); | |
3995 | ||
3996 | if (fd_from < 0 || fd_userns < 0) | |
3997 | return log_trace(0, "Finished receiving idmapped mount file descriptors from child"); | |
3998 | ||
3999 | attr.attr_set = MOUNT_ATTR_IDMAP; | |
4000 | attr.userns_fd = fd_userns; | |
4001 | ret = mount_setattr(fd_from, "", | |
4002 | AT_EMPTY_PATH | | |
4003 | (opts.recursive ? AT_RECURSIVE : 0), | |
4004 | &attr, sizeof(attr)); | |
4005 | if (ret) | |
4006 | return syserror("Failed to idmap detached %smount %d to %d", | |
4007 | opts.recursive ? "recursive " : "", | |
4008 | fd_from, fd_userns); | |
4009 | ||
5a782dca CB |
4010 | ret = lxc_abstract_unix_send_credential(handler->data_sock[1], |
4011 | &mnt_seq, | |
4012 | sizeof(mnt_seq)); | |
1b82d721 CB |
4013 | if (ret < 0) |
4014 | return syserror("Parent failed to notify child that detached %smount %d was idmapped to user namespace %d", | |
4015 | opts.recursive ? "recursive " : "", | |
4016 | fd_from, fd_userns); | |
4017 | ||
4018 | TRACE("Parent idmapped detached %smount %d to user namespace %d", | |
4019 | opts.recursive ? "recursive " : "", fd_from, fd_userns); | |
5a782dca | 4020 | mnt_seq++; |
1b82d721 CB |
4021 | } |
4022 | } | |
4023 | ||
493ae3fe CB |
4024 | static int lxc_recv_ttys_from_child(struct lxc_handler *handler) |
4025 | { | |
4026 | int i; | |
4027 | struct lxc_terminal_info *tty; | |
4028 | int ret = -1; | |
4029 | int sock = handler->data_sock[1]; | |
4030 | struct lxc_conf *conf = handler->conf; | |
4031 | struct lxc_tty_info *ttys = &conf->ttys; | |
4032 | ||
4033 | if (!conf->ttys.max) | |
4034 | return 0; | |
4035 | ||
4036 | ttys->tty = malloc(sizeof(*ttys->tty) * ttys->max); | |
4037 | if (!ttys->tty) | |
4038 | return -1; | |
4039 | ||
4040 | for (i = 0; i < conf->ttys.max; i++) { | |
4041 | int ttyx = -EBADF, ttyy = -EBADF; | |
4042 | ||
4043 | ret = lxc_abstract_unix_recv_two_fds(sock, &ttyx, &ttyy); | |
4044 | if (ret < 0) | |
4045 | break; | |
4046 | ||
4047 | tty = &ttys->tty[i]; | |
4048 | tty->busy = -1; | |
4049 | tty->ptx = ttyx; | |
4050 | tty->pty = ttyy; | |
4051 | TRACE("Received pty with ptx fd %d and pty fd %d from child", tty->ptx, tty->pty); | |
4052 | } | |
4053 | ||
4054 | if (ret < 0) | |
4055 | SYSERROR("Failed to receive %zu ttys from child", ttys->max); | |
4056 | else | |
4057 | TRACE("Received %zu ttys from child", ttys->max); | |
4058 | ||
4059 | return ret; | |
4060 | } | |
4061 | ||
4062 | int lxc_sync_fds_parent(struct lxc_handler *handler) | |
4063 | { | |
4064 | int ret; | |
4065 | ||
4066 | ret = lxc_seccomp_recv_notifier_fd(&handler->conf->seccomp, handler->data_sock[1]); | |
4067 | if (ret < 0) | |
4068 | return syserror_ret(ret, "Failed to receive seccomp notify fd from child"); | |
4069 | ||
42c0d056 | 4070 | ret = lxc_recv_devpts_from_child(handler); |
493ae3fe CB |
4071 | if (ret < 0) |
4072 | return syserror_ret(ret, "Failed to receive devpts fd from child"); | |
4073 | ||
4074 | /* Read tty fds allocated by child. */ | |
4075 | ret = lxc_recv_ttys_from_child(handler); | |
4076 | if (ret < 0) | |
4077 | return syserror_ret(ret, "Failed to receive tty info from child process"); | |
4078 | ||
4079 | if (handler->ns_clone_flags & CLONE_NEWNET) { | |
4080 | ret = lxc_network_recv_name_and_ifindex_from_child(handler); | |
4081 | if (ret < 0) | |
4082 | return syserror_ret(ret, "Failed to receive names and ifindices for network devices from child"); | |
4083 | } | |
4084 | ||
4085 | TRACE("Finished syncing file descriptors with child"); | |
4086 | return 0; | |
4087 | } | |
4088 | ||
111ed96e CB |
4089 | int lxc_sync_fds_child(struct lxc_handler *handler) |
4090 | { | |
4091 | int ret; | |
4092 | ||
4093 | ret = lxc_seccomp_send_notifier_fd(&handler->conf->seccomp, handler->data_sock[0]); | |
4094 | if (ret < 0) | |
4095 | return syserror_ret(ret, "Failed to send seccomp notify fd to parent"); | |
4096 | ||
4097 | ret = lxc_send_devpts_to_parent(handler); | |
4098 | if (ret < 0) | |
4099 | return syserror_ret(ret, "Failed to send seccomp devpts fd to parent"); | |
4100 | ||
4101 | ret = lxc_send_ttys_to_parent(handler); | |
4102 | if (ret < 0) | |
4103 | return syserror_ret(ret, "Failed to send tty file descriptors to parent"); | |
4104 | ||
4105 | if (handler->ns_clone_flags & CLONE_NEWNET) { | |
4106 | ret = lxc_network_send_name_and_ifindex_to_parent(handler); | |
4107 | if (ret < 0) | |
4108 | return syserror_ret(ret, "Failed to send network device names and ifindices to parent"); | |
4109 | } | |
4110 | ||
4111 | TRACE("Finished syncing file descriptors with parent"); | |
4112 | return 0; | |
4113 | } | |
4114 | ||
3b988b33 | 4115 | int lxc_setup(struct lxc_handler *handler) |
35120d9c | 4116 | { |
2187efd3 | 4117 | int ret; |
0fd73091 | 4118 | const char *lxcpath = handler->lxcpath, *name = handler->name; |
35120d9c | 4119 | struct lxc_conf *lxc_conf = handler->conf; |
35120d9c | 4120 | |
4b875ef9 CB |
4121 | ret = lxc_rootfs_prepare_child(handler); |
4122 | if (ret < 0) | |
4123 | return syserror("Failed to prepare rootfs"); | |
4124 | ||
8ce1abc2 | 4125 | ret = lxc_setup_rootfs_prepare_root(lxc_conf, name, lxcpath); |
55022530 CB |
4126 | if (ret < 0) |
4127 | return log_error(-1, "Failed to setup rootfs"); | |
35120d9c | 4128 | |
b87ee312 | 4129 | if (handler->nsfd[LXC_NS_UTS] == -EBADF) { |
8353b4c9 | 4130 | ret = setup_utsname(lxc_conf->utsname); |
55022530 CB |
4131 | if (ret < 0) |
4132 | return log_error(-1, "Failed to setup the utsname %s", name); | |
0ad19a3f | 4133 | } |
4134 | ||
8f818a84 | 4135 | if (!lxc_conf->keyring_disable_session) { |
d701d729 | 4136 | ret = lxc_setup_keyring(handler->lsm_ops, lxc_conf); |
8f818a84 | 4137 | if (ret < 0) |
d701d729 | 4138 | return log_error(-1, "Failed to setup container keyring"); |
8f818a84 | 4139 | } |
b25291da | 4140 | |
e389f2af | 4141 | if (handler->ns_clone_flags & CLONE_NEWNET) { |
6bc4165d CB |
4142 | ret = lxc_network_recv_from_parent(handler); |
4143 | if (ret < 0) | |
4144 | return log_error(-1, "Failed to receive veth names from parent"); | |
4145 | ||
e389f2af CB |
4146 | ret = lxc_setup_network_in_child_namespaces(lxc_conf, |
4147 | &lxc_conf->network); | |
55022530 CB |
4148 | if (ret < 0) |
4149 | return log_error(-1, "Failed to setup network"); | |
790255cf CB |
4150 | } |
4151 | ||
bc6928ff | 4152 | if (lxc_conf->autodev > 0) { |
63012bdd | 4153 | ret = mount_autodev(name, &lxc_conf->rootfs, lxc_conf->autodevtmpfssize, lxcpath); |
55022530 CB |
4154 | if (ret < 0) |
4155 | return log_error(-1, "Failed to mount \"/dev\""); | |
c6883f38 SH |
4156 | } |
4157 | ||
8353b4c9 CB |
4158 | /* Do automatic mounts (mainly /proc and /sys), but exclude those that |
4159 | * need to wait until other stuff has finished. | |
368bbc02 | 4160 | */ |
6d25a524 | 4161 | ret = lxc_mount_auto_mounts(handler, lxc_conf->auto_mounts & ~LXC_AUTO_CGROUP_MASK); |
55022530 CB |
4162 | if (ret < 0) |
4163 | return log_error(-1, "Failed to setup first automatic mounts"); | |
368bbc02 | 4164 | |
48e5dcc8 | 4165 | ret = setup_mount_fstab(&lxc_conf->rootfs, lxc_conf->fstab, name, lxcpath); |
55022530 CB |
4166 | if (ret < 0) |
4167 | return log_error(-1, "Failed to setup mounts"); | |
576f946d | 4168 | |
c631115d FA |
4169 | if (!lxc_list_empty(&lxc_conf->mount_list)) { |
4170 | ret = setup_mount_entries(lxc_conf, &lxc_conf->rootfs, | |
4171 | &lxc_conf->mount_list, name, lxcpath); | |
55022530 CB |
4172 | if (ret < 0) |
4173 | return log_error(-1, "Failed to setup mount entries"); | |
c631115d FA |
4174 | } |
4175 | ||
1b82d721 CB |
4176 | if (!lxc_sync_wake_parent(handler, START_SYNC_IDMAPPED_MOUNTS)) |
4177 | return -1; | |
4178 | ||
4179 | ret = lxc_idmapped_mounts_child(handler); | |
4180 | if (ret) | |
4181 | return syserror("Failed to attached detached idmapped mounts"); | |
4182 | ||
1f0a3b6e CB |
4183 | lxc_conf->rootfs.dfd_dev = open_at(lxc_conf->rootfs.dfd_mnt, "dev", |
4184 | PROTECT_OPATH_DIRECTORY, PROTECT_LOOKUP_BENEATH_XDEV, 0); | |
4185 | if (lxc_conf->rootfs.dfd_dev < 0 && errno != ENOENT) | |
4186 | return log_error_errno(-errno, errno, "Failed to open \"/dev\""); | |
4187 | ||
8353b4c9 CB |
4188 | /* Now mount only cgroups, if wanted. Before, /sys could not have been |
4189 | * mounted. It is guaranteed to be mounted now either through | |
4190 | * automatically or via fstab entries. | |
368bbc02 | 4191 | */ |
6d25a524 | 4192 | ret = lxc_mount_auto_mounts(handler, lxc_conf->auto_mounts & LXC_AUTO_CGROUP_MASK); |
55022530 CB |
4193 | if (ret < 0) |
4194 | return log_error(-1, "Failed to setup remaining automatic mounts"); | |
368bbc02 | 4195 | |
8353b4c9 | 4196 | ret = run_lxc_hooks(name, "mount", lxc_conf, NULL); |
55022530 CB |
4197 | if (ret < 0) |
4198 | return log_error(-1, "Failed to run mount hooks"); | |
773fb9ca | 4199 | |
bc6928ff | 4200 | if (lxc_conf->autodev > 0) { |
8353b4c9 | 4201 | ret = run_lxc_hooks(name, "autodev", lxc_conf, NULL); |
55022530 CB |
4202 | if (ret < 0) |
4203 | return log_error(-1, "Failed to run autodev hooks"); | |
06749971 | 4204 | |
8353b4c9 | 4205 | ret = lxc_fill_autodev(&lxc_conf->rootfs); |
55022530 CB |
4206 | if (ret < 0) |
4207 | return log_error(-1, "Failed to populate \"/dev\""); | |
91c3830e | 4208 | } |
368bbc02 | 4209 | |
75193660 | 4210 | /* Make sure any start hooks are in the container */ |
55022530 CB |
4211 | if (!verify_start_hooks(lxc_conf)) |
4212 | return log_error(-1, "Failed to verify start hooks"); | |
75193660 | 4213 | |
cf68ffd9 CB |
4214 | ret = lxc_create_tmp_proc_mount(lxc_conf); |
4215 | if (ret < 0) | |
3dd3fc31 | 4216 | return log_error(-1, "Failed to mount transient procfs instance for LSMs"); |
cf68ffd9 | 4217 | |
96a980e1 | 4218 | ret = lxc_setup_devpts_child(handler); |
289b707b CB |
4219 | if (ret < 0) |
4220 | return log_error(-1, "Failed to prepare new devpts instance"); | |
4221 | ||
f3dff080 | 4222 | ret = lxc_setup_console(handler, &lxc_conf->rootfs, &lxc_conf->console, |
37c74fd1 | 4223 | lxc_conf->ttys.dir); |
55022530 CB |
4224 | if (ret < 0) |
4225 | return log_error(-1, "Failed to setup console"); | |
6e590161 | 4226 | |
6a2ca1b4 CB |
4227 | ret = lxc_create_ttys(handler); |
4228 | if (ret < 0) | |
4229 | return log_error(-1, "Failed to create ttys"); | |
4230 | ||
ed8704d0 | 4231 | ret = lxc_setup_dev_symlinks(&lxc_conf->rootfs); |
55022530 CB |
4232 | if (ret < 0) |
4233 | return log_error(-1, "Failed to setup \"/dev\" symlinks"); | |
69aa6655 | 4234 | |
8ce1abc2 | 4235 | ret = lxc_setup_rootfs_switch_root(&lxc_conf->rootfs); |
55022530 CB |
4236 | if (ret < 0) |
4237 | return log_error(-1, "Failed to pivot root into rootfs"); | |
ed502555 | 4238 | |
20502652 CB |
4239 | /* Setting the boot-id is best-effort for now. */ |
4240 | if (lxc_conf->autodev > 0) | |
4241 | (void)lxc_setup_boot_id(); | |
4242 | ||
8353b4c9 | 4243 | ret = setup_personality(lxc_conf->personality); |
55022530 | 4244 | if (ret < 0) |
9c601e1f | 4245 | return syserror("Failed to set personality"); |
cccc74b5 | 4246 | |
8353b4c9 CB |
4247 | /* Set sysctl value to a path under /proc/sys as determined from the |
4248 | * key. For e.g. net.ipv4.ip_forward translated to | |
4249 | * /proc/sys/net/ipv4/ip_forward. | |
7edd0540 L |
4250 | */ |
4251 | if (!lxc_list_empty(&lxc_conf->sysctls)) { | |
4252 | ret = setup_sysctl_parameters(&lxc_conf->sysctls); | |
55022530 CB |
4253 | if (ret < 0) |
4254 | return log_error(-1, "Failed to setup sysctl parameters"); | |
7edd0540 L |
4255 | } |
4256 | ||
97a8f74f | 4257 | if (!lxc_list_empty(&lxc_conf->keepcaps)) { |
55022530 CB |
4258 | if (!lxc_list_empty(&lxc_conf->caps)) |
4259 | return log_error(-1, "Container requests lxc.cap.drop and lxc.cap.keep: either use lxc.cap.drop or lxc.cap.keep, not both"); | |
8353b4c9 | 4260 | |
55022530 CB |
4261 | if (dropcaps_except(&lxc_conf->keepcaps)) |
4262 | return log_error(-1, "Failed to keep capabilities"); | |
97a8f74f | 4263 | } else if (setup_caps(&lxc_conf->caps)) { |
55022530 | 4264 | return log_error(-1, "Failed to drop capabilities"); |
81810dd1 DL |
4265 | } |
4266 | ||
79ff643d | 4267 | put_lxc_rootfs(&handler->conf->rootfs, true); |
8353b4c9 | 4268 | NOTICE("The container \"%s\" is set up", name); |
cd54d859 | 4269 | |
0ad19a3f | 4270 | return 0; |
4271 | } | |
26ddeedd | 4272 | |
3f60c2f7 | 4273 | int run_lxc_hooks(const char *name, char *hookname, struct lxc_conf *conf, |
14a7b0f9 | 4274 | char *argv[]) |
26ddeedd | 4275 | { |
26ddeedd | 4276 | struct lxc_list *it; |
3ea957c6 RK |
4277 | int which; |
4278 | ||
4279 | for (which = 0; which < NUM_LXC_HOOKS; which ++) { | |
71528742 | 4280 | if (strequal(hookname, lxchook_names[which])) |
3ea957c6 RK |
4281 | break; |
4282 | } | |
4283 | ||
4284 | if (which >= NUM_LXC_HOOKS) | |
26ddeedd | 4285 | return -1; |
3f60c2f7 | 4286 | |
0fd73091 | 4287 | lxc_list_for_each (it, &conf->hooks[which]) { |
26ddeedd | 4288 | int ret; |
3f60c2f7 CB |
4289 | char *hook = it->elem; |
4290 | ||
4291 | ret = run_script_argv(name, conf->hooks_version, "lxc", hook, | |
14a7b0f9 | 4292 | hookname, argv); |
3f60c2f7 CB |
4293 | if (ret < 0) |
4294 | return -1; | |
26ddeedd | 4295 | } |
3f60c2f7 | 4296 | |
26ddeedd SH |
4297 | return 0; |
4298 | } | |
72d0e1cb | 4299 | |
72d0e1cb SG |
4300 | int lxc_clear_config_caps(struct lxc_conf *c) |
4301 | { | |
1a0e70ac | 4302 | struct lxc_list *it, *next; |
72d0e1cb | 4303 | |
0fd73091 | 4304 | lxc_list_for_each_safe (it, &c->caps, next) { |
72d0e1cb SG |
4305 | lxc_list_del(it); |
4306 | free(it->elem); | |
4307 | free(it); | |
4308 | } | |
0fd73091 | 4309 | |
cc36133d | 4310 | lxc_list_init(&c->caps); |
72d0e1cb SG |
4311 | return 0; |
4312 | } | |
4313 | ||
c7e345ae CB |
4314 | static int lxc_free_idmap(struct lxc_list *id_map) |
4315 | { | |
27c27d73 SH |
4316 | struct lxc_list *it, *next; |
4317 | ||
46bc6f2a | 4318 | lxc_list_for_each_safe(it, id_map, next) { |
27c27d73 SH |
4319 | lxc_list_del(it); |
4320 | free(it->elem); | |
4321 | free(it); | |
4322 | } | |
c7e345ae | 4323 | |
cc36133d | 4324 | lxc_list_init(id_map); |
27c27d73 SH |
4325 | return 0; |
4326 | } | |
7e621263 CB |
4327 | |
4328 | static int __lxc_free_idmap(struct lxc_list *id_map) | |
4329 | { | |
4330 | lxc_free_idmap(id_map); | |
4331 | free(id_map); | |
4332 | return 0; | |
4333 | } | |
4334 | define_cleanup_function(struct lxc_list *, __lxc_free_idmap); | |
27c27d73 | 4335 | |
4355ab5f SH |
4336 | int lxc_clear_idmaps(struct lxc_conf *c) |
4337 | { | |
4338 | return lxc_free_idmap(&c->id_map); | |
4339 | } | |
4340 | ||
1fb86a7c SH |
4341 | int lxc_clear_config_keepcaps(struct lxc_conf *c) |
4342 | { | |
0fd73091 | 4343 | struct lxc_list *it, *next; |
1fb86a7c | 4344 | |
0fd73091 | 4345 | lxc_list_for_each_safe (it, &c->keepcaps, next) { |
1fb86a7c SH |
4346 | lxc_list_del(it); |
4347 | free(it->elem); | |
4348 | free(it); | |
4349 | } | |
0fd73091 | 4350 | |
cc36133d | 4351 | lxc_list_init(&c->keepcaps); |
1fb86a7c SH |
4352 | return 0; |
4353 | } | |
4354 | ||
a3ed9b81 | 4355 | int lxc_clear_namespace(struct lxc_conf *c) |
4356 | { | |
ced5587c CB |
4357 | for (int i = 0; i < LXC_NS_MAX; i++) |
4358 | free_disarm(c->ns_share[i]); | |
4359 | ||
a3ed9b81 | 4360 | return 0; |
4361 | } | |
4362 | ||
54860ed0 | 4363 | int lxc_clear_cgroups(struct lxc_conf *c, const char *key, int version) |
72d0e1cb | 4364 | { |
54860ed0 | 4365 | char *global_token, *namespaced_token; |
ab1a6cac | 4366 | size_t namespaced_token_len; |
54860ed0 | 4367 | struct lxc_list *it, *next, *list; |
ab1a6cac | 4368 | const char *k = key; |
54860ed0 | 4369 | bool all = false; |
72d0e1cb | 4370 | |
54860ed0 | 4371 | if (version == CGROUP2_SUPER_MAGIC) { |
d6c06927 CB |
4372 | global_token = "lxc.cgroup2"; |
4373 | namespaced_token = "lxc.cgroup2."; | |
4374 | namespaced_token_len = STRLITERALLEN("lxc.cgroup2."); | |
54860ed0 CB |
4375 | list = &c->cgroup2; |
4376 | } else if (version == CGROUP_SUPER_MAGIC) { | |
d6c06927 CB |
4377 | global_token = "lxc.cgroup"; |
4378 | namespaced_token = "lxc.cgroup."; | |
4379 | namespaced_token_len = STRLITERALLEN("lxc.cgroup."); | |
54860ed0 CB |
4380 | list = &c->cgroup; |
4381 | } else { | |
d6c06927 | 4382 | return ret_errno(EINVAL); |
54860ed0 CB |
4383 | } |
4384 | ||
71528742 | 4385 | if (strequal(key, global_token)) |
72d0e1cb | 4386 | all = true; |
eed95eb0 | 4387 | else if (strnequal(key, namespaced_token, namespaced_token_len)) |
ab1a6cac | 4388 | k += namespaced_token_len; |
a6390f01 | 4389 | else |
d6c06927 | 4390 | return ret_errno(EINVAL); |
72d0e1cb | 4391 | |
ced5587c | 4392 | lxc_list_for_each_safe(it, list, next) { |
72d0e1cb | 4393 | struct lxc_cgroup *cg = it->elem; |
54860ed0 | 4394 | |
71528742 | 4395 | if (!all && !strequal(cg->subsystem, k)) |
72d0e1cb | 4396 | continue; |
54860ed0 | 4397 | |
72d0e1cb SG |
4398 | lxc_list_del(it); |
4399 | free(cg->subsystem); | |
4400 | free(cg->value); | |
4401 | free(cg); | |
4402 | free(it); | |
4403 | } | |
e409b214 | 4404 | |
cc36133d CB |
4405 | if (all) |
4406 | lxc_list_init(list); | |
4407 | ||
72d0e1cb SG |
4408 | return 0; |
4409 | } | |
4410 | ||
a7744f12 | 4411 | static inline void lxc_clear_cgroups_devices(struct lxc_conf *conf) |
4bfb655e | 4412 | { |
a7744f12 | 4413 | lxc_clear_cgroup2_devices(&conf->bpf_devices); |
4bfb655e CB |
4414 | } |
4415 | ||
c6d09e15 WB |
4416 | int lxc_clear_limits(struct lxc_conf *c, const char *key) |
4417 | { | |
4418 | struct lxc_list *it, *next; | |
c6d09e15 | 4419 | const char *k = NULL; |
0fd73091 | 4420 | bool all = false; |
c6d09e15 | 4421 | |
71528742 | 4422 | if (strequal(key, "lxc.limit") || strequal(key, "lxc.prlimit")) |
c6d09e15 | 4423 | all = true; |
eed95eb0 | 4424 | else if (strnequal(key, "lxc.limit.", STRLITERALLEN("lxc.limit."))) |
6333c915 | 4425 | k = key + STRLITERALLEN("lxc.limit."); |
eed95eb0 | 4426 | else if (strnequal(key, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit."))) |
6333c915 | 4427 | k = key + STRLITERALLEN("lxc.prlimit."); |
c6d09e15 | 4428 | else |
786467cb | 4429 | return ret_errno(EINVAL); |
c6d09e15 | 4430 | |
0fd73091 | 4431 | lxc_list_for_each_safe (it, &c->limits, next) { |
c6d09e15 | 4432 | struct lxc_limit *lim = it->elem; |
0fd73091 | 4433 | |
71528742 | 4434 | if (!all && !strequal(lim->resource, k)) |
c6d09e15 | 4435 | continue; |
0fd73091 | 4436 | |
c6d09e15 | 4437 | lxc_list_del(it); |
786467cb CB |
4438 | |
4439 | free_disarm(lim->resource); | |
c6d09e15 | 4440 | free(lim); |
59bc24cd | 4441 | free(it); |
c6d09e15 | 4442 | } |
b668653c | 4443 | |
786467cb CB |
4444 | if (all) |
4445 | lxc_list_init(&c->limits); | |
4446 | ||
c6d09e15 WB |
4447 | return 0; |
4448 | } | |
4449 | ||
7edd0540 L |
4450 | int lxc_clear_sysctls(struct lxc_conf *c, const char *key) |
4451 | { | |
4452 | struct lxc_list *it, *next; | |
7edd0540 | 4453 | const char *k = NULL; |
0fd73091 | 4454 | bool all = false; |
7edd0540 | 4455 | |
71528742 | 4456 | if (strequal(key, "lxc.sysctl")) |
7edd0540 | 4457 | all = true; |
eed95eb0 | 4458 | else if (strnequal(key, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl."))) |
6333c915 | 4459 | k = key + STRLITERALLEN("lxc.sysctl."); |
7edd0540 L |
4460 | else |
4461 | return -1; | |
4462 | ||
4a2c9b40 | 4463 | lxc_list_for_each_safe(it, &c->sysctls, next) { |
7edd0540 | 4464 | struct lxc_sysctl *elem = it->elem; |
0fd73091 | 4465 | |
71528742 | 4466 | if (!all && !strequal(elem->key, k)) |
7edd0540 | 4467 | continue; |
0fd73091 | 4468 | |
7edd0540 L |
4469 | lxc_list_del(it); |
4470 | free(elem->key); | |
4471 | free(elem->value); | |
4472 | free(elem); | |
4473 | free(it); | |
4474 | } | |
0fd73091 | 4475 | |
4a2c9b40 CB |
4476 | if (all) |
4477 | lxc_list_init(&c->sysctls); | |
4478 | ||
7edd0540 L |
4479 | return 0; |
4480 | } | |
4481 | ||
61d7a733 YT |
4482 | int lxc_clear_procs(struct lxc_conf *c, const char *key) |
4483 | { | |
0fd73091 | 4484 | struct lxc_list *it, *next; |
61d7a733 | 4485 | const char *k = NULL; |
0fd73091 | 4486 | bool all = false; |
61d7a733 | 4487 | |
71528742 | 4488 | if (strequal(key, "lxc.proc")) |
61d7a733 | 4489 | all = true; |
eed95eb0 | 4490 | else if (strnequal(key, "lxc.proc.", STRLITERALLEN("lxc.proc."))) |
6333c915 | 4491 | k = key + STRLITERALLEN("lxc.proc."); |
61d7a733 YT |
4492 | else |
4493 | return -1; | |
4494 | ||
ced5587c | 4495 | lxc_list_for_each_safe(it, &c->procs, next) { |
61d7a733 | 4496 | struct lxc_proc *proc = it->elem; |
0fd73091 | 4497 | |
71528742 | 4498 | if (!all && !strequal(proc->filename, k)) |
61d7a733 | 4499 | continue; |
0fd73091 | 4500 | |
61d7a733 YT |
4501 | lxc_list_del(it); |
4502 | free(proc->filename); | |
4503 | free(proc->value); | |
4504 | free(proc); | |
4505 | free(it); | |
4506 | } | |
4507 | ||
cc36133d CB |
4508 | if (all) |
4509 | lxc_list_init(&c->procs); | |
4510 | ||
61d7a733 YT |
4511 | return 0; |
4512 | } | |
4513 | ||
ee1e7aa0 SG |
4514 | int lxc_clear_groups(struct lxc_conf *c) |
4515 | { | |
0fd73091 | 4516 | struct lxc_list *it, *next; |
ee1e7aa0 | 4517 | |
0fd73091 | 4518 | lxc_list_for_each_safe (it, &c->groups, next) { |
ee1e7aa0 SG |
4519 | lxc_list_del(it); |
4520 | free(it->elem); | |
4521 | free(it); | |
4522 | } | |
0fd73091 | 4523 | |
cc36133d | 4524 | lxc_list_init(&c->groups); |
ee1e7aa0 SG |
4525 | return 0; |
4526 | } | |
4527 | ||
ab799c0b SG |
4528 | int lxc_clear_environment(struct lxc_conf *c) |
4529 | { | |
0fd73091 | 4530 | struct lxc_list *it, *next; |
ab799c0b | 4531 | |
0fd73091 | 4532 | lxc_list_for_each_safe (it, &c->environment, next) { |
ab799c0b SG |
4533 | lxc_list_del(it); |
4534 | free(it->elem); | |
4535 | free(it); | |
4536 | } | |
0fd73091 | 4537 | |
cc36133d | 4538 | lxc_list_init(&c->environment); |
ab799c0b SG |
4539 | return 0; |
4540 | } | |
4541 | ||
72d0e1cb SG |
4542 | int lxc_clear_mount_entries(struct lxc_conf *c) |
4543 | { | |
0fd73091 | 4544 | struct lxc_list *it, *next; |
72d0e1cb | 4545 | |
0fd73091 | 4546 | lxc_list_for_each_safe (it, &c->mount_list, next) { |
72d0e1cb SG |
4547 | lxc_list_del(it); |
4548 | free(it->elem); | |
4549 | free(it); | |
4550 | } | |
0fd73091 | 4551 | |
cc36133d | 4552 | lxc_list_init(&c->mount_list); |
72d0e1cb SG |
4553 | return 0; |
4554 | } | |
4555 | ||
b099e9e9 SH |
4556 | int lxc_clear_automounts(struct lxc_conf *c) |
4557 | { | |
4558 | c->auto_mounts = 0; | |
4559 | return 0; | |
4560 | } | |
4561 | ||
12a50cc6 | 4562 | int lxc_clear_hooks(struct lxc_conf *c, const char *key) |
72d0e1cb | 4563 | { |
0fd73091 CB |
4564 | struct lxc_list *it, *next; |
4565 | const char *k = NULL; | |
4566 | bool all = false, done = false; | |
72d0e1cb | 4567 | |
71528742 | 4568 | if (strequal(key, "lxc.hook")) |
17ed13a3 | 4569 | all = true; |
eed95eb0 | 4570 | else if (strnequal(key, "lxc.hook.", STRLITERALLEN("lxc.hook."))) |
6333c915 | 4571 | k = key + STRLITERALLEN("lxc.hook."); |
a6390f01 WB |
4572 | else |
4573 | return -1; | |
17ed13a3 | 4574 | |
ced5587c | 4575 | for (int i = 0; i < NUM_LXC_HOOKS; i++) { |
71528742 | 4576 | if (all || strequal(k, lxchook_names[i])) { |
0fd73091 | 4577 | lxc_list_for_each_safe (it, &c->hooks[i], next) { |
17ed13a3 SH |
4578 | lxc_list_del(it); |
4579 | free(it->elem); | |
4580 | free(it); | |
4581 | } | |
cc36133d | 4582 | lxc_list_init(&c->hooks[i]); |
0fd73091 | 4583 | |
17ed13a3 | 4584 | done = true; |
72d0e1cb SG |
4585 | } |
4586 | } | |
17ed13a3 | 4587 | |
55022530 CB |
4588 | if (!done) |
4589 | return log_error(-1, "Invalid hook key: %s", key); | |
0fd73091 | 4590 | |
72d0e1cb SG |
4591 | return 0; |
4592 | } | |
8eb5694b | 4593 | |
4184c3e1 SH |
4594 | static inline void lxc_clear_aliens(struct lxc_conf *conf) |
4595 | { | |
0fd73091 | 4596 | struct lxc_list *it, *next; |
4184c3e1 | 4597 | |
0fd73091 | 4598 | lxc_list_for_each_safe (it, &conf->aliens, next) { |
4184c3e1 SH |
4599 | lxc_list_del(it); |
4600 | free(it->elem); | |
4601 | free(it); | |
4602 | } | |
cc36133d CB |
4603 | |
4604 | lxc_list_init(&conf->aliens); | |
4184c3e1 SH |
4605 | } |
4606 | ||
c7b15d1e | 4607 | void lxc_clear_includes(struct lxc_conf *conf) |
f979ac15 | 4608 | { |
0fd73091 | 4609 | struct lxc_list *it, *next; |
f979ac15 | 4610 | |
ced5587c | 4611 | lxc_list_for_each_safe(it, &conf->includes, next) { |
f979ac15 SH |
4612 | lxc_list_del(it); |
4613 | free(it->elem); | |
4614 | free(it); | |
4615 | } | |
cc36133d CB |
4616 | |
4617 | lxc_list_init(&conf->includes); | |
f979ac15 SH |
4618 | } |
4619 | ||
1800f924 WB |
4620 | int lxc_clear_apparmor_raw(struct lxc_conf *c) |
4621 | { | |
4622 | struct lxc_list *it, *next; | |
4623 | ||
4624 | lxc_list_for_each_safe (it, &c->lsm_aa_raw, next) { | |
4625 | lxc_list_del(it); | |
4626 | free(it->elem); | |
4627 | free(it); | |
4628 | } | |
4629 | ||
cc36133d | 4630 | lxc_list_init(&c->lsm_aa_raw); |
1800f924 WB |
4631 | return 0; |
4632 | } | |
4633 | ||
8eb5694b SH |
4634 | void lxc_conf_free(struct lxc_conf *conf) |
4635 | { | |
4636 | if (!conf) | |
4637 | return; | |
0fd73091 | 4638 | |
858377e4 SH |
4639 | if (current_config == conf) |
4640 | current_config = NULL; | |
aed105d5 | 4641 | lxc_terminal_conf_free(&conf->console); |
f10fad2f | 4642 | free(conf->rootfs.mount); |
b3b8c97f | 4643 | free(conf->rootfs.bdev_type); |
f10fad2f ME |
4644 | free(conf->rootfs.options); |
4645 | free(conf->rootfs.path); | |
79ff643d | 4646 | put_lxc_rootfs(&conf->rootfs, true); |
f10fad2f | 4647 | free(conf->logfile); |
858377e4 SH |
4648 | if (conf->logfd != -1) |
4649 | close(conf->logfd); | |
f10fad2f | 4650 | free(conf->utsname); |
885766f5 CB |
4651 | free(conf->ttys.dir); |
4652 | free(conf->ttys.tty_names); | |
f10fad2f ME |
4653 | free(conf->fstab); |
4654 | free(conf->rcfile); | |
5cda27c1 | 4655 | free(conf->execute_cmd); |
f10fad2f | 4656 | free(conf->init_cmd); |
bf31b337 | 4657 | free(conf->init_groups.list); |
3c491553 | 4658 | free(conf->init_cwd); |
6b0d5538 | 4659 | free(conf->unexpanded_config); |
76d0127f | 4660 | free(conf->syslog); |
c302b476 | 4661 | lxc_free_networks(&conf->network); |
f10fad2f | 4662 | free(conf->lsm_aa_profile); |
1800f924 | 4663 | free(conf->lsm_aa_profile_computed); |
f10fad2f | 4664 | free(conf->lsm_se_context); |
1ed59e6d | 4665 | free(conf->lsm_se_keyring_context); |
c3e3c21a | 4666 | lxc_seccomp_free(&conf->seccomp); |
8eb5694b | 4667 | lxc_clear_config_caps(conf); |
1fb86a7c | 4668 | lxc_clear_config_keepcaps(conf); |
54860ed0 CB |
4669 | lxc_clear_cgroups(conf, "lxc.cgroup", CGROUP_SUPER_MAGIC); |
4670 | lxc_clear_cgroups(conf, "lxc.cgroup2", CGROUP2_SUPER_MAGIC); | |
a7744f12 | 4671 | lxc_clear_cgroups_devices(conf); |
17ed13a3 | 4672 | lxc_clear_hooks(conf, "lxc.hook"); |
8eb5694b | 4673 | lxc_clear_mount_entries(conf); |
27c27d73 | 4674 | lxc_clear_idmaps(conf); |
ee1e7aa0 | 4675 | lxc_clear_groups(conf); |
f979ac15 | 4676 | lxc_clear_includes(conf); |
761d81ca | 4677 | lxc_clear_aliens(conf); |
ab799c0b | 4678 | lxc_clear_environment(conf); |
240d4b74 | 4679 | lxc_clear_limits(conf, "lxc.prlimit"); |
7edd0540 | 4680 | lxc_clear_sysctls(conf, "lxc.sysctl"); |
61d7a733 | 4681 | lxc_clear_procs(conf, "lxc.proc"); |
1800f924 | 4682 | lxc_clear_apparmor_raw(conf); |
a3ed9b81 | 4683 | lxc_clear_namespace(conf); |
43654d34 | 4684 | free(conf->cgroup_meta.dir); |
a900cbaf | 4685 | free(conf->cgroup_meta.monitor_dir); |
eb60b564 | 4686 | free(conf->cgroup_meta.monitor_pivot_dir); |
a900cbaf WB |
4687 | free(conf->cgroup_meta.container_dir); |
4688 | free(conf->cgroup_meta.namespace_dir); | |
43654d34 | 4689 | free(conf->cgroup_meta.controllers); |
7a41e857 LT |
4690 | free(conf->shmount.path_host); |
4691 | free(conf->shmount.path_cont); | |
8eb5694b SH |
4692 | free(conf); |
4693 | } | |
4355ab5f SH |
4694 | |
4695 | struct userns_fn_data { | |
4696 | int (*fn)(void *); | |
c9b7c33e | 4697 | const char *fn_name; |
4355ab5f SH |
4698 | void *arg; |
4699 | int p[2]; | |
4700 | }; | |
4701 | ||
4702 | static int run_userns_fn(void *data) | |
4703 | { | |
766c5b6d | 4704 | struct userns_fn_data *d = data; |
adaffdd7 | 4705 | int ret; |
4355ab5f | 4706 | char c; |
4355ab5f | 4707 | |
766c5b6d | 4708 | close_prot_errno_disarm(d->p[1]); |
f8aa4bf3 | 4709 | |
766c5b6d CB |
4710 | /* |
4711 | * Wait for parent to finish establishing a new mapping in the user | |
f8aa4bf3 CB |
4712 | * namespace we are executing in. |
4713 | */ | |
adaffdd7 | 4714 | ret = lxc_read_nointr(d->p[0], &c, 1); |
766c5b6d | 4715 | close_prot_errno_disarm(d->p[0]); |
adaffdd7 CB |
4716 | if (ret != 1) |
4717 | return -1; | |
f8aa4bf3 | 4718 | |
c9b7c33e | 4719 | if (d->fn_name) |
adaffdd7 | 4720 | TRACE("Calling function \"%s\"", d->fn_name); |
0fd73091 | 4721 | |
f8aa4bf3 | 4722 | /* Call function to run. */ |
4355ab5f SH |
4723 | return d->fn(d->arg); |
4724 | } | |
4725 | ||
7581a82f | 4726 | static struct id_map *mapped_nsid_add(const struct lxc_conf *conf, unsigned id, |
db7cfe23 CB |
4727 | enum idtype idtype) |
4728 | { | |
5173b710 CB |
4729 | const struct id_map *map; |
4730 | struct id_map *retmap; | |
db7cfe23 CB |
4731 | |
4732 | map = find_mapped_nsid_entry(conf, id, idtype); | |
4733 | if (!map) | |
4734 | return NULL; | |
4735 | ||
b8e43ef0 | 4736 | retmap = zalloc(sizeof(*retmap)); |
db7cfe23 CB |
4737 | if (!retmap) |
4738 | return NULL; | |
4739 | ||
4740 | memcpy(retmap, map, sizeof(*retmap)); | |
4741 | return retmap; | |
4742 | } | |
4743 | ||
86c78011 | 4744 | static struct id_map *find_mapped_hostid_entry(const struct lxc_list *idmap, |
c4333195 | 4745 | unsigned id, enum idtype idtype) |
f8aa4bf3 | 4746 | { |
f8aa4bf3 | 4747 | struct id_map *map; |
0fd73091 | 4748 | struct lxc_list *it; |
f8aa4bf3 CB |
4749 | struct id_map *retmap = NULL; |
4750 | ||
86c78011 | 4751 | lxc_list_for_each (it, idmap) { |
f8aa4bf3 CB |
4752 | map = it->elem; |
4753 | if (map->idtype != idtype) | |
4754 | continue; | |
4755 | ||
4756 | if (id >= map->hostid && id < map->hostid + map->range) { | |
4757 | retmap = map; | |
4758 | break; | |
4759 | } | |
4760 | } | |
4761 | ||
f8aa4bf3 CB |
4762 | return retmap; |
4763 | } | |
4764 | ||
0fd73091 | 4765 | /* Allocate a new {g,u}id mapping for the given {g,u}id. Re-use an already |
f8aa4bf3 | 4766 | * existing one or establish a new one. |
4355ab5f | 4767 | */ |
7581a82f | 4768 | static struct id_map *mapped_hostid_add(const struct lxc_conf *conf, uid_t id, |
0fd73091 | 4769 | enum idtype type) |
4355ab5f | 4770 | { |
55022530 | 4771 | __do_free struct id_map *entry = NULL; |
28a2d9e7 | 4772 | int hostid_mapped; |
55022530 | 4773 | struct id_map *tmp = NULL; |
c4333195 | 4774 | |
b8e43ef0 | 4775 | entry = zalloc(sizeof(*entry)); |
c4333195 CB |
4776 | if (!entry) |
4777 | return NULL; | |
f8aa4bf3 | 4778 | |
28a2d9e7 | 4779 | /* Reuse existing mapping. */ |
86c78011 | 4780 | tmp = find_mapped_hostid_entry(&conf->id_map, id, type); |
1758c195 CB |
4781 | if (tmp) { |
4782 | memcpy(entry, tmp, sizeof(*entry)); | |
4783 | } else { | |
4784 | /* Find new mapping. */ | |
4785 | hostid_mapped = find_unmapped_nsid(conf, type); | |
4786 | if (hostid_mapped < 0) | |
4787 | return log_debug(NULL, "Failed to find free mapping for id %d", id); | |
4788 | ||
4789 | entry->idtype = type; | |
4790 | entry->nsid = hostid_mapped; | |
4791 | entry->hostid = (unsigned long)id; | |
4792 | entry->range = 1; | |
4793 | } | |
4355ab5f | 4794 | |
55022530 | 4795 | return move_ptr(entry); |
4355ab5f SH |
4796 | } |
4797 | ||
dbfcdf86 CB |
4798 | static struct lxc_list *get_minimal_idmap(const struct lxc_conf *conf, |
4799 | uid_t *resuid, gid_t *resgid) | |
4355ab5f | 4800 | { |
00d6cfe2 CB |
4801 | __do_free struct id_map *container_root_uid = NULL, |
4802 | *container_root_gid = NULL, | |
4803 | *host_uid_map = NULL, *host_gid_map = NULL; | |
4804 | __do_free struct lxc_list *idmap = NULL; | |
f8aa4bf3 | 4805 | uid_t euid, egid; |
4160c3a0 CB |
4806 | uid_t nsuid = (conf->root_nsuid_map != NULL) ? 0 : conf->init_uid; |
4807 | gid_t nsgid = (conf->root_nsgid_map != NULL) ? 0 : conf->init_gid; | |
00d6cfe2 | 4808 | struct lxc_list *tmplist = NULL; |
4355ab5f | 4809 | |
db7cfe23 | 4810 | /* Find container root mappings. */ |
4160c3a0 | 4811 | container_root_uid = mapped_nsid_add(conf, nsuid, ID_TYPE_UID); |
55022530 CB |
4812 | if (!container_root_uid) |
4813 | return log_debug(NULL, "Failed to find mapping for namespace uid %d", 0); | |
dcf0ffdf CB |
4814 | euid = geteuid(); |
4815 | if (euid >= container_root_uid->hostid && | |
4816 | euid < (container_root_uid->hostid + container_root_uid->range)) | |
2c996219 | 4817 | host_uid_map = move_ptr(container_root_uid); |
f8aa4bf3 | 4818 | |
4160c3a0 | 4819 | container_root_gid = mapped_nsid_add(conf, nsgid, ID_TYPE_GID); |
55022530 CB |
4820 | if (!container_root_gid) |
4821 | return log_debug(NULL, "Failed to find mapping for namespace gid %d", 0); | |
dcf0ffdf CB |
4822 | egid = getegid(); |
4823 | if (egid >= container_root_gid->hostid && | |
4824 | egid < (container_root_gid->hostid + container_root_gid->range)) | |
2c996219 | 4825 | host_gid_map = move_ptr(container_root_gid); |
f8aa4bf3 CB |
4826 | |
4827 | /* Check whether the {g,u}id of the user has a mapping. */ | |
954b7d9b | 4828 | if (!host_uid_map) |
c4333195 | 4829 | host_uid_map = mapped_hostid_add(conf, euid, ID_TYPE_UID); |
55022530 CB |
4830 | if (!host_uid_map) |
4831 | return log_debug(NULL, "Failed to find mapping for uid %d", euid); | |
f8aa4bf3 | 4832 | |
dcf0ffdf CB |
4833 | if (!host_gid_map) |
4834 | host_gid_map = mapped_hostid_add(conf, egid, ID_TYPE_GID); | |
55022530 CB |
4835 | if (!host_gid_map) |
4836 | return log_debug(NULL, "Failed to find mapping for gid %d", egid); | |
28a2d9e7 CB |
4837 | |
4838 | /* Allocate new {g,u}id map list. */ | |
b8e43ef0 | 4839 | idmap = lxc_list_new(); |
28a2d9e7 | 4840 | if (!idmap) |
00d6cfe2 | 4841 | return NULL; |
28a2d9e7 | 4842 | |
f8aa4bf3 | 4843 | /* Add container root to the map. */ |
b8e43ef0 | 4844 | tmplist = lxc_list_new(); |
f8aa4bf3 | 4845 | if (!tmplist) |
00d6cfe2 | 4846 | return NULL; |
47649d5b CB |
4847 | /* idmap will now keep track of that memory. */ |
4848 | lxc_list_add_elem(tmplist, move_ptr(host_uid_map)); | |
f8aa4bf3 | 4849 | lxc_list_add_tail(idmap, tmplist); |
28a2d9e7 | 4850 | |
2c996219 | 4851 | if (container_root_uid) { |
28a2d9e7 | 4852 | /* Add container root to the map. */ |
b8e43ef0 | 4853 | tmplist = lxc_list_new(); |
28a2d9e7 | 4854 | if (!tmplist) |
00d6cfe2 | 4855 | return NULL; |
47649d5b CB |
4856 | /* idmap will now keep track of that memory. */ |
4857 | lxc_list_add_elem(tmplist, move_ptr(container_root_uid)); | |
28a2d9e7 | 4858 | lxc_list_add_tail(idmap, tmplist); |
28a2d9e7 | 4859 | } |
f8aa4bf3 | 4860 | |
b8e43ef0 | 4861 | tmplist = lxc_list_new(); |
f8aa4bf3 | 4862 | if (!tmplist) |
00d6cfe2 | 4863 | return NULL; |
47649d5b CB |
4864 | /* idmap will now keep track of that memory. */ |
4865 | lxc_list_add_elem(tmplist, move_ptr(host_gid_map)); | |
f8aa4bf3 | 4866 | lxc_list_add_tail(idmap, tmplist); |
28a2d9e7 | 4867 | |
2c996219 | 4868 | if (container_root_gid) { |
b8e43ef0 | 4869 | tmplist = lxc_list_new(); |
28a2d9e7 | 4870 | if (!tmplist) |
00d6cfe2 | 4871 | return NULL; |
47649d5b CB |
4872 | /* idmap will now keep track of that memory. */ |
4873 | lxc_list_add_elem(tmplist, move_ptr(container_root_gid)); | |
28a2d9e7 | 4874 | lxc_list_add_tail(idmap, tmplist); |
28a2d9e7 | 4875 | } |
f8aa4bf3 | 4876 | |
dbfcdf86 CB |
4877 | TRACE("Allocated minimal idmapping for ns uid %d and ns gid %d", nsuid, nsgid); |
4878 | ||
4879 | if (resuid) | |
4880 | *resuid = nsuid; | |
4881 | if (resgid) | |
4882 | *resgid = nsgid; | |
00d6cfe2 | 4883 | return move_ptr(idmap); |
dcf0ffdf CB |
4884 | } |
4885 | ||
766c5b6d CB |
4886 | /* |
4887 | * Run a function in a new user namespace. | |
dcf0ffdf CB |
4888 | * The caller's euid/egid will be mapped if it is not already. |
4889 | * Afaict, userns_exec_1() is only used to operate based on privileges for the | |
4890 | * user's own {g,u}id on the host and for the container root's unmapped {g,u}id. | |
4891 | * This means we require only to establish a mapping from: | |
4892 | * - the container root {g,u}id as seen from the host > user's host {g,u}id | |
4893 | * - the container root -> some sub{g,u}id | |
915e3dbd | 4894 | * The former we add, if the user did not specify a mapping. The latter we |
6f3fd27f | 4895 | * retrieve from the container's configured {g,u}id mappings as it must have been |
dcf0ffdf CB |
4896 | * there to start the container in the first place. |
4897 | */ | |
7581a82f | 4898 | int userns_exec_1(const struct lxc_conf *conf, int (*fn)(void *), void *data, |
dcf0ffdf CB |
4899 | const char *fn_name) |
4900 | { | |
7e621263 | 4901 | call_cleaner(__lxc_free_idmap) struct lxc_list *idmap = NULL; |
0fd73091 CB |
4902 | int ret = -1, status = -1; |
4903 | char c = '1'; | |
46bc6f2a CB |
4904 | struct userns_fn_data d = { |
4905 | .arg = data, | |
4906 | .fn = fn, | |
4907 | .fn_name = fn_name, | |
4908 | }; | |
766c5b6d CB |
4909 | pid_t pid; |
4910 | int pipe_fds[2]; | |
dcf0ffdf | 4911 | |
2b2655a8 CB |
4912 | if (!conf) |
4913 | return -EINVAL; | |
4914 | ||
dbfcdf86 | 4915 | idmap = get_minimal_idmap(conf, NULL, NULL); |
dcf0ffdf | 4916 | if (!idmap) |
766c5b6d | 4917 | return ret_errno(ENOENT); |
dcf0ffdf | 4918 | |
766c5b6d CB |
4919 | ret = pipe2(pipe_fds, O_CLOEXEC); |
4920 | if (ret < 0) | |
4921 | return -errno; | |
4922 | ||
766c5b6d CB |
4923 | d.p[0] = pipe_fds[0]; |
4924 | d.p[1] = pipe_fds[1]; | |
dcf0ffdf CB |
4925 | |
4926 | /* Clone child in new user namespace. */ | |
a59440be | 4927 | pid = lxc_raw_clone_cb(run_userns_fn, &d, CLONE_NEWUSER, NULL); |
dcf0ffdf | 4928 | if (pid < 0) { |
0fd73091 | 4929 | ERROR("Failed to clone process in new user namespace"); |
dcf0ffdf CB |
4930 | goto on_error; |
4931 | } | |
4932 | ||
766c5b6d | 4933 | close_prot_errno_disarm(pipe_fds[0]); |
dcf0ffdf | 4934 | |
62fef886 | 4935 | if (lxc_log_trace()) { |
dcf0ffdf | 4936 | struct id_map *map; |
0fd73091 | 4937 | struct lxc_list *it; |
dcf0ffdf | 4938 | |
766c5b6d | 4939 | lxc_list_for_each(it, idmap) { |
f8aa4bf3 | 4940 | map = it->elem; |
766c5b6d CB |
4941 | TRACE("Establishing %cid mapping for \"%d\" in new user namespace: nsuid %lu - hostid %lu - range %lu", |
4942 | (map->idtype == ID_TYPE_UID) ? 'u' : 'g', pid, map->nsid, map->hostid, map->range); | |
f8aa4bf3 | 4943 | } |
4355ab5f SH |
4944 | } |
4945 | ||
f8aa4bf3 | 4946 | /* Set up {g,u}id mapping for user namespace of child process. */ |
4355ab5f | 4947 | ret = lxc_map_ids(idmap, pid); |
f8aa4bf3 | 4948 | if (ret < 0) { |
0fd73091 | 4949 | ERROR("Error setting up {g,u}id mappings for child process \"%d\"", pid); |
f8aa4bf3 | 4950 | goto on_error; |
4355ab5f SH |
4951 | } |
4952 | ||
f8aa4bf3 | 4953 | /* Tell child to proceed. */ |
766c5b6d | 4954 | if (lxc_write_nointr(pipe_fds[1], &c, 1) != 1) { |
dcf0ffdf | 4955 | SYSERROR("Failed telling child process \"%d\" to proceed", pid); |
f8aa4bf3 | 4956 | goto on_error; |
4355ab5f SH |
4957 | } |
4958 | ||
686dd5d1 | 4959 | on_error: |
766c5b6d CB |
4960 | close_prot_errno_disarm(pipe_fds[0]); |
4961 | close_prot_errno_disarm(pipe_fds[1]); | |
f8aa4bf3 | 4962 | |
ee1b16bc TA |
4963 | /* Wait for child to finish. */ |
4964 | if (pid > 0) | |
4965 | status = wait_for_pid(pid); | |
4966 | ||
686dd5d1 CB |
4967 | if (status < 0) |
4968 | ret = -1; | |
4969 | ||
f8aa4bf3 | 4970 | return ret; |
4355ab5f | 4971 | } |
97e9cfa0 | 4972 | |
d1783ef4 CB |
4973 | int userns_exec_minimal(const struct lxc_conf *conf, |
4974 | int (*fn_parent)(void *), void *fn_parent_data, | |
4975 | int (*fn_child)(void *), void *fn_child_data) | |
edf88289 | 4976 | { |
7e621263 | 4977 | call_cleaner(__lxc_free_idmap) struct lxc_list *idmap = NULL; |
dbfcdf86 CB |
4978 | uid_t resuid = LXC_INVALID_UID; |
4979 | gid_t resgid = LXC_INVALID_GID; | |
edf88289 | 4980 | char c = '1'; |
dbfcdf86 | 4981 | ssize_t ret; |
edf88289 CB |
4982 | pid_t pid; |
4983 | int sock_fds[2]; | |
4984 | ||
d1783ef4 | 4985 | if (!conf || !fn_child) |
dbfcdf86 | 4986 | return ret_errno(EINVAL); |
edf88289 | 4987 | |
dbfcdf86 | 4988 | idmap = get_minimal_idmap(conf, &resuid, &resgid); |
edf88289 CB |
4989 | if (!idmap) |
4990 | return ret_errno(ENOENT); | |
4991 | ||
4992 | ret = socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sock_fds); | |
4993 | if (ret < 0) | |
4994 | return -errno; | |
4995 | ||
4996 | pid = fork(); | |
4997 | if (pid < 0) { | |
dbfcdf86 | 4998 | SYSERROR("Failed to create new process"); |
edf88289 CB |
4999 | goto on_error; |
5000 | } | |
5001 | ||
5002 | if (pid == 0) { | |
5003 | close_prot_errno_disarm(sock_fds[1]); | |
5004 | ||
5005 | ret = unshare(CLONE_NEWUSER); | |
dbfcdf86 CB |
5006 | if (ret < 0) { |
5007 | SYSERROR("Failed to unshare new user namespace"); | |
edf88289 | 5008 | _exit(EXIT_FAILURE); |
dbfcdf86 | 5009 | } |
edf88289 | 5010 | |
dbfcdf86 CB |
5011 | ret = lxc_write_nointr(sock_fds[0], &c, 1); |
5012 | if (ret != 1) | |
edf88289 CB |
5013 | _exit(EXIT_FAILURE); |
5014 | ||
5015 | ret = lxc_read_nointr(sock_fds[0], &c, 1); | |
5016 | if (ret != 1) | |
5017 | _exit(EXIT_FAILURE); | |
5018 | ||
5019 | close_prot_errno_disarm(sock_fds[0]); | |
5020 | ||
8917c382 | 5021 | if (!lxc_drop_groups() && errno != EPERM) |
edf88289 CB |
5022 | _exit(EXIT_FAILURE); |
5023 | ||
dbfcdf86 CB |
5024 | ret = setresgid(resgid, resgid, resgid); |
5025 | if (ret < 0) { | |
5026 | SYSERROR("Failed to setresgid(%d, %d, %d)", | |
5027 | resgid, resgid, resgid); | |
edf88289 | 5028 | _exit(EXIT_FAILURE); |
dbfcdf86 CB |
5029 | } |
5030 | ||
5031 | ret = setresuid(resuid, resuid, resuid); | |
5032 | if (ret < 0) { | |
5033 | SYSERROR("Failed to setresuid(%d, %d, %d)", | |
5034 | resuid, resuid, resuid); | |
5035 | _exit(EXIT_FAILURE); | |
5036 | } | |
edf88289 | 5037 | |
d1783ef4 | 5038 | ret = fn_child(fn_child_data); |
dbfcdf86 CB |
5039 | if (ret) { |
5040 | SYSERROR("Running function in new user namespace failed"); | |
edf88289 | 5041 | _exit(EXIT_FAILURE); |
dbfcdf86 | 5042 | } |
edf88289 CB |
5043 | |
5044 | _exit(EXIT_SUCCESS); | |
5045 | } | |
5046 | ||
5047 | close_prot_errno_disarm(sock_fds[0]); | |
5048 | ||
62fef886 | 5049 | if (lxc_log_trace()) { |
edf88289 CB |
5050 | struct id_map *map; |
5051 | struct lxc_list *it; | |
5052 | ||
5053 | lxc_list_for_each(it, idmap) { | |
5054 | map = it->elem; | |
5055 | TRACE("Establishing %cid mapping for \"%d\" in new user namespace: nsuid %lu - hostid %lu - range %lu", | |
5056 | (map->idtype == ID_TYPE_UID) ? 'u' : 'g', pid, map->nsid, map->hostid, map->range); | |
5057 | } | |
5058 | } | |
5059 | ||
5060 | ret = lxc_read_nointr(sock_fds[1], &c, 1); | |
5061 | if (ret != 1) { | |
5062 | SYSERROR("Failed waiting for child process %d\" to tell us to proceed", pid); | |
5063 | goto on_error; | |
5064 | } | |
5065 | ||
5066 | /* Set up {g,u}id mapping for user namespace of child process. */ | |
5067 | ret = lxc_map_ids(idmap, pid); | |
5068 | if (ret < 0) { | |
5069 | ERROR("Error setting up {g,u}id mappings for child process \"%d\"", pid); | |
5070 | goto on_error; | |
5071 | } | |
5072 | ||
5073 | /* Tell child to proceed. */ | |
5074 | ret = lxc_write_nointr(sock_fds[1], &c, 1); | |
5075 | if (ret != 1) { | |
5076 | SYSERROR("Failed telling child process \"%d\" to proceed", pid); | |
5077 | goto on_error; | |
5078 | } | |
5079 | ||
d1783ef4 CB |
5080 | if (fn_parent && fn_parent(fn_parent_data)) { |
5081 | SYSERROR("Running parent function failed"); | |
5082 | _exit(EXIT_FAILURE); | |
5083 | } | |
5084 | ||
edf88289 CB |
5085 | on_error: |
5086 | close_prot_errno_disarm(sock_fds[0]); | |
5087 | close_prot_errno_disarm(sock_fds[1]); | |
5088 | ||
5089 | /* Wait for child to finish. */ | |
dbfcdf86 CB |
5090 | if (pid < 0) |
5091 | return -1; | |
edf88289 | 5092 | |
dbfcdf86 | 5093 | return wait_for_pid(pid); |
edf88289 CB |
5094 | } |
5095 | ||
415a8851 CB |
5096 | int userns_exec_full(struct lxc_conf *conf, int (*fn)(void *), void *data, |
5097 | const char *fn_name) | |
5098 | { | |
5099 | pid_t pid; | |
5100 | uid_t euid, egid; | |
415a8851 CB |
5101 | int p[2]; |
5102 | struct id_map *map; | |
5103 | struct lxc_list *cur; | |
0fd73091 | 5104 | struct userns_fn_data d; |
415a8851 | 5105 | int ret = -1; |
0fd73091 | 5106 | char c = '1'; |
415a8851 CB |
5107 | struct lxc_list *idmap = NULL, *tmplist = NULL; |
5108 | struct id_map *container_root_uid = NULL, *container_root_gid = NULL, | |
5109 | *host_uid_map = NULL, *host_gid_map = NULL; | |
5110 | ||
2b2655a8 CB |
5111 | if (!conf) |
5112 | return -EINVAL; | |
5113 | ||
979f9e34 | 5114 | ret = pipe2(p, O_CLOEXEC); |
415a8851 CB |
5115 | if (ret < 0) { |
5116 | SYSERROR("opening pipe"); | |
5117 | return -1; | |
5118 | } | |
5119 | d.fn = fn; | |
5120 | d.fn_name = fn_name; | |
5121 | d.arg = data; | |
5122 | d.p[0] = p[0]; | |
5123 | d.p[1] = p[1]; | |
5124 | ||
5125 | /* Clone child in new user namespace. */ | |
33258b95 | 5126 | pid = lxc_clone(run_userns_fn, &d, CLONE_NEWUSER, NULL); |
415a8851 | 5127 | if (pid < 0) { |
0fd73091 | 5128 | ERROR("Failed to clone process in new user namespace"); |
415a8851 CB |
5129 | goto on_error; |
5130 | } | |
5131 | ||
5132 | close(p[0]); | |
5133 | p[0] = -1; | |
5134 | ||
5135 | euid = geteuid(); | |
5136 | egid = getegid(); | |
5137 | ||
5138 | /* Allocate new {g,u}id map list. */ | |
b8e43ef0 | 5139 | idmap = lxc_list_new(); |
415a8851 CB |
5140 | if (!idmap) |
5141 | goto on_error; | |
415a8851 CB |
5142 | |
5143 | /* Find container root. */ | |
0fd73091 | 5144 | lxc_list_for_each (cur, &conf->id_map) { |
415a8851 CB |
5145 | struct id_map *tmpmap; |
5146 | ||
b8e43ef0 | 5147 | tmplist = lxc_list_new(); |
415a8851 CB |
5148 | if (!tmplist) |
5149 | goto on_error; | |
5150 | ||
b8e43ef0 | 5151 | tmpmap = zalloc(sizeof(*tmpmap)); |
415a8851 CB |
5152 | if (!tmpmap) { |
5153 | free(tmplist); | |
5154 | goto on_error; | |
5155 | } | |
5156 | ||
5157 | memset(tmpmap, 0, sizeof(*tmpmap)); | |
5158 | memcpy(tmpmap, cur->elem, sizeof(*tmpmap)); | |
5159 | tmplist->elem = tmpmap; | |
5160 | ||
5161 | lxc_list_add_tail(idmap, tmplist); | |
5162 | ||
5163 | map = cur->elem; | |
5164 | ||
5165 | if (map->idtype == ID_TYPE_UID) | |
5166 | if (euid >= map->hostid && euid < map->hostid + map->range) | |
5167 | host_uid_map = map; | |
5168 | ||
5169 | if (map->idtype == ID_TYPE_GID) | |
5170 | if (egid >= map->hostid && egid < map->hostid + map->range) | |
5171 | host_gid_map = map; | |
5172 | ||
5173 | if (map->nsid != 0) | |
5174 | continue; | |
5175 | ||
5176 | if (map->idtype == ID_TYPE_UID) | |
5177 | if (container_root_uid == NULL) | |
5178 | container_root_uid = map; | |
5179 | ||
5180 | if (map->idtype == ID_TYPE_GID) | |
5181 | if (container_root_gid == NULL) | |
5182 | container_root_gid = map; | |
5183 | } | |
5184 | ||
5185 | if (!container_root_uid || !container_root_gid) { | |
5186 | ERROR("No mapping for container root found"); | |
5187 | goto on_error; | |
5188 | } | |
5189 | ||
5190 | /* Check whether the {g,u}id of the user has a mapping. */ | |
5191 | if (!host_uid_map) | |
c4333195 | 5192 | host_uid_map = mapped_hostid_add(conf, euid, ID_TYPE_UID); |
415a8851 CB |
5193 | else |
5194 | host_uid_map = container_root_uid; | |
5195 | ||
5196 | if (!host_gid_map) | |
c4333195 | 5197 | host_gid_map = mapped_hostid_add(conf, egid, ID_TYPE_GID); |
415a8851 CB |
5198 | else |
5199 | host_gid_map = container_root_gid; | |
5200 | ||
5201 | if (!host_uid_map) { | |
5202 | DEBUG("Failed to find mapping for uid %d", euid); | |
5203 | goto on_error; | |
5204 | } | |
5205 | ||
5206 | if (!host_gid_map) { | |
5207 | DEBUG("Failed to find mapping for gid %d", egid); | |
5208 | goto on_error; | |
5209 | } | |
5210 | ||
5211 | if (host_uid_map && (host_uid_map != container_root_uid)) { | |
5212 | /* Add container root to the map. */ | |
b8e43ef0 | 5213 | tmplist = lxc_list_new(); |
415a8851 CB |
5214 | if (!tmplist) |
5215 | goto on_error; | |
5216 | lxc_list_add_elem(tmplist, host_uid_map); | |
5217 | lxc_list_add_tail(idmap, tmplist); | |
5218 | } | |
5219 | /* idmap will now keep track of that memory. */ | |
5220 | host_uid_map = NULL; | |
5221 | ||
5222 | if (host_gid_map && (host_gid_map != container_root_gid)) { | |
b8e43ef0 | 5223 | tmplist = lxc_list_new(); |
415a8851 CB |
5224 | if (!tmplist) |
5225 | goto on_error; | |
5226 | lxc_list_add_elem(tmplist, host_gid_map); | |
5227 | lxc_list_add_tail(idmap, tmplist); | |
5228 | } | |
5229 | /* idmap will now keep track of that memory. */ | |
5230 | host_gid_map = NULL; | |
5231 | ||
62fef886 | 5232 | if (lxc_log_trace()) { |
0fd73091 | 5233 | lxc_list_for_each (cur, idmap) { |
415a8851 CB |
5234 | map = cur->elem; |
5235 | TRACE("establishing %cid mapping for \"%d\" in new " | |
5236 | "user namespace: nsuid %lu - hostid %lu - range " | |
5237 | "%lu", | |
5238 | (map->idtype == ID_TYPE_UID) ? 'u' : 'g', pid, | |
5239 | map->nsid, map->hostid, map->range); | |
5240 | } | |
5241 | } | |
5242 | ||
5243 | /* Set up {g,u}id mapping for user namespace of child process. */ | |
5244 | ret = lxc_map_ids(idmap, pid); | |
5245 | if (ret < 0) { | |
0fd73091 | 5246 | ERROR("error setting up {g,u}id mappings for child process \"%d\"", pid); |
415a8851 CB |
5247 | goto on_error; |
5248 | } | |
5249 | ||
5250 | /* Tell child to proceed. */ | |
489f39be | 5251 | if (lxc_write_nointr(p[1], &c, 1) != 1) { |
0fd73091 | 5252 | SYSERROR("Failed telling child process \"%d\" to proceed", pid); |
415a8851 CB |
5253 | goto on_error; |
5254 | } | |
5255 | ||
686dd5d1 | 5256 | on_error: |
ee1b16bc TA |
5257 | if (p[0] != -1) |
5258 | close(p[0]); | |
5259 | close(p[1]); | |
5260 | ||
415a8851 | 5261 | /* Wait for child to finish. */ |
686dd5d1 CB |
5262 | if (pid > 0) |
5263 | ret = wait_for_pid(pid); | |
415a8851 | 5264 | |
7e621263 CB |
5265 | if (idmap) |
5266 | __lxc_free_idmap(idmap); | |
80758b4b | 5267 | |
415a8851 CB |
5268 | if (host_uid_map && (host_uid_map != container_root_uid)) |
5269 | free(host_uid_map); | |
5270 | if (host_gid_map && (host_gid_map != container_root_gid)) | |
5271 | free(host_gid_map); | |
5272 | ||
415a8851 CB |
5273 | return ret; |
5274 | } | |
5275 | ||
234998b4 CB |
5276 | static int add_idmap_entry(struct lxc_list *idmap, enum idtype idtype, |
5277 | unsigned long nsid, unsigned long hostid, | |
5278 | unsigned long range) | |
5279 | { | |
5280 | __do_free struct id_map *new_idmap = NULL; | |
5281 | __do_free struct lxc_list *new_list = NULL; | |
5282 | ||
5283 | new_idmap = zalloc(sizeof(*new_idmap)); | |
5284 | if (!new_idmap) | |
5285 | return ret_errno(ENOMEM); | |
5286 | ||
5287 | new_idmap->idtype = idtype; | |
5288 | new_idmap->hostid = hostid; | |
5289 | new_idmap->nsid = nsid; | |
5290 | new_idmap->range = range; | |
5291 | ||
5292 | new_list = zalloc(sizeof(*new_list)); | |
5293 | if (!new_list) | |
5294 | return ret_errno(ENOMEM); | |
5295 | ||
5296 | new_list->elem = move_ptr(new_idmap); | |
5297 | lxc_list_add_tail(idmap, move_ptr(new_list)); | |
5298 | ||
5299 | INFO("Adding id map: type %c nsid %lu hostid %lu range %lu", | |
5300 | idtype == ID_TYPE_UID ? 'u' : 'g', nsid, hostid, range); | |
5301 | return 0; | |
5302 | } | |
5303 | ||
5304 | int userns_exec_mapped_root(const char *path, int path_fd, | |
5305 | const struct lxc_conf *conf) | |
5306 | { | |
7e621263 | 5307 | call_cleaner(__lxc_free_idmap) struct lxc_list *idmap = NULL; |
234998b4 CB |
5308 | __do_close int fd = -EBADF; |
5309 | int target_fd = -EBADF; | |
5310 | char c = '1'; | |
5311 | ssize_t ret; | |
5312 | pid_t pid; | |
5313 | int sock_fds[2]; | |
5314 | uid_t container_host_uid, hostuid; | |
5315 | gid_t container_host_gid, hostgid; | |
5316 | struct stat st; | |
5317 | ||
5318 | if (!conf || (!path && path_fd < 0)) | |
5319 | return ret_errno(EINVAL); | |
5320 | ||
5321 | if (!path) | |
5322 | path = "(null)"; | |
5323 | ||
5324 | container_host_uid = get_mapped_rootid(conf, ID_TYPE_UID); | |
5325 | if (!uid_valid(container_host_uid)) | |
5326 | return log_error(-1, "No uid mapping for container root"); | |
5327 | ||
5328 | container_host_gid = get_mapped_rootid(conf, ID_TYPE_GID); | |
5329 | if (!gid_valid(container_host_gid)) | |
5330 | return log_error(-1, "No gid mapping for container root"); | |
5331 | ||
cf68ffd9 | 5332 | if (path_fd < 0) { |
a72c68f7 | 5333 | fd = open(path, O_CLOEXEC | O_NOCTTY); |
234998b4 CB |
5334 | if (fd < 0) |
5335 | return log_error_errno(-errno, errno, "Failed to open \"%s\"", path); | |
5336 | target_fd = fd; | |
5337 | } else { | |
5338 | target_fd = path_fd; | |
5339 | } | |
5340 | ||
5341 | hostuid = geteuid(); | |
5342 | /* We are root so chown directly. */ | |
5343 | if (hostuid == 0) { | |
5344 | ret = fchown(target_fd, container_host_uid, container_host_gid); | |
5345 | if (ret) | |
5346 | return log_error_errno(-errno, errno, | |
5347 | "Failed to fchown(%d(%s), %d, %d)", | |
5348 | target_fd, path, container_host_uid, | |
5349 | container_host_gid); | |
5350 | return log_trace(0, "Chowned %d(%s) to uid %d and %d", target_fd, path, | |
5351 | container_host_uid, container_host_gid); | |
5352 | } | |
5353 | ||
5354 | /* The container's root host id matches */ | |
5355 | if (container_host_uid == hostuid) | |
5356 | return log_info(0, "Container root id is mapped to our uid"); | |
5357 | ||
5358 | /* Get the current ids of our target. */ | |
5359 | ret = fstat(target_fd, &st); | |
5360 | if (ret) | |
5361 | return log_error_errno(-errno, errno, "Failed to stat \"%s\"", path); | |
5362 | ||
5363 | hostgid = getegid(); | |
5364 | if (st.st_uid == hostuid && mapped_hostid(st.st_gid, conf, ID_TYPE_GID) < 0) { | |
5365 | ret = fchown(target_fd, -1, hostgid); | |
5366 | if (ret) | |
5367 | return log_error_errno(-errno, errno, | |
5368 | "Failed to fchown(%d(%s), -1, %d)", | |
5369 | target_fd, path, hostgid); | |
2e8013f9 | 5370 | TRACE("Chowned %d(%s) to -1:%d", target_fd, path, hostgid); |
234998b4 CB |
5371 | } |
5372 | ||
b8e43ef0 | 5373 | idmap = lxc_list_new(); |
234998b4 CB |
5374 | if (!idmap) |
5375 | return -ENOMEM; | |
234998b4 CB |
5376 | |
5377 | /* "u:0:rootuid:1" */ | |
5378 | ret = add_idmap_entry(idmap, ID_TYPE_UID, 0, container_host_uid, 1); | |
5379 | if (ret < 0) | |
5380 | return log_error_errno(ret, -ret, "Failed to add idmap entry"); | |
5381 | ||
5382 | /* "u:hostuid:hostuid:1" */ | |
5383 | ret = add_idmap_entry(idmap, ID_TYPE_UID, hostuid, hostuid, 1); | |
5384 | if (ret < 0) | |
5385 | return log_error_errno(ret, -ret, "Failed to add idmap entry"); | |
5386 | ||
5387 | /* "g:0:rootgid:1" */ | |
5388 | ret = add_idmap_entry(idmap, ID_TYPE_GID, 0, container_host_gid, 1); | |
5389 | if (ret < 0) | |
5390 | return log_error_errno(ret, -ret, "Failed to add idmap entry"); | |
5391 | ||
5392 | /* "g:hostgid:hostgid:1" */ | |
5393 | ret = add_idmap_entry(idmap, ID_TYPE_GID, hostgid, hostgid, 1); | |
5394 | if (ret < 0) | |
5395 | return log_error_errno(ret, -ret, "Failed to add idmap entry"); | |
5396 | ||
5397 | if (hostgid != st.st_gid) { | |
5398 | /* "g:pathgid:rootgid+pathgid:1" */ | |
5399 | ret = add_idmap_entry(idmap, ID_TYPE_GID, st.st_gid, | |
5400 | container_host_gid + (gid_t)st.st_gid, 1); | |
5401 | if (ret < 0) | |
5402 | return log_error_errno(ret, -ret, "Failed to add idmap entry"); | |
5403 | } | |
5404 | ||
5405 | ret = socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sock_fds); | |
5406 | if (ret < 0) | |
5407 | return -errno; | |
5408 | ||
5409 | pid = fork(); | |
5410 | if (pid < 0) { | |
5411 | SYSERROR("Failed to create new process"); | |
5412 | goto on_error; | |
5413 | } | |
5414 | ||
5415 | if (pid == 0) { | |
5416 | close_prot_errno_disarm(sock_fds[1]); | |
5417 | ||
5418 | ret = unshare(CLONE_NEWUSER); | |
5419 | if (ret < 0) { | |
5420 | SYSERROR("Failed to unshare new user namespace"); | |
5421 | _exit(EXIT_FAILURE); | |
5422 | } | |
5423 | ||
5424 | ret = lxc_write_nointr(sock_fds[0], &c, 1); | |
5425 | if (ret != 1) | |
5426 | _exit(EXIT_FAILURE); | |
5427 | ||
5428 | ret = lxc_read_nointr(sock_fds[0], &c, 1); | |
5429 | if (ret != 1) | |
5430 | _exit(EXIT_FAILURE); | |
5431 | ||
5432 | close_prot_errno_disarm(sock_fds[0]); | |
5433 | ||
5434 | if (!lxc_switch_uid_gid(0, 0)) | |
5435 | _exit(EXIT_FAILURE); | |
5436 | ||
8917c382 | 5437 | if (!lxc_drop_groups()) |
234998b4 CB |
5438 | _exit(EXIT_FAILURE); |
5439 | ||
8053a085 | 5440 | ret = fchown(target_fd, 0, st.st_gid); |
234998b4 | 5441 | if (ret) { |
8ea93a0f | 5442 | SYSERROR("Failed to chown %d(%s) to 0:%d", target_fd, path, st.st_gid); |
234998b4 CB |
5443 | _exit(EXIT_FAILURE); |
5444 | } | |
5445 | ||
2e8013f9 | 5446 | TRACE("Chowned %d(%s) to 0:%d", target_fd, path, st.st_gid); |
234998b4 CB |
5447 | _exit(EXIT_SUCCESS); |
5448 | } | |
5449 | ||
5450 | close_prot_errno_disarm(sock_fds[0]); | |
5451 | ||
62fef886 | 5452 | if (lxc_log_trace()) { |
234998b4 CB |
5453 | struct id_map *map; |
5454 | struct lxc_list *it; | |
5455 | ||
5456 | lxc_list_for_each(it, idmap) { | |
5457 | map = it->elem; | |
5458 | TRACE("Establishing %cid mapping for \"%d\" in new user namespace: nsuid %lu - hostid %lu - range %lu", | |
5459 | (map->idtype == ID_TYPE_UID) ? 'u' : 'g', pid, map->nsid, map->hostid, map->range); | |
5460 | } | |
5461 | } | |
5462 | ||
5463 | ret = lxc_read_nointr(sock_fds[1], &c, 1); | |
5464 | if (ret != 1) { | |
5465 | SYSERROR("Failed waiting for child process %d\" to tell us to proceed", pid); | |
5466 | goto on_error; | |
5467 | } | |
5468 | ||
5469 | /* Set up {g,u}id mapping for user namespace of child process. */ | |
5470 | ret = lxc_map_ids(idmap, pid); | |
5471 | if (ret < 0) { | |
5472 | ERROR("Error setting up {g,u}id mappings for child process \"%d\"", pid); | |
5473 | goto on_error; | |
5474 | } | |
5475 | ||
5476 | /* Tell child to proceed. */ | |
5477 | ret = lxc_write_nointr(sock_fds[1], &c, 1); | |
5478 | if (ret != 1) { | |
5479 | SYSERROR("Failed telling child process \"%d\" to proceed", pid); | |
5480 | goto on_error; | |
5481 | } | |
5482 | ||
5483 | on_error: | |
5484 | close_prot_errno_disarm(sock_fds[0]); | |
5485 | close_prot_errno_disarm(sock_fds[1]); | |
5486 | ||
5487 | /* Wait for child to finish. */ | |
5488 | if (pid < 0) | |
5489 | return -1; | |
5490 | ||
5491 | return wait_for_pid(pid); | |
5492 | } | |
5493 | ||
a96a8e8c | 5494 | /* not thread-safe, do not use from api without first forking */ |
0fd73091 | 5495 | static char *getuname(void) |
97e9cfa0 | 5496 | { |
4f410b2a | 5497 | __do_free char *buf = NULL; |
cb7aa5e8 DJ |
5498 | struct passwd pwent; |
5499 | struct passwd *pwentp = NULL; | |
cb7aa5e8 DJ |
5500 | size_t bufsize; |
5501 | int ret; | |
97e9cfa0 | 5502 | |
cb7aa5e8 DJ |
5503 | bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); |
5504 | if (bufsize == -1) | |
5505 | bufsize = 1024; | |
5506 | ||
b8e43ef0 | 5507 | buf = zalloc(bufsize); |
cb7aa5e8 | 5508 | if (!buf) |
97e9cfa0 SH |
5509 | return NULL; |
5510 | ||
cb7aa5e8 DJ |
5511 | ret = getpwuid_r(geteuid(), &pwent, buf, bufsize, &pwentp); |
5512 | if (!pwentp) { | |
5513 | if (ret == 0) | |
5514 | WARN("Could not find matched password record."); | |
5515 | ||
55022530 | 5516 | return log_error(NULL, "Failed to get password record - %u", geteuid()); |
cb7aa5e8 DJ |
5517 | } |
5518 | ||
4f410b2a | 5519 | return strdup(pwent.pw_name); |
97e9cfa0 SH |
5520 | } |
5521 | ||
a96a8e8c | 5522 | /* not thread-safe, do not use from api without first forking */ |
97e9cfa0 SH |
5523 | static char *getgname(void) |
5524 | { | |
4f410b2a | 5525 | __do_free char *buf = NULL; |
3de9fb4c DJ |
5526 | struct group grent; |
5527 | struct group *grentp = NULL; | |
3de9fb4c DJ |
5528 | size_t bufsize; |
5529 | int ret; | |
5530 | ||
5531 | bufsize = sysconf(_SC_GETGR_R_SIZE_MAX); | |
5532 | if (bufsize == -1) | |
5533 | bufsize = 1024; | |
5534 | ||
b8e43ef0 | 5535 | buf = zalloc(bufsize); |
3de9fb4c DJ |
5536 | if (!buf) |
5537 | return NULL; | |
5538 | ||
5539 | ret = getgrgid_r(getegid(), &grent, buf, bufsize, &grentp); | |
5540 | if (!grentp) { | |
5541 | if (ret == 0) | |
5542 | WARN("Could not find matched group record"); | |
97e9cfa0 | 5543 | |
55022530 | 5544 | return log_error(NULL, "Failed to get group record - %u", getegid()); |
3de9fb4c DJ |
5545 | } |
5546 | ||
4f410b2a | 5547 | return strdup(grent.gr_name); |
97e9cfa0 SH |
5548 | } |
5549 | ||
a96a8e8c | 5550 | /* not thread-safe, do not use from api without first forking */ |
97e9cfa0 SH |
5551 | void suggest_default_idmap(void) |
5552 | { | |
3a6e3bf5 | 5553 | __do_free char *gname = NULL, *line = NULL, *uname = NULL; |
4aae564f | 5554 | __do_fclose FILE *subuid_f = NULL, *subgid_f = NULL; |
97e9cfa0 | 5555 | unsigned int uid = 0, urange = 0, gid = 0, grange = 0; |
97e9cfa0 SH |
5556 | size_t len = 0; |
5557 | ||
0fd73091 CB |
5558 | uname = getuname(); |
5559 | if (!uname) | |
97e9cfa0 SH |
5560 | return; |
5561 | ||
0fd73091 | 5562 | gname = getgname(); |
3a6e3bf5 | 5563 | if (!gname) |
97e9cfa0 | 5564 | return; |
97e9cfa0 | 5565 | |
4110345b | 5566 | subuid_f = fopen(subuidfile, "re"); |
4aae564f | 5567 | if (!subuid_f) { |
97e9cfa0 | 5568 | ERROR("Your system is not configured with subuids"); |
97e9cfa0 SH |
5569 | return; |
5570 | } | |
0fd73091 | 5571 | |
4aae564f | 5572 | while (getline(&line, &len, subuid_f) != -1) { |
0fd73091 | 5573 | char *p, *p2; |
b7930180 | 5574 | size_t no_newline = 0; |
0fd73091 CB |
5575 | |
5576 | p = strchr(line, ':'); | |
97e9cfa0 SH |
5577 | if (*line == '#') |
5578 | continue; | |
5579 | if (!p) | |
5580 | continue; | |
5581 | *p = '\0'; | |
5582 | p++; | |
0fd73091 | 5583 | |
71528742 | 5584 | if (!strequal(line, uname)) |
97e9cfa0 | 5585 | continue; |
0fd73091 | 5586 | |
97e9cfa0 SH |
5587 | p2 = strchr(p, ':'); |
5588 | if (!p2) | |
5589 | continue; | |
5590 | *p2 = '\0'; | |
5591 | p2++; | |
5592 | if (!*p2) | |
5593 | continue; | |
b7930180 CB |
5594 | no_newline = strcspn(p2, "\n"); |
5595 | p2[no_newline] = '\0'; | |
5596 | ||
b7b2fde4 | 5597 | if (lxc_safe_uint(p, &uid) < 0) |
0fd73091 | 5598 | WARN("Could not parse UID"); |
b7b2fde4 | 5599 | if (lxc_safe_uint(p2, &urange) < 0) |
0fd73091 | 5600 | WARN("Could not parse UID range"); |
97e9cfa0 | 5601 | } |
97e9cfa0 | 5602 | |
4110345b | 5603 | subgid_f = fopen(subgidfile, "re"); |
4aae564f | 5604 | if (!subgid_f) { |
97e9cfa0 | 5605 | ERROR("Your system is not configured with subgids"); |
97e9cfa0 SH |
5606 | return; |
5607 | } | |
0fd73091 | 5608 | |
4aae564f | 5609 | while (getline(&line, &len, subgid_f) != -1) { |
0fd73091 | 5610 | char *p, *p2; |
b7930180 | 5611 | size_t no_newline = 0; |
0fd73091 CB |
5612 | |
5613 | p = strchr(line, ':'); | |
97e9cfa0 SH |
5614 | if (*line == '#') |
5615 | continue; | |
5616 | if (!p) | |
5617 | continue; | |
5618 | *p = '\0'; | |
5619 | p++; | |
0fd73091 | 5620 | |
71528742 | 5621 | if (!strequal(line, uname)) |
97e9cfa0 | 5622 | continue; |
0fd73091 | 5623 | |
97e9cfa0 SH |
5624 | p2 = strchr(p, ':'); |
5625 | if (!p2) | |
5626 | continue; | |
5627 | *p2 = '\0'; | |
5628 | p2++; | |
5629 | if (!*p2) | |
5630 | continue; | |
b7930180 CB |
5631 | no_newline = strcspn(p2, "\n"); |
5632 | p2[no_newline] = '\0'; | |
5633 | ||
b7b2fde4 | 5634 | if (lxc_safe_uint(p, &gid) < 0) |
0fd73091 | 5635 | WARN("Could not parse GID"); |
b7b2fde4 | 5636 | if (lxc_safe_uint(p2, &grange) < 0) |
0fd73091 | 5637 | WARN("Could not parse GID range"); |
97e9cfa0 | 5638 | } |
97e9cfa0 | 5639 | |
97e9cfa0 SH |
5640 | if (!urange || !grange) { |
5641 | ERROR("You do not have subuids or subgids allocated"); | |
5642 | ERROR("Unprivileged containers require subuids and subgids"); | |
5643 | return; | |
5644 | } | |
5645 | ||
5646 | ERROR("You must either run as root, or define uid mappings"); | |
5647 | ERROR("To pass uid mappings to lxc-create, you could create"); | |
5648 | ERROR("~/.config/lxc/default.conf:"); | |
5649 | ERROR("lxc.include = %s", LXC_DEFAULT_CONFIG); | |
bdcbb6b3 CB |
5650 | ERROR("lxc.idmap = u 0 %u %u", uid, urange); |
5651 | ERROR("lxc.idmap = g 0 %u %u", gid, grange); | |
97e9cfa0 | 5652 | } |
aaf26830 | 5653 | |
a7307747 SH |
5654 | static void free_cgroup_settings(struct lxc_list *result) |
5655 | { | |
5656 | struct lxc_list *iterator, *next; | |
5657 | ||
0fd73091 | 5658 | lxc_list_for_each_safe (iterator, result, next) { |
a7307747 | 5659 | lxc_list_del(iterator); |
55022530 | 5660 | free_disarm(iterator); |
a7307747 | 5661 | } |
55022530 | 5662 | free_disarm(result); |
a7307747 SH |
5663 | } |
5664 | ||
0fd73091 | 5665 | /* Return the list of cgroup_settings sorted according to the following rules |
aaf26830 KT |
5666 | * 1. Put memory.limit_in_bytes before memory.memsw.limit_in_bytes |
5667 | */ | |
0fd73091 | 5668 | struct lxc_list *sort_cgroup_settings(struct lxc_list *cgroup_settings) |
aaf26830 KT |
5669 | { |
5670 | struct lxc_list *result; | |
aaf26830 | 5671 | struct lxc_cgroup *cg = NULL; |
0fd73091 | 5672 | struct lxc_list *it = NULL, *item = NULL, *memsw_limit = NULL; |
aaf26830 | 5673 | |
b8e43ef0 | 5674 | result = lxc_list_new(); |
0fd73091 | 5675 | if (!result) |
fac7c663 | 5676 | return NULL; |
aaf26830 KT |
5677 | lxc_list_init(result); |
5678 | ||
0fd73091 CB |
5679 | /* Iterate over the cgroup settings and copy them to the output list. */ |
5680 | lxc_list_for_each (it, cgroup_settings) { | |
b8e43ef0 | 5681 | item = zalloc(sizeof(*item)); |
fac7c663 | 5682 | if (!item) { |
a7307747 | 5683 | free_cgroup_settings(result); |
fac7c663 KT |
5684 | return NULL; |
5685 | } | |
0fd73091 | 5686 | |
aaf26830 KT |
5687 | item->elem = it->elem; |
5688 | cg = it->elem; | |
71528742 | 5689 | if (strequal(cg->subsystem, "memory.memsw.limit_in_bytes")) { |
aaf26830 KT |
5690 | /* Store the memsw_limit location */ |
5691 | memsw_limit = item; | |
71528742 | 5692 | } else if (strequal(cg->subsystem, "memory.limit_in_bytes") && |
0fd73091 CB |
5693 | memsw_limit != NULL) { |
5694 | /* lxc.cgroup.memory.memsw.limit_in_bytes is found | |
5695 | * before lxc.cgroup.memory.limit_in_bytes, swap these | |
5696 | * two items */ | |
aaf26830 KT |
5697 | item->elem = memsw_limit->elem; |
5698 | memsw_limit->elem = it->elem; | |
5699 | } | |
5700 | lxc_list_add_tail(result, item); | |
5701 | } | |
5702 | ||
5703 | return result; | |
a7307747 | 5704 | } |