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