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