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