]>
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> | |
0ad19a3f | 18 | #include <stdio.h> |
0ad19a3f | 19 | #include <stdlib.h> |
0ad19a3f | 20 | #include <string.h> |
8f3e280e CB |
21 | #include <sys/mman.h> |
22 | #include <sys/mount.h> | |
23 | #include <sys/param.h> | |
24 | #include <sys/prctl.h> | |
6a49f05e | 25 | #include <sys/sendfile.h> |
8f3e280e | 26 | #include <sys/socket.h> |
9d257a2a | 27 | #include <sys/stat.h> |
2d76d1d7 | 28 | #include <sys/syscall.h> |
9d257a2a | 29 | #include <sys/sysmacros.h> |
97e9cfa0 | 30 | #include <sys/types.h> |
8f3e280e CB |
31 | #include <sys/utsname.h> |
32 | #include <sys/wait.h> | |
9d257a2a CB |
33 | #include <time.h> |
34 | #include <unistd.h> | |
1d52bdf7 | 35 | |
d38dd64a CB |
36 | #include "af_unix.h" |
37 | #include "caps.h" | |
38 | #include "cgroup.h" | |
bf651989 | 39 | #include "cgroup2_devices.h" |
d38dd64a CB |
40 | #include "conf.h" |
41 | #include "config.h" | |
42 | #include "confile.h" | |
43 | #include "confile_utils.h" | |
44 | #include "error.h" | |
45 | #include "log.h" | |
46 | #include "lsm/lsm.h" | |
47 | #include "lxclock.h" | |
48 | #include "lxcseccomp.h" | |
49 | #include "macro.h" | |
2f443e88 | 50 | #include "memory_utils.h" |
d38dd64a CB |
51 | #include "namespace.h" |
52 | #include "network.h" | |
53 | #include "parse.h" | |
13be2733 | 54 | #include "raw_syscalls.h" |
d38dd64a CB |
55 | #include "ringbuf.h" |
56 | #include "start.h" | |
57 | #include "storage.h" | |
58 | #include "storage/overlay.h" | |
6b3d24d7 | 59 | #include "syscall_wrappers.h" |
d38dd64a CB |
60 | #include "terminal.h" |
61 | #include "utils.h" | |
20502652 | 62 | #include "uuid.h" |
d38dd64a | 63 | |
af6824fc | 64 | #ifdef MAJOR_IN_MKDEV |
9d257a2a | 65 | #include <sys/mkdev.h> |
af6824fc | 66 | #endif |
af6824fc | 67 | |
614305f3 | 68 | #ifdef HAVE_STATVFS |
2938f7c8 | 69 | #include <sys/statvfs.h> |
614305f3 | 70 | #endif |
e827ff7e SG |
71 | |
72 | #if HAVE_PTY_H | |
b0a33c1e | 73 | #include <pty.h> |
e827ff7e SG |
74 | #else |
75 | #include <../include/openpty.h> | |
76 | #endif | |
0ad19a3f | 77 | |
9d257a2a CB |
78 | #if HAVE_LIBCAP |
79 | #include <sys/capability.h> | |
80 | #endif | |
81 | ||
82 | #if HAVE_SYS_PERSONALITY_H | |
83 | #include <sys/personality.h> | |
84 | #endif | |
85 | ||
f1e05b90 DJ |
86 | #ifndef HAVE_STRLCAT |
87 | #include "include/strlcat.h" | |
88 | #endif | |
89 | ||
9d257a2a CB |
90 | #if IS_BIONIC |
91 | #include <../include/lxcmntent.h> | |
92 | #else | |
93 | #include <mntent.h> | |
94 | #endif | |
95 | ||
96 | #if !defined(HAVE_PRLIMIT) && defined(HAVE_PRLIMIT64) | |
97 | #include <../include/prlimit.h> | |
98 | #endif | |
99 | ||
ac2cecc4 | 100 | lxc_log_define(conf, lxc); |
e5bda9ee | 101 | |
0fd73091 CB |
102 | /* The lxc_conf of the container currently being worked on in an API call. |
103 | * This is used in the error calls. | |
104 | */ | |
105 | #ifdef HAVE_TLS | |
d7f19646 | 106 | thread_local struct lxc_conf *current_config; |
0fd73091 CB |
107 | #else |
108 | struct lxc_conf *current_config; | |
109 | #endif | |
8912711c | 110 | |
0fd73091 CB |
111 | char *lxchook_names[NUM_LXC_HOOKS] = { |
112 | "pre-start", | |
113 | "pre-mount", | |
114 | "mount", | |
115 | "autodev", | |
116 | "start", | |
117 | "stop", | |
118 | "post-stop", | |
119 | "clone", | |
120 | "destroy", | |
121 | "start-host" | |
122 | }; | |
72d0e1cb | 123 | |
998ac676 RT |
124 | struct mount_opt { |
125 | char *name; | |
126 | int clear; | |
127 | int flag; | |
128 | }; | |
129 | ||
81810dd1 DL |
130 | struct caps_opt { |
131 | char *name; | |
132 | int value; | |
133 | }; | |
134 | ||
c6d09e15 WB |
135 | struct limit_opt { |
136 | char *name; | |
137 | int value; | |
138 | }; | |
139 | ||
998ac676 | 140 | static struct mount_opt mount_opt[] = { |
470b359b CB |
141 | { "async", 1, MS_SYNCHRONOUS }, |
142 | { "atime", 1, MS_NOATIME }, | |
143 | { "bind", 0, MS_BIND }, | |
88d413d5 | 144 | { "defaults", 0, 0 }, |
88d413d5 | 145 | { "dev", 1, MS_NODEV }, |
470b359b | 146 | { "diratime", 1, MS_NODIRATIME }, |
88d413d5 | 147 | { "dirsync", 0, MS_DIRSYNC }, |
470b359b | 148 | { "exec", 1, MS_NOEXEC }, |
8912711c | 149 | { "lazytime", 0, MS_LAZYTIME }, |
88d413d5 | 150 | { "mand", 0, MS_MANDLOCK }, |
88d413d5 | 151 | { "noatime", 0, MS_NOATIME }, |
470b359b | 152 | { "nodev", 0, MS_NODEV }, |
88d413d5 | 153 | { "nodiratime", 0, MS_NODIRATIME }, |
470b359b CB |
154 | { "noexec", 0, MS_NOEXEC }, |
155 | { "nomand", 1, MS_MANDLOCK }, | |
156 | { "norelatime", 1, MS_RELATIME }, | |
157 | { "nostrictatime", 1, MS_STRICTATIME }, | |
158 | { "nosuid", 0, MS_NOSUID }, | |
88d413d5 SW |
159 | { "rbind", 0, MS_BIND|MS_REC }, |
160 | { "relatime", 0, MS_RELATIME }, | |
470b359b CB |
161 | { "remount", 0, MS_REMOUNT }, |
162 | { "ro", 0, MS_RDONLY }, | |
163 | { "rw", 1, MS_RDONLY }, | |
88d413d5 | 164 | { "strictatime", 0, MS_STRICTATIME }, |
470b359b CB |
165 | { "suid", 1, MS_NOSUID }, |
166 | { "sync", 0, MS_SYNCHRONOUS }, | |
88d413d5 | 167 | { NULL, 0, 0 }, |
998ac676 RT |
168 | }; |
169 | ||
d840039e | 170 | static struct mount_opt propagation_opt[] = { |
0fd73091 CB |
171 | { "private", 0, MS_PRIVATE }, |
172 | { "shared", 0, MS_SHARED }, | |
173 | { "slave", 0, MS_SLAVE }, | |
174 | { "unbindable", 0, MS_UNBINDABLE }, | |
175 | { "rprivate", 0, MS_PRIVATE|MS_REC }, | |
176 | { "rshared", 0, MS_SHARED|MS_REC }, | |
177 | { "rslave", 0, MS_SLAVE|MS_REC }, | |
178 | { "runbindable", 0, MS_UNBINDABLE|MS_REC }, | |
179 | { NULL, 0, 0 }, | |
d840039e YT |
180 | }; |
181 | ||
81810dd1 | 182 | static struct caps_opt caps_opt[] = { |
8560cd36 | 183 | #if HAVE_LIBCAP |
0fd73091 CB |
184 | { "chown", CAP_CHOWN }, |
185 | { "dac_override", CAP_DAC_OVERRIDE }, | |
186 | { "dac_read_search", CAP_DAC_READ_SEARCH }, | |
187 | { "fowner", CAP_FOWNER }, | |
188 | { "fsetid", CAP_FSETID }, | |
189 | { "kill", CAP_KILL }, | |
190 | { "setgid", CAP_SETGID }, | |
191 | { "setuid", CAP_SETUID }, | |
192 | { "setpcap", CAP_SETPCAP }, | |
193 | { "linux_immutable", CAP_LINUX_IMMUTABLE }, | |
194 | { "net_bind_service", CAP_NET_BIND_SERVICE }, | |
195 | { "net_broadcast", CAP_NET_BROADCAST }, | |
196 | { "net_admin", CAP_NET_ADMIN }, | |
197 | { "net_raw", CAP_NET_RAW }, | |
198 | { "ipc_lock", CAP_IPC_LOCK }, | |
199 | { "ipc_owner", CAP_IPC_OWNER }, | |
200 | { "sys_module", CAP_SYS_MODULE }, | |
201 | { "sys_rawio", CAP_SYS_RAWIO }, | |
202 | { "sys_chroot", CAP_SYS_CHROOT }, | |
203 | { "sys_ptrace", CAP_SYS_PTRACE }, | |
204 | { "sys_pacct", CAP_SYS_PACCT }, | |
205 | { "sys_admin", CAP_SYS_ADMIN }, | |
206 | { "sys_boot", CAP_SYS_BOOT }, | |
207 | { "sys_nice", CAP_SYS_NICE }, | |
208 | { "sys_resource", CAP_SYS_RESOURCE }, | |
209 | { "sys_time", CAP_SYS_TIME }, | |
210 | { "sys_tty_config", CAP_SYS_TTY_CONFIG }, | |
211 | { "mknod", CAP_MKNOD }, | |
212 | { "lease", CAP_LEASE }, | |
57b837e2 | 213 | #ifdef CAP_AUDIT_READ |
0fd73091 | 214 | { "audit_read", CAP_AUDIT_READ }, |
57b837e2 | 215 | #endif |
9527e566 | 216 | #ifdef CAP_AUDIT_WRITE |
0fd73091 | 217 | { "audit_write", CAP_AUDIT_WRITE }, |
9527e566 FW |
218 | #endif |
219 | #ifdef CAP_AUDIT_CONTROL | |
0fd73091 | 220 | { "audit_control", CAP_AUDIT_CONTROL }, |
9527e566 | 221 | #endif |
0fd73091 CB |
222 | { "setfcap", CAP_SETFCAP }, |
223 | { "mac_override", CAP_MAC_OVERRIDE }, | |
224 | { "mac_admin", CAP_MAC_ADMIN }, | |
5170c716 | 225 | #ifdef CAP_SYSLOG |
0fd73091 | 226 | { "syslog", CAP_SYSLOG }, |
5170c716 CS |
227 | #endif |
228 | #ifdef CAP_WAKE_ALARM | |
0fd73091 | 229 | { "wake_alarm", CAP_WAKE_ALARM }, |
5170c716 | 230 | #endif |
2b54359b | 231 | #ifdef CAP_BLOCK_SUSPEND |
0fd73091 | 232 | { "block_suspend", CAP_BLOCK_SUSPEND }, |
2b54359b | 233 | #endif |
495d2046 | 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 SH |
297 | |
298 | output = malloc(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 | |
6f8d00d2 CB |
369 | buffer = malloc(size); |
370 | if (!buffer) | |
371 | return -ENOMEM; | |
372 | ||
327cce76 | 373 | if (hook_version == 0) |
3f60c2f7 | 374 | buf_pos = snprintf(buffer, size, "exec %s %s %s %s", script, name, section, hookname); |
327cce76 | 375 | else |
3f60c2f7 | 376 | buf_pos = snprintf(buffer, size, "exec %s", script); |
55022530 CB |
377 | if (buf_pos < 0 || (size_t)buf_pos >= size) |
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 CB |
391 | |
392 | if (strcmp(section, "net") == 0) { | |
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 | |
a8144263 | 405 | if (strcmp(argv[0], "macvlan") == 0) { |
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); |
a8144263 | 410 | } else if (strcmp(argv[0], "phys") == 0) { |
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); |
a8144263 | 415 | } else if (strcmp(argv[0], "veth") == 0) { |
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 | ||
586b1ce7 | 434 | ret = snprintf(buffer + buf_pos, len, " %s", argv[i]); |
55022530 CB |
435 | if (ret < 0 || (size_t)ret >= len) |
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); |
6d1a5f93 | 469 | ret = snprintf(buffer, size, "exec %s %s %s", script, name, section); |
0fd73091 | 470 | if (ret < 0 || ret >= size) |
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 SH |
476 | int rc; |
477 | rc = snprintf(buffer + ret, len, " %s", p); | |
7b5a2435 DJ |
478 | if (rc < 0 || rc >= len) { |
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 | ||
0fd73091 | 489 | /* pin_rootfs |
63fc76c3 | 490 | * if rootfs is a directory, then open ${rootfs}/.lxc-keep for writing for |
b7ed4bf0 CS |
491 | * the duration of the container run, to prevent the container from marking |
492 | * the underlying fs readonly on shutdown. unlink the file immediately so | |
63fc76c3 GJ |
493 | * no name pollution is happens. |
494 | * don't unlink on NFS to avoid random named stale handles. | |
0c547523 SH |
495 | * return -1 on error. |
496 | * return -2 if nothing needed to be pinned. | |
497 | * return an open fd (>=0) if we pinned it. | |
498 | */ | |
499 | int pin_rootfs(const char *rootfs) | |
500 | { | |
957c4704 | 501 | __do_free char *absrootfs = NULL; |
0fd73091 | 502 | int fd, ret; |
6b5a54cd | 503 | char absrootfspin[PATH_MAX]; |
0c547523 | 504 | struct stat s; |
63fc76c3 | 505 | struct statfs sfs; |
0c547523 | 506 | |
e99ee0de | 507 | if (rootfs == NULL || strlen(rootfs) == 0) |
0d03360a | 508 | return -2; |
e99ee0de | 509 | |
74e7b662 | 510 | absrootfs = realpath(rootfs, NULL); |
511 | if (!absrootfs) | |
9be53773 | 512 | return -2; |
0c547523 | 513 | |
0fd73091 | 514 | ret = stat(absrootfs, &s); |
957c4704 | 515 | if (ret < 0) |
0c547523 | 516 | return -1; |
0c547523 | 517 | |
957c4704 | 518 | if (!S_ISDIR(s.st_mode)) |
0c547523 SH |
519 | return -2; |
520 | ||
55022530 CB |
521 | ret = snprintf(absrootfspin, sizeof(absrootfspin), "%s/.lxc-keep", absrootfs); |
522 | if (ret < 0 || (size_t)ret >= sizeof(absrootfspin)) | |
0c547523 | 523 | return -1; |
0c547523 | 524 | |
55022530 | 525 | fd = open(absrootfspin, O_CREAT | O_RDWR, S_IWUSR | S_IRUSR | O_CLOEXEC); |
b7ed4bf0 CS |
526 | if (fd < 0) |
527 | return fd; | |
0fd73091 | 528 | |
205fc010 CB |
529 | ret = fstatfs (fd, &sfs); |
530 | if (ret < 0) | |
531 | return fd; | |
63fc76c3 | 532 | |
55022530 CB |
533 | if (sfs.f_type == NFS_SUPER_MAGIC) |
534 | return log_debug(fd, "Rootfs on NFS, not unlinking pin file \"%s\"", absrootfspin); | |
63fc76c3 | 535 | |
b7ed4bf0 | 536 | (void)unlink(absrootfspin); |
0fd73091 | 537 | |
0c547523 SH |
538 | return fd; |
539 | } | |
540 | ||
0fd73091 CB |
541 | /* If we are asking to remount something, make sure that any NOEXEC etc are |
542 | * honored. | |
e2a7e8dc | 543 | */ |
5ae72b98 | 544 | unsigned long add_required_remount_flags(const char *s, const char *d, |
5285689c | 545 | unsigned long flags) |
e2a7e8dc | 546 | { |
614305f3 | 547 | #ifdef HAVE_STATVFS |
0fd73091 | 548 | int ret; |
e2a7e8dc SH |
549 | struct statvfs sb; |
550 | unsigned long required_flags = 0; | |
551 | ||
e2a7e8dc SH |
552 | if (!s) |
553 | s = d; | |
554 | ||
555 | if (!s) | |
556 | return flags; | |
0fd73091 CB |
557 | |
558 | ret = statvfs(s, &sb); | |
559 | if (ret < 0) | |
e2a7e8dc SH |
560 | return flags; |
561 | ||
69eadddb CB |
562 | if (flags & MS_REMOUNT) { |
563 | if (sb.f_flag & MS_NOSUID) | |
564 | required_flags |= MS_NOSUID; | |
565 | if (sb.f_flag & MS_NODEV) | |
566 | required_flags |= MS_NODEV; | |
567 | if (sb.f_flag & MS_RDONLY) | |
568 | required_flags |= MS_RDONLY; | |
569 | if (sb.f_flag & MS_NOEXEC) | |
570 | required_flags |= MS_NOEXEC; | |
571 | } | |
572 | ||
573 | if (sb.f_flag & MS_NOATIME) | |
574 | required_flags |= MS_NOATIME; | |
575 | if (sb.f_flag & MS_NODIRATIME) | |
576 | required_flags |= MS_NODIRATIME; | |
577 | if (sb.f_flag & MS_LAZYTIME) | |
578 | required_flags |= MS_LAZYTIME; | |
579 | if (sb.f_flag & MS_RELATIME) | |
580 | required_flags |= MS_RELATIME; | |
581 | if (sb.f_flag & MS_STRICTATIME) | |
582 | required_flags |= MS_STRICTATIME; | |
e2a7e8dc SH |
583 | |
584 | return flags | required_flags; | |
614305f3 SH |
585 | #else |
586 | return flags; | |
587 | #endif | |
e2a7e8dc SH |
588 | } |
589 | ||
6b741397 CB |
590 | static int add_shmount_to_list(struct lxc_conf *conf) |
591 | { | |
6b5a54cd | 592 | char new_mount[PATH_MAX]; |
0d190408 | 593 | /* Offset for the leading '/' since the path_cont |
6b741397 CB |
594 | * is absolute inside the container. |
595 | */ | |
596 | int offset = 1, ret = -1; | |
0d190408 | 597 | |
6b741397 CB |
598 | ret = snprintf(new_mount, sizeof(new_mount), |
599 | "%s %s none bind,create=dir 0 0", conf->shmount.path_host, | |
600 | conf->shmount.path_cont + offset); | |
60534030 | 601 | if (ret < 0 || (size_t)ret >= sizeof(new_mount)) |
0d190408 LT |
602 | return -1; |
603 | ||
6b741397 | 604 | return add_elem_to_mount_list(new_mount, conf); |
0d190408 LT |
605 | } |
606 | ||
4fb3cba5 | 607 | static int lxc_mount_auto_mounts(struct lxc_conf *conf, int flags, struct lxc_handler *handler) |
368bbc02 | 608 | { |
0fd73091 | 609 | int i, r; |
b06b8511 CS |
610 | static struct { |
611 | int match_mask; | |
612 | int match_flag; | |
613 | const char *source; | |
614 | const char *destination; | |
615 | const char *fstype; | |
616 | unsigned long flags; | |
617 | const char *options; | |
618 | } default_mounts[] = { | |
0fd73091 CB |
619 | /* Read-only bind-mounting... In older kernels, doing that |
620 | * required to do one MS_BIND mount and then | |
621 | * MS_REMOUNT|MS_RDONLY the same one. According to mount(2) | |
622 | * manpage, MS_BIND honors MS_RDONLY from kernel 2.6.26 | |
623 | * onwards. However, this apparently does not work on kernel | |
624 | * 3.8. Unfortunately, on that very same kernel, doing the same | |
625 | * trick as above doesn't seem to work either, there one needs | |
626 | * to ALSO specify MS_BIND for the remount, otherwise the | |
627 | * entire fs is remounted read-only or the mount fails because | |
628 | * it's busy... MS_REMOUNT|MS_BIND|MS_RDONLY seems to work for | |
629 | * kernels as low as 2.6.32... | |
368bbc02 | 630 | */ |
0fd73091 | 631 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "proc", "%r/proc", "proc", MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL }, |
592fd47a | 632 | /* proc/tty is used as a temporary placeholder for proc/sys/net which we'll move back in a few steps */ |
0fd73091 CB |
633 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sys/net", "%r/proc/tty", NULL, MS_BIND, NULL }, |
634 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sys", "%r/proc/sys", NULL, MS_BIND, NULL }, | |
635 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, NULL, "%r/proc/sys", NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL }, | |
636 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/tty", "%r/proc/sys/net", NULL, MS_MOVE, NULL }, | |
637 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sysrq-trigger", "%r/proc/sysrq-trigger", NULL, MS_BIND, NULL }, | |
638 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, NULL, "%r/proc/sysrq-trigger", NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL }, | |
639 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW, "proc", "%r/proc", "proc", MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL }, | |
640 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RW, "sysfs", "%r/sys", "sysfs", 0, NULL }, | |
641 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RO, "sysfs", "%r/sys", "sysfs", MS_RDONLY, NULL }, | |
642 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "sysfs", "%r/sys", "sysfs", MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL }, | |
d1c203f4 | 643 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "%r/sys", "%r/sys", NULL, MS_BIND, NULL }, |
0fd73091 CB |
644 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, NULL, "%r/sys", NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL }, |
645 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "sysfs", "%r/sys/devices/virtual/net", "sysfs", 0, NULL }, | |
646 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "%r/sys/devices/virtual/net/devices/virtual/net", "%r/sys/devices/virtual/net", NULL, MS_BIND, NULL }, | |
647 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, NULL, "%r/sys/devices/virtual/net", NULL, MS_REMOUNT|MS_BIND|MS_NOSUID|MS_NODEV|MS_NOEXEC, NULL }, | |
648 | { 0, 0, NULL, NULL, NULL, 0, NULL } | |
b06b8511 | 649 | }; |
368bbc02 | 650 | |
b06b8511 | 651 | for (i = 0; default_mounts[i].match_mask; i++) { |
8db92302 | 652 | __do_free char *destination = NULL, *source = NULL; |
0fd73091 CB |
653 | int saved_errno; |
654 | unsigned long mflags; | |
0fd73091 CB |
655 | if ((flags & default_mounts[i].match_mask) != default_mounts[i].match_flag) |
656 | continue; | |
657 | ||
658 | if (default_mounts[i].source) { | |
cc4fd506 | 659 | /* will act like strdup if %r is not present */ |
0fd73091 CB |
660 | source = lxc_string_replace("%r", conf->rootfs.path ? conf->rootfs.mount : "", default_mounts[i].source); |
661 | if (!source) | |
cc4fd506 | 662 | return -1; |
0fd73091 | 663 | } |
f24a52d5 | 664 | |
55022530 CB |
665 | if (!default_mounts[i].destination) |
666 | return log_error(-1, "BUG: auto mounts destination %d was NULL", i); | |
0fd73091 CB |
667 | |
668 | /* will act like strdup if %r is not present */ | |
669 | destination = lxc_string_replace("%r", conf->rootfs.path ? conf->rootfs.mount : "", default_mounts[i].destination); | |
55022530 | 670 | if (!destination) |
0fd73091 | 671 | return -1; |
0fd73091 CB |
672 | |
673 | mflags = add_required_remount_flags(source, destination, | |
674 | default_mounts[i].flags); | |
675 | r = safe_mount(source, destination, default_mounts[i].fstype, | |
676 | mflags, default_mounts[i].options, | |
677 | conf->rootfs.path ? conf->rootfs.mount : NULL); | |
678 | saved_errno = errno; | |
679 | if (r < 0 && errno == ENOENT) { | |
55022530 | 680 | INFO("Mount source or target for \"%s\" on \"%s\" does not exist. Skipping", source, destination); |
0fd73091 CB |
681 | r = 0; |
682 | } else if (r < 0) { | |
683 | SYSERROR("Failed to mount \"%s\" on \"%s\" with flags %lu", source, destination, mflags); | |
684 | } | |
685 | ||
0fd73091 CB |
686 | if (r < 0) { |
687 | errno = saved_errno; | |
688 | return -1; | |
368bbc02 | 689 | } |
368bbc02 CS |
690 | } |
691 | ||
b06b8511 | 692 | if (flags & LXC_AUTO_CGROUP_MASK) { |
0769b82a CS |
693 | int cg_flags; |
694 | ||
3f69fb12 | 695 | cg_flags = flags & (LXC_AUTO_CGROUP_MASK & ~LXC_AUTO_CGROUP_FORCE); |
0fd73091 CB |
696 | /* If the type of cgroup mount was not specified, it depends on |
697 | * the container's capabilities as to what makes sense: if we | |
698 | * have CAP_SYS_ADMIN, the read-only part can be remounted | |
699 | * read-write anyway, so we may as well default to read-write; | |
700 | * then the admin will not be given a false sense of security. | |
701 | * (And if they really want mixed r/o r/w, then they can | |
702 | * explicitly specify :mixed.) OTOH, if the container lacks | |
703 | * CAP_SYS_ADMIN, do only default to :mixed, because then the | |
704 | * container can't remount it read-write. | |
705 | */ | |
0769b82a CS |
706 | if (cg_flags == LXC_AUTO_CGROUP_NOSPEC || cg_flags == LXC_AUTO_CGROUP_FULL_NOSPEC) { |
707 | int has_sys_admin = 0; | |
b0ee5983 CB |
708 | |
709 | if (!lxc_list_empty(&conf->keepcaps)) | |
0769b82a | 710 | has_sys_admin = in_caplist(CAP_SYS_ADMIN, &conf->keepcaps); |
b0ee5983 | 711 | else |
0769b82a | 712 | has_sys_admin = !in_caplist(CAP_SYS_ADMIN, &conf->caps); |
b0ee5983 CB |
713 | |
714 | if (cg_flags == LXC_AUTO_CGROUP_NOSPEC) | |
0769b82a | 715 | cg_flags = has_sys_admin ? LXC_AUTO_CGROUP_RW : LXC_AUTO_CGROUP_MIXED; |
b0ee5983 | 716 | else |
0769b82a | 717 | cg_flags = has_sys_admin ? LXC_AUTO_CGROUP_FULL_RW : LXC_AUTO_CGROUP_FULL_MIXED; |
0769b82a | 718 | } |
0fd73091 | 719 | |
3f69fb12 | 720 | if (flags & LXC_AUTO_CGROUP_FORCE) |
0fd73091 CB |
721 | cg_flags |= LXC_AUTO_CGROUP_FORCE; |
722 | ||
2202afc9 CB |
723 | if (!handler->cgroup_ops->mount(handler->cgroup_ops, |
724 | handler, | |
725 | conf->rootfs.path ? conf->rootfs.mount : "", | |
55022530 CB |
726 | cg_flags)) |
727 | return log_error_errno(-1, errno, "Failed to mount \"/sys/fs/cgroup\""); | |
368bbc02 CS |
728 | } |
729 | ||
0d190408 LT |
730 | if (flags & LXC_AUTO_SHMOUNTS_MASK) { |
731 | int ret = add_shmount_to_list(conf); | |
55022530 CB |
732 | if (ret < 0) |
733 | return log_error(-1, "Failed to add shmount entry to container config"); | |
0d190408 LT |
734 | } |
735 | ||
368bbc02 | 736 | return 0; |
368bbc02 CS |
737 | } |
738 | ||
4e5440c6 | 739 | static int setup_utsname(struct utsname *utsname) |
0ad19a3f | 740 | { |
0fd73091 CB |
741 | int ret; |
742 | ||
4e5440c6 DL |
743 | if (!utsname) |
744 | return 0; | |
0ad19a3f | 745 | |
0fd73091 | 746 | ret = sethostname(utsname->nodename, strlen(utsname->nodename)); |
55022530 CB |
747 | if (ret < 0) |
748 | return log_error_errno(-1, errno, "Failed to set the hostname to \"%s\"", | |
749 | utsname->nodename); | |
0ad19a3f | 750 | |
0fd73091 | 751 | INFO("Set hostname to \"%s\"", utsname->nodename); |
cd54d859 | 752 | |
0ad19a3f | 753 | return 0; |
754 | } | |
755 | ||
69aa6655 DE |
756 | struct dev_symlinks { |
757 | const char *oldpath; | |
758 | const char *name; | |
759 | }; | |
760 | ||
761 | static const struct dev_symlinks dev_symlinks[] = { | |
0fd73091 CB |
762 | { "/proc/self/fd", "fd" }, |
763 | { "/proc/self/fd/0", "stdin" }, | |
764 | { "/proc/self/fd/1", "stdout" }, | |
765 | { "/proc/self/fd/2", "stderr" }, | |
69aa6655 DE |
766 | }; |
767 | ||
ed8704d0 | 768 | static int lxc_setup_dev_symlinks(const struct lxc_rootfs *rootfs) |
69aa6655 | 769 | { |
0fd73091 | 770 | int i, ret; |
6b5a54cd | 771 | char path[PATH_MAX]; |
09227be2 | 772 | struct stat s; |
69aa6655 | 773 | |
69aa6655 DE |
774 | for (i = 0; i < sizeof(dev_symlinks) / sizeof(dev_symlinks[0]); i++) { |
775 | const struct dev_symlinks *d = &dev_symlinks[i]; | |
0fd73091 CB |
776 | |
777 | ret = snprintf(path, sizeof(path), "%s/dev/%s", | |
778 | rootfs->path ? rootfs->mount : "", d->name); | |
55022530 | 779 | if (ret < 0 || (size_t)ret >= sizeof(path)) |
69aa6655 | 780 | return -1; |
09227be2 | 781 | |
0fd73091 CB |
782 | /* Stat the path first. If we don't get an error accept it as |
783 | * is and don't try to create it | |
09227be2 | 784 | */ |
0fd73091 CB |
785 | ret = stat(path, &s); |
786 | if (ret == 0) | |
09227be2 | 787 | continue; |
09227be2 | 788 | |
69aa6655 DE |
789 | ret = symlink(d->oldpath, path); |
790 | if (ret && errno != EEXIST) { | |
55022530 | 791 | if (errno == EROFS) |
0fd73091 | 792 | WARN("Failed to create \"%s\". Read-only filesystem", path); |
55022530 CB |
793 | else |
794 | return log_error_errno(-1, errno, "Failed to create \"%s\"", path); | |
69aa6655 DE |
795 | } |
796 | } | |
0fd73091 | 797 | |
69aa6655 DE |
798 | return 0; |
799 | } | |
800 | ||
2187efd3 | 801 | /* Build a space-separate list of ptys to pass to systemd. */ |
885766f5 | 802 | static bool append_ttyname(char **pp, char *name) |
b0a33c1e | 803 | { |
393903d1 | 804 | char *p; |
f1e05b90 | 805 | size_t size; |
393903d1 SH |
806 | |
807 | if (!*pp) { | |
808 | *pp = malloc(strlen(name) + strlen("container_ttys=") + 1); | |
809 | if (!*pp) | |
810 | return false; | |
0fd73091 | 811 | |
393903d1 SH |
812 | sprintf(*pp, "container_ttys=%s", name); |
813 | return true; | |
814 | } | |
0fd73091 | 815 | |
f1e05b90 DJ |
816 | size = strlen(*pp) + strlen(name) + 2; |
817 | p = realloc(*pp, size); | |
393903d1 SH |
818 | if (!p) |
819 | return false; | |
0fd73091 | 820 | |
393903d1 | 821 | *pp = p; |
f1e05b90 DJ |
822 | (void)strlcat(p, " ", size); |
823 | (void)strlcat(p, name, size); | |
0fd73091 | 824 | |
393903d1 SH |
825 | return true; |
826 | } | |
827 | ||
2187efd3 | 828 | static int lxc_setup_ttys(struct lxc_conf *conf) |
393903d1 | 829 | { |
9e1045e3 | 830 | int i, ret; |
0e4be3cf | 831 | const struct lxc_tty_info *ttys = &conf->ttys; |
885766f5 | 832 | char *ttydir = ttys->dir; |
6b5a54cd | 833 | char path[PATH_MAX], lxcpath[PATH_MAX]; |
b0a33c1e | 834 | |
e8bd4e43 | 835 | if (!conf->rootfs.path) |
bc9bd0e3 DL |
836 | return 0; |
837 | ||
885766f5 | 838 | for (i = 0; i < ttys->max; i++) { |
0e4be3cf | 839 | struct lxc_terminal_info *tty = &ttys->tty[i]; |
b0a33c1e | 840 | |
e8bd4e43 | 841 | ret = snprintf(path, sizeof(path), "/dev/tty%d", i + 1); |
73363c61 | 842 | if (ret < 0 || (size_t)ret >= sizeof(path)) |
7c6ef2a2 | 843 | return -1; |
9e1045e3 | 844 | |
7c6ef2a2 SH |
845 | if (ttydir) { |
846 | /* create dev/lxc/tty%d" */ | |
9e1045e3 CB |
847 | ret = snprintf(lxcpath, sizeof(lxcpath), |
848 | "/dev/%s/tty%d", ttydir, i + 1); | |
73363c61 | 849 | if (ret < 0 || (size_t)ret >= sizeof(lxcpath)) |
7c6ef2a2 | 850 | return -1; |
9e1045e3 | 851 | |
adc1c715 | 852 | ret = mknod(lxcpath, S_IFREG | 0000, 0); |
9e1045e3 | 853 | if (ret < 0 && errno != EEXIST) { |
73363c61 | 854 | SYSERROR("Failed to create \"%s\"", lxcpath); |
7c6ef2a2 SH |
855 | return -1; |
856 | } | |
9e1045e3 | 857 | |
7c6ef2a2 | 858 | ret = unlink(path); |
9e1045e3 | 859 | if (ret < 0 && errno != ENOENT) { |
73363c61 | 860 | SYSERROR("Failed to unlink \"%s\"", path); |
7c6ef2a2 SH |
861 | return -1; |
862 | } | |
b0a33c1e | 863 | |
2520facd | 864 | ret = mount(tty->name, lxcpath, "none", MS_BIND, 0); |
9e1045e3 | 865 | if (ret < 0) { |
55022530 | 866 | SYSWARN("Failed to bind mount \"%s\" onto \"%s\"", tty->name, lxcpath); |
7c6ef2a2 SH |
867 | continue; |
868 | } | |
55022530 | 869 | DEBUG("Bind mounted \"%s\" onto \"%s\"", tty->name, lxcpath); |
13954cce | 870 | |
9e1045e3 CB |
871 | ret = snprintf(lxcpath, sizeof(lxcpath), "%s/tty%d", |
872 | ttydir, i + 1); | |
73363c61 | 873 | if (ret < 0 || (size_t)ret >= sizeof(lxcpath)) |
9ba8130c | 874 | return -1; |
9e1045e3 | 875 | |
7c6ef2a2 | 876 | ret = symlink(lxcpath, path); |
55022530 CB |
877 | if (ret < 0) |
878 | return log_error_errno(-1, errno, "Failed to create symlink \"%s\" -> \"%s\"", path, lxcpath); | |
7c6ef2a2 | 879 | } else { |
9e1045e3 CB |
880 | /* If we populated /dev, then we need to create |
881 | * /dev/ttyN | |
882 | */ | |
d3ccc04e CB |
883 | ret = mknod(path, S_IFREG | 0000, 0); |
884 | if (ret < 0) /* this isn't fatal, continue */ | |
6d1400b5 | 885 | SYSERROR("Failed to create \"%s\"", path); |
9e1045e3 | 886 | |
2520facd | 887 | ret = mount(tty->name, path, "none", MS_BIND, 0); |
9e1045e3 | 888 | if (ret < 0) { |
2520facd | 889 | SYSERROR("Failed to mount '%s'->'%s'", tty->name, path); |
7c6ef2a2 SH |
890 | continue; |
891 | } | |
9e1045e3 | 892 | |
d3ccc04e | 893 | DEBUG("Bind mounted \"%s\" onto \"%s\"", tty->name, path); |
393903d1 | 894 | } |
9e1045e3 | 895 | |
55022530 CB |
896 | if (!append_ttyname(&conf->ttys.tty_names, tty->name)) |
897 | return log_error(-1, "Error setting up container_ttys string"); | |
b0a33c1e | 898 | } |
899 | ||
885766f5 | 900 | INFO("Finished setting up %zu /dev/tty<N> device(s)", ttys->max); |
b0a33c1e | 901 | return 0; |
902 | } | |
903 | ||
663014ee | 904 | int lxc_allocate_ttys(struct lxc_conf *conf) |
2187efd3 | 905 | { |
55022530 | 906 | __do_free struct lxc_terminal_info *tty_new = NULL; |
fca23691 | 907 | int ret; |
0fd73091 | 908 | struct lxc_tty_info *ttys = &conf->ttys; |
2187efd3 CB |
909 | |
910 | /* no tty in the configuration */ | |
885766f5 | 911 | if (ttys->max == 0) |
2187efd3 CB |
912 | return 0; |
913 | ||
55022530 CB |
914 | tty_new = malloc(sizeof(struct lxc_terminal_info) * ttys->max); |
915 | if (!tty_new) | |
2187efd3 | 916 | return -ENOMEM; |
55022530 | 917 | ttys->tty = tty_new; |
2187efd3 | 918 | |
55022530 | 919 | for (size_t i = 0; i < ttys->max; i++) { |
0e4be3cf | 920 | struct lxc_terminal_info *tty = &ttys->tty[i]; |
2187efd3 | 921 | |
386e6768 CB |
922 | tty->master = -EBADF; |
923 | tty->slave = -EBADF; | |
77a39805 CB |
924 | ret = openpty(&tty->master, &tty->slave, NULL, NULL, NULL); |
925 | if (ret < 0) { | |
885766f5 | 926 | ttys->max = i; |
0e4be3cf | 927 | lxc_delete_tty(ttys); |
55022530 | 928 | return log_error_errno(-ENOTTY, ENOTTY, "Failed to create tty %zu", i); |
2187efd3 CB |
929 | } |
930 | ||
77a39805 CB |
931 | ret = ttyname_r(tty->slave, tty->name, sizeof(tty->name)); |
932 | if (ret < 0) { | |
77a39805 CB |
933 | ttys->max = i; |
934 | lxc_delete_tty(ttys); | |
55022530 | 935 | return log_error_errno(-ENOTTY, ENOTTY, "Failed to retrieve name of tty %zu slave", i); |
77a39805 CB |
936 | } |
937 | ||
0fd73091 | 938 | DEBUG("Created tty \"%s\" with master fd %d and slave fd %d", |
2520facd | 939 | tty->name, tty->master, tty->slave); |
2187efd3 CB |
940 | |
941 | /* Prevent leaking the file descriptors to the container */ | |
615f24ff | 942 | ret = fd_cloexec(tty->master, true); |
2187efd3 | 943 | if (ret < 0) |
b78872c0 CB |
944 | SYSWARN("Failed to set FD_CLOEXEC flag on master fd %d of tty device \"%s\"", |
945 | tty->master, tty->name); | |
2187efd3 | 946 | |
615f24ff | 947 | ret = fd_cloexec(tty->slave, true); |
2187efd3 | 948 | if (ret < 0) |
b78872c0 CB |
949 | SYSWARN("Failed to set FD_CLOEXEC flag on slave fd %d of tty device \"%s\"", |
950 | tty->slave, tty->name); | |
2187efd3 | 951 | |
7581d645 | 952 | tty->busy = -1; |
2187efd3 CB |
953 | } |
954 | ||
885766f5 | 955 | INFO("Finished creating %zu tty devices", ttys->max); |
55022530 | 956 | ttys->tty = move_ptr(tty_new); |
2187efd3 CB |
957 | return 0; |
958 | } | |
959 | ||
0e4be3cf | 960 | void lxc_delete_tty(struct lxc_tty_info *ttys) |
2187efd3 | 961 | { |
386e6768 CB |
962 | if (!ttys->tty) |
963 | return; | |
964 | ||
55022530 | 965 | for (int i = 0; i < ttys->max; i++) { |
0e4be3cf | 966 | struct lxc_terminal_info *tty = &ttys->tty[i]; |
55022530 CB |
967 | close_prot_errno_disarm(tty->master); |
968 | close_prot_errno_disarm(tty->slave); | |
2187efd3 CB |
969 | } |
970 | ||
55022530 | 971 | free_disarm(ttys->tty); |
2187efd3 CB |
972 | } |
973 | ||
974 | static int lxc_send_ttys_to_parent(struct lxc_handler *handler) | |
975 | { | |
976 | int i; | |
0fd73091 | 977 | int ret = -1; |
2187efd3 | 978 | struct lxc_conf *conf = handler->conf; |
0e4be3cf | 979 | struct lxc_tty_info *ttys = &conf->ttys; |
2187efd3 | 980 | int sock = handler->data_sock[0]; |
2187efd3 | 981 | |
885766f5 | 982 | if (ttys->max == 0) |
2187efd3 CB |
983 | return 0; |
984 | ||
885766f5 | 985 | for (i = 0; i < ttys->max; i++) { |
2187efd3 | 986 | int ttyfds[2]; |
0e4be3cf | 987 | struct lxc_terminal_info *tty = &ttys->tty[i]; |
2187efd3 | 988 | |
2520facd CB |
989 | ttyfds[0] = tty->master; |
990 | ttyfds[1] = tty->slave; | |
2187efd3 CB |
991 | |
992 | ret = lxc_abstract_unix_send_fds(sock, ttyfds, 2, NULL, 0); | |
993 | if (ret < 0) | |
994 | break; | |
995 | ||
55022530 CB |
996 | TRACE("Sent tty \"%s\" with master fd %d and slave fd %d to parent", |
997 | tty->name, tty->master, tty->slave); | |
2187efd3 CB |
998 | } |
999 | ||
1000 | if (ret < 0) | |
6d1400b5 | 1001 | SYSERROR("Failed to send %zu ttys to parent", ttys->max); |
2187efd3 | 1002 | else |
885766f5 | 1003 | TRACE("Sent %zu ttys to parent", ttys->max); |
2187efd3 CB |
1004 | |
1005 | return ret; | |
1006 | } | |
1007 | ||
1008 | static int lxc_create_ttys(struct lxc_handler *handler) | |
1009 | { | |
1010 | int ret = -1; | |
1011 | struct lxc_conf *conf = handler->conf; | |
1012 | ||
663014ee | 1013 | ret = lxc_allocate_ttys(conf); |
2187efd3 CB |
1014 | if (ret < 0) { |
1015 | ERROR("Failed to allocate ttys"); | |
1016 | goto on_error; | |
1017 | } | |
1018 | ||
1019 | ret = lxc_send_ttys_to_parent(handler); | |
1020 | if (ret < 0) { | |
1021 | ERROR("Failed to send ttys to parent"); | |
1022 | goto on_error; | |
1023 | } | |
1024 | ||
1025 | if (!conf->is_execute) { | |
1026 | ret = lxc_setup_ttys(conf); | |
1027 | if (ret < 0) { | |
1028 | ERROR("Failed to setup ttys"); | |
1029 | goto on_error; | |
1030 | } | |
1031 | } | |
1032 | ||
885766f5 CB |
1033 | if (conf->ttys.tty_names) { |
1034 | ret = setenv("container_ttys", conf->ttys.tty_names, 1); | |
2187efd3 | 1035 | if (ret < 0) |
885766f5 | 1036 | SYSERROR("Failed to set \"container_ttys=%s\"", conf->ttys.tty_names); |
2187efd3 CB |
1037 | } |
1038 | ||
1039 | ret = 0; | |
1040 | ||
1041 | on_error: | |
0e4be3cf | 1042 | lxc_delete_tty(&conf->ttys); |
2187efd3 CB |
1043 | |
1044 | return ret; | |
1045 | } | |
1046 | ||
7133b912 CB |
1047 | /* Just create a path for /dev under $lxcpath/$name and in rootfs If we hit an |
1048 | * error, log it but don't fail yet. | |
91c3830e | 1049 | */ |
7133b912 | 1050 | static int mount_autodev(const char *name, const struct lxc_rootfs *rootfs, |
63012bdd | 1051 | int autodevtmpfssize, const char *lxcpath) |
91c3830e | 1052 | { |
2f443e88 | 1053 | __do_free char *path = NULL; |
91c3830e | 1054 | int ret; |
87da4ec3 | 1055 | size_t clen; |
87e0e273 | 1056 | mode_t cur_mask; |
63012bdd | 1057 | char mount_options[128]; |
91c3830e | 1058 | |
7133b912 | 1059 | INFO("Preparing \"/dev\""); |
bc6928ff | 1060 | |
14221cbb | 1061 | /* $(rootfs->mount) + "/dev/pts" + '\0' */ |
ec50007f | 1062 | clen = (rootfs->path ? strlen(rootfs->mount) : 0) + 9; |
2f443e88 | 1063 | path = must_realloc(NULL, clen); |
63012bdd CK |
1064 | sprintf(mount_options, "size=%d,mode=755", (autodevtmpfssize != 0) ? autodevtmpfssize : 500000); |
1065 | DEBUG("Using mount options: %s", mount_options); | |
bc6928ff | 1066 | |
ec50007f | 1067 | ret = snprintf(path, clen, "%s/dev", rootfs->path ? rootfs->mount : ""); |
7133b912 | 1068 | if (ret < 0 || (size_t)ret >= clen) |
91c3830e | 1069 | return -1; |
bc6928ff | 1070 | |
87e0e273 CB |
1071 | cur_mask = umask(S_IXUSR | S_IXGRP | S_IXOTH); |
1072 | ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); | |
1073 | if (ret < 0 && errno != EEXIST) { | |
1074 | SYSERROR("Failed to create \"/dev\" directory"); | |
1075 | ret = -errno; | |
1076 | goto reset_umask; | |
bc6928ff | 1077 | } |
87da4ec3 | 1078 | |
63012bdd CK |
1079 | ret = safe_mount("none", path, "tmpfs", 0, mount_options, |
1080 | rootfs->path ? rootfs->mount : NULL ); | |
7133b912 CB |
1081 | if (ret < 0) { |
1082 | SYSERROR("Failed to mount tmpfs on \"%s\"", path); | |
87e0e273 | 1083 | goto reset_umask; |
91c3830e | 1084 | } |
87e0e273 | 1085 | TRACE("Mounted tmpfs on \"%s\"", path); |
87da4ec3 | 1086 | |
ec50007f | 1087 | ret = snprintf(path, clen, "%s/dev/pts", rootfs->path ? rootfs->mount : ""); |
87e0e273 CB |
1088 | if (ret < 0 || (size_t)ret >= clen) { |
1089 | ret = -1; | |
1090 | goto reset_umask; | |
1091 | } | |
87da4ec3 | 1092 | |
7133b912 | 1093 | /* If we are running on a devtmpfs mapping, dev/pts may already exist. |
bc6928ff MW |
1094 | * If not, then create it and exit if that fails... |
1095 | */ | |
87e0e273 CB |
1096 | ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
1097 | if (ret < 0 && errno != EEXIST) { | |
1098 | SYSERROR("Failed to create directory \"%s\"", path); | |
1099 | ret = -errno; | |
1100 | goto reset_umask; | |
91c3830e SH |
1101 | } |
1102 | ||
87e0e273 CB |
1103 | ret = 0; |
1104 | ||
1105 | reset_umask: | |
1106 | (void)umask(cur_mask); | |
1107 | ||
7133b912 | 1108 | INFO("Prepared \"/dev\""); |
87e0e273 | 1109 | return ret; |
91c3830e SH |
1110 | } |
1111 | ||
5e73416f | 1112 | struct lxc_device_node { |
74a3920a | 1113 | const char *name; |
5e73416f CB |
1114 | const mode_t mode; |
1115 | const int maj; | |
1116 | const int min; | |
c6883f38 SH |
1117 | }; |
1118 | ||
5e73416f | 1119 | static const struct lxc_device_node lxc_devices[] = { |
06749971 | 1120 | { "full", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 7 }, |
5e73416f | 1121 | { "null", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 3 }, |
06749971 CB |
1122 | { "random", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 8 }, |
1123 | { "tty", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 5, 0 }, | |
5e73416f CB |
1124 | { "urandom", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 9 }, |
1125 | { "zero", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 5 }, | |
c6883f38 SH |
1126 | }; |
1127 | ||
5067e4dd CB |
1128 | |
1129 | enum { | |
1130 | LXC_DEVNODE_BIND, | |
1131 | LXC_DEVNODE_MKNOD, | |
1132 | LXC_DEVNODE_PARTIAL, | |
1133 | LXC_DEVNODE_OPEN, | |
1134 | }; | |
1135 | ||
27245ff7 | 1136 | static int lxc_fill_autodev(const struct lxc_rootfs *rootfs) |
c6883f38 | 1137 | { |
5e73416f | 1138 | int i, ret; |
6b5a54cd | 1139 | char path[PATH_MAX]; |
3a32201c | 1140 | mode_t cmask; |
5067e4dd | 1141 | int use_mknod = LXC_DEVNODE_MKNOD; |
c6883f38 | 1142 | |
6b5a54cd | 1143 | ret = snprintf(path, PATH_MAX, "%s/dev", |
3999be0a | 1144 | rootfs->path ? rootfs->mount : ""); |
6b5a54cd | 1145 | if (ret < 0 || ret >= PATH_MAX) |
c6883f38 | 1146 | return -1; |
91c3830e | 1147 | |
0bbf8572 CB |
1148 | /* ignore, just don't try to fill in */ |
1149 | if (!dir_exists(path)) | |
9cb4d183 SH |
1150 | return 0; |
1151 | ||
3999be0a CB |
1152 | INFO("Populating \"/dev\""); |
1153 | ||
3a32201c | 1154 | cmask = umask(S_IXUSR | S_IXGRP | S_IXOTH); |
5e73416f | 1155 | for (i = 0; i < sizeof(lxc_devices) / sizeof(lxc_devices[0]); i++) { |
6b5a54cd | 1156 | char hostpath[PATH_MAX]; |
5e73416f | 1157 | const struct lxc_device_node *device = &lxc_devices[i]; |
0728ebf4 | 1158 | |
6b5a54cd | 1159 | ret = snprintf(path, PATH_MAX, "%s/dev/%s", |
5e73416f | 1160 | rootfs->path ? rootfs->mount : "", device->name); |
6b5a54cd | 1161 | if (ret < 0 || ret >= PATH_MAX) |
c6883f38 | 1162 | return -1; |
0bbf8572 | 1163 | |
5067e4dd | 1164 | if (use_mknod >= LXC_DEVNODE_MKNOD) { |
5e73416f CB |
1165 | ret = mknod(path, device->mode, makedev(device->maj, device->min)); |
1166 | if (ret == 0 || (ret < 0 && errno == EEXIST)) { | |
1167 | DEBUG("Created device node \"%s\"", path); | |
5067e4dd | 1168 | } else if (ret < 0) { |
55022530 CB |
1169 | if (errno != EPERM) |
1170 | return log_error_errno(-1, errno, "Failed to create device node \"%s\"", path); | |
0bbf8572 | 1171 | |
5067e4dd | 1172 | use_mknod = LXC_DEVNODE_BIND; |
9cb4d183 | 1173 | } |
3999be0a | 1174 | |
5067e4dd CB |
1175 | /* Device nodes are fully useable. */ |
1176 | if (use_mknod == LXC_DEVNODE_OPEN) | |
1177 | continue; | |
1178 | ||
1179 | if (use_mknod == LXC_DEVNODE_MKNOD) { | |
1180 | /* See | |
1181 | * - https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=55956b59df336f6738da916dbb520b6e37df9fbd | |
1182 | * - https://lists.linuxfoundation.org/pipermail/containers/2018-June/039176.html | |
1183 | */ | |
1184 | ret = open(path, O_RDONLY | O_CLOEXEC); | |
1185 | if (ret >= 0) { | |
ae2a3d81 | 1186 | close_prot_errno_disarm(ret); |
5067e4dd CB |
1187 | /* Device nodes are fully useable. */ |
1188 | use_mknod = LXC_DEVNODE_OPEN; | |
1189 | continue; | |
1190 | } | |
1191 | ||
1192 | SYSTRACE("Failed to open \"%s\" device", path); | |
1193 | /* Device nodes are only partially useable. */ | |
1194 | use_mknod = LXC_DEVNODE_PARTIAL; | |
1195 | } | |
5e73416f CB |
1196 | } |
1197 | ||
5067e4dd CB |
1198 | if (use_mknod != LXC_DEVNODE_PARTIAL) { |
1199 | /* If we are dealing with partially functional device | |
1200 | * nodes the prio mknod() call will have created the | |
1201 | * device node so we can use it as a bind-mount target. | |
1202 | */ | |
1203 | ret = mknod(path, S_IFREG | 0000, 0); | |
55022530 CB |
1204 | if (ret < 0 && errno != EEXIST) |
1205 | return log_error_errno(-1, errno, "Failed to create file \"%s\"", path); | |
5e73416f CB |
1206 | } |
1207 | ||
1208 | /* Fallback to bind-mounting the device from the host. */ | |
6b5a54cd CB |
1209 | ret = snprintf(hostpath, PATH_MAX, "/dev/%s", device->name); |
1210 | if (ret < 0 || ret >= PATH_MAX) | |
5e73416f CB |
1211 | return -1; |
1212 | ||
1213 | ret = safe_mount(hostpath, path, 0, MS_BIND, NULL, | |
1214 | rootfs->path ? rootfs->mount : NULL); | |
55022530 CB |
1215 | if (ret < 0) |
1216 | return log_error_errno(-1, errno, "Failed to bind mount host device node \"%s\" onto \"%s\"", | |
1217 | hostpath, path); | |
1218 | DEBUG("Bind mounted host device node \"%s\" onto \"%s\"", hostpath, path); | |
c6883f38 | 1219 | } |
5e73416f | 1220 | (void)umask(cmask); |
c6883f38 | 1221 | |
3999be0a | 1222 | INFO("Populated \"/dev\""); |
c6883f38 SH |
1223 | return 0; |
1224 | } | |
1225 | ||
8ce1abc2 | 1226 | static int lxc_mount_rootfs(struct lxc_conf *conf) |
0ad19a3f | 1227 | { |
9aa76a17 | 1228 | int ret; |
10bc1861 | 1229 | struct lxc_storage *bdev; |
8ce1abc2 | 1230 | const struct lxc_rootfs *rootfs = &conf->rootfs; |
cc28d0b0 | 1231 | |
a0f379bf | 1232 | if (!rootfs->path) { |
0fd73091 | 1233 | ret = mount("", "/", NULL, MS_SLAVE | MS_REC, 0); |
55022530 CB |
1234 | if (ret < 0) |
1235 | return log_error_errno(-1, errno, "Failed to remount \"/\" MS_REC | MS_SLAVE"); | |
0fd73091 | 1236 | |
c69bd12f | 1237 | return 0; |
a0f379bf | 1238 | } |
0ad19a3f | 1239 | |
0fd73091 | 1240 | ret = access(rootfs->mount, F_OK); |
55022530 CB |
1241 | if (ret != 0) |
1242 | return log_error_errno(-1, errno, "Failed to access to \"%s\". Check it is present", | |
1243 | rootfs->mount); | |
b1789442 | 1244 | |
8a388ed4 | 1245 | bdev = storage_init(conf); |
55022530 CB |
1246 | if (!bdev) |
1247 | return log_error(-1, "Failed to mount rootfs \"%s\" onto \"%s\" with options \"%s\"", | |
1248 | rootfs->path, rootfs->mount, | |
1249 | rootfs->options ? rootfs->options : "(null)"); | |
9aa76a17 CB |
1250 | |
1251 | ret = bdev->ops->mount(bdev); | |
10bc1861 | 1252 | storage_put(bdev); |
55022530 CB |
1253 | if (ret < 0) |
1254 | return log_error(-1, "Failed to mount rootfs \"%s\" onto \"%s\" with options \"%s\"", | |
1255 | rootfs->path, rootfs->mount, | |
1256 | rootfs->options ? rootfs->options : "(null)"); | |
0ad19a3f | 1257 | |
0fd73091 | 1258 | DEBUG("Mounted rootfs \"%s\" onto \"%s\" with options \"%s\"", |
91c3e281 CB |
1259 | rootfs->path, rootfs->mount, |
1260 | rootfs->options ? rootfs->options : "(null)"); | |
9aa76a17 | 1261 | |
ac778708 DL |
1262 | return 0; |
1263 | } | |
1264 | ||
8ce1abc2 | 1265 | int lxc_chroot(const struct lxc_rootfs *rootfs) |
91e93c71 | 1266 | { |
b8d88764 | 1267 | __do_free char *nroot = NULL; |
0fd73091 | 1268 | int i, ret; |
8ce1abc2 | 1269 | char *root = rootfs->mount; |
91e93c71 | 1270 | |
74e7b662 | 1271 | nroot = realpath(root, NULL); |
55022530 CB |
1272 | if (!nroot) |
1273 | return log_error_errno(-1, errno, "Failed to resolve \"%s\"", root); | |
91e93c71 | 1274 | |
0fd73091 | 1275 | ret = chdir("/"); |
b8d88764 | 1276 | if (ret < 0) |
0fd73091 | 1277 | return -1; |
91e93c71 | 1278 | |
0fd73091 CB |
1279 | /* We could use here MS_MOVE, but in userns this mount is locked and |
1280 | * can't be moved. | |
91e93c71 | 1281 | */ |
8ce1abc2 | 1282 | ret = mount(nroot, "/", NULL, MS_REC | MS_BIND, NULL); |
55022530 CB |
1283 | if (ret < 0) |
1284 | return log_error_errno(-1, errno, "Failed to mount \"%s\" onto \"/\" as MS_REC | MS_BIND", nroot); | |
91e93c71 | 1285 | |
0fd73091 | 1286 | ret = mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL); |
55022530 CB |
1287 | if (ret < 0) |
1288 | return log_error_errno(-1, errno, "Failed to remount \"/\""); | |
91e93c71 | 1289 | |
aa899945 | 1290 | /* The following code cleans up inherited mounts which are not required |
0fd73091 | 1291 | * for CT. |
91e93c71 AV |
1292 | * |
1293 | * The mountinfo file shows not all mounts, if a few points have been | |
1294 | * unmounted between read operations from the mountinfo. So we need to | |
1295 | * read mountinfo a few times. | |
1296 | * | |
7ded5fa7 | 1297 | * This loop can be skipped if a container uses userns, because all |
91e93c71 AV |
1298 | * inherited mounts are locked and we should live with all this trash. |
1299 | */ | |
0fd73091 | 1300 | for (;;) { |
4fdd1f72 | 1301 | __do_fclose FILE *f = NULL; |
f3d38164 CB |
1302 | __do_free char *line = NULL; |
1303 | char *slider1, *slider2; | |
91e93c71 | 1304 | int progress = 0; |
f3d38164 | 1305 | size_t len = 0; |
91e93c71 | 1306 | |
4110345b | 1307 | f = fopen("./proc/self/mountinfo", "re"); |
55022530 CB |
1308 | if (!f) |
1309 | return log_error_errno(-1, errno, "Failed to open \"/proc/self/mountinfo\""); | |
0fd73091 | 1310 | |
f3d38164 CB |
1311 | while (getline(&line, &len, f) > 0) { |
1312 | for (slider1 = line, i = 0; slider1 && i < 4; i++) | |
1313 | slider1 = strchr(slider1 + 1, ' '); | |
0fd73091 | 1314 | |
f3d38164 | 1315 | if (!slider1) |
91e93c71 | 1316 | continue; |
0fd73091 | 1317 | |
f3d38164 CB |
1318 | slider2 = strchr(slider1 + 1, ' '); |
1319 | if (!slider2) | |
91e93c71 AV |
1320 | continue; |
1321 | ||
f3d38164 CB |
1322 | *slider2 = '\0'; |
1323 | *slider1 = '.'; | |
91e93c71 | 1324 | |
f3d38164 | 1325 | if (strcmp(slider1 + 1, "/") == 0) |
91e93c71 | 1326 | continue; |
0fd73091 | 1327 | |
f3d38164 | 1328 | if (strcmp(slider1 + 1, "/proc") == 0) |
91e93c71 AV |
1329 | continue; |
1330 | ||
f3d38164 | 1331 | ret = umount2(slider1, MNT_DETACH); |
0fd73091 | 1332 | if (ret == 0) |
91e93c71 AV |
1333 | progress++; |
1334 | } | |
0fd73091 | 1335 | |
91e93c71 AV |
1336 | if (!progress) |
1337 | break; | |
1338 | } | |
1339 | ||
7ded5fa7 | 1340 | /* This also can be skipped if a container uses userns. */ |
0fd73091 | 1341 | (void)umount2("./proc", MNT_DETACH); |
91e93c71 AV |
1342 | |
1343 | /* It is weird, but chdir("..") moves us in a new root */ | |
0fd73091 | 1344 | ret = chdir(".."); |
55022530 CB |
1345 | if (ret < 0) |
1346 | return log_error_errno(-1, errno, "Failed to chdir(\"..\")"); | |
91e93c71 | 1347 | |
0fd73091 | 1348 | ret = chroot("."); |
55022530 CB |
1349 | if (ret < 0) |
1350 | return log_error_errno(-1, errno, "Failed to chroot(\".\")"); | |
91e93c71 AV |
1351 | |
1352 | return 0; | |
1353 | } | |
1354 | ||
8ce1abc2 CB |
1355 | /* (The following explanation is copied verbatim from the kernel.) |
1356 | * | |
1357 | * pivot_root Semantics: | |
1358 | * Moves the root file system of the current process to the directory put_old, | |
1359 | * makes new_root as the new root file system of the current process, and sets | |
1360 | * root/cwd of all processes which had them on the current root to new_root. | |
1361 | * | |
1362 | * Restrictions: | |
1363 | * The new_root and put_old must be directories, and must not be on the | |
1364 | * same file system as the current process root. The put_old must be | |
1365 | * underneath new_root, i.e. adding a non-zero number of /.. to the string | |
1366 | * pointed to by put_old must yield the same directory as new_root. No other | |
1367 | * file system may be mounted on put_old. After all, new_root is a mountpoint. | |
1368 | * | |
1369 | * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. | |
1370 | * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives | |
1371 | * in this situation. | |
1372 | * | |
1373 | * Notes: | |
1374 | * - we don't move root/cwd if they are not at the root (reason: if something | |
1375 | * cared enough to change them, it's probably wrong to force them elsewhere) | |
1376 | * - it's okay to pick a root that isn't the root of a file system, e.g. | |
1377 | * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, | |
1378 | * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root | |
1379 | * first. | |
1380 | */ | |
1381 | static int lxc_pivot_root(const char *rootfs) | |
ac778708 | 1382 | { |
f62cf1d4 | 1383 | __do_close int oldroot = -EBADF, newroot = -EBADF; |
b0d7aac4 | 1384 | int ret; |
0fd73091 | 1385 | |
7806ebd7 | 1386 | oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC); |
55022530 CB |
1387 | if (oldroot < 0) |
1388 | return log_error_errno(-1, errno, "Failed to open old root directory"); | |
ac778708 | 1389 | |
7806ebd7 | 1390 | newroot = open(rootfs, O_DIRECTORY | O_RDONLY | O_CLOEXEC); |
55022530 CB |
1391 | if (newroot < 0) |
1392 | return log_error_errno(-1, errno, "Failed to open new root directory"); | |
0fd73091 | 1393 | |
8ce1abc2 CB |
1394 | /* change into new root fs */ |
1395 | ret = fchdir(newroot); | |
55022530 CB |
1396 | if (ret < 0) |
1397 | return log_error_errno(-1, errno, "Failed to change to new rootfs \"%s\"", rootfs); | |
39c7b795 | 1398 | |
8ce1abc2 CB |
1399 | /* pivot_root into our new root fs */ |
1400 | ret = pivot_root(".", "."); | |
55022530 CB |
1401 | if (ret < 0) |
1402 | return log_error_errno(-1, errno, "Failed to pivot_root()"); | |
39c7b795 | 1403 | |
8ce1abc2 CB |
1404 | /* At this point the old-root is mounted on top of our new-root. To |
1405 | * unmounted it we must not be chdir'd into it, so escape back to | |
1406 | * old-root. | |
1407 | */ | |
1408 | ret = fchdir(oldroot); | |
55022530 CB |
1409 | if (ret < 0) |
1410 | return log_error_errno(-1, errno, "Failed to enter old root directory"); | |
c69bd12f | 1411 | |
8ce1abc2 CB |
1412 | /* Make oldroot rslave to make sure our umounts don't propagate to the |
1413 | * host. | |
1414 | */ | |
1415 | ret = mount("", ".", "", MS_SLAVE | MS_REC, NULL); | |
55022530 CB |
1416 | if (ret < 0) |
1417 | return log_error_errno(-1, errno, "Failed to make oldroot rslave"); | |
8ce1abc2 CB |
1418 | |
1419 | ret = umount2(".", MNT_DETACH); | |
55022530 CB |
1420 | if (ret < 0) |
1421 | return log_error_errno(-1, errno, "Failed to detach old root directory"); | |
8ce1abc2 CB |
1422 | |
1423 | ret = fchdir(newroot); | |
55022530 CB |
1424 | if (ret < 0) |
1425 | return log_error_errno(-1, errno, "Failed to re-enter new root directory"); | |
8ce1abc2 | 1426 | |
8ce1abc2 CB |
1427 | TRACE("pivot_root(\"%s\") successful", rootfs); |
1428 | ||
b0d7aac4 | 1429 | return 0; |
0ad19a3f | 1430 | } |
1431 | ||
8ce1abc2 CB |
1432 | static int lxc_setup_rootfs_switch_root(const struct lxc_rootfs *rootfs) |
1433 | { | |
55022530 CB |
1434 | if (!rootfs->path) |
1435 | return log_debug(0, "Container does not have a rootfs"); | |
8ce1abc2 CB |
1436 | |
1437 | if (detect_ramfs_rootfs()) | |
1438 | return lxc_chroot(rootfs); | |
1439 | ||
1440 | return lxc_pivot_root(rootfs->mount); | |
0ad19a3f | 1441 | } |
1442 | ||
8ce1abc2 CB |
1443 | static const struct id_map *find_mapped_nsid_entry(struct lxc_conf *conf, |
1444 | unsigned id, | |
1445 | enum idtype idtype) | |
f4900711 CB |
1446 | { |
1447 | struct lxc_list *it; | |
1448 | struct id_map *map; | |
1449 | struct id_map *retmap = NULL; | |
1450 | ||
dcf0ffdf CB |
1451 | /* Shortcut for container's root mappings. */ |
1452 | if (id == 0) { | |
1453 | if (idtype == ID_TYPE_UID) | |
1454 | return conf->root_nsuid_map; | |
1455 | ||
1456 | if (idtype == ID_TYPE_GID) | |
1457 | return conf->root_nsgid_map; | |
1458 | } | |
1459 | ||
f4900711 CB |
1460 | lxc_list_for_each(it, &conf->id_map) { |
1461 | map = it->elem; | |
1462 | if (map->idtype != idtype) | |
1463 | continue; | |
1464 | ||
1465 | if (id >= map->nsid && id < map->nsid + map->range) { | |
1466 | retmap = map; | |
1467 | break; | |
1468 | } | |
1469 | } | |
1470 | ||
1471 | return retmap; | |
1472 | } | |
1473 | ||
1474 | static int lxc_setup_devpts(struct lxc_conf *conf) | |
3c26f34e | 1475 | { |
70761e5e | 1476 | int ret; |
ce155c60 | 1477 | char **opts; |
9d28c4f9 | 1478 | char devpts_mntopts[256]; |
ce155c60 CB |
1479 | char *mntopt_sets[5]; |
1480 | char default_devpts_mntopts[256] = "gid=5,newinstance,ptmxmode=0666,mode=0620"; | |
77890c6d | 1481 | |
55022530 CB |
1482 | if (conf->pty_max <= 0) |
1483 | return log_debug(0, "No new devpts instance will be mounted since no pts devices are requested"); | |
3c26f34e | 1484 | |
e528c735 CB |
1485 | ret = snprintf(devpts_mntopts, sizeof(devpts_mntopts), "%s,max=%zu", |
1486 | default_devpts_mntopts, conf->pty_max); | |
9d28c4f9 CB |
1487 | if (ret < 0 || (size_t)ret >= sizeof(devpts_mntopts)) |
1488 | return -1; | |
1489 | ||
29a7b484 | 1490 | (void)umount2("/dev/pts", MNT_DETACH); |
7e40254a | 1491 | |
70761e5e CB |
1492 | /* Create mountpoint for devpts instance. */ |
1493 | ret = mkdir("/dev/pts", 0755); | |
55022530 CB |
1494 | if (ret < 0 && errno != EEXIST) |
1495 | return log_error_errno(-1, errno, "Failed to create \"/dev/pts\" directory"); | |
3c26f34e | 1496 | |
ce155c60 CB |
1497 | /* gid=5 && max= */ |
1498 | mntopt_sets[0] = devpts_mntopts; | |
dfbd4730 | 1499 | |
ce155c60 | 1500 | /* !gid=5 && max= */ |
6333c915 | 1501 | mntopt_sets[1] = devpts_mntopts + STRLITERALLEN("gid=5") + 1; |
ce155c60 CB |
1502 | |
1503 | /* gid=5 && !max= */ | |
1504 | mntopt_sets[2] = default_devpts_mntopts; | |
1505 | ||
1506 | /* !gid=5 && !max= */ | |
6333c915 | 1507 | mntopt_sets[3] = default_devpts_mntopts + STRLITERALLEN("gid=5") + 1; |
ce155c60 CB |
1508 | |
1509 | /* end */ | |
1510 | mntopt_sets[4] = NULL; | |
1511 | ||
1512 | for (ret = -1, opts = mntopt_sets; opts && *opts; opts++) { | |
1513 | /* mount new devpts instance */ | |
1514 | ret = mount("devpts", "/dev/pts", "devpts", MS_NOSUID | MS_NOEXEC, *opts); | |
1515 | if (ret == 0) | |
1516 | break; | |
1517 | } | |
1518 | ||
55022530 CB |
1519 | if (ret < 0) |
1520 | return log_error_errno(-1, errno, "Failed to mount new devpts instance"); | |
ce155c60 | 1521 | DEBUG("Mount new devpts instance with options \"%s\"", *opts); |
70761e5e | 1522 | |
d5cb35d6 | 1523 | /* Remove any pre-existing /dev/ptmx file. */ |
b29e05d6 CB |
1524 | ret = remove("/dev/ptmx"); |
1525 | if (ret < 0) { | |
55022530 CB |
1526 | if (errno != ENOENT) |
1527 | return log_error_errno(-1, errno, "Failed to remove existing \"/dev/ptmx\" file"); | |
b29e05d6 | 1528 | } else { |
0fd73091 | 1529 | DEBUG("Removed existing \"/dev/ptmx\" file"); |
3c26f34e | 1530 | } |
1531 | ||
d5cb35d6 | 1532 | /* Create dummy /dev/ptmx file as bind mountpoint for /dev/pts/ptmx. */ |
3b7e332f | 1533 | ret = mknod("/dev/ptmx", S_IFREG | 0000, 0); |
55022530 CB |
1534 | if (ret < 0 && errno != EEXIST) |
1535 | return log_error_errno(-1, errno, "Failed to create dummy \"/dev/ptmx\" file as bind mount target"); | |
0fd73091 | 1536 | DEBUG("Created dummy \"/dev/ptmx\" file as bind mount target"); |
77890c6d | 1537 | |
d5cb35d6 | 1538 | /* Fallback option: create symlink /dev/ptmx -> /dev/pts/ptmx */ |
e87bd19c | 1539 | ret = mount("/dev/pts/ptmx", "/dev/ptmx", NULL, MS_BIND, NULL); |
55022530 CB |
1540 | if (!ret) |
1541 | return log_debug(0, "Bind mounted \"/dev/pts/ptmx\" to \"/dev/ptmx\""); | |
1542 | else | |
d5cb35d6 | 1543 | /* Fallthrough and try to create a symlink. */ |
0fd73091 | 1544 | ERROR("Failed to bind mount \"/dev/pts/ptmx\" to \"/dev/ptmx\""); |
d5cb35d6 CB |
1545 | |
1546 | /* Remove the dummy /dev/ptmx file we created above. */ | |
1547 | ret = remove("/dev/ptmx"); | |
55022530 CB |
1548 | if (ret < 0) |
1549 | return log_error_errno(-1, errno, "Failed to remove existing \"/dev/ptmx\""); | |
d5cb35d6 CB |
1550 | |
1551 | /* Fallback option: Create symlink /dev/ptmx -> /dev/pts/ptmx. */ | |
1552 | ret = symlink("/dev/pts/ptmx", "/dev/ptmx"); | |
55022530 CB |
1553 | if (ret < 0) |
1554 | return log_error_errno(-1, errno, "Failed to create symlink from \"/dev/ptmx\" to \"/dev/pts/ptmx\""); | |
0fd73091 | 1555 | DEBUG("Created symlink from \"/dev/ptmx\" to \"/dev/pts/ptmx\""); |
cd54d859 | 1556 | |
3c26f34e | 1557 | return 0; |
1558 | } | |
1559 | ||
cccc74b5 DL |
1560 | static int setup_personality(int persona) |
1561 | { | |
0fd73091 CB |
1562 | int ret; |
1563 | ||
1564 | #if HAVE_SYS_PERSONALITY_H | |
cccc74b5 DL |
1565 | if (persona == -1) |
1566 | return 0; | |
1567 | ||
0fd73091 | 1568 | ret = personality(persona); |
55022530 CB |
1569 | if (ret < 0) |
1570 | return log_error_errno(-1, errno, "Failed to set personality to \"0x%x\"", persona); | |
cccc74b5 | 1571 | |
0fd73091 CB |
1572 | INFO("Set personality to \"0x%x\"", persona); |
1573 | #endif | |
cccc74b5 DL |
1574 | |
1575 | return 0; | |
1576 | } | |
1577 | ||
3d7d929a | 1578 | static int lxc_setup_dev_console(const struct lxc_rootfs *rootfs, |
dcad02f8 | 1579 | const struct lxc_terminal *console) |
6e590161 | 1580 | { |
882671aa | 1581 | int ret; |
6b5a54cd | 1582 | char path[PATH_MAX]; |
86530b0a | 1583 | char *rootfs_path = rootfs->path ? rootfs->mount : ""; |
52e35957 | 1584 | |
8b1b1210 CB |
1585 | if (console->path && !strcmp(console->path, "none")) |
1586 | return 0; | |
1587 | ||
86530b0a | 1588 | ret = snprintf(path, sizeof(path), "%s/dev/console", rootfs_path); |
3d7d929a | 1589 | if (ret < 0 || (size_t)ret >= sizeof(path)) |
7c6ef2a2 | 1590 | return -1; |
52e35957 | 1591 | |
8b1b1210 CB |
1592 | /* When we are asked to setup a console we remove any previous |
1593 | * /dev/console bind-mounts. | |
1594 | */ | |
a7ba3c7f CB |
1595 | if (file_exists(path)) { |
1596 | ret = lxc_unstack_mountpoint(path, false); | |
55022530 CB |
1597 | if (ret < 0) |
1598 | return log_error_errno(-ret, errno, "Failed to unmount \"%s\"", path); | |
1599 | else | |
86530b0a | 1600 | DEBUG("Cleared all (%d) mounts from \"%s\"", ret, path); |
8b1b1210 CB |
1601 | } |
1602 | ||
1603 | /* For unprivileged containers autodev or automounts will already have | |
1604 | * taken care of creating /dev/console. | |
1605 | */ | |
882671aa | 1606 | ret = mknod(path, S_IFREG | 0000, 0); |
55022530 CB |
1607 | if (ret < 0 && errno != EEXIST) |
1608 | return log_error_errno(-errno, errno, "Failed to create console"); | |
52e35957 | 1609 | |
e581b9b5 | 1610 | ret = fchmod(console->slave, S_IXUSR | S_IXGRP); |
55022530 CB |
1611 | if (ret < 0) |
1612 | return log_error_errno(-errno, errno, "Failed to set mode \"0%o\" to \"%s\"", S_IXUSR | S_IXGRP, console->name); | |
13954cce | 1613 | |
86530b0a | 1614 | ret = safe_mount(console->name, path, "none", MS_BIND, 0, rootfs_path); |
55022530 CB |
1615 | if (ret < 0) |
1616 | return log_error_errno(-1, errno, "Failed to mount \"%s\" on \"%s\"", console->name, path); | |
6e590161 | 1617 | |
86530b0a | 1618 | DEBUG("Mounted pts device \"%s\" onto \"%s\"", console->name, path); |
7c6ef2a2 SH |
1619 | return 0; |
1620 | } | |
1621 | ||
3d7d929a | 1622 | static int lxc_setup_ttydir_console(const struct lxc_rootfs *rootfs, |
dcad02f8 | 1623 | const struct lxc_terminal *console, |
3d7d929a | 1624 | char *ttydir) |
7c6ef2a2 | 1625 | { |
3b7e332f | 1626 | int ret; |
6b5a54cd | 1627 | char path[PATH_MAX], lxcpath[PATH_MAX]; |
86530b0a | 1628 | char *rootfs_path = rootfs->path ? rootfs->mount : ""; |
7c6ef2a2 | 1629 | |
3dc035f1 L |
1630 | if (console->path && !strcmp(console->path, "none")) |
1631 | return 0; | |
1632 | ||
7c6ef2a2 | 1633 | /* create rootfs/dev/<ttydir> directory */ |
86530b0a | 1634 | ret = snprintf(path, sizeof(path), "%s/dev/%s", rootfs_path, ttydir); |
3d7d929a | 1635 | if (ret < 0 || (size_t)ret >= sizeof(path)) |
7c6ef2a2 | 1636 | return -1; |
3d7d929a | 1637 | |
7c6ef2a2 | 1638 | ret = mkdir(path, 0755); |
55022530 CB |
1639 | if (ret && errno != EEXIST) |
1640 | return log_error_errno(-errno, errno, "Failed to create \"%s\"", path); | |
4742cd9a | 1641 | DEBUG("Created directory for console and tty devices at \"%s\"", path); |
7c6ef2a2 | 1642 | |
86530b0a | 1643 | ret = snprintf(lxcpath, sizeof(lxcpath), "%s/dev/%s/console", rootfs_path, ttydir); |
3d7d929a CB |
1644 | if (ret < 0 || (size_t)ret >= sizeof(lxcpath)) |
1645 | return -1; | |
1646 | ||
3b7e332f | 1647 | ret = mknod(lxcpath, S_IFREG | 0000, 0); |
55022530 CB |
1648 | if (ret < 0 && errno != EEXIST) |
1649 | return log_error_errno(-errno, errno, "Failed to create \"%s\"", lxcpath); | |
7c6ef2a2 | 1650 | |
86530b0a | 1651 | ret = snprintf(path, sizeof(path), "%s/dev/console", rootfs_path); |
3dc035f1 | 1652 | if (ret < 0 || (size_t)ret >= sizeof(path)) |
7c6ef2a2 | 1653 | return -1; |
2a12fefd | 1654 | |
3dc035f1 | 1655 | if (file_exists(path)) { |
a7ba3c7f | 1656 | ret = lxc_unstack_mountpoint(path, false); |
55022530 CB |
1657 | if (ret < 0) |
1658 | return log_error_errno(-ret, errno, "Failed to unmount \"%s\"", path); | |
1659 | else | |
86530b0a | 1660 | DEBUG("Cleared all (%d) mounts from \"%s\"", ret, path); |
3dc035f1 | 1661 | } |
2a12fefd | 1662 | |
3b7e332f | 1663 | ret = mknod(path, S_IFREG | 0000, 0); |
55022530 CB |
1664 | if (ret < 0 && errno != EEXIST) |
1665 | return log_error_errno(-errno, errno, "Failed to create console"); | |
7c6ef2a2 | 1666 | |
e581b9b5 | 1667 | ret = fchmod(console->slave, S_IXUSR | S_IXGRP); |
55022530 CB |
1668 | if (ret < 0) |
1669 | return log_error_errno(-errno, errno, "Failed to set mode \"0%o\" to \"%s\"", S_IXUSR | S_IXGRP, console->name); | |
2a12fefd | 1670 | |
3dc035f1 | 1671 | /* bind mount console->name to '/dev/<ttydir>/console' */ |
86530b0a | 1672 | ret = safe_mount(console->name, lxcpath, "none", MS_BIND, 0, rootfs_path); |
55022530 CB |
1673 | if (ret < 0) |
1674 | return log_error_errno(-1, errno, "Failed to mount \"%s\" on \"%s\"", console->name, lxcpath); | |
86530b0a | 1675 | DEBUG("Mounted \"%s\" onto \"%s\"", console->name, lxcpath); |
3dc035f1 L |
1676 | |
1677 | /* bind mount '/dev/<ttydir>/console' to '/dev/console' */ | |
86530b0a | 1678 | ret = safe_mount(lxcpath, path, "none", MS_BIND, 0, rootfs_path); |
55022530 CB |
1679 | if (ret < 0) |
1680 | return log_error_errno(-1, errno, "Failed to mount \"%s\" on \"%s\"", console->name, lxcpath); | |
86530b0a | 1681 | DEBUG("Mounted \"%s\" onto \"%s\"", console->name, lxcpath); |
3dc035f1 | 1682 | |
86530b0a | 1683 | DEBUG("Console has been setup under \"%s\" and mounted to \"%s\"", lxcpath, path); |
6e590161 | 1684 | return 0; |
1685 | } | |
1686 | ||
3d7d929a | 1687 | static int lxc_setup_console(const struct lxc_rootfs *rootfs, |
dcad02f8 | 1688 | const struct lxc_terminal *console, char *ttydir) |
7c6ef2a2 | 1689 | { |
3d7d929a | 1690 | |
7c6ef2a2 | 1691 | if (!ttydir) |
3d7d929a | 1692 | return lxc_setup_dev_console(rootfs, console); |
7c6ef2a2 | 1693 | |
3d7d929a | 1694 | return lxc_setup_ttydir_console(rootfs, console, ttydir); |
7c6ef2a2 SH |
1695 | } |
1696 | ||
efed99a4 | 1697 | static void parse_mntopt(char *opt, unsigned long *flags, char **data, size_t size) |
998ac676 RT |
1698 | { |
1699 | struct mount_opt *mo; | |
1700 | ||
85c2de39 MB |
1701 | /* If '=' is contained in opt, the option must go into data. */ |
1702 | if (!strchr(opt, '=')) { | |
1703 | ||
1704 | /* If opt is found in mount_opt, set or clear flags. | |
1705 | * Otherwise append it to data. */ | |
1706 | size_t opt_len = strlen(opt); | |
1707 | for (mo = &mount_opt[0]; mo->name != NULL; mo++) { | |
1708 | size_t mo_name_len = strlen(mo->name); | |
1709 | if (opt_len == mo_name_len && strncmp(opt, mo->name, mo_name_len) == 0) { | |
1710 | if (mo->clear) | |
1711 | *flags &= ~mo->flag; | |
1712 | else | |
1713 | *flags |= mo->flag; | |
1714 | return; | |
1715 | } | |
998ac676 RT |
1716 | } |
1717 | } | |
1718 | ||
f1e05b90 DJ |
1719 | if (strlen(*data)) |
1720 | (void)strlcat(*data, ",", size); | |
efed99a4 | 1721 | |
f1e05b90 | 1722 | (void)strlcat(*data, opt, size); |
998ac676 RT |
1723 | } |
1724 | ||
0fd73091 | 1725 | int parse_mntopts(const char *mntopts, unsigned long *mntflags, char **mntdata) |
998ac676 | 1726 | { |
a71f619c CB |
1727 | __do_free char *data = NULL, *s = NULL; |
1728 | char *p; | |
efed99a4 | 1729 | size_t size; |
998ac676 | 1730 | |
911324ef | 1731 | *mntdata = NULL; |
91656ce5 | 1732 | *mntflags = 0L; |
911324ef DL |
1733 | |
1734 | if (!mntopts) | |
998ac676 RT |
1735 | return 0; |
1736 | ||
911324ef | 1737 | s = strdup(mntopts); |
0fd73091 | 1738 | if (!s) |
998ac676 | 1739 | return -1; |
998ac676 | 1740 | |
efed99a4 DJ |
1741 | size = strlen(s) + 1; |
1742 | data = malloc(size); | |
a71f619c | 1743 | if (!data) |
998ac676 | 1744 | return -1; |
998ac676 RT |
1745 | *data = 0; |
1746 | ||
8db9d26f | 1747 | lxc_iterate_parts(p, s, ",") |
efed99a4 | 1748 | parse_mntopt(p, mntflags, &data, size); |
998ac676 RT |
1749 | |
1750 | if (*data) | |
a71f619c | 1751 | *mntdata = move_ptr(data); |
998ac676 RT |
1752 | |
1753 | return 0; | |
1754 | } | |
1755 | ||
d840039e YT |
1756 | static void parse_propagationopt(char *opt, unsigned long *flags) |
1757 | { | |
1758 | struct mount_opt *mo; | |
1759 | ||
1760 | /* If opt is found in propagation_opt, set or clear flags. */ | |
d840039e | 1761 | for (mo = &propagation_opt[0]; mo->name != NULL; mo++) { |
0fd73091 CB |
1762 | if (strncmp(opt, mo->name, strlen(mo->name)) != 0) |
1763 | continue; | |
1764 | ||
1765 | if (mo->clear) | |
1766 | *flags &= ~mo->flag; | |
1767 | else | |
1768 | *flags |= mo->flag; | |
1769 | ||
1770 | return; | |
d840039e YT |
1771 | } |
1772 | } | |
1773 | ||
8ce1abc2 | 1774 | int parse_propagationopts(const char *mntopts, unsigned long *pflags) |
d840039e | 1775 | { |
dfd2e059 CB |
1776 | __do_free char *s = NULL; |
1777 | char *p; | |
d840039e YT |
1778 | |
1779 | if (!mntopts) | |
1780 | return 0; | |
1781 | ||
1782 | s = strdup(mntopts); | |
55022530 CB |
1783 | if (!s) |
1784 | return log_error_errno(-ENOMEM, errno, "Failed to allocate memory"); | |
d840039e | 1785 | |
0fd73091 | 1786 | *pflags = 0L; |
8db9d26f | 1787 | lxc_iterate_parts(p, s, ",") |
d840039e | 1788 | parse_propagationopt(p, pflags); |
0fd73091 | 1789 | |
d840039e YT |
1790 | return 0; |
1791 | } | |
1792 | ||
6fd5e769 SH |
1793 | static void null_endofword(char *word) |
1794 | { | |
1795 | while (*word && *word != ' ' && *word != '\t') | |
1796 | word++; | |
1797 | *word = '\0'; | |
1798 | } | |
1799 | ||
0fd73091 | 1800 | /* skip @nfields spaces in @src */ |
6fd5e769 SH |
1801 | static char *get_field(char *src, int nfields) |
1802 | { | |
6fd5e769 | 1803 | int i; |
0fd73091 | 1804 | char *p = src; |
6fd5e769 SH |
1805 | |
1806 | for (i = 0; i < nfields; i++) { | |
1807 | while (*p && *p != ' ' && *p != '\t') | |
1808 | p++; | |
0fd73091 | 1809 | |
6fd5e769 SH |
1810 | if (!*p) |
1811 | break; | |
0fd73091 | 1812 | |
6fd5e769 SH |
1813 | p++; |
1814 | } | |
0fd73091 | 1815 | |
6fd5e769 SH |
1816 | return p; |
1817 | } | |
1818 | ||
911324ef DL |
1819 | static int mount_entry(const char *fsname, const char *target, |
1820 | const char *fstype, unsigned long mountflags, | |
d840039e YT |
1821 | unsigned long pflags, const char *data, bool optional, |
1822 | bool dev, bool relative, const char *rootfs) | |
911324ef | 1823 | { |
0ac4b28a | 1824 | int ret; |
6b5a54cd | 1825 | char srcbuf[PATH_MAX]; |
181437fd | 1826 | const char *srcpath = fsname; |
614305f3 | 1827 | #ifdef HAVE_STATVFS |
2938f7c8 | 1828 | struct statvfs sb; |
614305f3 | 1829 | #endif |
2938f7c8 | 1830 | |
181437fd | 1831 | if (relative) { |
55022530 CB |
1832 | ret = snprintf(srcbuf, sizeof(srcbuf), "%s/%s", rootfs ? rootfs : "/", fsname ? fsname : ""); |
1833 | if (ret < 0 || ret >= sizeof(srcbuf)) | |
1834 | return log_error_errno(-1, errno, "source path is too long"); | |
181437fd YT |
1835 | srcpath = srcbuf; |
1836 | } | |
1837 | ||
1838 | ret = safe_mount(srcpath, target, fstype, mountflags & ~MS_REMOUNT, data, | |
0ac4b28a CB |
1839 | rootfs); |
1840 | if (ret < 0) { | |
55022530 CB |
1841 | if (optional) |
1842 | return log_info_errno(0, errno, "Failed to mount \"%s\" on \"%s\" (optional)", | |
1843 | srcpath ? srcpath : "(null)", target); | |
0ac4b28a | 1844 | |
55022530 CB |
1845 | return log_error_errno(-1, errno, "Failed to mount \"%s\" on \"%s\"", |
1846 | srcpath ? srcpath : "(null)", target); | |
911324ef DL |
1847 | } |
1848 | ||
1849 | if ((mountflags & MS_REMOUNT) || (mountflags & MS_BIND)) { | |
f7591785 | 1850 | unsigned long required_flags = 0; |
0ac4b28a | 1851 | |
55022530 CB |
1852 | DEBUG("Remounting \"%s\" on \"%s\" to respect bind or remount options", |
1853 | srcpath ? srcpath : "(none)", target ? target : "(none)"); | |
0ac4b28a | 1854 | |
7c5b6e7c | 1855 | if (mountflags & MS_RDONLY) |
f7591785 | 1856 | required_flags |= MS_RDONLY; |
614305f3 | 1857 | #ifdef HAVE_STATVFS |
181437fd | 1858 | if (srcpath && statvfs(srcpath, &sb) == 0) { |
2938f7c8 SH |
1859 | if (sb.f_flag & MS_NOSUID) |
1860 | required_flags |= MS_NOSUID; | |
0ac4b28a | 1861 | |
ae7a770e | 1862 | if (sb.f_flag & MS_NODEV && !dev) |
2938f7c8 | 1863 | required_flags |= MS_NODEV; |
0ac4b28a | 1864 | |
2938f7c8 SH |
1865 | if (sb.f_flag & MS_RDONLY) |
1866 | required_flags |= MS_RDONLY; | |
0ac4b28a | 1867 | |
2938f7c8 SH |
1868 | if (sb.f_flag & MS_NOEXEC) |
1869 | required_flags |= MS_NOEXEC; | |
0ac4b28a | 1870 | |
55022530 CB |
1871 | DEBUG("Flags for \"%s\" were %lu, required extra flags are %lu", |
1872 | srcpath, sb.f_flag, required_flags); | |
0ac4b28a CB |
1873 | |
1874 | /* If this was a bind mount request, and required_flags | |
2938f7c8 | 1875 | * does not have any flags which are not already in |
0ac4b28a | 1876 | * mountflags, then skip the remount. |
2938f7c8 | 1877 | */ |
15f3e22b CB |
1878 | if (!(mountflags & MS_REMOUNT) && !(required_flags & ~mountflags)) { |
1879 | DEBUG("Mountflags already were %lu, skipping remount", mountflags); | |
1880 | goto skipremount; | |
2938f7c8 | 1881 | } |
0ac4b28a | 1882 | |
2938f7c8 | 1883 | mountflags |= required_flags; |
6fd5e769 | 1884 | } |
614305f3 | 1885 | #endif |
911324ef | 1886 | |
181437fd | 1887 | ret = mount(srcpath, target, fstype, mountflags | MS_REMOUNT, data); |
0ac4b28a | 1888 | if (ret < 0) { |
55022530 CB |
1889 | if (optional) |
1890 | return log_info_errno(0, errno, "Failed to mount \"%s\" on \"%s\" (optional)", | |
1891 | srcpath ? srcpath : "(null)", | |
1892 | target); | |
1893 | ||
1894 | return log_error_errno(-1, errno, "Failed to mount \"%s\" on \"%s\"", | |
1895 | srcpath ? srcpath : "(null)", | |
1896 | target); | |
911324ef DL |
1897 | } |
1898 | } | |
1899 | ||
a3ed9b81 | 1900 | #ifdef HAVE_STATVFS |
1901 | skipremount: | |
1902 | #endif | |
d840039e YT |
1903 | if (pflags) { |
1904 | ret = mount(NULL, target, NULL, pflags, NULL); | |
1905 | if (ret < 0) { | |
55022530 CB |
1906 | if (optional) |
1907 | return log_info_errno(0, errno, "Failed to change mount propagation for \"%s\" (optional)", target); | |
1908 | else | |
1909 | return log_error_errno(-1, errno, "Failed to change mount propagation for \"%s\" (optional)", target); | |
d840039e YT |
1910 | } |
1911 | DEBUG("Changed mount propagation for \"%s\"", target); | |
1912 | } | |
1913 | ||
0103eb53 | 1914 | DEBUG("Mounted \"%s\" on \"%s\" with filesystem type \"%s\"", |
181437fd | 1915 | srcpath ? srcpath : "(null)", target, fstype); |
911324ef DL |
1916 | |
1917 | return 0; | |
1918 | } | |
1919 | ||
c5e30de4 | 1920 | /* Remove "optional", "create=dir", and "create=file" from mntopt */ |
4e4ca161 SH |
1921 | static void cull_mntent_opt(struct mntent *mntent) |
1922 | { | |
1923 | int i; | |
0fd73091 CB |
1924 | char *list[] = { |
1925 | "create=dir", | |
1926 | "create=file", | |
1927 | "optional", | |
1928 | "relative", | |
1929 | NULL | |
1930 | }; | |
c5e30de4 CB |
1931 | |
1932 | for (i = 0; list[i]; i++) { | |
1933 | char *p, *p2; | |
1934 | ||
1935 | p = strstr(mntent->mnt_opts, list[i]); | |
1936 | if (!p) | |
4e4ca161 | 1937 | continue; |
c5e30de4 | 1938 | |
4e4ca161 SH |
1939 | p2 = strchr(p, ','); |
1940 | if (!p2) { | |
1941 | /* no more mntopts, so just chop it here */ | |
1942 | *p = '\0'; | |
1943 | continue; | |
1944 | } | |
c5e30de4 CB |
1945 | |
1946 | memmove(p, p2 + 1, strlen(p2 + 1) + 1); | |
4e4ca161 SH |
1947 | } |
1948 | } | |
1949 | ||
4d5b72a1 | 1950 | static int mount_entry_create_dir_file(const struct mntent *mntent, |
749f98d9 CB |
1951 | const char *path, |
1952 | const struct lxc_rootfs *rootfs, | |
0fd73091 | 1953 | const char *lxc_name, const char *lxc_path) |
0ad19a3f | 1954 | { |
7a76eeaa | 1955 | __do_free char *p1 = NULL; |
3b7e332f | 1956 | int ret; |
7a76eeaa | 1957 | char *p2; |
911324ef | 1958 | |
12e6ab5d | 1959 | if (strncmp(mntent->mnt_type, "overlay", 7) == 0) { |
749f98d9 | 1960 | ret = ovl_mkdir(mntent, rootfs, lxc_name, lxc_path); |
12e6ab5d CB |
1961 | if (ret < 0) |
1962 | return -1; | |
1963 | } | |
6e46cc0d | 1964 | |
34cfffb3 | 1965 | if (hasmntopt(mntent, "create=dir")) { |
749f98d9 | 1966 | ret = mkdir_p(path, 0755); |
55022530 CB |
1967 | if (ret < 0 && errno != EEXIST) |
1968 | return log_error_errno(-1, errno, "Failed to create directory \"%s\"", path); | |
34cfffb3 SG |
1969 | } |
1970 | ||
0fd73091 CB |
1971 | if (!hasmntopt(mntent, "create=file")) |
1972 | return 0; | |
749f98d9 | 1973 | |
0fd73091 CB |
1974 | ret = access(path, F_OK); |
1975 | if (ret == 0) | |
1976 | return 0; | |
749f98d9 | 1977 | |
0fd73091 CB |
1978 | p1 = strdup(path); |
1979 | if (!p1) | |
1980 | return -1; | |
749f98d9 | 1981 | |
0fd73091 | 1982 | p2 = dirname(p1); |
749f98d9 | 1983 | |
0fd73091 | 1984 | ret = mkdir_p(p2, 0755); |
55022530 CB |
1985 | if (ret < 0 && errno != EEXIST) |
1986 | return log_error_errno(-1, errno, "Failed to create directory \"%s\"", path); | |
749f98d9 | 1987 | |
3b7e332f CB |
1988 | ret = mknod(path, S_IFREG | 0000, 0); |
1989 | if (ret < 0 && errno != EEXIST) | |
1990 | return -errno; | |
0fd73091 | 1991 | |
749f98d9 | 1992 | return 0; |
4d5b72a1 NC |
1993 | } |
1994 | ||
ec50007f CB |
1995 | /* rootfs, lxc_name, and lxc_path can be NULL when the container is created |
1996 | * without a rootfs. */ | |
db4aba38 | 1997 | static inline int mount_entry_on_generic(struct mntent *mntent, |
d8b712bc CB |
1998 | const char *path, |
1999 | const struct lxc_rootfs *rootfs, | |
2000 | const char *lxc_name, | |
2001 | const char *lxc_path) | |
4d5b72a1 | 2002 | { |
fd214f37 | 2003 | __do_free char *mntdata = NULL; |
d8b712bc | 2004 | int ret; |
949d0338 | 2005 | unsigned long mntflags; |
181437fd | 2006 | bool dev, optional, relative; |
949d0338 | 2007 | unsigned long pflags = 0; |
ec50007f | 2008 | char *rootfs_path = NULL; |
d8b712bc CB |
2009 | |
2010 | optional = hasmntopt(mntent, "optional") != NULL; | |
2011 | dev = hasmntopt(mntent, "dev") != NULL; | |
181437fd | 2012 | relative = hasmntopt(mntent, "relative") != NULL; |
d8b712bc | 2013 | |
ec50007f CB |
2014 | if (rootfs && rootfs->path) |
2015 | rootfs_path = rootfs->mount; | |
2016 | ||
d8b712bc CB |
2017 | ret = mount_entry_create_dir_file(mntent, path, rootfs, lxc_name, |
2018 | lxc_path); | |
2019 | if (ret < 0) { | |
2020 | if (optional) | |
2021 | return 0; | |
608e3567 | 2022 | |
d8b712bc CB |
2023 | return -1; |
2024 | } | |
4e4ca161 SH |
2025 | cull_mntent_opt(mntent); |
2026 | ||
d840039e YT |
2027 | ret = parse_propagationopts(mntent->mnt_opts, &pflags); |
2028 | if (ret < 0) | |
2029 | return -1; | |
2030 | ||
d8b712bc CB |
2031 | ret = parse_mntopts(mntent->mnt_opts, &mntflags, &mntdata); |
2032 | if (ret < 0) | |
a17b1e65 | 2033 | return -1; |
a17b1e65 | 2034 | |
6e46cc0d | 2035 | ret = mount_entry(mntent->mnt_fsname, path, mntent->mnt_type, mntflags, |
d840039e | 2036 | pflags, mntdata, optional, dev, relative, rootfs_path); |
68c152ef | 2037 | |
911324ef DL |
2038 | return ret; |
2039 | } | |
2040 | ||
db4aba38 NC |
2041 | static inline int mount_entry_on_systemfs(struct mntent *mntent) |
2042 | { | |
1433c9f9 | 2043 | int ret; |
6b5a54cd | 2044 | char path[PATH_MAX]; |
1433c9f9 CB |
2045 | |
2046 | /* For containers created without a rootfs all mounts are treated as | |
07667a6a CB |
2047 | * absolute paths starting at / on the host. |
2048 | */ | |
1433c9f9 CB |
2049 | if (mntent->mnt_dir[0] != '/') |
2050 | ret = snprintf(path, sizeof(path), "/%s", mntent->mnt_dir); | |
2051 | else | |
2052 | ret = snprintf(path, sizeof(path), "%s", mntent->mnt_dir); | |
07667a6a | 2053 | if (ret < 0 || ret >= sizeof(path)) |
1433c9f9 | 2054 | return -1; |
1433c9f9 CB |
2055 | |
2056 | return mount_entry_on_generic(mntent, path, NULL, NULL, NULL); | |
db4aba38 NC |
2057 | } |
2058 | ||
4e4ca161 | 2059 | static int mount_entry_on_absolute_rootfs(struct mntent *mntent, |
80a881b2 | 2060 | const struct lxc_rootfs *rootfs, |
0a2dddd4 CB |
2061 | const char *lxc_name, |
2062 | const char *lxc_path) | |
911324ef | 2063 | { |
bdd2b34c | 2064 | int offset; |
013bd428 | 2065 | char *aux; |
67e571de | 2066 | const char *lxcpath; |
6b5a54cd | 2067 | char path[PATH_MAX]; |
bdd2b34c | 2068 | int ret = 0; |
0ad19a3f | 2069 | |
593e8478 | 2070 | lxcpath = lxc_global_config_value("lxc.lxcpath"); |
bdd2b34c | 2071 | if (!lxcpath) |
2a59a681 | 2072 | return -1; |
2a59a681 | 2073 | |
bdd2b34c CB |
2074 | /* If rootfs->path is a blockdev path, allow container fstab to use |
2075 | * <lxcpath>/<name>/rootfs" as the target prefix. | |
2076 | */ | |
6b5a54cd CB |
2077 | ret = snprintf(path, PATH_MAX, "%s/%s/rootfs", lxcpath, lxc_name); |
2078 | if (ret < 0 || ret >= PATH_MAX) | |
80a881b2 SH |
2079 | goto skipvarlib; |
2080 | ||
2081 | aux = strstr(mntent->mnt_dir, path); | |
2082 | if (aux) { | |
2083 | offset = strlen(path); | |
2084 | goto skipabs; | |
2085 | } | |
2086 | ||
2087 | skipvarlib: | |
013bd428 | 2088 | aux = strstr(mntent->mnt_dir, rootfs->path); |
55022530 CB |
2089 | if (!aux) |
2090 | return log_warn(ret, "Ignoring mount point \"%s\"", mntent->mnt_dir); | |
80a881b2 SH |
2091 | offset = strlen(rootfs->path); |
2092 | ||
2093 | skipabs: | |
6b5a54cd CB |
2094 | ret = snprintf(path, PATH_MAX, "%s/%s", rootfs->mount, aux + offset); |
2095 | if (ret < 0 || ret >= PATH_MAX) | |
a17b1e65 | 2096 | return -1; |
a17b1e65 | 2097 | |
0a2dddd4 | 2098 | return mount_entry_on_generic(mntent, path, rootfs, lxc_name, lxc_path); |
911324ef | 2099 | } |
d330fe7b | 2100 | |
4e4ca161 | 2101 | static int mount_entry_on_relative_rootfs(struct mntent *mntent, |
0a2dddd4 CB |
2102 | const struct lxc_rootfs *rootfs, |
2103 | const char *lxc_name, | |
2104 | const char *lxc_path) | |
911324ef | 2105 | { |
911324ef | 2106 | int ret; |
6b5a54cd | 2107 | char path[PATH_MAX]; |
d330fe7b | 2108 | |
34cfffb3 | 2109 | /* relative to root mount point */ |
6e46cc0d | 2110 | ret = snprintf(path, sizeof(path), "%s/%s", rootfs->mount, mntent->mnt_dir); |
0fd73091 | 2111 | if (ret < 0 || (size_t)ret >= sizeof(path)) |
9ba8130c | 2112 | return -1; |
911324ef | 2113 | |
0a2dddd4 | 2114 | return mount_entry_on_generic(mntent, path, rootfs, lxc_name, lxc_path); |
911324ef DL |
2115 | } |
2116 | ||
06749971 CB |
2117 | static int mount_file_entries(const struct lxc_conf *conf, |
2118 | const struct lxc_rootfs *rootfs, FILE *file, | |
1ae3c19f | 2119 | const char *lxc_name, const char *lxc_path) |
911324ef | 2120 | { |
9d03d857 | 2121 | char buf[PATH_MAX]; |
0fd73091 | 2122 | struct mntent mntent; |
e76b8764 | 2123 | |
aaf901be | 2124 | while (getmntent_r(file, &mntent, buf, sizeof(buf))) { |
9d03d857 CB |
2125 | int ret; |
2126 | ||
1ae3c19f CB |
2127 | if (!rootfs->path) |
2128 | ret = mount_entry_on_systemfs(&mntent); | |
2129 | else if (mntent.mnt_dir[0] != '/') | |
2130 | ret = mount_entry_on_relative_rootfs(&mntent, rootfs, | |
2131 | lxc_name, lxc_path); | |
2132 | else | |
2133 | ret = mount_entry_on_absolute_rootfs(&mntent, rootfs, | |
9d03d857 | 2134 | lxc_name, lxc_path); |
1ae3c19f CB |
2135 | if (ret < 0) |
2136 | return -1; | |
0ad19a3f | 2137 | } |
cd54d859 | 2138 | |
55022530 CB |
2139 | if (!feof(file) || ferror(file)) |
2140 | return log_error(-1, "Failed to parse mount entries"); | |
9d03d857 CB |
2141 | |
2142 | return 0; | |
e7938e9e MN |
2143 | } |
2144 | ||
55022530 CB |
2145 | static inline void __auto_endmntent__(FILE **f) |
2146 | { | |
2147 | if (*f) | |
2148 | endmntent(*f); | |
2149 | } | |
2150 | ||
2151 | #define __do_endmntent __attribute__((__cleanup__(__auto_endmntent__))) | |
2152 | ||
06749971 CB |
2153 | static int setup_mount(const struct lxc_conf *conf, |
2154 | const struct lxc_rootfs *rootfs, const char *fstab, | |
42dff448 | 2155 | const char *lxc_name, const char *lxc_path) |
e7938e9e | 2156 | { |
55022530 | 2157 | __do_endmntent FILE *f = NULL; |
e7938e9e MN |
2158 | int ret; |
2159 | ||
2160 | if (!fstab) | |
2161 | return 0; | |
2162 | ||
55022530 CB |
2163 | f = setmntent(fstab, "re"); |
2164 | if (!f) | |
2165 | return log_error_errno(-1, errno, "Failed to open \"%s\"", fstab); | |
e7938e9e | 2166 | |
06749971 | 2167 | ret = mount_file_entries(conf, rootfs, f, lxc_name, lxc_path); |
42dff448 CB |
2168 | if (ret < 0) |
2169 | ERROR("Failed to set up mount entries"); | |
e7938e9e | 2170 | |
0ad19a3f | 2171 | return ret; |
2172 | } | |
2173 | ||
1800f924 WB |
2174 | /* |
2175 | * In order for nested containers to be able to mount /proc and /sys they need | |
2176 | * to see a "pure" proc and sysfs mount points with nothing mounted on top | |
2177 | * (like lxcfs). | |
2178 | * For this we provide proc and sysfs in /dev/.lxc/{proc,sys} while using an | |
2179 | * apparmor rule to deny access to them. This is mostly for convenience: The | |
2180 | * container's root user can mount them anyway and thus has access to the two | |
2181 | * file systems. But a non-root user in the container should not be allowed to | |
2182 | * access them as a side effect without explicitly allowing it. | |
2183 | */ | |
2184 | static const char nesting_helpers[] = | |
dc691e34 CB |
2185 | "proc dev/.lxc/proc proc create=dir,optional 0 0\n" |
2186 | "sys dev/.lxc/sys sysfs create=dir,optional 0 0\n"; | |
1800f924 WB |
2187 | |
2188 | FILE *make_anonymous_mount_file(struct lxc_list *mount, | |
2189 | bool include_nesting_helpers) | |
e7938e9e | 2190 | { |
f62cf1d4 | 2191 | __do_close int fd = -EBADF; |
4110345b | 2192 | FILE *f; |
5ef5c9a3 | 2193 | int ret; |
e7938e9e | 2194 | char *mount_entry; |
5ef5c9a3 | 2195 | struct lxc_list *iterator; |
5ef5c9a3 | 2196 | |
0fd73091 | 2197 | fd = memfd_create(".lxc_mount_file", MFD_CLOEXEC); |
5ef5c9a3 | 2198 | if (fd < 0) { |
a324e7eb CB |
2199 | char template[] = P_tmpdir "/.lxc_mount_file_XXXXXX"; |
2200 | ||
5ef5c9a3 CB |
2201 | if (errno != ENOSYS) |
2202 | return NULL; | |
a324e7eb CB |
2203 | |
2204 | fd = lxc_make_tmpfile(template, true); | |
55022530 CB |
2205 | if (fd < 0) |
2206 | return log_error_errno(NULL, errno, "Could not create temporary mount file"); | |
0fd73091 | 2207 | |
6bd04140 | 2208 | TRACE("Created temporary mount file"); |
5ef5c9a3 | 2209 | } |
e7938e9e | 2210 | |
0fd73091 CB |
2211 | lxc_list_for_each (iterator, mount) { |
2212 | size_t len; | |
2213 | ||
e7938e9e | 2214 | mount_entry = iterator->elem; |
0fd73091 | 2215 | len = strlen(mount_entry); |
5ef5c9a3 | 2216 | |
489f39be | 2217 | ret = lxc_write_nointr(fd, mount_entry, len); |
0fd73091 | 2218 | if (ret != len) |
79bcf5ee | 2219 | return NULL; |
0fd73091 | 2220 | |
489f39be | 2221 | ret = lxc_write_nointr(fd, "\n", 1); |
0fd73091 | 2222 | if (ret != 1) |
79bcf5ee | 2223 | return NULL; |
e7938e9e MN |
2224 | } |
2225 | ||
1800f924 WB |
2226 | if (include_nesting_helpers) { |
2227 | ret = lxc_write_nointr(fd, nesting_helpers, | |
6333c915 CB |
2228 | STRARRAYLEN(nesting_helpers)); |
2229 | if (ret != STRARRAYLEN(nesting_helpers)) | |
79bcf5ee | 2230 | return NULL; |
1800f924 WB |
2231 | } |
2232 | ||
0fd73091 CB |
2233 | ret = lseek(fd, 0, SEEK_SET); |
2234 | if (ret < 0) | |
79bcf5ee | 2235 | return NULL; |
0fd73091 | 2236 | |
4110345b CB |
2237 | f = fdopen(fd, "re+"); |
2238 | if (f) | |
2239 | move_fd(fd); /* Transfer ownership of fd. */ | |
2240 | return f; | |
9fc7f8c0 TA |
2241 | } |
2242 | ||
06749971 CB |
2243 | static int setup_mount_entries(const struct lxc_conf *conf, |
2244 | const struct lxc_rootfs *rootfs, | |
5ef5c9a3 CB |
2245 | struct lxc_list *mount, const char *lxc_name, |
2246 | const char *lxc_path) | |
9fc7f8c0 | 2247 | { |
c85ced65 | 2248 | __do_fclose FILE *f = NULL; |
9fc7f8c0 | 2249 | |
1800f924 | 2250 | f = make_anonymous_mount_file(mount, conf->lsm_aa_allow_nesting); |
19b5d755 | 2251 | if (!f) |
9fc7f8c0 | 2252 | return -1; |
e7938e9e | 2253 | |
c85ced65 | 2254 | return mount_file_entries(conf, rootfs, f, lxc_name, lxc_path); |
e7938e9e MN |
2255 | } |
2256 | ||
bab88e68 CS |
2257 | static int parse_cap(const char *cap) |
2258 | { | |
84760c11 | 2259 | size_t i; |
2260 | int capid = -1; | |
0fd73091 CB |
2261 | size_t end = sizeof(caps_opt) / sizeof(caps_opt[0]); |
2262 | char *ptr = NULL; | |
bab88e68 | 2263 | |
0fd73091 | 2264 | if (strcmp(cap, "none") == 0) |
7035407c DE |
2265 | return -2; |
2266 | ||
8560cd36 | 2267 | for (i = 0; i < end; i++) { |
bab88e68 CS |
2268 | if (strcmp(cap, caps_opt[i].name)) |
2269 | continue; | |
2270 | ||
2271 | capid = caps_opt[i].value; | |
2272 | break; | |
2273 | } | |
2274 | ||
2275 | if (capid < 0) { | |
0fd73091 CB |
2276 | /* Try to see if it's numeric, so the user may specify |
2277 | * capabilities that the running kernel knows about but we | |
2278 | * don't | |
2279 | */ | |
bab88e68 CS |
2280 | errno = 0; |
2281 | capid = strtol(cap, &ptr, 10); | |
2282 | if (!ptr || *ptr != '\0' || errno != 0) | |
2283 | /* not a valid number */ | |
2284 | capid = -1; | |
2285 | else if (capid > lxc_caps_last_cap()) | |
2286 | /* we have a number but it's not a valid | |
2287 | * capability */ | |
2288 | capid = -1; | |
2289 | } | |
2290 | ||
2291 | return capid; | |
2292 | } | |
2293 | ||
0769b82a CS |
2294 | int in_caplist(int cap, struct lxc_list *caps) |
2295 | { | |
0769b82a | 2296 | int capid; |
0fd73091 | 2297 | struct lxc_list *iterator; |
0769b82a | 2298 | |
0fd73091 | 2299 | lxc_list_for_each (iterator, caps) { |
0769b82a CS |
2300 | capid = parse_cap(iterator->elem); |
2301 | if (capid == cap) | |
2302 | return 1; | |
2303 | } | |
2304 | ||
2305 | return 0; | |
2306 | } | |
2307 | ||
81810dd1 DL |
2308 | static int setup_caps(struct lxc_list *caps) |
2309 | { | |
bab88e68 | 2310 | int capid; |
0fd73091 CB |
2311 | char *drop_entry; |
2312 | struct lxc_list *iterator; | |
81810dd1 | 2313 | |
0fd73091 CB |
2314 | lxc_list_for_each (iterator, caps) { |
2315 | int ret; | |
81810dd1 DL |
2316 | |
2317 | drop_entry = iterator->elem; | |
2318 | ||
bab88e68 | 2319 | capid = parse_cap(drop_entry); |
55022530 CB |
2320 | if (capid < 0) |
2321 | return log_error(-1, "unknown capability %s", drop_entry); | |
81810dd1 | 2322 | |
b81689a1 CB |
2323 | ret = prctl(PR_CAPBSET_DROP, prctl_arg(capid), prctl_arg(0), |
2324 | prctl_arg(0), prctl_arg(0)); | |
55022530 CB |
2325 | if (ret < 0) |
2326 | return log_error_errno(-1, errno, "Failed to remove %s capability", drop_entry); | |
0fd73091 | 2327 | DEBUG("Dropped %s (%d) capability", drop_entry, capid); |
81810dd1 DL |
2328 | } |
2329 | ||
0fd73091 | 2330 | DEBUG("Capabilities have been setup"); |
1fb86a7c SH |
2331 | return 0; |
2332 | } | |
2333 | ||
2334 | static int dropcaps_except(struct lxc_list *caps) | |
2335 | { | |
2f443e88 | 2336 | __do_free int *caplist = NULL; |
0fd73091 | 2337 | int i, capid, numcaps; |
1fb86a7c | 2338 | char *keep_entry; |
0fd73091 | 2339 | struct lxc_list *iterator; |
1fb86a7c | 2340 | |
0fd73091 | 2341 | numcaps = lxc_caps_last_cap() + 1; |
2caf9a97 SH |
2342 | if (numcaps <= 0 || numcaps > 200) |
2343 | return -1; | |
0fd73091 | 2344 | TRACE("Found %d capabilities", numcaps); |
2caf9a97 | 2345 | |
1a0e70ac | 2346 | /* caplist[i] is 1 if we keep capability i */ |
2f443e88 | 2347 | caplist = must_realloc(NULL, numcaps * sizeof(int)); |
1fb86a7c SH |
2348 | memset(caplist, 0, numcaps * sizeof(int)); |
2349 | ||
0fd73091 | 2350 | lxc_list_for_each (iterator, caps) { |
1fb86a7c SH |
2351 | keep_entry = iterator->elem; |
2352 | ||
bab88e68 | 2353 | capid = parse_cap(keep_entry); |
7035407c DE |
2354 | if (capid == -2) |
2355 | continue; | |
2356 | ||
55022530 CB |
2357 | if (capid < 0) |
2358 | return log_error(-1, "Unknown capability %s", keep_entry); | |
1fb86a7c | 2359 | |
0fd73091 | 2360 | DEBUG("Keep capability %s (%d)", keep_entry, capid); |
1fb86a7c SH |
2361 | caplist[capid] = 1; |
2362 | } | |
0fd73091 CB |
2363 | |
2364 | for (i = 0; i < numcaps; i++) { | |
2365 | int ret; | |
2366 | ||
1fb86a7c SH |
2367 | if (caplist[i]) |
2368 | continue; | |
0fd73091 | 2369 | |
b81689a1 CB |
2370 | ret = prctl(PR_CAPBSET_DROP, prctl_arg(i), prctl_arg(0), |
2371 | prctl_arg(0), prctl_arg(0)); | |
55022530 CB |
2372 | if (ret < 0) |
2373 | return log_error_errno(-1, errno, "Failed to remove capability %d", i); | |
1fb86a7c SH |
2374 | } |
2375 | ||
0fd73091 | 2376 | DEBUG("Capabilities have been setup"); |
81810dd1 DL |
2377 | return 0; |
2378 | } | |
2379 | ||
0fd73091 CB |
2380 | static int parse_resource(const char *res) |
2381 | { | |
2382 | int ret; | |
c6d09e15 WB |
2383 | size_t i; |
2384 | int resid = -1; | |
2385 | ||
0fd73091 | 2386 | for (i = 0; i < sizeof(limit_opt) / sizeof(limit_opt[0]); ++i) |
c6d09e15 WB |
2387 | if (strcmp(res, limit_opt[i].name) == 0) |
2388 | return limit_opt[i].value; | |
c6d09e15 | 2389 | |
0fd73091 | 2390 | /* Try to see if it's numeric, so the user may specify |
c6d09e15 | 2391 | * resources that the running kernel knows about but |
0fd73091 CB |
2392 | * we don't. |
2393 | */ | |
2394 | ret = lxc_safe_int(res, &resid); | |
2395 | if (ret < 0) | |
2396 | return -1; | |
2397 | ||
2398 | return resid; | |
c6d09e15 WB |
2399 | } |
2400 | ||
0fd73091 CB |
2401 | int setup_resource_limits(struct lxc_list *limits, pid_t pid) |
2402 | { | |
2403 | int resid; | |
c6d09e15 WB |
2404 | struct lxc_list *it; |
2405 | struct lxc_limit *lim; | |
c6d09e15 | 2406 | |
0fd73091 | 2407 | lxc_list_for_each (it, limits) { |
c6d09e15 WB |
2408 | lim = it->elem; |
2409 | ||
2410 | resid = parse_resource(lim->resource); | |
55022530 CB |
2411 | if (resid < 0) |
2412 | return log_error(-1, "Unknown resource %s", lim->resource); | |
c6d09e15 | 2413 | |
f48b5fd8 | 2414 | #if HAVE_PRLIMIT || HAVE_PRLIMIT64 |
55022530 CB |
2415 | if (prlimit(pid, resid, &lim->limit, NULL) != 0) |
2416 | return log_error_errno(-1, errno, "Failed to set limit %s", lim->resource); | |
2de12765 CB |
2417 | |
2418 | TRACE("Setup \"%s\" limit", lim->resource); | |
f48b5fd8 | 2419 | #else |
55022530 | 2420 | return log_error(-1, "Cannot set limit \"%s\" as prlimit is missing", lim->resource); |
f48b5fd8 | 2421 | #endif |
c6d09e15 | 2422 | } |
0fd73091 | 2423 | |
c6d09e15 WB |
2424 | return 0; |
2425 | } | |
2426 | ||
7edd0540 L |
2427 | int setup_sysctl_parameters(struct lxc_list *sysctls) |
2428 | { | |
e6f76452 | 2429 | __do_free char *tmp = NULL; |
7edd0540 L |
2430 | struct lxc_list *it; |
2431 | struct lxc_sysctl *elem; | |
0fd73091 | 2432 | int ret = 0; |
6b5a54cd | 2433 | char filename[PATH_MAX] = {0}; |
7edd0540 | 2434 | |
0fd73091 | 2435 | lxc_list_for_each (it, sysctls) { |
7edd0540 L |
2436 | elem = it->elem; |
2437 | tmp = lxc_string_replace(".", "/", elem->key); | |
55022530 CB |
2438 | if (!tmp) |
2439 | return log_error(-1, "Failed to replace key %s", elem->key); | |
7edd0540 L |
2440 | |
2441 | ret = snprintf(filename, sizeof(filename), "/proc/sys/%s", tmp); | |
55022530 CB |
2442 | if (ret < 0 || (size_t)ret >= sizeof(filename)) |
2443 | return log_error(-1, "Error setting up sysctl parameters path"); | |
7edd0540 | 2444 | |
0fd73091 | 2445 | ret = lxc_write_to_file(filename, elem->value, |
7cea5905 | 2446 | strlen(elem->value), false, 0666); |
55022530 CB |
2447 | if (ret < 0) |
2448 | return log_error_errno(-1, errno, "Failed to setup sysctl parameters %s to %s", | |
2449 | elem->key, elem->value); | |
7edd0540 | 2450 | } |
0fd73091 | 2451 | |
7edd0540 L |
2452 | return 0; |
2453 | } | |
2454 | ||
61d7a733 YT |
2455 | int setup_proc_filesystem(struct lxc_list *procs, pid_t pid) |
2456 | { | |
0c669152 | 2457 | __do_free char *tmp = NULL; |
61d7a733 YT |
2458 | struct lxc_list *it; |
2459 | struct lxc_proc *elem; | |
0fd73091 | 2460 | int ret = 0; |
6b5a54cd | 2461 | char filename[PATH_MAX] = {0}; |
61d7a733 | 2462 | |
0fd73091 | 2463 | lxc_list_for_each (it, procs) { |
61d7a733 YT |
2464 | elem = it->elem; |
2465 | tmp = lxc_string_replace(".", "/", elem->filename); | |
55022530 CB |
2466 | if (!tmp) |
2467 | return log_error(-1, "Failed to replace key %s", elem->filename); | |
61d7a733 YT |
2468 | |
2469 | ret = snprintf(filename, sizeof(filename), "/proc/%d/%s", pid, tmp); | |
55022530 CB |
2470 | if (ret < 0 || (size_t)ret >= sizeof(filename)) |
2471 | return log_error(-1, "Error setting up proc filesystem path"); | |
61d7a733 | 2472 | |
0fd73091 | 2473 | ret = lxc_write_to_file(filename, elem->value, |
7cea5905 | 2474 | strlen(elem->value), false, 0666); |
55022530 CB |
2475 | if (ret < 0) |
2476 | return log_error_errno(-1, errno, "Failed to setup proc filesystem %s to %s", elem->filename, elem->value); | |
61d7a733 | 2477 | } |
0fd73091 | 2478 | |
61d7a733 YT |
2479 | return 0; |
2480 | } | |
2481 | ||
ae9242c8 SH |
2482 | static char *default_rootfs_mount = LXCROOTFSMOUNT; |
2483 | ||
7b379ab3 | 2484 | struct lxc_conf *lxc_conf_init(void) |
089cd8b8 | 2485 | { |
26ddeedd | 2486 | int i; |
0fd73091 | 2487 | struct lxc_conf *new; |
7b379ab3 | 2488 | |
13277ec4 | 2489 | new = malloc(sizeof(*new)); |
0fd73091 | 2490 | if (!new) |
7b379ab3 | 2491 | return NULL; |
7b379ab3 MN |
2492 | memset(new, 0, sizeof(*new)); |
2493 | ||
4b73005c | 2494 | new->loglevel = LXC_LOG_LEVEL_NOTSET; |
cccc74b5 | 2495 | new->personality = -1; |
124fa0a8 | 2496 | new->autodev = 1; |
3a784510 | 2497 | new->console.buffer_size = 0; |
596a818d DE |
2498 | new->console.log_path = NULL; |
2499 | new->console.log_fd = -1; | |
861813e5 | 2500 | new->console.log_size = 0; |
28a4b0e5 | 2501 | new->console.path = NULL; |
63376d7d | 2502 | new->console.peer = -1; |
fb87aa6a CB |
2503 | new->console.proxy.busy = -1; |
2504 | new->console.proxy.master = -1; | |
2505 | new->console.proxy.slave = -1; | |
63376d7d DL |
2506 | new->console.master = -1; |
2507 | new->console.slave = -1; | |
2508 | new->console.name[0] = '\0'; | |
732375f5 | 2509 | memset(&new->console.ringbuf, 0, sizeof(struct lxc_ringbuf)); |
d2e30e99 | 2510 | new->maincmd_fd = -1; |
258f8051 | 2511 | new->monitor_signal_pdeath = SIGKILL; |
76a26f55 | 2512 | new->nbd_idx = -1; |
54c30e29 | 2513 | new->rootfs.mount = strdup(default_rootfs_mount); |
53f3f048 | 2514 | if (!new->rootfs.mount) { |
53f3f048 SH |
2515 | free(new); |
2516 | return NULL; | |
2517 | } | |
6e54330c | 2518 | new->rootfs.managed = true; |
858377e4 | 2519 | new->logfd = -1; |
7b379ab3 | 2520 | lxc_list_init(&new->cgroup); |
54860ed0 | 2521 | lxc_list_init(&new->cgroup2); |
4bfb655e | 2522 | lxc_list_init(&new->devices); |
7b379ab3 MN |
2523 | lxc_list_init(&new->network); |
2524 | lxc_list_init(&new->mount_list); | |
81810dd1 | 2525 | lxc_list_init(&new->caps); |
1fb86a7c | 2526 | lxc_list_init(&new->keepcaps); |
f6d3e3e4 | 2527 | lxc_list_init(&new->id_map); |
46ad64ab CB |
2528 | new->root_nsuid_map = NULL; |
2529 | new->root_nsgid_map = NULL; | |
f979ac15 | 2530 | lxc_list_init(&new->includes); |
4184c3e1 | 2531 | lxc_list_init(&new->aliens); |
7c661726 | 2532 | lxc_list_init(&new->environment); |
c6d09e15 | 2533 | lxc_list_init(&new->limits); |
7edd0540 | 2534 | lxc_list_init(&new->sysctls); |
61d7a733 | 2535 | lxc_list_init(&new->procs); |
44ae0fb6 | 2536 | new->hooks_version = 0; |
28d9e29e | 2537 | for (i = 0; i < NUM_LXC_HOOKS; i++) |
26ddeedd | 2538 | lxc_list_init(&new->hooks[i]); |
ee1e7aa0 | 2539 | lxc_list_init(&new->groups); |
d39b10eb | 2540 | lxc_list_init(&new->state_clients); |
fe4de9a6 | 2541 | new->lsm_aa_profile = NULL; |
1800f924 | 2542 | lxc_list_init(&new->lsm_aa_raw); |
fe4de9a6 | 2543 | new->lsm_se_context = NULL; |
4fef78bc | 2544 | new->lsm_se_keyring_context = NULL; |
8f818a84 | 2545 | new->keyring_disable_session = false; |
7a0bcca3 | 2546 | new->tmp_umount_proc = false; |
7a41e857 LT |
2547 | new->tmp_umount_proc = 0; |
2548 | new->shmount.path_host = NULL; | |
2549 | new->shmount.path_cont = NULL; | |
7b379ab3 | 2550 | |
72bb04e4 PT |
2551 | /* if running in a new user namespace, init and COMMAND |
2552 | * default to running as UID/GID 0 when using lxc-execute */ | |
2553 | new->init_uid = 0; | |
2554 | new->init_gid = 0; | |
43654d34 | 2555 | memset(&new->cgroup_meta, 0, sizeof(struct lxc_cgroup)); |
b074bbf1 | 2556 | memset(&new->ns_share, 0, sizeof(char *) * LXC_NS_MAX); |
c3e3c21a | 2557 | seccomp_conf_init(new); |
72bb04e4 | 2558 | |
7b379ab3 | 2559 | return new; |
089cd8b8 DL |
2560 | } |
2561 | ||
344c9d81 | 2562 | int write_id_mapping(enum idtype idtype, pid_t pid, const char *buf, |
a19b974f | 2563 | size_t buf_size) |
f6d3e3e4 | 2564 | { |
f62cf1d4 | 2565 | __do_close int fd = -EBADF; |
76bcd422 | 2566 | int ret; |
6b5a54cd | 2567 | char path[PATH_MAX]; |
f6d3e3e4 | 2568 | |
a19b974f | 2569 | if (geteuid() != 0 && idtype == ID_TYPE_GID) { |
f62cf1d4 | 2570 | __do_close int setgroups_fd = -EBADF; |
a19b974f | 2571 | |
6b5a54cd CB |
2572 | ret = snprintf(path, PATH_MAX, "/proc/%d/setgroups", pid); |
2573 | if (ret < 0 || ret >= PATH_MAX) | |
a19b974f | 2574 | return -E2BIG; |
a19b974f | 2575 | |
76bcd422 | 2576 | setgroups_fd = open(path, O_WRONLY); |
55022530 CB |
2577 | if (setgroups_fd < 0 && errno != ENOENT) |
2578 | return log_error_errno(-1, errno, "Failed to open \"%s\"", path); | |
a19b974f | 2579 | |
76bcd422 CB |
2580 | if (setgroups_fd >= 0) { |
2581 | ret = lxc_write_nointr(setgroups_fd, "deny\n", | |
2582 | STRLITERALLEN("deny\n")); | |
55022530 CB |
2583 | if (ret != STRLITERALLEN("deny\n")) |
2584 | return log_error_errno(-1, errno, "Failed to write \"deny\" to \"/proc/%d/setgroups\"", pid); | |
395b1a3e | 2585 | TRACE("Wrote \"deny\" to \"/proc/%d/setgroups\"", pid); |
a19b974f | 2586 | } |
a19b974f CB |
2587 | } |
2588 | ||
6b5a54cd | 2589 | ret = snprintf(path, PATH_MAX, "/proc/%d/%cid_map", pid, |
29053180 | 2590 | idtype == ID_TYPE_UID ? 'u' : 'g'); |
6b5a54cd | 2591 | if (ret < 0 || ret >= PATH_MAX) |
f6d3e3e4 | 2592 | return -E2BIG; |
29053180 | 2593 | |
55022530 CB |
2594 | fd = open(path, O_WRONLY | O_CLOEXEC); |
2595 | if (fd < 0) | |
2596 | return log_error_errno(-1, errno, "Failed to open \"%s\"", path); | |
29053180 | 2597 | |
29053180 | 2598 | ret = lxc_write_nointr(fd, buf, buf_size); |
55022530 CB |
2599 | if (ret != buf_size) |
2600 | return log_error_errno(-1, errno, "Failed to write %cid mapping to \"%s\"", | |
2601 | idtype == ID_TYPE_UID ? 'u' : 'g', path); | |
29053180 CB |
2602 | |
2603 | return 0; | |
f6d3e3e4 SH |
2604 | } |
2605 | ||
6e50e704 CB |
2606 | /* Check whether a binary exist and has either CAP_SETUID, CAP_SETGID or both. |
2607 | * | |
2608 | * @return 1 if functional binary was found | |
2609 | * @return 0 if binary exists but is lacking privilege | |
2610 | * @return -ENOENT if binary does not exist | |
2611 | * @return -EINVAL if cap to check is neither CAP_SETUID nor CAP_SETGID | |
6e50e704 | 2612 | */ |
df6a2945 CB |
2613 | static int idmaptool_on_path_and_privileged(const char *binary, cap_value_t cap) |
2614 | { | |
48411df2 | 2615 | __do_free char *path = NULL; |
df6a2945 CB |
2616 | int ret; |
2617 | struct stat st; | |
df6a2945 | 2618 | |
3275932b | 2619 | errno = EINVAL; |
6e50e704 | 2620 | if (cap != CAP_SETUID && cap != CAP_SETGID) |
3275932b | 2621 | return -1; |
6e50e704 | 2622 | |
3275932b | 2623 | errno = ENOENT; |
df6a2945 CB |
2624 | path = on_path(binary, NULL); |
2625 | if (!path) | |
3275932b | 2626 | return -1; |
df6a2945 CB |
2627 | |
2628 | ret = stat(path, &st); | |
3275932b CB |
2629 | if (ret < 0) |
2630 | return -1; | |
df6a2945 CB |
2631 | |
2632 | /* Check if the binary is setuid. */ | |
55022530 CB |
2633 | if (st.st_mode & S_ISUID) |
2634 | return log_debug(1, "The binary \"%s\" does have the setuid bit set", path); | |
df6a2945 | 2635 | |
0fd73091 | 2636 | #if HAVE_LIBCAP && LIBCAP_SUPPORTS_FILE_CAPABILITIES |
df6a2945 CB |
2637 | /* Check if it has the CAP_SETUID capability. */ |
2638 | if ((cap & CAP_SETUID) && | |
2639 | lxc_file_cap_is_set(path, CAP_SETUID, CAP_EFFECTIVE) && | |
55022530 CB |
2640 | lxc_file_cap_is_set(path, CAP_SETUID, CAP_PERMITTED)) |
2641 | return log_debug(1, "The binary \"%s\" has CAP_SETUID in its CAP_EFFECTIVE and CAP_PERMITTED sets", path); | |
df6a2945 CB |
2642 | |
2643 | /* Check if it has the CAP_SETGID capability. */ | |
2644 | if ((cap & CAP_SETGID) && | |
2645 | lxc_file_cap_is_set(path, CAP_SETGID, CAP_EFFECTIVE) && | |
55022530 CB |
2646 | lxc_file_cap_is_set(path, CAP_SETGID, CAP_PERMITTED)) |
2647 | return log_debug(1, "The binary \"%s\" has CAP_SETGID in its CAP_EFFECTIVE and CAP_PERMITTED sets", path); | |
0fd73091 | 2648 | #else |
69924fff CB |
2649 | /* If we cannot check for file capabilities we need to give the benefit |
2650 | * of the doubt. Otherwise we might fail even though all the necessary | |
2651 | * file capabilities are set. | |
2652 | */ | |
55022530 | 2653 | DEBUG("Cannot check for file capabilities as full capability support is missing. Manual intervention needed"); |
0fd73091 | 2654 | #endif |
df6a2945 | 2655 | |
3275932b | 2656 | return 1; |
df6a2945 CB |
2657 | } |
2658 | ||
986ef930 CB |
2659 | int lxc_map_ids_exec_wrapper(void *args) |
2660 | { | |
2661 | execl("/bin/sh", "sh", "-c", (char *)args, (char *)NULL); | |
2662 | return -1; | |
2663 | } | |
2664 | ||
f6d3e3e4 SH |
2665 | int lxc_map_ids(struct lxc_list *idmap, pid_t pid) |
2666 | { | |
0fd73091 | 2667 | int fill, left; |
986ef930 | 2668 | char u_or_g; |
4bc3b759 | 2669 | char *pos; |
6b5a54cd | 2670 | char cmd_output[PATH_MAX]; |
0fd73091 CB |
2671 | struct id_map *map; |
2672 | struct lxc_list *iterator; | |
2673 | enum idtype type; | |
986ef930 CB |
2674 | /* strlen("new@idmap") = 9 |
2675 | * + | |
2676 | * strlen(" ") = 1 | |
2677 | * + | |
d33968ad | 2678 | * INTTYPE_TO_STRLEN(uint32_t) |
986ef930 CB |
2679 | * + |
2680 | * strlen(" ") = 1 | |
2681 | * | |
2682 | * We add some additional space to make sure that we really have | |
2683 | * LXC_IDMAPLEN bytes available for our the {g,u]id mapping. | |
2684 | */ | |
0fd73091 | 2685 | int ret = 0, gidmap = 0, uidmap = 0; |
d33968ad | 2686 | char mapbuf[9 + 1 + INTTYPE_TO_STRLEN(uint32_t) + 1 + LXC_IDMAPLEN] = {0}; |
0fd73091 | 2687 | bool had_entry = false, use_shadow = false; |
c724025c JC |
2688 | int hostuid, hostgid; |
2689 | ||
2690 | hostuid = geteuid(); | |
2691 | hostgid = getegid(); | |
df6a2945 CB |
2692 | |
2693 | /* If new{g,u}idmap exists, that is, if shadow is handing out subuid | |
2694 | * ranges, then insist that root also reserve ranges in subuid. This | |
22038de5 SH |
2695 | * will protected it by preventing another user from being handed the |
2696 | * range by shadow. | |
2697 | */ | |
df6a2945 | 2698 | uidmap = idmaptool_on_path_and_privileged("newuidmap", CAP_SETUID); |
6e50e704 CB |
2699 | if (uidmap == -ENOENT) |
2700 | WARN("newuidmap binary is missing"); | |
2701 | else if (!uidmap) | |
2702 | WARN("newuidmap is lacking necessary privileges"); | |
2703 | ||
df6a2945 | 2704 | gidmap = idmaptool_on_path_and_privileged("newgidmap", CAP_SETGID); |
6e50e704 CB |
2705 | if (gidmap == -ENOENT) |
2706 | WARN("newgidmap binary is missing"); | |
2707 | else if (!gidmap) | |
2708 | WARN("newgidmap is lacking necessary privileges"); | |
2709 | ||
df6a2945 | 2710 | if (uidmap > 0 && gidmap > 0) { |
0fd73091 | 2711 | DEBUG("Functional newuidmap and newgidmap binary found"); |
4bc3b759 | 2712 | use_shadow = true; |
df6a2945 | 2713 | } else { |
99d43365 CB |
2714 | /* In case unprivileged users run application containers via |
2715 | * execute() or a start*() there are valid cases where they may | |
2716 | * only want to map their own {g,u}id. Let's not block them from | |
2717 | * doing so by requiring geteuid() == 0. | |
2718 | */ | |
2719 | DEBUG("No newuidmap and newgidmap binary found. Trying to " | |
c724025c JC |
2720 | "write directly with euid %d", hostuid); |
2721 | } | |
2722 | ||
2723 | /* Check if we really need to use newuidmap and newgidmap. | |
2724 | * If the user is only remapping his own {g,u}id, we don't need it. | |
2725 | */ | |
2726 | if (use_shadow && lxc_list_len(idmap) == 2) { | |
2727 | use_shadow = false; | |
2728 | lxc_list_for_each(iterator, idmap) { | |
2729 | map = iterator->elem; | |
2730 | if (map->idtype == ID_TYPE_UID && map->range == 1 && | |
2731 | map->nsid == hostuid && map->hostid == hostuid) | |
2732 | continue; | |
2733 | if (map->idtype == ID_TYPE_GID && map->range == 1 && | |
2734 | map->nsid == hostgid && map->hostid == hostgid) | |
2735 | continue; | |
2736 | use_shadow = true; | |
2737 | break; | |
2738 | } | |
0e6e3a41 | 2739 | } |
251d0d2a | 2740 | |
986ef930 CB |
2741 | for (type = ID_TYPE_UID, u_or_g = 'u'; type <= ID_TYPE_GID; |
2742 | type++, u_or_g = 'g') { | |
2743 | pos = mapbuf; | |
2744 | ||
0e6e3a41 | 2745 | if (use_shadow) |
986ef930 | 2746 | pos += sprintf(mapbuf, "new%cidmap %d", u_or_g, pid); |
4f7521b4 | 2747 | |
cf3ef16d | 2748 | lxc_list_for_each(iterator, idmap) { |
251d0d2a | 2749 | map = iterator->elem; |
cf3ef16d SH |
2750 | if (map->idtype != type) |
2751 | continue; | |
2752 | ||
4bc3b759 CB |
2753 | had_entry = true; |
2754 | ||
986ef930 | 2755 | left = LXC_IDMAPLEN - (pos - mapbuf); |
d1838f34 | 2756 | fill = snprintf(pos, left, "%s%lu %lu %lu%s", |
4bc3b759 CB |
2757 | use_shadow ? " " : "", map->nsid, |
2758 | map->hostid, map->range, | |
0e6e3a41 | 2759 | use_shadow ? "" : "\n"); |
55022530 CB |
2760 | /* |
2761 | * The kernel only takes <= 4k for writes to | |
2762 | * /proc/<pid>/{g,u}id_map | |
2763 | */ | |
2764 | if (fill <= 0 || fill >= left) | |
2765 | return log_error_errno(-1, errno, "Too many %cid mappings defined", u_or_g); | |
4bc3b759 | 2766 | |
cf3ef16d | 2767 | pos += fill; |
251d0d2a | 2768 | } |
cf3ef16d | 2769 | if (!had_entry) |
4f7521b4 | 2770 | continue; |
cf3ef16d | 2771 | |
d85813cd | 2772 | /* Try to catch the output of new{g,u}idmap to make debugging |
986ef930 CB |
2773 | * easier. |
2774 | */ | |
2775 | if (use_shadow) { | |
2776 | ret = run_command(cmd_output, sizeof(cmd_output), | |
2777 | lxc_map_ids_exec_wrapper, | |
2778 | (void *)mapbuf); | |
55022530 CB |
2779 | if (ret < 0) |
2780 | return log_error(-1, "new%cidmap failed to write mapping \"%s\": %s", u_or_g, cmd_output, mapbuf); | |
54fbbeb5 | 2781 | TRACE("new%cidmap wrote mapping \"%s\"", u_or_g, mapbuf); |
d1838f34 | 2782 | } else { |
986ef930 | 2783 | ret = write_id_mapping(type, pid, mapbuf, pos - mapbuf); |
55022530 CB |
2784 | if (ret < 0) |
2785 | return log_error(-1, "Failed to write mapping: %s", mapbuf); | |
54fbbeb5 | 2786 | TRACE("Wrote mapping \"%s\"", mapbuf); |
d1838f34 | 2787 | } |
986ef930 CB |
2788 | |
2789 | memset(mapbuf, 0, sizeof(mapbuf)); | |
f6d3e3e4 | 2790 | } |
251d0d2a | 2791 | |
986ef930 | 2792 | return 0; |
f6d3e3e4 SH |
2793 | } |
2794 | ||
0fd73091 | 2795 | /* Return the host uid/gid to which the container root is mapped in val. |
0b3a6504 | 2796 | * Return true if id was found, false otherwise. |
cf3ef16d | 2797 | */ |
facdf925 CB |
2798 | static bool get_mapped_rootid(const struct lxc_conf *conf, enum idtype idtype, |
2799 | unsigned long *val) | |
cf3ef16d | 2800 | { |
4160c3a0 | 2801 | unsigned nsid; |
0fd73091 CB |
2802 | struct id_map *map; |
2803 | struct lxc_list *it; | |
4160c3a0 CB |
2804 | |
2805 | if (idtype == ID_TYPE_UID) | |
2806 | nsid = (conf->root_nsuid_map != NULL) ? 0 : conf->init_uid; | |
2807 | else | |
2808 | nsid = (conf->root_nsgid_map != NULL) ? 0 : conf->init_gid; | |
cf3ef16d | 2809 | |
0fd73091 | 2810 | lxc_list_for_each (it, &conf->id_map) { |
cf3ef16d | 2811 | map = it->elem; |
7b50c609 | 2812 | if (map->idtype != idtype) |
cf3ef16d | 2813 | continue; |
4160c3a0 | 2814 | if (map->nsid != nsid) |
cf3ef16d | 2815 | continue; |
2a9a80cb SH |
2816 | *val = map->hostid; |
2817 | return true; | |
cf3ef16d | 2818 | } |
4160c3a0 | 2819 | |
2a9a80cb | 2820 | return false; |
cf3ef16d SH |
2821 | } |
2822 | ||
facdf925 | 2823 | int mapped_hostid(unsigned id, const struct lxc_conf *conf, enum idtype idtype) |
cf3ef16d | 2824 | { |
cf3ef16d | 2825 | struct id_map *map; |
0fd73091 CB |
2826 | struct lxc_list *it; |
2827 | ||
2828 | lxc_list_for_each (it, &conf->id_map) { | |
cf3ef16d | 2829 | map = it->elem; |
2133f58c | 2830 | if (map->idtype != idtype) |
cf3ef16d | 2831 | continue; |
0fd73091 | 2832 | |
cf3ef16d | 2833 | if (id >= map->hostid && id < map->hostid + map->range) |
57d116ab | 2834 | return (id - map->hostid) + map->nsid; |
cf3ef16d | 2835 | } |
0fd73091 | 2836 | |
57d116ab | 2837 | return -1; |
cf3ef16d SH |
2838 | } |
2839 | ||
339efad9 | 2840 | int find_unmapped_nsid(struct lxc_conf *conf, enum idtype idtype) |
cf3ef16d | 2841 | { |
cf3ef16d | 2842 | struct id_map *map; |
0fd73091 | 2843 | struct lxc_list *it; |
2133f58c | 2844 | unsigned int freeid = 0; |
0fd73091 | 2845 | |
cf3ef16d | 2846 | again: |
0fd73091 | 2847 | lxc_list_for_each (it, &conf->id_map) { |
cf3ef16d | 2848 | map = it->elem; |
2133f58c | 2849 | if (map->idtype != idtype) |
cf3ef16d | 2850 | continue; |
0fd73091 | 2851 | |
cf3ef16d SH |
2852 | if (freeid >= map->nsid && freeid < map->nsid + map->range) { |
2853 | freeid = map->nsid + map->range; | |
2854 | goto again; | |
2855 | } | |
2856 | } | |
0fd73091 | 2857 | |
cf3ef16d SH |
2858 | return freeid; |
2859 | } | |
2860 | ||
f4f52cb5 CB |
2861 | int chown_mapped_root_exec_wrapper(void *args) |
2862 | { | |
2863 | execvp("lxc-usernsexec", args); | |
2864 | return -1; | |
2865 | } | |
2866 | ||
0fd73091 | 2867 | /* chown_mapped_root: for an unprivileged user with uid/gid X to |
7b50c609 TS |
2868 | * chown a dir to subuid/subgid Y, he needs to run chown as root |
2869 | * in a userns where nsid 0 is mapped to hostuid/hostgid Y, and | |
2870 | * nsid Y is mapped to hostuid/hostgid X. That way, the container | |
2871 | * root is privileged with respect to hostuid/hostgid X, allowing | |
2872 | * him to do the chown. | |
f6d3e3e4 | 2873 | */ |
facdf925 | 2874 | int chown_mapped_root(const char *path, const struct lxc_conf *conf) |
f6d3e3e4 | 2875 | { |
f4f52cb5 | 2876 | uid_t rootuid, rootgid; |
2a9a80cb | 2877 | unsigned long val; |
f4f52cb5 CB |
2878 | int hostuid, hostgid, ret; |
2879 | struct stat sb; | |
2880 | char map1[100], map2[100], map3[100], map4[100], map5[100]; | |
2881 | char ugid[100]; | |
41dc7155 | 2882 | const char *args1[] = {"lxc-usernsexec", |
f4f52cb5 CB |
2883 | "-m", map1, |
2884 | "-m", map2, | |
2885 | "-m", map3, | |
2886 | "-m", map5, | |
2887 | "--", "chown", ugid, path, | |
2888 | NULL}; | |
41dc7155 | 2889 | const char *args2[] = {"lxc-usernsexec", |
f4f52cb5 CB |
2890 | "-m", map1, |
2891 | "-m", map2, | |
2892 | "-m", map3, | |
2893 | "-m", map4, | |
2894 | "-m", map5, | |
2895 | "--", "chown", ugid, path, | |
2896 | NULL}; | |
6b5a54cd | 2897 | char cmd_output[PATH_MAX]; |
f4f52cb5 CB |
2898 | |
2899 | hostuid = geteuid(); | |
2900 | hostgid = getegid(); | |
f6d3e3e4 | 2901 | |
55022530 CB |
2902 | if (!get_mapped_rootid(conf, ID_TYPE_UID, &val)) |
2903 | return log_error(-1, "No uid mapping for container root"); | |
f4f52cb5 | 2904 | rootuid = (uid_t)val; |
0fd73091 | 2905 | |
55022530 CB |
2906 | if (!get_mapped_rootid(conf, ID_TYPE_GID, &val)) |
2907 | return log_error(-1, "No gid mapping for container root"); | |
f4f52cb5 | 2908 | rootgid = (gid_t)val; |
2a9a80cb | 2909 | |
f4f52cb5 | 2910 | if (hostuid == 0) { |
55022530 CB |
2911 | if (chown(path, rootuid, rootgid) < 0) |
2912 | return log_error(-1, "Error chowning %s", path); | |
0fd73091 | 2913 | |
c4d10a05 SH |
2914 | return 0; |
2915 | } | |
f3d7e4ca | 2916 | |
55022530 CB |
2917 | /* nothing to do */ |
2918 | if (rootuid == hostuid) | |
2919 | return log_info(0, "Container root is our uid; no need to chown"); | |
f3d7e4ca | 2920 | |
bbdbf8f0 | 2921 | /* save the current gid of "path" */ |
55022530 CB |
2922 | if (stat(path, &sb) < 0) |
2923 | return log_error(-1, "Error stat %s", path); | |
7b50c609 | 2924 | |
bbdbf8f0 CB |
2925 | /* Update the path argument in case this was overlayfs. */ |
2926 | args1[sizeof(args1) / sizeof(args1[0]) - 2] = path; | |
2927 | args2[sizeof(args2) / sizeof(args2[0]) - 2] = path; | |
2928 | ||
f4f52cb5 CB |
2929 | /* |
2930 | * A file has to be group-owned by a gid mapped into the | |
2931 | * container, or the container won't be privileged over it. | |
2932 | */ | |
2933 | DEBUG("trying to chown \"%s\" to %d", path, hostgid); | |
2934 | if (sb.st_uid == hostuid && | |
2935 | mapped_hostid(sb.st_gid, conf, ID_TYPE_GID) < 0 && | |
55022530 CB |
2936 | chown(path, -1, hostgid) < 0) |
2937 | return log_error(-1, "Failed chgrping %s", path); | |
f6d3e3e4 | 2938 | |
1a0e70ac | 2939 | /* "u:0:rootuid:1" */ |
f4f52cb5 | 2940 | ret = snprintf(map1, 100, "u:0:%d:1", rootuid); |
55022530 CB |
2941 | if (ret < 0 || ret >= 100) |
2942 | return log_error(-1, "Error uid printing map string"); | |
7b50c609 | 2943 | |
1a0e70ac | 2944 | /* "u:hostuid:hostuid:1" */ |
f4f52cb5 | 2945 | ret = snprintf(map2, 100, "u:%d:%d:1", hostuid, hostuid); |
55022530 CB |
2946 | if (ret < 0 || ret >= 100) |
2947 | return log_error(-1, "Error uid printing map string"); | |
c4d10a05 | 2948 | |
1a0e70ac | 2949 | /* "g:0:rootgid:1" */ |
f4f52cb5 | 2950 | ret = snprintf(map3, 100, "g:0:%d:1", rootgid); |
55022530 CB |
2951 | if (ret < 0 || ret >= 100) |
2952 | return log_error(-1, "Error gid printing map string"); | |
98e5ba51 | 2953 | |
1a0e70ac | 2954 | /* "g:pathgid:rootgid+pathgid:1" */ |
f4f52cb5 CB |
2955 | ret = snprintf(map4, 100, "g:%d:%d:1", (gid_t)sb.st_gid, |
2956 | rootgid + (gid_t)sb.st_gid); | |
55022530 CB |
2957 | if (ret < 0 || ret >= 100) |
2958 | return log_error(-1, "Error gid printing map string"); | |
c4d10a05 | 2959 | |
1a0e70ac | 2960 | /* "g:hostgid:hostgid:1" */ |
f4f52cb5 | 2961 | ret = snprintf(map5, 100, "g:%d:%d:1", hostgid, hostgid); |
55022530 CB |
2962 | if (ret < 0 || ret >= 100) |
2963 | return log_error(-1, "Error gid printing map string"); | |
7b50c609 | 2964 | |
1a0e70ac | 2965 | /* "0:pathgid" (chown) */ |
f4f52cb5 | 2966 | ret = snprintf(ugid, 100, "0:%d", (gid_t)sb.st_gid); |
55022530 CB |
2967 | if (ret < 0 || ret >= 100) |
2968 | return log_error(-1, "Error owner printing format string for chown"); | |
7b50c609 | 2969 | |
f4f52cb5 CB |
2970 | if (hostgid == sb.st_gid) |
2971 | ret = run_command(cmd_output, sizeof(cmd_output), | |
2972 | chown_mapped_root_exec_wrapper, | |
2973 | (void *)args1); | |
2974 | else | |
2975 | ret = run_command(cmd_output, sizeof(cmd_output), | |
2976 | chown_mapped_root_exec_wrapper, | |
2977 | (void *)args2); | |
2978 | if (ret < 0) | |
2979 | ERROR("lxc-usernsexec failed: %s", cmd_output); | |
7b50c609 | 2980 | |
f4f52cb5 | 2981 | return ret; |
f6d3e3e4 SH |
2982 | } |
2983 | ||
943144d9 CB |
2984 | /* NOTE: Must not be called from inside the container namespace! */ |
2985 | int lxc_create_tmp_proc_mount(struct lxc_conf *conf) | |
5112cd70 SH |
2986 | { |
2987 | int mounted; | |
2988 | ||
943144d9 | 2989 | mounted = lxc_mount_proc_if_needed(conf->rootfs.path ? conf->rootfs.mount : ""); |
5112cd70 | 2990 | if (mounted == -1) { |
0fd73091 | 2991 | SYSERROR("Failed to mount proc in the container"); |
01958b1f | 2992 | /* continue only if there is no rootfs */ |
943144d9 | 2993 | if (conf->rootfs.path) |
01958b1f | 2994 | return -1; |
5112cd70 | 2995 | } else if (mounted == 1) { |
7a0bcca3 | 2996 | conf->tmp_umount_proc = true; |
5112cd70 | 2997 | } |
943144d9 | 2998 | |
5112cd70 SH |
2999 | return 0; |
3000 | } | |
3001 | ||
3002 | void tmp_proc_unmount(struct lxc_conf *lxc_conf) | |
3003 | { | |
7a0bcca3 | 3004 | if (!lxc_conf->tmp_umount_proc) |
0fd73091 CB |
3005 | return; |
3006 | ||
7a0bcca3 CB |
3007 | (void)umount2("/proc", MNT_DETACH); |
3008 | lxc_conf->tmp_umount_proc = false; | |
5112cd70 SH |
3009 | } |
3010 | ||
0fd73091 | 3011 | /* Walk /proc/mounts and change any shared entries to slave. */ |
6a0c909a | 3012 | void remount_all_slave(void) |
e995d7a2 | 3013 | { |
7969675f | 3014 | __do_free char *line = NULL; |
003be47b | 3015 | __do_fclose FILE *f = NULL; |
f62cf1d4 | 3016 | __do_close int memfd = -EBADF, mntinfo_fd = -EBADF; |
003be47b | 3017 | int ret; |
6a49f05e | 3018 | ssize_t copied; |
e995d7a2 SH |
3019 | size_t len = 0; |
3020 | ||
6a49f05e | 3021 | mntinfo_fd = open("/proc/self/mountinfo", O_RDONLY | O_CLOEXEC); |
fea3b91d DJ |
3022 | if (mntinfo_fd < 0) { |
3023 | SYSERROR("Failed to open \"/proc/self/mountinfo\""); | |
6a49f05e | 3024 | return; |
fea3b91d | 3025 | } |
6a49f05e CB |
3026 | |
3027 | memfd = memfd_create(".lxc_mountinfo", MFD_CLOEXEC); | |
3028 | if (memfd < 0) { | |
3029 | char template[] = P_tmpdir "/.lxc_mountinfo_XXXXXX"; | |
3030 | ||
3031 | if (errno != ENOSYS) { | |
fea3b91d | 3032 | SYSERROR("Failed to create temporary in-memory file"); |
6a49f05e CB |
3033 | return; |
3034 | } | |
3035 | ||
3036 | memfd = lxc_make_tmpfile(template, true); | |
fea3b91d | 3037 | if (memfd < 0) { |
fea3b91d DJ |
3038 | WARN("Failed to create temporary file"); |
3039 | return; | |
3040 | } | |
6a49f05e CB |
3041 | } |
3042 | ||
6a49f05e | 3043 | again: |
7c4d9466 | 3044 | copied = lxc_sendfile_nointr(memfd, mntinfo_fd, NULL, LXC_SENDFILE_MAX); |
6a49f05e CB |
3045 | if (copied < 0) { |
3046 | if (errno == EINTR) | |
3047 | goto again; | |
3048 | ||
fea3b91d | 3049 | SYSERROR("Failed to copy \"/proc/self/mountinfo\""); |
6a49f05e CB |
3050 | return; |
3051 | } | |
6a49f05e | 3052 | |
6a49f05e CB |
3053 | ret = lseek(memfd, 0, SEEK_SET); |
3054 | if (ret < 0) { | |
fea3b91d | 3055 | SYSERROR("Failed to reset file descriptor offset"); |
6a49f05e CB |
3056 | return; |
3057 | } | |
3058 | ||
4110345b | 3059 | f = fdopen(memfd, "re"); |
e995d7a2 | 3060 | if (!f) { |
003be47b | 3061 | SYSERROR("Failed to open copy of \"/proc/self/mountinfo\" to mark all shared. Continuing"); |
e995d7a2 SH |
3062 | return; |
3063 | } | |
3064 | ||
003be47b CB |
3065 | /* |
3066 | * After a successful fdopen() memfd will be closed when calling | |
3067 | * fclose(f). Calling close(memfd) afterwards is undefined. | |
3068 | */ | |
3069 | move_fd(memfd); | |
3070 | ||
e995d7a2 | 3071 | while (getline(&line, &len, f) != -1) { |
0fd73091 CB |
3072 | char *opts, *target; |
3073 | ||
e995d7a2 SH |
3074 | target = get_field(line, 4); |
3075 | if (!target) | |
3076 | continue; | |
0fd73091 | 3077 | |
e995d7a2 SH |
3078 | opts = get_field(target, 2); |
3079 | if (!opts) | |
3080 | continue; | |
0fd73091 | 3081 | |
e995d7a2 SH |
3082 | null_endofword(opts); |
3083 | if (!strstr(opts, "shared")) | |
3084 | continue; | |
0fd73091 | 3085 | |
e995d7a2 | 3086 | null_endofword(target); |
0fd73091 CB |
3087 | ret = mount(NULL, target, NULL, MS_SLAVE, NULL); |
3088 | if (ret < 0) { | |
3089 | SYSERROR("Failed to make \"%s\" MS_SLAVE", target); | |
e995d7a2 | 3090 | ERROR("Continuing..."); |
6a49f05e | 3091 | continue; |
e995d7a2 | 3092 | } |
6a49f05e | 3093 | TRACE("Remounted \"%s\" as MS_SLAVE", target); |
e995d7a2 | 3094 | } |
6a49f05e | 3095 | TRACE("Remounted all mount table entries as MS_SLAVE"); |
e995d7a2 SH |
3096 | } |
3097 | ||
794248d0 | 3098 | static int lxc_execute_bind_init(struct lxc_handler *handler) |
2322903b SH |
3099 | { |
3100 | int ret; | |
794248d0 CB |
3101 | char *p; |
3102 | char path[PATH_MAX], destpath[PATH_MAX]; | |
3103 | struct lxc_conf *conf = handler->conf; | |
9d9c111c SH |
3104 | |
3105 | /* If init exists in the container, don't bind mount a static one */ | |
3106 | p = choose_init(conf->rootfs.mount); | |
3107 | if (p) { | |
22f835ba | 3108 | __do_free char *old = p; |
41089848 TA |
3109 | |
3110 | p = strdup(old + strlen(conf->rootfs.mount)); | |
41089848 TA |
3111 | if (!p) |
3112 | return -ENOMEM; | |
3113 | ||
3114 | INFO("Found existing init at \"%s\"", p); | |
3115 | goto out; | |
9d9c111c | 3116 | } |
2322903b SH |
3117 | |
3118 | ret = snprintf(path, PATH_MAX, SBINDIR "/init.lxc.static"); | |
0fd73091 | 3119 | if (ret < 0 || ret >= PATH_MAX) |
8353b4c9 | 3120 | return -1; |
2322903b | 3121 | |
55022530 CB |
3122 | if (!file_exists(path)) |
3123 | return log_error_errno(-1, errno, "The file \"%s\" does not exist on host", path); | |
2322903b | 3124 | |
794248d0 | 3125 | ret = snprintf(destpath, PATH_MAX, "%s" P_tmpdir "%s", conf->rootfs.mount, "/.lxc-init"); |
0fd73091 | 3126 | if (ret < 0 || ret >= PATH_MAX) |
8353b4c9 | 3127 | return -1; |
2322903b SH |
3128 | |
3129 | if (!file_exists(destpath)) { | |
794248d0 | 3130 | ret = mknod(destpath, S_IFREG | 0000, 0); |
55022530 CB |
3131 | if (ret < 0 && errno != EEXIST) |
3132 | return log_error_errno(-1, errno, "Failed to create dummy \"%s\" file as bind mount target", destpath); | |
2322903b SH |
3133 | } |
3134 | ||
592fd47a | 3135 | ret = safe_mount(path, destpath, "none", MS_BIND, NULL, conf->rootfs.mount); |
55022530 CB |
3136 | if (ret < 0) |
3137 | return log_error_errno(-1, errno, "Failed to bind mount lxc.init.static into container"); | |
8353b4c9 | 3138 | |
794248d0 CB |
3139 | p = strdup(destpath + strlen(conf->rootfs.mount)); |
3140 | if (!p) | |
3141 | return -ENOMEM; | |
794248d0 | 3142 | |
8353b4c9 | 3143 | INFO("Bind mounted lxc.init.static into container at \"%s\"", path); |
41089848 | 3144 | out: |
4b5b3a2a | 3145 | ((struct execute_args *)handler->data)->init_fd = -1; |
41089848 | 3146 | ((struct execute_args *)handler->data)->init_path = p; |
8353b4c9 | 3147 | return 0; |
2322903b SH |
3148 | } |
3149 | ||
0fd73091 CB |
3150 | /* This does the work of remounting / if it is shared, calling the container |
3151 | * pre-mount hooks, and mounting the rootfs. | |
35120d9c | 3152 | */ |
8ce1abc2 CB |
3153 | int lxc_setup_rootfs_prepare_root(struct lxc_conf *conf, const char *name, |
3154 | const char *lxcpath) | |
0ad19a3f | 3155 | { |
0fd73091 CB |
3156 | int ret; |
3157 | ||
35120d9c | 3158 | if (conf->rootfs_setup) { |
35120d9c | 3159 | const char *path = conf->rootfs.mount; |
0fd73091 CB |
3160 | |
3161 | /* The rootfs was set up in another namespace. bind-mount it to | |
3162 | * give us a mount in our own ns so we can pivot_root to it | |
3163 | */ | |
3164 | ret = mount(path, path, "rootfs", MS_BIND, NULL); | |
55022530 CB |
3165 | if (ret < 0) |
3166 | return log_error(-1, "Failed to bind mount container / onto itself"); | |
0fd73091 | 3167 | |
55022530 | 3168 | return log_trace(0, "Bind mounted container / onto itself"); |
35120d9c | 3169 | } |
d4ef7c50 | 3170 | |
e995d7a2 SH |
3171 | remount_all_slave(); |
3172 | ||
0fd73091 | 3173 | ret = run_lxc_hooks(name, "pre-mount", conf, NULL); |
55022530 CB |
3174 | if (ret < 0) |
3175 | return log_error(-1, "Failed to run pre-mount hooks"); | |
35120d9c | 3176 | |
8ce1abc2 | 3177 | ret = lxc_mount_rootfs(conf); |
55022530 CB |
3178 | if (ret < 0) |
3179 | return log_error(-1, "Failed to setup rootfs for"); | |
35120d9c SH |
3180 | |
3181 | conf->rootfs_setup = true; | |
3182 | return 0; | |
3183 | } | |
3184 | ||
1c1c7051 SH |
3185 | static bool verify_start_hooks(struct lxc_conf *conf) |
3186 | { | |
6b5a54cd | 3187 | char path[PATH_MAX]; |
0fd73091 CB |
3188 | struct lxc_list *it; |
3189 | ||
3190 | lxc_list_for_each (it, &conf->hooks[LXCHOOK_START]) { | |
1c1c7051 | 3191 | int ret; |
0fd73091 | 3192 | char *hookname = it->elem; |
1c1c7051 | 3193 | |
6b5a54cd | 3194 | ret = snprintf(path, PATH_MAX, "%s%s", |
0fd73091 CB |
3195 | conf->rootfs.path ? conf->rootfs.mount : "", |
3196 | hookname); | |
6b5a54cd | 3197 | if (ret < 0 || ret >= PATH_MAX) |
1c1c7051 | 3198 | return false; |
0fd73091 | 3199 | |
75193660 | 3200 | ret = access(path, X_OK); |
55022530 CB |
3201 | if (ret < 0) |
3202 | return log_error_errno(false, errno, "Start hook \"%s\" not found in container", hookname); | |
0fd73091 | 3203 | |
6a0c909a | 3204 | return true; |
1c1c7051 SH |
3205 | } |
3206 | ||
3207 | return true; | |
3208 | } | |
3209 | ||
4b5b3a2a TA |
3210 | static bool execveat_supported(void) |
3211 | { | |
13be2733 | 3212 | lxc_raw_execveat(-1, "", NULL, NULL, AT_EMPTY_PATH); |
4b5b3a2a TA |
3213 | if (errno == ENOSYS) |
3214 | return false; | |
3215 | ||
3216 | return true; | |
4b5b3a2a TA |
3217 | } |
3218 | ||
20502652 CB |
3219 | static int lxc_setup_boot_id(void) |
3220 | { | |
3221 | int ret; | |
3222 | const char *boot_id_path = "/proc/sys/kernel/random/boot_id"; | |
3223 | const char *mock_boot_id_path = "/dev/.lxc-boot-id"; | |
3224 | lxc_id128_t n; | |
3225 | ||
3226 | if (access(boot_id_path, F_OK)) | |
3227 | return 0; | |
3228 | ||
3229 | memset(&n, 0, sizeof(n)); | |
3230 | if (lxc_id128_randomize(&n)) { | |
3231 | SYSERROR("Failed to generate random data for uuid"); | |
3232 | return -1; | |
3233 | } | |
3234 | ||
3235 | ret = lxc_id128_write(mock_boot_id_path, n); | |
3236 | if (ret < 0) { | |
3237 | SYSERROR("Failed to write uuid to %s", mock_boot_id_path); | |
3238 | return -1; | |
3239 | } | |
3240 | ||
3241 | ret = chmod(mock_boot_id_path, 0444); | |
3242 | if (ret < 0) { | |
3243 | SYSERROR("Failed to chown %s", mock_boot_id_path); | |
3244 | (void)unlink(mock_boot_id_path); | |
3245 | return -1; | |
3246 | } | |
3247 | ||
3248 | ret = mount(mock_boot_id_path, boot_id_path, NULL, MS_BIND, NULL); | |
3249 | if (ret < 0) { | |
3250 | SYSERROR("Failed to mount %s to %s", mock_boot_id_path, | |
3251 | boot_id_path); | |
3252 | (void)unlink(mock_boot_id_path); | |
3253 | return -1; | |
3254 | } | |
3255 | ||
3256 | ret = mount(NULL, boot_id_path, NULL, | |
3257 | (MS_BIND | MS_REMOUNT | MS_RDONLY | MS_NOSUID | MS_NOEXEC | | |
3258 | MS_NODEV), | |
3259 | NULL); | |
3260 | if (ret < 0) { | |
3261 | SYSERROR("Failed to remount %s read-only", boot_id_path); | |
3262 | (void)unlink(mock_boot_id_path); | |
3263 | return -1; | |
3264 | } | |
3265 | ||
3266 | return 0; | |
3267 | } | |
3268 | ||
3b988b33 | 3269 | int lxc_setup(struct lxc_handler *handler) |
35120d9c | 3270 | { |
2187efd3 | 3271 | int ret; |
0fd73091 | 3272 | const char *lxcpath = handler->lxcpath, *name = handler->name; |
35120d9c | 3273 | struct lxc_conf *lxc_conf = handler->conf; |
4fef78bc | 3274 | char *keyring_context = NULL; |
35120d9c | 3275 | |
8ce1abc2 | 3276 | ret = lxc_setup_rootfs_prepare_root(lxc_conf, name, lxcpath); |
55022530 CB |
3277 | if (ret < 0) |
3278 | return log_error(-1, "Failed to setup rootfs"); | |
35120d9c | 3279 | |
b87ee312 | 3280 | if (handler->nsfd[LXC_NS_UTS] == -EBADF) { |
8353b4c9 | 3281 | ret = setup_utsname(lxc_conf->utsname); |
55022530 CB |
3282 | if (ret < 0) |
3283 | return log_error(-1, "Failed to setup the utsname %s", name); | |
0ad19a3f | 3284 | } |
3285 | ||
8f818a84 MB |
3286 | if (!lxc_conf->keyring_disable_session) { |
3287 | if (lxc_conf->lsm_se_keyring_context) { | |
3288 | keyring_context = lxc_conf->lsm_se_keyring_context; | |
3289 | } else if (lxc_conf->lsm_se_context) { | |
3290 | keyring_context = lxc_conf->lsm_se_context; | |
3291 | } | |
4fef78bc | 3292 | |
8f818a84 MB |
3293 | ret = lxc_setup_keyring(keyring_context); |
3294 | if (ret < 0) | |
3295 | return -1; | |
3296 | } | |
b25291da | 3297 | |
e389f2af CB |
3298 | if (handler->ns_clone_flags & CLONE_NEWNET) { |
3299 | ret = lxc_setup_network_in_child_namespaces(lxc_conf, | |
3300 | &lxc_conf->network); | |
55022530 CB |
3301 | if (ret < 0) |
3302 | return log_error(-1, "Failed to setup network"); | |
0ad19a3f | 3303 | |
e389f2af | 3304 | ret = lxc_network_send_name_and_ifindex_to_parent(handler); |
55022530 CB |
3305 | if (ret < 0) |
3306 | return log_error(-1, "Failed to send network device names and ifindices to parent"); | |
790255cf CB |
3307 | } |
3308 | ||
bc6928ff | 3309 | if (lxc_conf->autodev > 0) { |
63012bdd | 3310 | ret = mount_autodev(name, &lxc_conf->rootfs, lxc_conf->autodevtmpfssize, lxcpath); |
55022530 CB |
3311 | if (ret < 0) |
3312 | return log_error(-1, "Failed to mount \"/dev\""); | |
c6883f38 SH |
3313 | } |
3314 | ||
8353b4c9 CB |
3315 | /* Do automatic mounts (mainly /proc and /sys), but exclude those that |
3316 | * need to wait until other stuff has finished. | |
368bbc02 | 3317 | */ |
8353b4c9 | 3318 | ret = lxc_mount_auto_mounts(lxc_conf, lxc_conf->auto_mounts & ~LXC_AUTO_CGROUP_MASK, handler); |
55022530 CB |
3319 | if (ret < 0) |
3320 | return log_error(-1, "Failed to setup first automatic mounts"); | |
368bbc02 | 3321 | |
8353b4c9 | 3322 | ret = setup_mount(lxc_conf, &lxc_conf->rootfs, lxc_conf->fstab, name, lxcpath); |
55022530 CB |
3323 | if (ret < 0) |
3324 | return log_error(-1, "Failed to setup mounts"); | |
576f946d | 3325 | |
c631115d FA |
3326 | if (!lxc_list_empty(&lxc_conf->mount_list)) { |
3327 | ret = setup_mount_entries(lxc_conf, &lxc_conf->rootfs, | |
3328 | &lxc_conf->mount_list, name, lxcpath); | |
55022530 CB |
3329 | if (ret < 0) |
3330 | return log_error(-1, "Failed to setup mount entries"); | |
c631115d FA |
3331 | } |
3332 | ||
8353b4c9 | 3333 | if (lxc_conf->is_execute) { |
4b5b3a2a TA |
3334 | if (execveat_supported()) { |
3335 | int fd; | |
3336 | char path[PATH_MAX]; | |
3337 | ||
3338 | ret = snprintf(path, PATH_MAX, SBINDIR "/init.lxc.static"); | |
55022530 CB |
3339 | if (ret < 0 || ret >= PATH_MAX) |
3340 | return log_error(-1, "Path to init.lxc.static too long"); | |
4b5b3a2a TA |
3341 | |
3342 | fd = open(path, O_PATH | O_CLOEXEC); | |
55022530 CB |
3343 | if (fd < 0) |
3344 | return log_error_errno(-1, errno, "Unable to open lxc.init.static"); | |
4b5b3a2a TA |
3345 | |
3346 | ((struct execute_args *)handler->data)->init_fd = fd; | |
3347 | ((struct execute_args *)handler->data)->init_path = NULL; | |
3348 | } else { | |
3349 | ret = lxc_execute_bind_init(handler); | |
55022530 CB |
3350 | if (ret < 0) |
3351 | return log_error(-1, "Failed to bind-mount the lxc init system"); | |
8353b4c9 CB |
3352 | } |
3353 | } | |
2322903b | 3354 | |
8353b4c9 CB |
3355 | /* Now mount only cgroups, if wanted. Before, /sys could not have been |
3356 | * mounted. It is guaranteed to be mounted now either through | |
3357 | * automatically or via fstab entries. | |
368bbc02 | 3358 | */ |
8353b4c9 | 3359 | ret = lxc_mount_auto_mounts(lxc_conf, lxc_conf->auto_mounts & LXC_AUTO_CGROUP_MASK, handler); |
55022530 CB |
3360 | if (ret < 0) |
3361 | return log_error(-1, "Failed to setup remaining automatic mounts"); | |
368bbc02 | 3362 | |
8353b4c9 | 3363 | ret = run_lxc_hooks(name, "mount", lxc_conf, NULL); |
55022530 CB |
3364 | if (ret < 0) |
3365 | return log_error(-1, "Failed to run mount hooks"); | |
773fb9ca | 3366 | |
bc6928ff | 3367 | if (lxc_conf->autodev > 0) { |
8353b4c9 | 3368 | ret = run_lxc_hooks(name, "autodev", lxc_conf, NULL); |
55022530 CB |
3369 | if (ret < 0) |
3370 | return log_error(-1, "Failed to run autodev hooks"); | |
06749971 | 3371 | |
8353b4c9 | 3372 | ret = lxc_fill_autodev(&lxc_conf->rootfs); |
55022530 CB |
3373 | if (ret < 0) |
3374 | return log_error(-1, "Failed to populate \"/dev\""); | |
91c3830e | 3375 | } |
368bbc02 | 3376 | |
75193660 | 3377 | /* Make sure any start hooks are in the container */ |
55022530 CB |
3378 | if (!verify_start_hooks(lxc_conf)) |
3379 | return log_error(-1, "Failed to verify start hooks"); | |
75193660 | 3380 | |
ed8704d0 | 3381 | ret = lxc_setup_console(&lxc_conf->rootfs, &lxc_conf->console, |
885766f5 | 3382 | lxc_conf->ttys.dir); |
55022530 CB |
3383 | if (ret < 0) |
3384 | return log_error(-1, "Failed to setup console"); | |
6e590161 | 3385 | |
ed8704d0 | 3386 | ret = lxc_setup_dev_symlinks(&lxc_conf->rootfs); |
55022530 CB |
3387 | if (ret < 0) |
3388 | return log_error(-1, "Failed to setup \"/dev\" symlinks"); | |
69aa6655 | 3389 | |
8353b4c9 | 3390 | ret = lxc_create_tmp_proc_mount(lxc_conf); |
55022530 CB |
3391 | if (ret < 0) |
3392 | return log_error(-1, "Failed to \"/proc\" LSMs"); | |
e075f5d9 | 3393 | |
8ce1abc2 | 3394 | ret = lxc_setup_rootfs_switch_root(&lxc_conf->rootfs); |
55022530 CB |
3395 | if (ret < 0) |
3396 | return log_error(-1, "Failed to pivot root into rootfs"); | |
ed502555 | 3397 | |
20502652 CB |
3398 | /* Setting the boot-id is best-effort for now. */ |
3399 | if (lxc_conf->autodev > 0) | |
3400 | (void)lxc_setup_boot_id(); | |
3401 | ||
8353b4c9 | 3402 | ret = lxc_setup_devpts(lxc_conf); |
55022530 CB |
3403 | if (ret < 0) |
3404 | return log_error(-1, "Failed to setup new devpts instance"); | |
3c26f34e | 3405 | |
2187efd3 CB |
3406 | ret = lxc_create_ttys(handler); |
3407 | if (ret < 0) | |
e8bd4e43 | 3408 | return -1; |
e8bd4e43 | 3409 | |
8353b4c9 | 3410 | ret = setup_personality(lxc_conf->personality); |
55022530 CB |
3411 | if (ret < 0) |
3412 | return log_error(-1, "Failed to set personality"); | |
cccc74b5 | 3413 | |
8353b4c9 CB |
3414 | /* Set sysctl value to a path under /proc/sys as determined from the |
3415 | * key. For e.g. net.ipv4.ip_forward translated to | |
3416 | * /proc/sys/net/ipv4/ip_forward. | |
7edd0540 L |
3417 | */ |
3418 | if (!lxc_list_empty(&lxc_conf->sysctls)) { | |
3419 | ret = setup_sysctl_parameters(&lxc_conf->sysctls); | |
55022530 CB |
3420 | if (ret < 0) |
3421 | return log_error(-1, "Failed to setup sysctl parameters"); | |
7edd0540 L |
3422 | } |
3423 | ||
97a8f74f | 3424 | if (!lxc_list_empty(&lxc_conf->keepcaps)) { |
55022530 CB |
3425 | if (!lxc_list_empty(&lxc_conf->caps)) |
3426 | 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 | 3427 | |
55022530 CB |
3428 | if (dropcaps_except(&lxc_conf->keepcaps)) |
3429 | return log_error(-1, "Failed to keep capabilities"); | |
97a8f74f | 3430 | } else if (setup_caps(&lxc_conf->caps)) { |
55022530 | 3431 | return log_error(-1, "Failed to drop capabilities"); |
81810dd1 DL |
3432 | } |
3433 | ||
8353b4c9 | 3434 | NOTICE("The container \"%s\" is set up", name); |
cd54d859 | 3435 | |
0ad19a3f | 3436 | return 0; |
3437 | } | |
26ddeedd | 3438 | |
3f60c2f7 | 3439 | int run_lxc_hooks(const char *name, char *hookname, struct lxc_conf *conf, |
14a7b0f9 | 3440 | char *argv[]) |
26ddeedd | 3441 | { |
26ddeedd | 3442 | struct lxc_list *it; |
3ea957c6 RK |
3443 | int which; |
3444 | ||
3445 | for (which = 0; which < NUM_LXC_HOOKS; which ++) { | |
3446 | if (strcmp(hookname, lxchook_names[which]) == 0) | |
3447 | break; | |
3448 | } | |
3449 | ||
3450 | if (which >= NUM_LXC_HOOKS) | |
26ddeedd | 3451 | return -1; |
3f60c2f7 | 3452 | |
0fd73091 | 3453 | lxc_list_for_each (it, &conf->hooks[which]) { |
26ddeedd | 3454 | int ret; |
3f60c2f7 CB |
3455 | char *hook = it->elem; |
3456 | ||
3457 | ret = run_script_argv(name, conf->hooks_version, "lxc", hook, | |
14a7b0f9 | 3458 | hookname, argv); |
3f60c2f7 CB |
3459 | if (ret < 0) |
3460 | return -1; | |
26ddeedd | 3461 | } |
3f60c2f7 | 3462 | |
26ddeedd SH |
3463 | return 0; |
3464 | } | |
72d0e1cb | 3465 | |
72d0e1cb SG |
3466 | int lxc_clear_config_caps(struct lxc_conf *c) |
3467 | { | |
1a0e70ac | 3468 | struct lxc_list *it, *next; |
72d0e1cb | 3469 | |
0fd73091 | 3470 | lxc_list_for_each_safe (it, &c->caps, next) { |
72d0e1cb SG |
3471 | lxc_list_del(it); |
3472 | free(it->elem); | |
3473 | free(it); | |
3474 | } | |
0fd73091 | 3475 | |
72d0e1cb SG |
3476 | return 0; |
3477 | } | |
3478 | ||
c7e345ae CB |
3479 | static int lxc_free_idmap(struct lxc_list *id_map) |
3480 | { | |
27c27d73 SH |
3481 | struct lxc_list *it, *next; |
3482 | ||
0fd73091 | 3483 | lxc_list_for_each_safe (it, id_map, next) { |
27c27d73 SH |
3484 | lxc_list_del(it); |
3485 | free(it->elem); | |
3486 | free(it); | |
3487 | } | |
c7e345ae | 3488 | |
27c27d73 SH |
3489 | return 0; |
3490 | } | |
3491 | ||
4355ab5f SH |
3492 | int lxc_clear_idmaps(struct lxc_conf *c) |
3493 | { | |
3494 | return lxc_free_idmap(&c->id_map); | |
3495 | } | |
3496 | ||
1fb86a7c SH |
3497 | int lxc_clear_config_keepcaps(struct lxc_conf *c) |
3498 | { | |
0fd73091 | 3499 | struct lxc_list *it, *next; |
1fb86a7c | 3500 | |
0fd73091 | 3501 | lxc_list_for_each_safe (it, &c->keepcaps, next) { |
1fb86a7c SH |
3502 | lxc_list_del(it); |
3503 | free(it->elem); | |
3504 | free(it); | |
3505 | } | |
0fd73091 | 3506 | |
1fb86a7c SH |
3507 | return 0; |
3508 | } | |
3509 | ||
a3ed9b81 | 3510 | int lxc_clear_namespace(struct lxc_conf *c) |
3511 | { | |
3512 | int i; | |
3513 | for (i = 0; i < LXC_NS_MAX; i++) { | |
3514 | free(c->ns_share[i]); | |
3515 | c->ns_share[i] = NULL; | |
3516 | } | |
3517 | return 0; | |
3518 | } | |
3519 | ||
54860ed0 | 3520 | int lxc_clear_cgroups(struct lxc_conf *c, const char *key, int version) |
72d0e1cb | 3521 | { |
54860ed0 | 3522 | char *global_token, *namespaced_token; |
ab1a6cac | 3523 | size_t namespaced_token_len; |
54860ed0 | 3524 | struct lxc_list *it, *next, *list; |
ab1a6cac | 3525 | const char *k = key; |
54860ed0 | 3526 | bool all = false; |
72d0e1cb | 3527 | |
54860ed0 CB |
3528 | if (version == CGROUP2_SUPER_MAGIC) { |
3529 | global_token = "lxc.cgroup2"; | |
3530 | namespaced_token = "lxc.cgroup2."; | |
6333c915 | 3531 | namespaced_token_len = STRLITERALLEN("lxc.cgroup2."); |
54860ed0 CB |
3532 | list = &c->cgroup2; |
3533 | } else if (version == CGROUP_SUPER_MAGIC) { | |
3534 | global_token = "lxc.cgroup"; | |
3535 | namespaced_token = "lxc.cgroup."; | |
6333c915 | 3536 | namespaced_token_len = STRLITERALLEN("lxc.cgroup."); |
54860ed0 CB |
3537 | list = &c->cgroup; |
3538 | } else { | |
ab1a6cac | 3539 | return -EINVAL; |
54860ed0 CB |
3540 | } |
3541 | ||
3542 | if (strcmp(key, global_token) == 0) | |
72d0e1cb | 3543 | all = true; |
6333c915 | 3544 | else if (strncmp(key, namespaced_token, namespaced_token_len) == 0) |
ab1a6cac | 3545 | k += namespaced_token_len; |
a6390f01 | 3546 | else |
ab1a6cac | 3547 | return -EINVAL; |
72d0e1cb | 3548 | |
0fd73091 | 3549 | lxc_list_for_each_safe (it, list, next) { |
72d0e1cb | 3550 | struct lxc_cgroup *cg = it->elem; |
54860ed0 | 3551 | |
72d0e1cb SG |
3552 | if (!all && strcmp(cg->subsystem, k) != 0) |
3553 | continue; | |
54860ed0 | 3554 | |
72d0e1cb SG |
3555 | lxc_list_del(it); |
3556 | free(cg->subsystem); | |
3557 | free(cg->value); | |
3558 | free(cg); | |
3559 | free(it); | |
3560 | } | |
e409b214 | 3561 | |
72d0e1cb SG |
3562 | return 0; |
3563 | } | |
3564 | ||
4bfb655e CB |
3565 | static void lxc_clear_devices(struct lxc_conf *conf) |
3566 | { | |
3567 | struct lxc_list *list = &conf->devices; | |
3568 | struct lxc_list *it, *next; | |
3569 | ||
3570 | lxc_list_for_each_safe(it, list, next) { | |
3571 | lxc_list_del(it); | |
3572 | free(it); | |
3573 | } | |
3574 | } | |
3575 | ||
c6d09e15 WB |
3576 | int lxc_clear_limits(struct lxc_conf *c, const char *key) |
3577 | { | |
3578 | struct lxc_list *it, *next; | |
c6d09e15 | 3579 | const char *k = NULL; |
0fd73091 | 3580 | bool all = false; |
c6d09e15 | 3581 | |
b668653c | 3582 | if (strcmp(key, "lxc.limit") == 0 || strcmp(key, "lxc.prlimit") == 0) |
c6d09e15 | 3583 | all = true; |
6333c915 CB |
3584 | else if (strncmp(key, "lxc.limit.", STRLITERALLEN("lxc.limit.")) == 0) |
3585 | k = key + STRLITERALLEN("lxc.limit."); | |
3586 | else if (strncmp(key, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit.")) == 0) | |
3587 | k = key + STRLITERALLEN("lxc.prlimit."); | |
c6d09e15 WB |
3588 | else |
3589 | return -1; | |
3590 | ||
0fd73091 | 3591 | lxc_list_for_each_safe (it, &c->limits, next) { |
c6d09e15 | 3592 | struct lxc_limit *lim = it->elem; |
0fd73091 | 3593 | |
c6d09e15 WB |
3594 | if (!all && strcmp(lim->resource, k) != 0) |
3595 | continue; | |
0fd73091 | 3596 | |
c6d09e15 WB |
3597 | lxc_list_del(it); |
3598 | free(lim->resource); | |
3599 | free(lim); | |
3600 | free(it); | |
3601 | } | |
b668653c | 3602 | |
c6d09e15 WB |
3603 | return 0; |
3604 | } | |
3605 | ||
7edd0540 L |
3606 | int lxc_clear_sysctls(struct lxc_conf *c, const char *key) |
3607 | { | |
3608 | struct lxc_list *it, *next; | |
7edd0540 | 3609 | const char *k = NULL; |
0fd73091 | 3610 | bool all = false; |
7edd0540 L |
3611 | |
3612 | if (strcmp(key, "lxc.sysctl") == 0) | |
3613 | all = true; | |
6333c915 CB |
3614 | else if (strncmp(key, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) == 0) |
3615 | k = key + STRLITERALLEN("lxc.sysctl."); | |
7edd0540 L |
3616 | else |
3617 | return -1; | |
3618 | ||
0fd73091 | 3619 | lxc_list_for_each_safe (it, &c->sysctls, next) { |
7edd0540 | 3620 | struct lxc_sysctl *elem = it->elem; |
0fd73091 | 3621 | |
7edd0540 L |
3622 | if (!all && strcmp(elem->key, k) != 0) |
3623 | continue; | |
0fd73091 | 3624 | |
7edd0540 L |
3625 | lxc_list_del(it); |
3626 | free(elem->key); | |
3627 | free(elem->value); | |
3628 | free(elem); | |
3629 | free(it); | |
3630 | } | |
0fd73091 | 3631 | |
7edd0540 L |
3632 | return 0; |
3633 | } | |
3634 | ||
61d7a733 YT |
3635 | int lxc_clear_procs(struct lxc_conf *c, const char *key) |
3636 | { | |
0fd73091 | 3637 | struct lxc_list *it, *next; |
61d7a733 | 3638 | const char *k = NULL; |
0fd73091 | 3639 | bool all = false; |
61d7a733 YT |
3640 | |
3641 | if (strcmp(key, "lxc.proc") == 0) | |
3642 | all = true; | |
6333c915 CB |
3643 | else if (strncmp(key, "lxc.proc.", STRLITERALLEN("lxc.proc.")) == 0) |
3644 | k = key + STRLITERALLEN("lxc.proc."); | |
61d7a733 YT |
3645 | else |
3646 | return -1; | |
3647 | ||
0fd73091 | 3648 | lxc_list_for_each_safe (it, &c->procs, next) { |
61d7a733 | 3649 | struct lxc_proc *proc = it->elem; |
0fd73091 | 3650 | |
61d7a733 YT |
3651 | if (!all && strcmp(proc->filename, k) != 0) |
3652 | continue; | |
0fd73091 | 3653 | |
61d7a733 YT |
3654 | lxc_list_del(it); |
3655 | free(proc->filename); | |
3656 | free(proc->value); | |
3657 | free(proc); | |
3658 | free(it); | |
3659 | } | |
3660 | ||
3661 | return 0; | |
3662 | } | |
3663 | ||
ee1e7aa0 SG |
3664 | int lxc_clear_groups(struct lxc_conf *c) |
3665 | { | |
0fd73091 | 3666 | struct lxc_list *it, *next; |
ee1e7aa0 | 3667 | |
0fd73091 | 3668 | lxc_list_for_each_safe (it, &c->groups, next) { |
ee1e7aa0 SG |
3669 | lxc_list_del(it); |
3670 | free(it->elem); | |
3671 | free(it); | |
3672 | } | |
0fd73091 | 3673 | |
ee1e7aa0 SG |
3674 | return 0; |
3675 | } | |
3676 | ||
ab799c0b SG |
3677 | int lxc_clear_environment(struct lxc_conf *c) |
3678 | { | |
0fd73091 | 3679 | struct lxc_list *it, *next; |
ab799c0b | 3680 | |
0fd73091 | 3681 | lxc_list_for_each_safe (it, &c->environment, next) { |
ab799c0b SG |
3682 | lxc_list_del(it); |
3683 | free(it->elem); | |
3684 | free(it); | |
3685 | } | |
0fd73091 | 3686 | |
ab799c0b SG |
3687 | return 0; |
3688 | } | |
3689 | ||
72d0e1cb SG |
3690 | int lxc_clear_mount_entries(struct lxc_conf *c) |
3691 | { | |
0fd73091 | 3692 | struct lxc_list *it, *next; |
72d0e1cb | 3693 | |
0fd73091 | 3694 | lxc_list_for_each_safe (it, &c->mount_list, next) { |
72d0e1cb SG |
3695 | lxc_list_del(it); |
3696 | free(it->elem); | |
3697 | free(it); | |
3698 | } | |
0fd73091 | 3699 | |
72d0e1cb SG |
3700 | return 0; |
3701 | } | |
3702 | ||
b099e9e9 SH |
3703 | int lxc_clear_automounts(struct lxc_conf *c) |
3704 | { | |
3705 | c->auto_mounts = 0; | |
3706 | return 0; | |
3707 | } | |
3708 | ||
12a50cc6 | 3709 | int lxc_clear_hooks(struct lxc_conf *c, const char *key) |
72d0e1cb | 3710 | { |
72d0e1cb | 3711 | int i; |
0fd73091 CB |
3712 | struct lxc_list *it, *next; |
3713 | const char *k = NULL; | |
3714 | bool all = false, done = false; | |
72d0e1cb | 3715 | |
17ed13a3 SH |
3716 | if (strcmp(key, "lxc.hook") == 0) |
3717 | all = true; | |
6333c915 CB |
3718 | else if (strncmp(key, "lxc.hook.", STRLITERALLEN("lxc.hook.")) == 0) |
3719 | k = key + STRLITERALLEN("lxc.hook."); | |
a6390f01 WB |
3720 | else |
3721 | return -1; | |
17ed13a3 | 3722 | |
0fd73091 | 3723 | for (i = 0; i < NUM_LXC_HOOKS; i++) { |
17ed13a3 | 3724 | if (all || strcmp(k, lxchook_names[i]) == 0) { |
0fd73091 | 3725 | lxc_list_for_each_safe (it, &c->hooks[i], next) { |
17ed13a3 SH |
3726 | lxc_list_del(it); |
3727 | free(it->elem); | |
3728 | free(it); | |
3729 | } | |
0fd73091 | 3730 | |
17ed13a3 | 3731 | done = true; |
72d0e1cb SG |
3732 | } |
3733 | } | |
17ed13a3 | 3734 | |
55022530 CB |
3735 | if (!done) |
3736 | return log_error(-1, "Invalid hook key: %s", key); | |
0fd73091 | 3737 | |
72d0e1cb SG |
3738 | return 0; |
3739 | } | |
8eb5694b | 3740 | |
4184c3e1 SH |
3741 | static inline void lxc_clear_aliens(struct lxc_conf *conf) |
3742 | { | |
0fd73091 | 3743 | struct lxc_list *it, *next; |
4184c3e1 | 3744 | |
0fd73091 | 3745 | lxc_list_for_each_safe (it, &conf->aliens, next) { |
4184c3e1 SH |
3746 | lxc_list_del(it); |
3747 | free(it->elem); | |
3748 | free(it); | |
3749 | } | |
3750 | } | |
3751 | ||
c7b15d1e | 3752 | void lxc_clear_includes(struct lxc_conf *conf) |
f979ac15 | 3753 | { |
0fd73091 | 3754 | struct lxc_list *it, *next; |
f979ac15 | 3755 | |
0fd73091 | 3756 | lxc_list_for_each_safe (it, &conf->includes, next) { |
f979ac15 SH |
3757 | lxc_list_del(it); |
3758 | free(it->elem); | |
3759 | free(it); | |
3760 | } | |
3761 | } | |
3762 | ||
1800f924 WB |
3763 | int lxc_clear_apparmor_raw(struct lxc_conf *c) |
3764 | { | |
3765 | struct lxc_list *it, *next; | |
3766 | ||
3767 | lxc_list_for_each_safe (it, &c->lsm_aa_raw, next) { | |
3768 | lxc_list_del(it); | |
3769 | free(it->elem); | |
3770 | free(it); | |
3771 | } | |
3772 | ||
3773 | return 0; | |
3774 | } | |
3775 | ||
8eb5694b SH |
3776 | void lxc_conf_free(struct lxc_conf *conf) |
3777 | { | |
3778 | if (!conf) | |
3779 | return; | |
0fd73091 | 3780 | |
858377e4 SH |
3781 | if (current_config == conf) |
3782 | current_config = NULL; | |
aed105d5 | 3783 | lxc_terminal_conf_free(&conf->console); |
f10fad2f | 3784 | free(conf->rootfs.mount); |
b3b8c97f | 3785 | free(conf->rootfs.bdev_type); |
f10fad2f ME |
3786 | free(conf->rootfs.options); |
3787 | free(conf->rootfs.path); | |
9dd75981 | 3788 | free(conf->rootfs.data); |
f10fad2f | 3789 | free(conf->logfile); |
858377e4 SH |
3790 | if (conf->logfd != -1) |
3791 | close(conf->logfd); | |
f10fad2f | 3792 | free(conf->utsname); |
885766f5 CB |
3793 | free(conf->ttys.dir); |
3794 | free(conf->ttys.tty_names); | |
f10fad2f ME |
3795 | free(conf->fstab); |
3796 | free(conf->rcfile); | |
5cda27c1 | 3797 | free(conf->execute_cmd); |
f10fad2f | 3798 | free(conf->init_cmd); |
3c491553 | 3799 | free(conf->init_cwd); |
6b0d5538 | 3800 | free(conf->unexpanded_config); |
76d0127f | 3801 | free(conf->syslog); |
c302b476 | 3802 | lxc_free_networks(&conf->network); |
f10fad2f | 3803 | free(conf->lsm_aa_profile); |
1800f924 | 3804 | free(conf->lsm_aa_profile_computed); |
f10fad2f | 3805 | free(conf->lsm_se_context); |
c3e3c21a | 3806 | lxc_seccomp_free(&conf->seccomp); |
8eb5694b | 3807 | lxc_clear_config_caps(conf); |
1fb86a7c | 3808 | lxc_clear_config_keepcaps(conf); |
54860ed0 CB |
3809 | lxc_clear_cgroups(conf, "lxc.cgroup", CGROUP_SUPER_MAGIC); |
3810 | lxc_clear_cgroups(conf, "lxc.cgroup2", CGROUP2_SUPER_MAGIC); | |
4bfb655e | 3811 | lxc_clear_devices(conf); |
bf651989 | 3812 | lxc_clear_cgroup2_devices(conf); |
17ed13a3 | 3813 | lxc_clear_hooks(conf, "lxc.hook"); |
8eb5694b | 3814 | lxc_clear_mount_entries(conf); |
27c27d73 | 3815 | lxc_clear_idmaps(conf); |
ee1e7aa0 | 3816 | lxc_clear_groups(conf); |
f979ac15 | 3817 | lxc_clear_includes(conf); |
761d81ca | 3818 | lxc_clear_aliens(conf); |
ab799c0b | 3819 | lxc_clear_environment(conf); |
240d4b74 | 3820 | lxc_clear_limits(conf, "lxc.prlimit"); |
7edd0540 | 3821 | lxc_clear_sysctls(conf, "lxc.sysctl"); |
61d7a733 | 3822 | lxc_clear_procs(conf, "lxc.proc"); |
1800f924 | 3823 | lxc_clear_apparmor_raw(conf); |
a3ed9b81 | 3824 | lxc_clear_namespace(conf); |
43654d34 CB |
3825 | free(conf->cgroup_meta.dir); |
3826 | free(conf->cgroup_meta.controllers); | |
7a41e857 LT |
3827 | free(conf->shmount.path_host); |
3828 | free(conf->shmount.path_cont); | |
8eb5694b SH |
3829 | free(conf); |
3830 | } | |
4355ab5f SH |
3831 | |
3832 | struct userns_fn_data { | |
3833 | int (*fn)(void *); | |
c9b7c33e | 3834 | const char *fn_name; |
4355ab5f SH |
3835 | void *arg; |
3836 | int p[2]; | |
3837 | }; | |
3838 | ||
3839 | static int run_userns_fn(void *data) | |
3840 | { | |
adaffdd7 | 3841 | int ret; |
4355ab5f | 3842 | char c; |
0fd73091 | 3843 | struct userns_fn_data *d = data; |
4355ab5f | 3844 | |
f8aa4bf3 | 3845 | /* Close write end of the pipe. */ |
4355ab5f | 3846 | close(d->p[1]); |
f8aa4bf3 CB |
3847 | |
3848 | /* Wait for parent to finish establishing a new mapping in the user | |
3849 | * namespace we are executing in. | |
3850 | */ | |
adaffdd7 | 3851 | ret = lxc_read_nointr(d->p[0], &c, 1); |
f8aa4bf3 | 3852 | /* Close read end of the pipe. */ |
4355ab5f | 3853 | close(d->p[0]); |
adaffdd7 CB |
3854 | if (ret != 1) |
3855 | return -1; | |
f8aa4bf3 | 3856 | |
c9b7c33e | 3857 | if (d->fn_name) |
adaffdd7 | 3858 | TRACE("Calling function \"%s\"", d->fn_name); |
0fd73091 | 3859 | |
f8aa4bf3 | 3860 | /* Call function to run. */ |
4355ab5f SH |
3861 | return d->fn(d->arg); |
3862 | } | |
3863 | ||
db7cfe23 CB |
3864 | static struct id_map *mapped_nsid_add(struct lxc_conf *conf, unsigned id, |
3865 | enum idtype idtype) | |
3866 | { | |
5173b710 CB |
3867 | const struct id_map *map; |
3868 | struct id_map *retmap; | |
db7cfe23 CB |
3869 | |
3870 | map = find_mapped_nsid_entry(conf, id, idtype); | |
3871 | if (!map) | |
3872 | return NULL; | |
3873 | ||
3874 | retmap = malloc(sizeof(*retmap)); | |
3875 | if (!retmap) | |
3876 | return NULL; | |
3877 | ||
3878 | memcpy(retmap, map, sizeof(*retmap)); | |
3879 | return retmap; | |
3880 | } | |
3881 | ||
c4333195 CB |
3882 | static struct id_map *find_mapped_hostid_entry(struct lxc_conf *conf, |
3883 | unsigned id, enum idtype idtype) | |
f8aa4bf3 | 3884 | { |
f8aa4bf3 | 3885 | struct id_map *map; |
0fd73091 | 3886 | struct lxc_list *it; |
f8aa4bf3 CB |
3887 | struct id_map *retmap = NULL; |
3888 | ||
0fd73091 | 3889 | lxc_list_for_each (it, &conf->id_map) { |
f8aa4bf3 CB |
3890 | map = it->elem; |
3891 | if (map->idtype != idtype) | |
3892 | continue; | |
3893 | ||
3894 | if (id >= map->hostid && id < map->hostid + map->range) { | |
3895 | retmap = map; | |
3896 | break; | |
3897 | } | |
3898 | } | |
3899 | ||
f8aa4bf3 CB |
3900 | return retmap; |
3901 | } | |
3902 | ||
0fd73091 | 3903 | /* Allocate a new {g,u}id mapping for the given {g,u}id. Re-use an already |
f8aa4bf3 | 3904 | * existing one or establish a new one. |
4355ab5f | 3905 | */ |
0fd73091 CB |
3906 | static struct id_map *mapped_hostid_add(struct lxc_conf *conf, uid_t id, |
3907 | enum idtype type) | |
4355ab5f | 3908 | { |
55022530 | 3909 | __do_free struct id_map *entry = NULL; |
28a2d9e7 | 3910 | int hostid_mapped; |
55022530 | 3911 | struct id_map *tmp = NULL; |
c4333195 CB |
3912 | |
3913 | entry = malloc(sizeof(*entry)); | |
3914 | if (!entry) | |
3915 | return NULL; | |
f8aa4bf3 | 3916 | |
28a2d9e7 | 3917 | /* Reuse existing mapping. */ |
c4333195 CB |
3918 | tmp = find_mapped_hostid_entry(conf, id, type); |
3919 | if (tmp) | |
3920 | return memcpy(entry, tmp, sizeof(*entry)); | |
f8aa4bf3 | 3921 | |
28a2d9e7 CB |
3922 | /* Find new mapping. */ |
3923 | hostid_mapped = find_unmapped_nsid(conf, type); | |
55022530 CB |
3924 | if (hostid_mapped < 0) |
3925 | return log_debug(NULL, "Failed to find free mapping for id %d", id); | |
f8aa4bf3 | 3926 | |
28a2d9e7 CB |
3927 | entry->idtype = type; |
3928 | entry->nsid = hostid_mapped; | |
3929 | entry->hostid = (unsigned long)id; | |
3930 | entry->range = 1; | |
4355ab5f | 3931 | |
55022530 | 3932 | return move_ptr(entry); |
4355ab5f SH |
3933 | } |
3934 | ||
dcf0ffdf | 3935 | struct lxc_list *get_minimal_idmap(struct lxc_conf *conf) |
4355ab5f | 3936 | { |
00d6cfe2 CB |
3937 | __do_free struct id_map *container_root_uid = NULL, |
3938 | *container_root_gid = NULL, | |
3939 | *host_uid_map = NULL, *host_gid_map = NULL; | |
3940 | __do_free struct lxc_list *idmap = NULL; | |
f8aa4bf3 | 3941 | uid_t euid, egid; |
4160c3a0 CB |
3942 | uid_t nsuid = (conf->root_nsuid_map != NULL) ? 0 : conf->init_uid; |
3943 | gid_t nsgid = (conf->root_nsgid_map != NULL) ? 0 : conf->init_gid; | |
00d6cfe2 | 3944 | struct lxc_list *tmplist = NULL; |
4355ab5f | 3945 | |
db7cfe23 | 3946 | /* Find container root mappings. */ |
4160c3a0 | 3947 | container_root_uid = mapped_nsid_add(conf, nsuid, ID_TYPE_UID); |
55022530 CB |
3948 | if (!container_root_uid) |
3949 | return log_debug(NULL, "Failed to find mapping for namespace uid %d", 0); | |
dcf0ffdf CB |
3950 | euid = geteuid(); |
3951 | if (euid >= container_root_uid->hostid && | |
3952 | euid < (container_root_uid->hostid + container_root_uid->range)) | |
db7cfe23 | 3953 | host_uid_map = container_root_uid; |
f8aa4bf3 | 3954 | |
4160c3a0 | 3955 | container_root_gid = mapped_nsid_add(conf, nsgid, ID_TYPE_GID); |
55022530 CB |
3956 | if (!container_root_gid) |
3957 | return log_debug(NULL, "Failed to find mapping for namespace gid %d", 0); | |
dcf0ffdf CB |
3958 | egid = getegid(); |
3959 | if (egid >= container_root_gid->hostid && | |
3960 | egid < (container_root_gid->hostid + container_root_gid->range)) | |
db7cfe23 | 3961 | host_gid_map = container_root_gid; |
f8aa4bf3 CB |
3962 | |
3963 | /* Check whether the {g,u}id of the user has a mapping. */ | |
954b7d9b | 3964 | if (!host_uid_map) |
c4333195 | 3965 | host_uid_map = mapped_hostid_add(conf, euid, ID_TYPE_UID); |
55022530 CB |
3966 | if (!host_uid_map) |
3967 | return log_debug(NULL, "Failed to find mapping for uid %d", euid); | |
f8aa4bf3 | 3968 | |
dcf0ffdf CB |
3969 | if (!host_gid_map) |
3970 | host_gid_map = mapped_hostid_add(conf, egid, ID_TYPE_GID); | |
55022530 CB |
3971 | if (!host_gid_map) |
3972 | return log_debug(NULL, "Failed to find mapping for gid %d", egid); | |
28a2d9e7 CB |
3973 | |
3974 | /* Allocate new {g,u}id map list. */ | |
3975 | idmap = malloc(sizeof(*idmap)); | |
3976 | if (!idmap) | |
00d6cfe2 | 3977 | return NULL; |
28a2d9e7 CB |
3978 | lxc_list_init(idmap); |
3979 | ||
f8aa4bf3 CB |
3980 | /* Add container root to the map. */ |
3981 | tmplist = malloc(sizeof(*tmplist)); | |
3982 | if (!tmplist) | |
00d6cfe2 | 3983 | return NULL; |
f8aa4bf3 CB |
3984 | lxc_list_add_elem(tmplist, container_root_uid); |
3985 | lxc_list_add_tail(idmap, tmplist); | |
28a2d9e7 | 3986 | |
1d90e064 | 3987 | if (host_uid_map && (host_uid_map != container_root_uid)) { |
28a2d9e7 | 3988 | /* idmap will now keep track of that memory. */ |
00d6cfe2 | 3989 | move_ptr(container_root_uid); |
28a2d9e7 CB |
3990 | |
3991 | /* Add container root to the map. */ | |
3992 | tmplist = malloc(sizeof(*tmplist)); | |
3993 | if (!tmplist) | |
00d6cfe2 | 3994 | return NULL; |
28a2d9e7 CB |
3995 | lxc_list_add_elem(tmplist, host_uid_map); |
3996 | lxc_list_add_tail(idmap, tmplist); | |
28a2d9e7 | 3997 | } |
1d90e064 | 3998 | /* idmap will now keep track of that memory. */ |
00d6cfe2 | 3999 | move_ptr(container_root_uid); |
1d90e064 | 4000 | /* idmap will now keep track of that memory. */ |
00d6cfe2 | 4001 | move_ptr(host_uid_map); |
f8aa4bf3 CB |
4002 | |
4003 | tmplist = malloc(sizeof(*tmplist)); | |
4004 | if (!tmplist) | |
00d6cfe2 | 4005 | return NULL; |
f8aa4bf3 CB |
4006 | lxc_list_add_elem(tmplist, container_root_gid); |
4007 | lxc_list_add_tail(idmap, tmplist); | |
28a2d9e7 | 4008 | |
1d90e064 | 4009 | if (host_gid_map && (host_gid_map != container_root_gid)) { |
28a2d9e7 | 4010 | /* idmap will now keep track of that memory. */ |
00d6cfe2 | 4011 | move_ptr(container_root_gid); |
28a2d9e7 CB |
4012 | |
4013 | tmplist = malloc(sizeof(*tmplist)); | |
4014 | if (!tmplist) | |
00d6cfe2 | 4015 | return NULL; |
28a2d9e7 CB |
4016 | lxc_list_add_elem(tmplist, host_gid_map); |
4017 | lxc_list_add_tail(idmap, tmplist); | |
28a2d9e7 | 4018 | } |
1d90e064 | 4019 | /* idmap will now keep track of that memory. */ |
00d6cfe2 | 4020 | move_ptr(container_root_gid); |
1d90e064 | 4021 | /* idmap will now keep track of that memory. */ |
00d6cfe2 | 4022 | move_ptr(host_gid_map); |
f8aa4bf3 | 4023 | |
dcf0ffdf | 4024 | TRACE("Allocated minimal idmapping"); |
00d6cfe2 | 4025 | return move_ptr(idmap); |
dcf0ffdf CB |
4026 | } |
4027 | ||
4028 | /* Run a function in a new user namespace. | |
4029 | * The caller's euid/egid will be mapped if it is not already. | |
4030 | * Afaict, userns_exec_1() is only used to operate based on privileges for the | |
4031 | * user's own {g,u}id on the host and for the container root's unmapped {g,u}id. | |
4032 | * This means we require only to establish a mapping from: | |
4033 | * - the container root {g,u}id as seen from the host > user's host {g,u}id | |
4034 | * - the container root -> some sub{g,u}id | |
915e3dbd | 4035 | * The former we add, if the user did not specify a mapping. The latter we |
6f3fd27f | 4036 | * retrieve from the container's configured {g,u}id mappings as it must have been |
dcf0ffdf CB |
4037 | * there to start the container in the first place. |
4038 | */ | |
4039 | int userns_exec_1(struct lxc_conf *conf, int (*fn)(void *), void *data, | |
4040 | const char *fn_name) | |
4041 | { | |
4042 | pid_t pid; | |
dcf0ffdf | 4043 | int p[2]; |
0fd73091 | 4044 | struct userns_fn_data d; |
dcf0ffdf | 4045 | struct lxc_list *idmap; |
0fd73091 CB |
4046 | int ret = -1, status = -1; |
4047 | char c = '1'; | |
dcf0ffdf | 4048 | |
2b2655a8 CB |
4049 | if (!conf) |
4050 | return -EINVAL; | |
4051 | ||
dcf0ffdf CB |
4052 | idmap = get_minimal_idmap(conf); |
4053 | if (!idmap) | |
4054 | return -1; | |
4055 | ||
979f9e34 | 4056 | ret = pipe2(p, O_CLOEXEC); |
dcf0ffdf CB |
4057 | if (ret < 0) { |
4058 | SYSERROR("Failed to create pipe"); | |
4059 | return -1; | |
4060 | } | |
4061 | d.fn = fn; | |
4062 | d.fn_name = fn_name; | |
4063 | d.arg = data; | |
4064 | d.p[0] = p[0]; | |
4065 | d.p[1] = p[1]; | |
4066 | ||
4067 | /* Clone child in new user namespace. */ | |
a59440be | 4068 | pid = lxc_raw_clone_cb(run_userns_fn, &d, CLONE_NEWUSER, NULL); |
dcf0ffdf | 4069 | if (pid < 0) { |
0fd73091 | 4070 | ERROR("Failed to clone process in new user namespace"); |
dcf0ffdf CB |
4071 | goto on_error; |
4072 | } | |
4073 | ||
4074 | close(p[0]); | |
4075 | p[0] = -1; | |
4076 | ||
4b73005c CB |
4077 | if (lxc_log_get_level() == LXC_LOG_LEVEL_TRACE || |
4078 | conf->loglevel == LXC_LOG_LEVEL_TRACE) { | |
dcf0ffdf | 4079 | struct id_map *map; |
0fd73091 | 4080 | struct lxc_list *it; |
dcf0ffdf | 4081 | |
0fd73091 | 4082 | lxc_list_for_each (it, idmap) { |
f8aa4bf3 | 4083 | map = it->elem; |
dcf0ffdf | 4084 | TRACE("Establishing %cid mapping for \"%d\" in new " |
f8aa4bf3 | 4085 | "user namespace: nsuid %lu - hostid %lu - range " |
0fd73091 CB |
4086 | "%lu", |
4087 | (map->idtype == ID_TYPE_UID) ? 'u' : 'g', pid, | |
4088 | map->nsid, map->hostid, map->range); | |
f8aa4bf3 | 4089 | } |
4355ab5f SH |
4090 | } |
4091 | ||
f8aa4bf3 | 4092 | /* Set up {g,u}id mapping for user namespace of child process. */ |
4355ab5f | 4093 | ret = lxc_map_ids(idmap, pid); |
f8aa4bf3 | 4094 | if (ret < 0) { |
0fd73091 | 4095 | ERROR("Error setting up {g,u}id mappings for child process \"%d\"", pid); |
f8aa4bf3 | 4096 | goto on_error; |
4355ab5f SH |
4097 | } |
4098 | ||
f8aa4bf3 | 4099 | /* Tell child to proceed. */ |
489f39be | 4100 | if (lxc_write_nointr(p[1], &c, 1) != 1) { |
dcf0ffdf | 4101 | SYSERROR("Failed telling child process \"%d\" to proceed", pid); |
f8aa4bf3 | 4102 | goto on_error; |
4355ab5f SH |
4103 | } |
4104 | ||
686dd5d1 | 4105 | on_error: |
4355ab5f SH |
4106 | if (p[0] != -1) |
4107 | close(p[0]); | |
4108 | close(p[1]); | |
f8aa4bf3 | 4109 | |
ee1b16bc TA |
4110 | /* Wait for child to finish. */ |
4111 | if (pid > 0) | |
4112 | status = wait_for_pid(pid); | |
4113 | ||
686dd5d1 CB |
4114 | if (status < 0) |
4115 | ret = -1; | |
4116 | ||
f8aa4bf3 | 4117 | return ret; |
4355ab5f | 4118 | } |
97e9cfa0 | 4119 | |
415a8851 CB |
4120 | int userns_exec_full(struct lxc_conf *conf, int (*fn)(void *), void *data, |
4121 | const char *fn_name) | |
4122 | { | |
4123 | pid_t pid; | |
4124 | uid_t euid, egid; | |
415a8851 CB |
4125 | int p[2]; |
4126 | struct id_map *map; | |
4127 | struct lxc_list *cur; | |
0fd73091 | 4128 | struct userns_fn_data d; |
415a8851 | 4129 | int ret = -1; |
0fd73091 | 4130 | char c = '1'; |
415a8851 CB |
4131 | struct lxc_list *idmap = NULL, *tmplist = NULL; |
4132 | struct id_map *container_root_uid = NULL, *container_root_gid = NULL, | |
4133 | *host_uid_map = NULL, *host_gid_map = NULL; | |
4134 | ||
2b2655a8 CB |
4135 | if (!conf) |
4136 | return -EINVAL; | |
4137 | ||
979f9e34 | 4138 | ret = pipe2(p, O_CLOEXEC); |
415a8851 CB |
4139 | if (ret < 0) { |
4140 | SYSERROR("opening pipe"); | |
4141 | return -1; | |
4142 | } | |
4143 | d.fn = fn; | |
4144 | d.fn_name = fn_name; | |
4145 | d.arg = data; | |
4146 | d.p[0] = p[0]; | |
4147 | d.p[1] = p[1]; | |
4148 | ||
4149 | /* Clone child in new user namespace. */ | |
33258b95 | 4150 | pid = lxc_clone(run_userns_fn, &d, CLONE_NEWUSER, NULL); |
415a8851 | 4151 | if (pid < 0) { |
0fd73091 | 4152 | ERROR("Failed to clone process in new user namespace"); |
415a8851 CB |
4153 | goto on_error; |
4154 | } | |
4155 | ||
4156 | close(p[0]); | |
4157 | p[0] = -1; | |
4158 | ||
4159 | euid = geteuid(); | |
4160 | egid = getegid(); | |
4161 | ||
4162 | /* Allocate new {g,u}id map list. */ | |
4163 | idmap = malloc(sizeof(*idmap)); | |
4164 | if (!idmap) | |
4165 | goto on_error; | |
4166 | lxc_list_init(idmap); | |
4167 | ||
4168 | /* Find container root. */ | |
0fd73091 | 4169 | lxc_list_for_each (cur, &conf->id_map) { |
415a8851 CB |
4170 | struct id_map *tmpmap; |
4171 | ||
4172 | tmplist = malloc(sizeof(*tmplist)); | |
4173 | if (!tmplist) | |
4174 | goto on_error; | |
4175 | ||
4176 | tmpmap = malloc(sizeof(*tmpmap)); | |
4177 | if (!tmpmap) { | |
4178 | free(tmplist); | |
4179 | goto on_error; | |
4180 | } | |
4181 | ||
4182 | memset(tmpmap, 0, sizeof(*tmpmap)); | |
4183 | memcpy(tmpmap, cur->elem, sizeof(*tmpmap)); | |
4184 | tmplist->elem = tmpmap; | |
4185 | ||
4186 | lxc_list_add_tail(idmap, tmplist); | |
4187 | ||
4188 | map = cur->elem; | |
4189 | ||
4190 | if (map->idtype == ID_TYPE_UID) | |
4191 | if (euid >= map->hostid && euid < map->hostid + map->range) | |
4192 | host_uid_map = map; | |
4193 | ||
4194 | if (map->idtype == ID_TYPE_GID) | |
4195 | if (egid >= map->hostid && egid < map->hostid + map->range) | |
4196 | host_gid_map = map; | |
4197 | ||
4198 | if (map->nsid != 0) | |
4199 | continue; | |
4200 | ||
4201 | if (map->idtype == ID_TYPE_UID) | |
4202 | if (container_root_uid == NULL) | |
4203 | container_root_uid = map; | |
4204 | ||
4205 | if (map->idtype == ID_TYPE_GID) | |
4206 | if (container_root_gid == NULL) | |
4207 | container_root_gid = map; | |
4208 | } | |
4209 | ||
4210 | if (!container_root_uid || !container_root_gid) { | |
4211 | ERROR("No mapping for container root found"); | |
4212 | goto on_error; | |
4213 | } | |
4214 | ||
4215 | /* Check whether the {g,u}id of the user has a mapping. */ | |
4216 | if (!host_uid_map) | |
c4333195 | 4217 | host_uid_map = mapped_hostid_add(conf, euid, ID_TYPE_UID); |
415a8851 CB |
4218 | else |
4219 | host_uid_map = container_root_uid; | |
4220 | ||
4221 | if (!host_gid_map) | |
c4333195 | 4222 | host_gid_map = mapped_hostid_add(conf, egid, ID_TYPE_GID); |
415a8851 CB |
4223 | else |
4224 | host_gid_map = container_root_gid; | |
4225 | ||
4226 | if (!host_uid_map) { | |
4227 | DEBUG("Failed to find mapping for uid %d", euid); | |
4228 | goto on_error; | |
4229 | } | |
4230 | ||
4231 | if (!host_gid_map) { | |
4232 | DEBUG("Failed to find mapping for gid %d", egid); | |
4233 | goto on_error; | |
4234 | } | |
4235 | ||
4236 | if (host_uid_map && (host_uid_map != container_root_uid)) { | |
4237 | /* Add container root to the map. */ | |
4238 | tmplist = malloc(sizeof(*tmplist)); | |
4239 | if (!tmplist) | |
4240 | goto on_error; | |
4241 | lxc_list_add_elem(tmplist, host_uid_map); | |
4242 | lxc_list_add_tail(idmap, tmplist); | |
4243 | } | |
4244 | /* idmap will now keep track of that memory. */ | |
4245 | host_uid_map = NULL; | |
4246 | ||
4247 | if (host_gid_map && (host_gid_map != container_root_gid)) { | |
4248 | tmplist = malloc(sizeof(*tmplist)); | |
4249 | if (!tmplist) | |
4250 | goto on_error; | |
4251 | lxc_list_add_elem(tmplist, host_gid_map); | |
4252 | lxc_list_add_tail(idmap, tmplist); | |
4253 | } | |
4254 | /* idmap will now keep track of that memory. */ | |
4255 | host_gid_map = NULL; | |
4256 | ||
4257 | if (lxc_log_get_level() == LXC_LOG_LEVEL_TRACE || | |
4258 | conf->loglevel == LXC_LOG_LEVEL_TRACE) { | |
0fd73091 | 4259 | lxc_list_for_each (cur, idmap) { |
415a8851 CB |
4260 | map = cur->elem; |
4261 | TRACE("establishing %cid mapping for \"%d\" in new " | |
4262 | "user namespace: nsuid %lu - hostid %lu - range " | |
4263 | "%lu", | |
4264 | (map->idtype == ID_TYPE_UID) ? 'u' : 'g', pid, | |
4265 | map->nsid, map->hostid, map->range); | |
4266 | } | |
4267 | } | |
4268 | ||
4269 | /* Set up {g,u}id mapping for user namespace of child process. */ | |
4270 | ret = lxc_map_ids(idmap, pid); | |
4271 | if (ret < 0) { | |
0fd73091 | 4272 | ERROR("error setting up {g,u}id mappings for child process \"%d\"", pid); |
415a8851 CB |
4273 | goto on_error; |
4274 | } | |
4275 | ||
4276 | /* Tell child to proceed. */ | |
489f39be | 4277 | if (lxc_write_nointr(p[1], &c, 1) != 1) { |
0fd73091 | 4278 | SYSERROR("Failed telling child process \"%d\" to proceed", pid); |
415a8851 CB |
4279 | goto on_error; |
4280 | } | |
4281 | ||
686dd5d1 | 4282 | on_error: |
ee1b16bc TA |
4283 | if (p[0] != -1) |
4284 | close(p[0]); | |
4285 | close(p[1]); | |
4286 | ||
415a8851 | 4287 | /* Wait for child to finish. */ |
686dd5d1 CB |
4288 | if (pid > 0) |
4289 | ret = wait_for_pid(pid); | |
415a8851 | 4290 | |
80758b4b | 4291 | if (idmap) { |
415a8851 | 4292 | lxc_free_idmap(idmap); |
80758b4b DJ |
4293 | free(idmap); |
4294 | } | |
4295 | ||
415a8851 CB |
4296 | if (host_uid_map && (host_uid_map != container_root_uid)) |
4297 | free(host_uid_map); | |
4298 | if (host_gid_map && (host_gid_map != container_root_gid)) | |
4299 | free(host_gid_map); | |
4300 | ||
415a8851 CB |
4301 | return ret; |
4302 | } | |
4303 | ||
a96a8e8c | 4304 | /* not thread-safe, do not use from api without first forking */ |
0fd73091 | 4305 | static char *getuname(void) |
97e9cfa0 | 4306 | { |
4f410b2a | 4307 | __do_free char *buf = NULL; |
cb7aa5e8 DJ |
4308 | struct passwd pwent; |
4309 | struct passwd *pwentp = NULL; | |
cb7aa5e8 DJ |
4310 | size_t bufsize; |
4311 | int ret; | |
97e9cfa0 | 4312 | |
cb7aa5e8 DJ |
4313 | bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); |
4314 | if (bufsize == -1) | |
4315 | bufsize = 1024; | |
4316 | ||
4317 | buf = malloc(bufsize); | |
4318 | if (!buf) | |
97e9cfa0 SH |
4319 | return NULL; |
4320 | ||
cb7aa5e8 DJ |
4321 | ret = getpwuid_r(geteuid(), &pwent, buf, bufsize, &pwentp); |
4322 | if (!pwentp) { | |
4323 | if (ret == 0) | |
4324 | WARN("Could not find matched password record."); | |
4325 | ||
55022530 | 4326 | return log_error(NULL, "Failed to get password record - %u", geteuid()); |
cb7aa5e8 DJ |
4327 | } |
4328 | ||
4f410b2a | 4329 | return strdup(pwent.pw_name); |
97e9cfa0 SH |
4330 | } |
4331 | ||
a96a8e8c | 4332 | /* not thread-safe, do not use from api without first forking */ |
97e9cfa0 SH |
4333 | static char *getgname(void) |
4334 | { | |
4f410b2a | 4335 | __do_free char *buf = NULL; |
3de9fb4c DJ |
4336 | struct group grent; |
4337 | struct group *grentp = NULL; | |
3de9fb4c DJ |
4338 | size_t bufsize; |
4339 | int ret; | |
4340 | ||
4341 | bufsize = sysconf(_SC_GETGR_R_SIZE_MAX); | |
4342 | if (bufsize == -1) | |
4343 | bufsize = 1024; | |
4344 | ||
4345 | buf = malloc(bufsize); | |
4346 | if (!buf) | |
4347 | return NULL; | |
4348 | ||
4349 | ret = getgrgid_r(getegid(), &grent, buf, bufsize, &grentp); | |
4350 | if (!grentp) { | |
4351 | if (ret == 0) | |
4352 | WARN("Could not find matched group record"); | |
97e9cfa0 | 4353 | |
55022530 | 4354 | return log_error(NULL, "Failed to get group record - %u", getegid()); |
3de9fb4c DJ |
4355 | } |
4356 | ||
4f410b2a | 4357 | return strdup(grent.gr_name); |
97e9cfa0 SH |
4358 | } |
4359 | ||
a96a8e8c | 4360 | /* not thread-safe, do not use from api without first forking */ |
97e9cfa0 SH |
4361 | void suggest_default_idmap(void) |
4362 | { | |
3a6e3bf5 | 4363 | __do_free char *gname = NULL, *line = NULL, *uname = NULL; |
4aae564f | 4364 | __do_fclose FILE *subuid_f = NULL, *subgid_f = NULL; |
97e9cfa0 | 4365 | unsigned int uid = 0, urange = 0, gid = 0, grange = 0; |
97e9cfa0 SH |
4366 | size_t len = 0; |
4367 | ||
0fd73091 CB |
4368 | uname = getuname(); |
4369 | if (!uname) | |
97e9cfa0 SH |
4370 | return; |
4371 | ||
0fd73091 | 4372 | gname = getgname(); |
3a6e3bf5 | 4373 | if (!gname) |
97e9cfa0 | 4374 | return; |
97e9cfa0 | 4375 | |
4110345b | 4376 | subuid_f = fopen(subuidfile, "re"); |
4aae564f | 4377 | if (!subuid_f) { |
97e9cfa0 | 4378 | ERROR("Your system is not configured with subuids"); |
97e9cfa0 SH |
4379 | return; |
4380 | } | |
0fd73091 | 4381 | |
4aae564f | 4382 | while (getline(&line, &len, subuid_f) != -1) { |
0fd73091 | 4383 | char *p, *p2; |
b7930180 | 4384 | size_t no_newline = 0; |
0fd73091 CB |
4385 | |
4386 | p = strchr(line, ':'); | |
97e9cfa0 SH |
4387 | if (*line == '#') |
4388 | continue; | |
4389 | if (!p) | |
4390 | continue; | |
4391 | *p = '\0'; | |
4392 | p++; | |
0fd73091 | 4393 | |
97e9cfa0 SH |
4394 | if (strcmp(line, uname)) |
4395 | continue; | |
0fd73091 | 4396 | |
97e9cfa0 SH |
4397 | p2 = strchr(p, ':'); |
4398 | if (!p2) | |
4399 | continue; | |
4400 | *p2 = '\0'; | |
4401 | p2++; | |
4402 | if (!*p2) | |
4403 | continue; | |
b7930180 CB |
4404 | no_newline = strcspn(p2, "\n"); |
4405 | p2[no_newline] = '\0'; | |
4406 | ||
b7b2fde4 | 4407 | if (lxc_safe_uint(p, &uid) < 0) |
0fd73091 | 4408 | WARN("Could not parse UID"); |
b7b2fde4 | 4409 | if (lxc_safe_uint(p2, &urange) < 0) |
0fd73091 | 4410 | WARN("Could not parse UID range"); |
97e9cfa0 | 4411 | } |
97e9cfa0 | 4412 | |
4110345b | 4413 | subgid_f = fopen(subgidfile, "re"); |
4aae564f | 4414 | if (!subgid_f) { |
97e9cfa0 | 4415 | ERROR("Your system is not configured with subgids"); |
97e9cfa0 SH |
4416 | return; |
4417 | } | |
0fd73091 | 4418 | |
4aae564f | 4419 | while (getline(&line, &len, subgid_f) != -1) { |
0fd73091 | 4420 | char *p, *p2; |
b7930180 | 4421 | size_t no_newline = 0; |
0fd73091 CB |
4422 | |
4423 | p = strchr(line, ':'); | |
97e9cfa0 SH |
4424 | if (*line == '#') |
4425 | continue; | |
4426 | if (!p) | |
4427 | continue; | |
4428 | *p = '\0'; | |
4429 | p++; | |
0fd73091 | 4430 | |
97e9cfa0 SH |
4431 | if (strcmp(line, uname)) |
4432 | continue; | |
0fd73091 | 4433 | |
97e9cfa0 SH |
4434 | p2 = strchr(p, ':'); |
4435 | if (!p2) | |
4436 | continue; | |
4437 | *p2 = '\0'; | |
4438 | p2++; | |
4439 | if (!*p2) | |
4440 | continue; | |
b7930180 CB |
4441 | no_newline = strcspn(p2, "\n"); |
4442 | p2[no_newline] = '\0'; | |
4443 | ||
b7b2fde4 | 4444 | if (lxc_safe_uint(p, &gid) < 0) |
0fd73091 | 4445 | WARN("Could not parse GID"); |
b7b2fde4 | 4446 | if (lxc_safe_uint(p2, &grange) < 0) |
0fd73091 | 4447 | WARN("Could not parse GID range"); |
97e9cfa0 | 4448 | } |
97e9cfa0 | 4449 | |
97e9cfa0 SH |
4450 | if (!urange || !grange) { |
4451 | ERROR("You do not have subuids or subgids allocated"); | |
4452 | ERROR("Unprivileged containers require subuids and subgids"); | |
4453 | return; | |
4454 | } | |
4455 | ||
4456 | ERROR("You must either run as root, or define uid mappings"); | |
4457 | ERROR("To pass uid mappings to lxc-create, you could create"); | |
4458 | ERROR("~/.config/lxc/default.conf:"); | |
4459 | ERROR("lxc.include = %s", LXC_DEFAULT_CONFIG); | |
bdcbb6b3 CB |
4460 | ERROR("lxc.idmap = u 0 %u %u", uid, urange); |
4461 | ERROR("lxc.idmap = g 0 %u %u", gid, grange); | |
97e9cfa0 | 4462 | } |
aaf26830 | 4463 | |
a7307747 SH |
4464 | static void free_cgroup_settings(struct lxc_list *result) |
4465 | { | |
4466 | struct lxc_list *iterator, *next; | |
4467 | ||
0fd73091 | 4468 | lxc_list_for_each_safe (iterator, result, next) { |
a7307747 | 4469 | lxc_list_del(iterator); |
55022530 | 4470 | free_disarm(iterator); |
a7307747 | 4471 | } |
55022530 | 4472 | free_disarm(result); |
a7307747 SH |
4473 | } |
4474 | ||
0fd73091 | 4475 | /* Return the list of cgroup_settings sorted according to the following rules |
aaf26830 KT |
4476 | * 1. Put memory.limit_in_bytes before memory.memsw.limit_in_bytes |
4477 | */ | |
0fd73091 | 4478 | struct lxc_list *sort_cgroup_settings(struct lxc_list *cgroup_settings) |
aaf26830 KT |
4479 | { |
4480 | struct lxc_list *result; | |
aaf26830 | 4481 | struct lxc_cgroup *cg = NULL; |
0fd73091 | 4482 | struct lxc_list *it = NULL, *item = NULL, *memsw_limit = NULL; |
aaf26830 KT |
4483 | |
4484 | result = malloc(sizeof(*result)); | |
0fd73091 | 4485 | if (!result) |
fac7c663 | 4486 | return NULL; |
aaf26830 KT |
4487 | lxc_list_init(result); |
4488 | ||
0fd73091 CB |
4489 | /* Iterate over the cgroup settings and copy them to the output list. */ |
4490 | lxc_list_for_each (it, cgroup_settings) { | |
aaf26830 | 4491 | item = malloc(sizeof(*item)); |
fac7c663 | 4492 | if (!item) { |
a7307747 | 4493 | free_cgroup_settings(result); |
fac7c663 KT |
4494 | return NULL; |
4495 | } | |
0fd73091 | 4496 | |
aaf26830 KT |
4497 | item->elem = it->elem; |
4498 | cg = it->elem; | |
4499 | if (strcmp(cg->subsystem, "memory.memsw.limit_in_bytes") == 0) { | |
4500 | /* Store the memsw_limit location */ | |
4501 | memsw_limit = item; | |
0fd73091 CB |
4502 | } else if (strcmp(cg->subsystem, "memory.limit_in_bytes") == 0 && |
4503 | memsw_limit != NULL) { | |
4504 | /* lxc.cgroup.memory.memsw.limit_in_bytes is found | |
4505 | * before lxc.cgroup.memory.limit_in_bytes, swap these | |
4506 | * two items */ | |
aaf26830 KT |
4507 | item->elem = memsw_limit->elem; |
4508 | memsw_limit->elem = it->elem; | |
4509 | } | |
4510 | lxc_list_add_tail(result, item); | |
4511 | } | |
4512 | ||
4513 | return result; | |
a7307747 | 4514 | } |