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