jail: call chdir(/) after pivot_root()
[project/procd.git] / jail / jail.c
1 /*
2 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14 #define _GNU_SOURCE
15 #include <sys/mount.h>
16 #include <sys/prctl.h>
17 #include <sys/wait.h>
18
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <libgen.h>
26 #include <sched.h>
27 #include <linux/limits.h>
28
29 #include "capabilities.h"
30 #include "elf.h"
31 #include "fs.h"
32 #include "jail.h"
33 #include "log.h"
34
35 #include <libubox/uloop.h>
36
37 #define STACK_SIZE (1024 * 1024)
38 #define OPT_ARGS "S:C:n:h:r:w:d:psuloc"
39
40 static struct {
41 char *name;
42 char *hostname;
43 char **jail_argv;
44 char *seccomp;
45 char *capabilities;
46 int no_new_privs;
47 int namespace;
48 int procfs;
49 int ronly;
50 int sysfs;
51 } opts;
52
53 extern int pivot_root(const char *new_root, const char *put_old);
54
55 int debug = 0;
56
57 static char child_stack[STACK_SIZE];
58
59 static int mkdir_p(char *dir, mode_t mask)
60 {
61 char *l = strrchr(dir, '/');
62 int ret;
63
64 if (!l)
65 return 0;
66
67 *l = '\0';
68
69 if (mkdir_p(dir, mask))
70 return -1;
71
72 *l = '/';
73
74 ret = mkdir(dir, mask);
75 if (ret && errno == EEXIST)
76 return 0;
77
78 if (ret)
79 ERROR("mkdir failed on %s: %s\n", dir, strerror(errno));
80
81 return ret;
82 }
83
84 int mount_bind(const char *root, const char *path, int readonly, int error)
85 {
86 struct stat s;
87 char new[PATH_MAX];
88 int fd;
89
90 if (stat(path, &s)) {
91 ERROR("stat(%s) failed: %s\n", path, strerror(errno));
92 return error;
93 }
94
95 snprintf(new, sizeof(new), "%s%s", root, path);
96 if (S_ISDIR(s.st_mode)) {
97 mkdir_p(new, 0755);
98 } else {
99 mkdir_p(dirname(new), 0755);
100 snprintf(new, sizeof(new), "%s%s", root, path);
101 fd = creat(new, 0644);
102 if (fd == -1) {
103 ERROR("failed to create %s: %s\n", new, strerror(errno));
104 return -1;
105 }
106 close(fd);
107 }
108
109 if (mount(path, new, NULL, MS_BIND, NULL)) {
110 ERROR("failed to mount -B %s %s: %s\n", path, new, strerror(errno));
111 return -1;
112 }
113
114 if (readonly && mount(NULL, new, NULL, MS_BIND | MS_REMOUNT | MS_RDONLY, NULL)) {
115 ERROR("failed to remount ro %s: %s\n", new, strerror(errno));
116 return -1;
117 }
118
119 DEBUG("mount -B %s %s\n", path, new);
120
121 return 0;
122 }
123
124 static int build_jail_fs(void)
125 {
126 char jail_root[] = "/tmp/ujail-XXXXXX";
127 if (mkdtemp(jail_root) == NULL) {
128 ERROR("mkdtemp(jail_root) failed: %s\n", strerror(errno));
129 return -1;
130 }
131
132 if (mount("tmpfs", jail_root, "tmpfs", MS_NOATIME, "mode=0755")) {
133 ERROR("tmpfs mount failed %s\n", strerror(errno));
134 return -1;
135 }
136
137 if (chdir(jail_root)) {
138 ERROR("failed to chdir() in the jail root\n");
139 return -1;
140 }
141
142 if (add_path_and_deps(*opts.jail_argv, 1, -1, 0)) {
143 ERROR("failed to load dependencies\n");
144 return -1;
145 }
146
147 if (opts.seccomp && add_path_and_deps("libpreload-seccomp.so", 1, -1, 1)) {
148 ERROR("failed to load libpreload-seccomp.so\n");
149 return -1;
150 }
151
152 if (mount_all(jail_root)) {
153 ERROR("mount_all() failed\n");
154 return -1;
155 }
156
157 char dirbuf[sizeof(jail_root) + 4];
158 snprintf(dirbuf, sizeof(dirbuf), "%s/old", jail_root);
159 mkdir(dirbuf, 0755);
160
161 if (pivot_root(jail_root, dirbuf) == -1) {
162 ERROR("pivot_root failed: %s\n", strerror(errno));
163 return -1;
164 }
165 if (chdir("/")) {
166 ERROR("chdir(/) failed: %s\n", strerror(errno));
167 return -1;
168 }
169
170 snprintf(dirbuf, sizeof(dirbuf), "/old%s", jail_root);
171 rmdir(dirbuf);
172 umount2("/old", MNT_DETACH);
173 rmdir("/old");
174
175 if (opts.procfs) {
176 mkdir("/proc", 0755);
177 mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
178 }
179 if (opts.sysfs) {
180 mkdir("/sys", 0755);
181 mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
182 }
183 if (opts.ronly)
184 mount(NULL, "/", NULL, MS_RDONLY | MS_REMOUNT, 0);
185
186 return 0;
187 }
188
189 #define MAX_ENVP 8
190 static char** build_envp(const char *seccomp)
191 {
192 static char *envp[MAX_ENVP];
193 static char preload_var[PATH_MAX];
194 static char seccomp_var[PATH_MAX];
195 static char debug_var[] = "LD_DEBUG=all";
196 const char *preload_lib = find_lib("libpreload-seccomp.so");
197 int count = 0;
198
199 if (seccomp && !preload_lib) {
200 ERROR("failed to add preload-lib to env\n");
201 return NULL;
202 }
203 if (seccomp) {
204 snprintf(seccomp_var, sizeof(seccomp_var), "SECCOMP_FILE=%s", seccomp);
205 envp[count++] = seccomp_var;
206 snprintf(preload_var, sizeof(preload_var), "LD_PRELOAD=%s", preload_lib);
207 envp[count++] = preload_var;
208 }
209 if (debug > 1)
210 envp[count++] = debug_var;
211
212 return envp;
213 }
214
215 static void usage(void)
216 {
217 fprintf(stderr, "ujail <options> -- <binary> <params ...>\n");
218 fprintf(stderr, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
219 fprintf(stderr, " -S <file>\tseccomp filter config\n");
220 fprintf(stderr, " -C <file>\tcapabilities drop config\n");
221 fprintf(stderr, " -c\t\tset PR_SET_NO_NEW_PRIVS\n");
222 fprintf(stderr, " -n <name>\tthe name of the jail\n");
223 fprintf(stderr, "namespace jail options:\n");
224 fprintf(stderr, " -h <hostname>\tchange the hostname of the jail\n");
225 fprintf(stderr, " -r <file>\treadonly files that should be staged\n");
226 fprintf(stderr, " -w <file>\twriteable files that should be staged\n");
227 fprintf(stderr, " -p\t\tjail has /proc\n");
228 fprintf(stderr, " -s\t\tjail has /sys\n");
229 fprintf(stderr, " -l\t\tjail has /dev/log\n");
230 fprintf(stderr, " -u\t\tjail has a ubus socket\n");
231 fprintf(stderr, " -o\t\tremont jail root (/) read only\n");
232 fprintf(stderr, "\nWarning: by default root inside the jail is the same\n\
233 and he has the same powers as root outside the jail,\n\
234 thus he can escape the jail and/or break stuff.\n\
235 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
236 If you use none of the namespace jail options,\n\
237 ujail will not use namespace/build a jail,\n\
238 and will only drop capabilities/apply seccomp filter.\n\n");
239 }
240
241 static int exec_jail(void)
242 {
243 char **envp = build_envp(opts.seccomp);
244 if (!envp)
245 exit(EXIT_FAILURE);
246
247 if (opts.capabilities && drop_capabilities(opts.capabilities))
248 exit(EXIT_FAILURE);
249
250 if (opts.no_new_privs && prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
251 ERROR("prctl(PR_SET_NO_NEW_PRIVS) failed: %s\n", strerror(errno));
252 exit(EXIT_FAILURE);
253 }
254
255 INFO("exec-ing %s\n", *opts.jail_argv);
256 execve(*opts.jail_argv, opts.jail_argv, envp);
257 /* we get there only if execve fails */
258 ERROR("failed to execve %s: %s\n", *opts.jail_argv, strerror(errno));
259 exit(EXIT_FAILURE);
260 }
261
262 static int spawn_jail(void *_notused)
263 {
264 if (opts.hostname && sethostname(opts.hostname, strlen(opts.hostname))) {
265 ERROR("sethostname(%s) failed: %s\n", opts.hostname, strerror(errno));
266 }
267
268 if (build_jail_fs()) {
269 ERROR("failed to build jail fs");
270 exit(EXIT_FAILURE);
271 }
272
273 return exec_jail();
274 }
275
276 static int jail_running = 1;
277 static int jail_return_code = 0;
278
279 static void jail_process_handler(struct uloop_process *c, int ret)
280 {
281 if (WIFEXITED(ret)) {
282 jail_return_code = WEXITSTATUS(ret);
283 INFO("jail (%d) exited with exit: %d\n", c->pid, jail_return_code);
284 } else {
285 jail_return_code = WTERMSIG(ret);
286 INFO("jail (%d) exited with signal: %d\n", c->pid, jail_return_code);
287 }
288 jail_running = 0;
289 uloop_end();
290 }
291
292 static struct uloop_process jail_process = {
293 .cb = jail_process_handler,
294 };
295
296 int main(int argc, char **argv)
297 {
298 uid_t uid = getuid();
299 char log[] = "/dev/log";
300 char ubus[] = "/var/run/ubus.sock";
301 int ch;
302
303 if (uid) {
304 ERROR("not root, aborting: %s\n", strerror(errno));
305 return EXIT_FAILURE;
306 }
307
308 umask(022);
309 mount_list_init();
310 init_library_search();
311
312 while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
313 switch (ch) {
314 case 'd':
315 debug = atoi(optarg);
316 break;
317 case 'p':
318 opts.namespace = 1;
319 opts.procfs = 1;
320 break;
321 case 'o':
322 opts.namespace = 1;
323 opts.ronly = 1;
324 break;
325 case 's':
326 opts.namespace = 1;
327 opts.sysfs = 1;
328 break;
329 case 'S':
330 opts.seccomp = optarg;
331 add_mount(optarg, 1, -1);
332 break;
333 case 'C':
334 opts.capabilities = optarg;
335 add_mount(optarg, 1, -1);
336 break;
337 case 'c':
338 opts.no_new_privs = 1;
339 break;
340 case 'n':
341 opts.name = optarg;
342 break;
343 case 'h':
344 opts.hostname = optarg;
345 break;
346 case 'r':
347 opts.namespace = 1;
348 add_path_and_deps(optarg, 1, 0, 0);
349 break;
350 case 'w':
351 opts.namespace = 1;
352 add_path_and_deps(optarg, 0, 0, 0);
353 break;
354 case 'u':
355 opts.namespace = 1;
356 add_mount(ubus, 0, -1);
357 break;
358 case 'l':
359 opts.namespace = 1;
360 add_mount(log, 0, -1);
361 break;
362 }
363 }
364
365 /* no <binary> param found */
366 if (argc - optind < 1) {
367 usage();
368 return EXIT_FAILURE;
369 }
370 if (!(opts.namespace||opts.capabilities||opts.seccomp)) {
371 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
372 usage();
373 return EXIT_FAILURE;
374 }
375 DEBUG("Using namespaces(%d), capabilities(%d), seccomp(%d)\n",
376 opts.namespace,
377 opts.capabilities != 0,
378 opts.seccomp != 0);
379
380 opts.jail_argv = &argv[optind];
381
382 if (opts.name)
383 prctl(PR_SET_NAME, opts.name, NULL, NULL, NULL);
384
385 uloop_init();
386 if (opts.namespace) {
387 jail_process.pid = clone(spawn_jail,
388 child_stack + STACK_SIZE,
389 CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWIPC | SIGCHLD, NULL);
390 } else {
391 jail_process.pid = fork();
392 }
393
394 if (jail_process.pid > 0) {
395 /* parent process */
396 uloop_process_add(&jail_process);
397 uloop_run();
398 uloop_done();
399 if (jail_running) {
400 DEBUG("uloop interrupted, killing jail process\n");
401 kill(jail_process.pid, SIGTERM);
402 waitpid(jail_process.pid, NULL, 0);
403 }
404 return jail_return_code;
405 } else if (jail_process.pid == 0) {
406 /* fork child process */
407 return exec_jail();
408 } else {
409 ERROR("failed to clone/fork: %s\n", strerror(errno));
410 return EXIT_FAILURE;
411 }
412 }