jail: Add MS_NODEV MS_NOEXEC MS_NOSUID mount options where needed
[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 <values.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <libgen.h>
28 #include <sched.h>
29
30 #include "elf.h"
31 #include "capabilities.h"
32
33 #include <libubox/list.h>
34 #include <libubox/uloop.h>
35
36 #define STACK_SIZE (1024 * 1024)
37 #define OPT_ARGS "P:S:C:n:r:w:d:psulo"
38
39 static struct {
40 char *path;
41 char *name;
42 char **jail_argv;
43 char *seccomp;
44 char *capabilities;
45 int namespace;
46 int procfs;
47 int ronly;
48 int sysfs;
49 } opts;
50
51 struct extra {
52 struct list_head list;
53
54 const char *path;
55 const char *name;
56 int readonly;
57 };
58
59 static LIST_HEAD(extras);
60
61 extern int pivot_root(const char *new_root, const char *put_old);
62
63 int debug = 0;
64
65 static char child_stack[STACK_SIZE];
66
67 static int mkdir_p(char *dir, mode_t mask)
68 {
69 char *l = strrchr(dir, '/');
70 int ret;
71
72 if (!l)
73 return 0;
74
75 *l = '\0';
76
77 if (mkdir_p(dir, mask))
78 return -1;
79
80 *l = '/';
81
82 ret = mkdir(dir, mask);
83 if (ret && errno == EEXIST)
84 return 0;
85
86 if (ret)
87 ERROR("mkdir failed on %s: %s\n", dir, strerror(errno));
88
89 return ret;
90 }
91
92 static int mount_bind(const char *root, const char *path, const char *name, int readonly, int error)
93 {
94 const char *p = path;
95 struct stat s;
96 char old[256];
97 char new[256];
98 int fd;
99
100 if (strstr(p, "local"))
101 p = "/lib";
102
103 snprintf(old, sizeof(old), "%s/%s", path, name);
104 snprintf(new, sizeof(new), "%s%s", root, p);
105
106 mkdir_p(new, 0755);
107
108 snprintf(new, sizeof(new), "%s%s/%s", root, p, name);
109
110 if (stat(old, &s)) {
111 ERROR("%s does not exist\n", old);
112 return error;
113 }
114
115 if (S_ISDIR(s.st_mode)) {
116 mkdir_p(new, 0755);
117 } else {
118 fd = creat(new, 0644);
119 if (fd == -1) {
120 ERROR("failed to create %s: %s\n", new, strerror(errno));
121 return -1;
122 }
123 close(fd);
124 }
125
126 if (mount(old, new, NULL, MS_BIND, NULL)) {
127 ERROR("failed to mount -B %s %s: %s\n", old, new, strerror(errno));
128 return -1;
129 }
130
131 if (readonly && mount(NULL, new, NULL, MS_BIND | MS_REMOUNT | MS_RDONLY, NULL)) {
132 ERROR("failed to remount ro %s: %s\n", new, strerror(errno));
133 return -1;
134 }
135
136 DEBUG("mount -B %s %s\n", old, new);
137
138 return 0;
139 }
140
141 static int build_jail_fs()
142 {
143 struct library *l;
144 struct extra *m;
145
146 if (mount("tmpfs", opts.path, "tmpfs", MS_NOATIME, "mode=0755")) {
147 ERROR("tmpfs mount failed %s\n", strerror(errno));
148 return -1;
149 }
150
151 if (chdir(opts.path)) {
152 ERROR("failed to chdir() in the jail root\n");
153 return -1;
154 }
155
156 avl_init(&libraries, avl_strcmp, false, NULL);
157 alloc_library_path("/lib64");
158 alloc_library_path("/lib");
159 alloc_library_path("/usr/lib");
160 load_ldso_conf("/etc/ld.so.conf");
161
162 if (elf_load_deps(*opts.jail_argv)) {
163 ERROR("failed to load dependencies\n");
164 return -1;
165 }
166
167 if (opts.seccomp && elf_load_deps("libpreload-seccomp.so")) {
168 ERROR("failed to load libpreload-seccomp.so\n");
169 return -1;
170 }
171
172 avl_for_each_element(&libraries, l, avl)
173 if (mount_bind(opts.path, l->path, l->name, 1, -1))
174 return -1;
175
176 list_for_each_entry(m, &extras, list)
177 if (mount_bind(opts.path, m->path, m->name, m->readonly, 0))
178 return -1;
179
180 char *mpoint;
181 if (asprintf(&mpoint, "%s/old", opts.path) < 0) {
182 ERROR("failed to alloc pivot path: %s\n", strerror(errno));
183 return -1;
184 }
185 mkdir_p(mpoint, 0755);
186 if (pivot_root(opts.path, mpoint) == -1) {
187 ERROR("pivot_root failed:%s\n", strerror(errno));
188 free(mpoint);
189 return -1;
190 }
191 free(mpoint);
192 umount2("/old", MNT_DETACH);
193 rmdir("/old");
194 if (opts.procfs) {
195 mkdir("/proc", 0755);
196 mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
197 }
198 if (opts.sysfs) {
199 mkdir("/sys", 0755);
200 mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
201 }
202 if (opts.ronly)
203 mount(NULL, "/", NULL, MS_RDONLY | MS_REMOUNT, 0);
204
205 return 0;
206 }
207
208 #define MAX_ENVP 8
209 static char** build_envp(const char *seccomp)
210 {
211 static char *envp[MAX_ENVP];
212 static char preload_var[64];
213 static char seccomp_var[64];
214 static char debug_var[] = "LD_DEBUG=all";
215 char *preload_lib = find_lib("libpreload-seccomp.so");
216 int count = 0;
217
218 if (seccomp && !preload_lib) {
219 ERROR("failed to add preload-lib to env\n");
220 return NULL;
221 }
222 if (seccomp) {
223 snprintf(seccomp_var, sizeof(seccomp_var), "SECCOMP_FILE=%s", seccomp);
224 envp[count++] = seccomp_var;
225 snprintf(preload_var, sizeof(preload_var), "LD_PRELOAD=%s", preload_lib);
226 envp[count++] = preload_var;
227 }
228 if (debug > 1)
229 envp[count++] = debug_var;
230
231 return envp;
232 }
233
234 static void usage(void)
235 {
236 fprintf(stderr, "ujail <options> -- <binary> <params ...>\n");
237 fprintf(stderr, " -d <num>\tshow debug log (increase num to increase verbosity)\n");
238 fprintf(stderr, " -S <file>\tseccomp filter config\n");
239 fprintf(stderr, " -C <file>\tcapabilities drop config\n");
240 fprintf(stderr, " -n <name>\tthe name of the jail\n");
241 fprintf(stderr, "namespace jail options:\n");
242 fprintf(stderr, " -P <path>\tpath where the jail will be staged\n");
243 fprintf(stderr, " -r <file>\treadonly files that should be staged\n");
244 fprintf(stderr, " -w <file>\twriteable files that should be staged\n");
245 fprintf(stderr, " -p\t\tjail has /proc\n");
246 fprintf(stderr, " -s\t\tjail has /sys\n");
247 fprintf(stderr, " -l\t\tjail has /dev/log\n");
248 fprintf(stderr, " -u\t\tjail has a ubus socket\n");
249 fprintf(stderr, " -o\t\tremont jail root (/) read only\n");
250 fprintf(stderr, "\nWarning: by default root inside the jail is the same\n\
251 and he has the same powers as root outside the jail,\n\
252 thus he can escape the jail and/or break stuff.\n\
253 Please use seccomp/capabilities (-S/-C) to restrict his powers\n\n\
254 If you use none of the namespace jail options,\n\
255 ujail will not use namespace/build a jail,\n\
256 and will only drop capabilities/apply seccomp filter.\n\n");
257 }
258
259 static int exec_jail()
260 {
261 char **envp = build_envp(opts.seccomp);
262 if (!envp)
263 exit(EXIT_FAILURE);
264
265 if (opts.capabilities && drop_capabilities(opts.capabilities))
266 exit(EXIT_FAILURE);
267
268 INFO("exec-ing %s\n", *opts.jail_argv);
269 execve(*opts.jail_argv, opts.jail_argv, envp);
270 //we get there only if execve fails
271 ERROR("failed to execve %s: %s\n", *opts.jail_argv, strerror(errno));
272 exit(EXIT_FAILURE);
273 }
274
275 static int spawn_jail(void *arg)
276 {
277 if (opts.name && sethostname(opts.name, strlen(opts.name))) {
278 ERROR("failed to sethostname: %s\n", strerror(errno));
279 }
280
281 if (build_jail_fs()) {
282 ERROR("failed to build jail fs");
283 exit(EXIT_FAILURE);
284 }
285
286 return exec_jail();
287 }
288
289 static int jail_running = 1;
290 static int jail_return_code = 0;
291
292 static void jail_process_handler(struct uloop_process *c, int ret)
293 {
294 if (WIFEXITED(ret)) {
295 jail_return_code = WEXITSTATUS(ret);
296 INFO("jail (%d) exited with exit: %d\n", c->pid, jail_return_code);
297 } else {
298 jail_return_code = WTERMSIG(ret);
299 INFO("jail (%d) exited with signal: %d\n", c->pid, jail_return_code);
300 }
301 jail_running = 0;
302 uloop_end();
303 }
304
305 static struct uloop_process jail_process = {
306 .cb = jail_process_handler,
307 };
308
309 static void add_extra(char *name, int readonly)
310 {
311 struct extra *f;
312
313 if (*name != '/') {
314 ERROR("%s is not an absolute path\n", name);
315 return;
316 }
317
318 f = calloc(1, sizeof(struct extra));
319
320 f->name = basename(name);
321 f->path = dirname(strdup(name));
322 f->readonly = readonly;
323
324 list_add_tail(&f->list, &extras);
325 }
326
327 int main(int argc, char **argv)
328 {
329 uid_t uid = getuid();
330 char log[] = "/dev/log";
331 char ubus[] = "/var/run/ubus.sock";
332 int ret = EXIT_SUCCESS;
333 int ch;
334
335 if (uid) {
336 ERROR("not root, aborting: %s\n", strerror(errno));
337 return EXIT_FAILURE;
338 }
339
340 umask(022);
341
342 while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
343 switch (ch) {
344 case 'd':
345 debug = atoi(optarg);
346 break;
347 case 'p':
348 opts.namespace = 1;
349 opts.procfs = 1;
350 break;
351 case 'o':
352 opts.namespace = 1;
353 opts.ronly = 1;
354 break;
355 case 's':
356 opts.namespace = 1;
357 opts.sysfs = 1;
358 break;
359 case 'S':
360 opts.seccomp = optarg;
361 add_extra(optarg, 1);
362 break;
363 case 'C':
364 opts.capabilities = optarg;
365 add_extra(optarg, 1);
366 break;
367 case 'P':
368 opts.namespace = 1;
369 opts.path = optarg;
370 break;
371 case 'n':
372 opts.name = optarg;
373 break;
374 case 'r':
375 opts.namespace = 1;
376 add_extra(optarg, 1);
377 break;
378 case 'w':
379 opts.namespace = 1;
380 add_extra(optarg, 0);
381 break;
382 case 'u':
383 opts.namespace = 1;
384 add_extra(ubus, 0);
385 break;
386 case 'l':
387 opts.namespace = 1;
388 add_extra(log, 0);
389 break;
390 }
391 }
392
393 //no <binary> param found
394 if (argc - optind < 1) {
395 usage();
396 return EXIT_FAILURE;
397 }
398 if (!(opts.namespace||opts.capabilities||opts.seccomp)) {
399 ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
400 usage();
401 return EXIT_FAILURE;
402 }
403 DEBUG("Using namespaces(%d), capabilities(%d), seccomp(%d)\n",
404 opts.namespace,
405 opts.capabilities != 0,
406 opts.seccomp != 0);
407
408 opts.jail_argv = &argv[optind];
409
410 if (opts.name)
411 prctl(PR_SET_NAME, opts.name, NULL, NULL, NULL);
412
413 if (opts.namespace && !opts.path && asprintf(&opts.path, "/tmp/%s", basename(*opts.jail_argv)) == -1) {
414 ERROR("failed to asprintf root path: %s\n", strerror(errno));
415 return EXIT_FAILURE;
416 }
417
418 if (opts.namespace && mkdir(opts.path, 0755)) {
419 ERROR("unable to create root path: %s (%s)\n", opts.path, strerror(errno));
420 return EXIT_FAILURE;
421 }
422
423 uloop_init();
424 if (opts.namespace) {
425 jail_process.pid = clone(spawn_jail,
426 child_stack + STACK_SIZE,
427 CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWIPC | SIGCHLD, argv);
428 } else {
429 jail_process.pid = fork();
430 }
431
432 if (jail_process.pid > 0) {
433 //parent process
434 uloop_process_add(&jail_process);
435 uloop_run();
436 uloop_done();
437 if (jail_running) {
438 DEBUG("uloop interrupted, killing jail process\n");
439 kill(jail_process.pid, SIGTERM);
440 waitpid(jail_process.pid, NULL, 0);
441 }
442 } else if (jail_process.pid == 0) {
443 //fork child process
444 return exec_jail();
445 } else {
446 ERROR("failed to clone/fork: %s\n", strerror(errno));
447 ret = EXIT_FAILURE;
448 }
449
450 if (opts.namespace && rmdir(opts.path)) {
451 ERROR("Unable to remove root path: %s (%s)\n", opts.path, strerror(errno));
452 ret = EXIT_FAILURE;
453 }
454
455 if (ret)
456 return ret;
457
458 return jail_return_code;
459 }