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