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