jail: don't fail if maskedPath cannot be found
[project/procd.git] / jail / fs.c
1 /*
2 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
3 * Copyright (C) 2015 Etienne Champetier <champetier.etienne@gmail.com>
4 * Copyright (C) 2020 Daniel Golle <daniel@makrotopia.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version 2.1
8 * as published by the Free Software Foundation
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 #define _GNU_SOURCE
17
18 #include <assert.h>
19 #include <elf.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <linux/limits.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/mman.h>
28 #include <unistd.h>
29 #include <libgen.h>
30
31 #include <libubox/avl.h>
32 #include <libubox/avl-cmp.h>
33 #include <libubox/blobmsg.h>
34 #include <libubox/list.h>
35
36 #include "elf.h"
37 #include "fs.h"
38 #include "jail.h"
39 #include "log.h"
40
41 #define UJAIL_NOAFILE "/tmp/.ujailnoafile"
42
43 struct mount {
44 struct avl_node avl;
45 const char *source;
46 const char *target;
47 const char *filesystemtype;
48 unsigned long mountflags;
49 unsigned long propflags;
50 const char *optstr;
51 int error;
52 bool inner;
53 };
54
55 struct avl_tree mounts;
56
57 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(%s, %d) failed: %m\n", dir, mask);
78
79 return ret;
80 }
81
82 static int do_mount(const char *root, const char *orig_source, const char *target, const char *filesystemtype,
83 unsigned long orig_mountflags, unsigned long propflags, const char *optstr, int error, bool inner)
84 {
85 struct stat s;
86 char new[PATH_MAX];
87 char *source = (char *)orig_source;
88 int fd;
89 bool is_bind = (orig_mountflags & MS_BIND);
90 bool is_mask = (source == (void *)(-1));
91 unsigned long mountflags = orig_mountflags;
92
93 if (source && is_bind && stat(source, &s)) {
94 ERROR("stat(%s) failed: %m\n", source);
95 return error;
96 }
97
98 if (!is_mask && orig_source && inner) {
99 if (asprintf(&source, "%s%s", root, orig_source) < 0)
100 return ENOMEM;
101 }
102
103 snprintf(new, sizeof(new), "%s%s", root, target?target:source);
104
105 if (is_mask) {
106 if (stat(new, &s))
107 return 0; /* doesn't exists, nothing to mask */
108
109 if (S_ISDIR(s.st_mode)) {/* use empty 0-sized tmpfs for directories */
110 if (mount(NULL, new, "tmpfs", MS_RDONLY | MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_NOATIME, "size=0,mode=000"))
111 return error;
112 } else {
113 /* mount-bind 0-sized file having mode 000 */
114 if (mount(UJAIL_NOAFILE, new, NULL, MS_BIND, NULL))
115 return error;
116
117 if (mount(UJAIL_NOAFILE, new, NULL, MS_REMOUNT | MS_BIND | MS_RDONLY | MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_NOATIME, NULL))
118 return error;
119 }
120
121 DEBUG("masked path %s\n", new);
122 return 0;
123 }
124
125
126 if (!is_bind || (source && S_ISDIR(s.st_mode))) {
127 mkdir_p(new, 0755);
128 } else if (is_bind && source) {
129 mkdir_p(dirname(new), 0755);
130 snprintf(new, sizeof(new), "%s%s", root, target?target:source);
131 fd = creat(new, 0644);
132 if (fd == -1) {
133 ERROR("creat(%s) failed: %m\n", new);
134 return error;
135 }
136 close(fd);
137 }
138
139 if (is_bind) {
140 if (mount(source?:new, new, filesystemtype, MS_BIND | (mountflags & MS_REC), optstr)) {
141 if (error)
142 ERROR("failed to mount -B %s %s: %m\n", source, new);
143
144 if (inner)
145 free(source);
146
147 return error;
148 }
149 mountflags |= MS_REMOUNT;
150 }
151
152 if (mount(source?:(is_bind?new:NULL), new, filesystemtype, mountflags, optstr)) {
153 if (error)
154 ERROR("failed to mount %s %s: %m\n", source, new);
155
156 if (inner)
157 free(source);
158
159 return error;
160 }
161
162 DEBUG("mount %s%s %s (%s)\n", (mountflags & MS_BIND)?"-B ":"", source, new,
163 (mountflags & MS_RDONLY)?"ro":"rw");
164
165 if (propflags && mount(NULL, new, NULL, propflags, NULL)) {
166 if (error)
167 ERROR("failed to mount --make-... %s \n", new);
168
169 if (inner)
170 free(source);
171
172 return error;
173 }
174
175 if (inner)
176 free(source);
177
178 return 0;
179 }
180
181 static int _add_mount(const char *source, const char *target, const char *filesystemtype,
182 unsigned long mountflags, unsigned long propflags, const char *optstr,
183 int error, bool inner)
184 {
185 assert(target != NULL);
186
187 if (avl_find(&mounts, target))
188 return 1;
189
190 struct mount *m;
191 m = calloc(1, sizeof(struct mount));
192 assert(m != NULL);
193 m->avl.key = m->target = strdup(target);
194 if (source) {
195 if (source != (void*)(-1))
196 m->source = strdup(source);
197 else
198 m->source = (void*)(-1);
199 }
200 if (filesystemtype)
201 m->filesystemtype = strdup(filesystemtype);
202
203 if (optstr)
204 m->optstr = strdup(optstr);
205
206 m->mountflags = mountflags;
207 m->propflags = propflags;
208 m->error = error;
209 m->inner = inner;
210
211 avl_insert(&mounts, &m->avl);
212 DEBUG("adding mount %s %s bind(%d) ro(%d) err(%d)\n", (m->source == (void*)(-1))?"mask":m->source, m->target,
213 !!(m->mountflags & MS_BIND), !!(m->mountflags & MS_RDONLY), m->error != 0);
214
215 return 0;
216 }
217
218 int add_mount(const char *source, const char *target, const char *filesystemtype,
219 unsigned long mountflags, unsigned long propflags, const char *optstr, int error)
220 {
221 return _add_mount(source, target, filesystemtype, mountflags, propflags, optstr, error, false);
222 }
223
224 int add_mount_inner(const char *source, const char *target, const char *filesystemtype,
225 unsigned long mountflags, unsigned long propflags, const char *optstr, int error)
226 {
227 return _add_mount(source, target, filesystemtype, mountflags, propflags, optstr, error, true);
228 }
229
230 int add_mount_bind(const char *path, int readonly, int error)
231 {
232 unsigned long mountflags = MS_BIND;
233
234 if (readonly)
235 mountflags |= MS_RDONLY;
236
237 return add_mount(path, path, NULL, mountflags, 0, NULL, error);
238 }
239
240 enum {
241 OCI_MOUNT_SOURCE,
242 OCI_MOUNT_DESTINATION,
243 OCI_MOUNT_TYPE,
244 OCI_MOUNT_OPTIONS,
245 __OCI_MOUNT_MAX,
246 };
247
248 static const struct blobmsg_policy oci_mount_policy[] = {
249 [OCI_MOUNT_SOURCE] = { "source", BLOBMSG_TYPE_STRING },
250 [OCI_MOUNT_DESTINATION] = { "destination", BLOBMSG_TYPE_STRING },
251 [OCI_MOUNT_TYPE] = { "type", BLOBMSG_TYPE_STRING },
252 [OCI_MOUNT_OPTIONS] = { "options", BLOBMSG_TYPE_ARRAY },
253 };
254
255 struct mount_opt {
256 struct list_head list;
257 char *optstr;
258 };
259
260 #ifndef MS_LAZYTIME
261 #define MS_LAZYTIME (1 << 25)
262 #endif
263
264 static int parseOCImountopts(struct blob_attr *msg, unsigned long *mount_flags, unsigned long *propagation_flags, char **mount_data, int *error)
265 {
266 struct blob_attr *cur;
267 int rem;
268 unsigned long mf = 0;
269 unsigned long pf = 0;
270 char *tmp;
271 struct list_head fsopts = LIST_HEAD_INIT(fsopts);
272 size_t len = 0;
273 struct mount_opt *opt;
274
275 blobmsg_for_each_attr(cur, msg, rem) {
276 tmp = blobmsg_get_string(cur);
277 if (!strcmp("ro", tmp))
278 mf |= MS_RDONLY;
279 else if (!strcmp("rw", tmp))
280 mf &= ~MS_RDONLY;
281 else if (!strcmp("bind", tmp))
282 mf = MS_BIND;
283 else if (!strcmp("rbind", tmp))
284 mf |= MS_BIND | MS_REC;
285 else if (!strcmp("sync", tmp))
286 mf |= MS_SYNCHRONOUS;
287 else if (!strcmp("async", tmp))
288 mf &= ~MS_SYNCHRONOUS;
289 else if (!strcmp("atime", tmp))
290 mf &= ~MS_NOATIME;
291 else if (!strcmp("noatime", tmp))
292 mf |= MS_NOATIME;
293 else if (!strcmp("defaults", tmp))
294 mf = 0; /* rw, suid, dev, exec, auto, nouser, and async */
295 else if (!strcmp("dev", tmp))
296 mf &= ~MS_NODEV;
297 else if (!strcmp("nodev", tmp))
298 mf |= MS_NODEV;
299 else if (!strcmp("iversion", tmp))
300 mf |= MS_I_VERSION;
301 else if (!strcmp("noiversion", tmp))
302 mf &= ~MS_I_VERSION;
303 else if (!strcmp("diratime", tmp))
304 mf &= ~MS_NODIRATIME;
305 else if (!strcmp("nodiratime", tmp))
306 mf |= MS_NODIRATIME;
307 else if (!strcmp("dirsync", tmp))
308 mf |= MS_DIRSYNC;
309 else if (!strcmp("exec", tmp))
310 mf &= ~MS_NOEXEC;
311 else if (!strcmp("noexec", tmp))
312 mf |= MS_NOEXEC;
313 else if (!strcmp("mand", tmp))
314 mf |= MS_MANDLOCK;
315 else if (!strcmp("nomand", tmp))
316 mf &= ~MS_MANDLOCK;
317 else if (!strcmp("relatime", tmp))
318 mf |= MS_RELATIME;
319 else if (!strcmp("norelatime", tmp))
320 mf &= ~MS_RELATIME;
321 else if (!strcmp("strictatime", tmp))
322 mf |= MS_STRICTATIME;
323 else if (!strcmp("nostrictatime", tmp))
324 mf &= ~MS_STRICTATIME;
325 else if (!strcmp("lazytime", tmp))
326 mf |= MS_LAZYTIME;
327 else if (!strcmp("nolazytime", tmp))
328 mf &= ~MS_LAZYTIME;
329 else if (!strcmp("suid", tmp))
330 mf &= ~MS_NOSUID;
331 else if (!strcmp("nosuid", tmp))
332 mf |= MS_NOSUID;
333 else if (!strcmp("remount", tmp))
334 mf |= MS_REMOUNT;
335 /* propagation flags */
336 else if (!strcmp("private", tmp))
337 pf |= MS_PRIVATE;
338 else if (!strcmp("rprivate", tmp))
339 pf |= MS_PRIVATE | MS_REC;
340 else if (!strcmp("slave", tmp))
341 pf |= MS_SLAVE;
342 else if (!strcmp("rslave", tmp))
343 pf |= MS_SLAVE | MS_REC;
344 else if (!strcmp("shared", tmp))
345 pf |= MS_SHARED;
346 else if (!strcmp("rshared", tmp))
347 pf |= MS_SHARED | MS_REC;
348 else if (!strcmp("unbindable", tmp))
349 pf |= MS_UNBINDABLE;
350 else if (!strcmp("runbindable", tmp))
351 pf |= MS_UNBINDABLE | MS_REC;
352 /* special case: 'nofail' */
353 else if(!strcmp("nofail", tmp))
354 *error = 0;
355 else if (!strcmp("auto", tmp) ||
356 !strcmp("noauto", tmp) ||
357 !strcmp("user", tmp) ||
358 !strcmp("group", tmp) ||
359 !strcmp("_netdev", tmp))
360 DEBUG("ignoring built-in mount option %s\n", tmp);
361 else {
362 /* filesystem-specific free-form option */
363 opt = calloc(1, sizeof(*opt));
364 opt->optstr = tmp;
365 list_add_tail(&opt->list, &fsopts);
366 }
367 };
368
369 *mount_flags = mf;
370 *propagation_flags = pf;
371
372 list_for_each_entry(opt, &fsopts, list) {
373 if (len)
374 ++len;
375
376 len += strlen(opt->optstr);
377 };
378
379 if (len) {
380 *mount_data = calloc(len + 1, sizeof(char));
381 if (!mount_data)
382 return ENOMEM;
383
384 len = 0;
385 list_for_each_entry(opt, &fsopts, list) {
386 if (len)
387 strcat(*mount_data, ",");
388
389 strcat(*mount_data, opt->optstr);
390 ++len;
391 };
392
393 list_del(&fsopts);
394 }
395
396 DEBUG("mount flags(%08lx) propagation(%08lx) fsopts(\"%s\")\n", mf, pf, *mount_data?:"");
397
398 return 0;
399 }
400
401 int parseOCImount(struct blob_attr *msg)
402 {
403 struct blob_attr *tb[__OCI_MOUNT_MAX];
404 unsigned long mount_flags = 0;
405 unsigned long propagation_flags = 0;
406 char *mount_data = NULL;
407 int ret, err = -1;
408
409 blobmsg_parse(oci_mount_policy, __OCI_MOUNT_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
410
411 if (!tb[OCI_MOUNT_DESTINATION])
412 return EINVAL;
413
414 if (tb[OCI_MOUNT_OPTIONS]) {
415 ret = parseOCImountopts(tb[OCI_MOUNT_OPTIONS], &mount_flags, &propagation_flags, &mount_data, &err);
416 if (ret)
417 return ret;
418 }
419
420 ret = add_mount(tb[OCI_MOUNT_SOURCE] ? blobmsg_get_string(tb[OCI_MOUNT_SOURCE]) : NULL,
421 blobmsg_get_string(tb[OCI_MOUNT_DESTINATION]),
422 tb[OCI_MOUNT_TYPE] ? blobmsg_get_string(tb[OCI_MOUNT_TYPE]) : NULL,
423 mount_flags, propagation_flags, mount_data, err);
424
425 if (mount_data)
426 free(mount_data);
427
428 return ret;
429 }
430
431 static void build_noafile(void) {
432 int fd;
433
434 fd = creat(UJAIL_NOAFILE, 0000);
435 if (fd == -1)
436 return;
437
438 close(fd);
439 return;
440 }
441
442 int mount_all(const char *jailroot) {
443 struct library *l;
444 struct mount *m;
445
446 build_noafile();
447
448 avl_for_each_element(&libraries, l, avl)
449 add_mount_bind(l->path, 1, -1);
450
451 avl_for_each_element(&mounts, m, avl)
452 if (do_mount(jailroot, m->source, m->target, m->filesystemtype, m->mountflags,
453 m->propflags, m->optstr, m->error, m->inner))
454 return -1;
455
456 return 0;
457 }
458
459 void mount_list_init(void) {
460 avl_init(&mounts, avl_strcmp, false, NULL);
461 }
462
463 static int add_script_interp(const char *path, const char *map, int size)
464 {
465 int start = 2;
466 while (start < size && map[start] != '/') {
467 start++;
468 }
469 if (start >= size) {
470 ERROR("bad script interp (%s)\n", path);
471 return -1;
472 }
473 int stop = start + 1;
474 while (stop < size && map[stop] > 0x20 && map[stop] <= 0x7e) {
475 stop++;
476 }
477 if (stop >= size || (stop-start) > PATH_MAX) {
478 ERROR("bad script interp (%s)\n", path);
479 return -1;
480 }
481 char buf[PATH_MAX];
482 strncpy(buf, map+start, stop-start);
483 return add_path_and_deps(buf, 1, -1, 0);
484 }
485
486 int add_path_and_deps(const char *path, int readonly, int error, int lib)
487 {
488 assert(path != NULL);
489
490 if (lib == 0 && path[0] != '/') {
491 ERROR("%s is not an absolute path\n", path);
492 return error;
493 }
494
495 char *map = NULL;
496 int fd, ret = -1;
497 if (path[0] == '/') {
498 if (avl_find(&mounts, path))
499 return 0;
500 fd = open(path, O_RDONLY|O_CLOEXEC);
501 if (fd == -1)
502 return error;
503 add_mount_bind(path, readonly, error);
504 } else {
505 if (avl_find(&libraries, path))
506 return 0;
507 char *fullpath;
508 fd = lib_open(&fullpath, path);
509 if (fd == -1)
510 return error;
511 if (fullpath) {
512 alloc_library(fullpath, path);
513 free(fullpath);
514 }
515 }
516
517 struct stat s;
518 if (fstat(fd, &s) == -1) {
519 ERROR("fstat(%s) failed: %m\n", path);
520 ret = error;
521 goto out;
522 }
523
524 if (!S_ISREG(s.st_mode)) {
525 ret = 0;
526 goto out;
527 }
528
529 /* too small to be an ELF or a script -> "normal" file */
530 if (s.st_size < 4) {
531 ret = 0;
532 goto out;
533 }
534
535 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
536 if (map == MAP_FAILED) {
537 ERROR("failed to mmap %s: %m\n", path);
538 ret = -1;
539 goto out;
540 }
541
542 if (map[0] == '#' && map[1] == '!') {
543 ret = add_script_interp(path, map, s.st_size);
544 goto out;
545 }
546
547 if (map[0] == ELFMAG0 && map[1] == ELFMAG1 && map[2] == ELFMAG2 && map[3] == ELFMAG3) {
548 ret = elf_load_deps(path, map);
549 goto out;
550 }
551
552 ret = 0;
553
554 out:
555 if (fd >= 0)
556 close(fd);
557 if (map)
558 munmap(map, s.st_size);
559
560 return ret;
561 }