3f090dd8e1940baa75f989b43b7528e8227c118d
[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 <string.h>
25 #include <sys/stat.h>
26 #include <sys/mman.h>
27 #include <unistd.h>
28 #include <libgen.h>
29
30 #include <libubox/avl.h>
31 #include <libubox/avl-cmp.h>
32 #include <libubox/blobmsg.h>
33 #include <libubox/list.h>
34
35 #include "elf.h"
36 #include "fs.h"
37 #include "jail.h"
38 #include "log.h"
39
40 struct mount {
41 struct avl_node avl;
42 const char *source;
43 const char *target;
44 const char *filesystemtype;
45 unsigned long mountflags;
46 const char *optstr;
47 int error;
48 };
49
50 struct avl_tree mounts;
51
52 int mkdir_p(char *dir, mode_t mask)
53 {
54 char *l = strrchr(dir, '/');
55 int ret;
56
57 if (!l)
58 return 0;
59
60 *l = '\0';
61
62 if (mkdir_p(dir, mask))
63 return -1;
64
65 *l = '/';
66
67 ret = mkdir(dir, mask);
68 if (ret && errno == EEXIST)
69 return 0;
70
71 if (ret)
72 ERROR("mkdir(%s, %d) failed: %m\n", dir, mask);
73
74 return ret;
75 }
76
77 static int do_mount(const char *root, const char *source, const char *target, const char *filesystemtype,
78 unsigned long orig_mountflags, const char *optstr, int error)
79 {
80 struct stat s;
81 char new[PATH_MAX];
82 int fd;
83 bool is_bind = (orig_mountflags & MS_BIND);
84 unsigned long mountflags = orig_mountflags;
85
86 if (is_bind && stat(source, &s)) {
87 ERROR("stat(%s) failed: %m\n", source);
88 return error;
89 }
90
91 snprintf(new, sizeof(new), "%s%s", root, target?target:source);
92
93 if (!is_bind || S_ISDIR(s.st_mode)) {
94 mkdir_p(new, 0755);
95 } else {
96 mkdir_p(dirname(new), 0755);
97 snprintf(new, sizeof(new), "%s%s", root, target?target:source);
98 fd = creat(new, 0644);
99 if (fd == -1) {
100 ERROR("creat(%s) failed: %m\n", new);
101 return -1;
102 }
103 close(fd);
104 }
105
106 if (mountflags & MS_BIND) {
107 if (mount(source, new, filesystemtype, MS_BIND, optstr)) {
108 ERROR("failed to mount -B %s %s: %m\n", source, new);
109 }
110 mountflags |= MS_REMOUNT;
111 }
112
113 if (mount(source, new, filesystemtype, mountflags, optstr)) {
114 ERROR("failed to mount %s %s: %m\n", source, new);
115 return -1;
116 }
117
118 DEBUG("mount %s%s %s (%s)\n", (mountflags & MS_BIND)?"-B ":"", source, new,
119 (mountflags & MS_RDONLY)?"ro":"rw");
120
121 return 0;
122 }
123
124 int add_mount(const char *source, const char *target, const char *filesystemtype,
125 unsigned long mountflags, const char *optstr, int error)
126 {
127 assert(target != NULL);
128
129 if (avl_find(&mounts, target))
130 return 1;
131
132 struct mount *m;
133 m = calloc(1, sizeof(struct mount));
134 assert(m != NULL);
135 m->avl.key = m->target = strdup(target);
136 if (source)
137 m->source = strdup(source);
138 if (filesystemtype)
139 m->filesystemtype = strdup(filesystemtype);
140 m->mountflags = mountflags;
141 m->error = error;
142
143 avl_insert(&mounts, &m->avl);
144 DEBUG("adding mount %s %s bind(%d) ro(%d) err(%d)\n", m->source, m->target,
145 !!(m->mountflags & MS_BIND), !!(m->mountflags & MS_RDONLY), m->error != 0);
146
147 return 0;
148 }
149
150 int add_mount_bind(const char *path, int readonly, int error)
151 {
152 unsigned long mountflags = MS_BIND;
153
154 if (readonly)
155 mountflags |= MS_RDONLY;
156
157 return add_mount(path, path, NULL, mountflags, NULL, error);
158 }
159
160
161 enum {
162 OCI_MOUNT_SOURCE,
163 OCI_MOUNT_DESTINATION,
164 OCI_MOUNT_TYPE,
165 OCI_MOUNT_OPTIONS,
166 __OCI_MOUNT_MAX,
167 };
168
169 static const struct blobmsg_policy oci_mount_policy[] = {
170 [OCI_MOUNT_SOURCE] = { "source", BLOBMSG_TYPE_STRING },
171 [OCI_MOUNT_DESTINATION] = { "destination", BLOBMSG_TYPE_STRING },
172 [OCI_MOUNT_TYPE] = { "type", BLOBMSG_TYPE_STRING },
173 [OCI_MOUNT_OPTIONS] = { "options", BLOBMSG_TYPE_ARRAY },
174 };
175
176 struct mount_opt {
177 struct list_head list;
178 char *optstr;
179 };
180
181 static int parseOCImountopts(struct blob_attr *msg, unsigned long *mount_flags, char **mount_data, int *error)
182 {
183 struct blob_attr *cur;
184 int rem;
185 unsigned long mf = 0;
186 char *tmp;
187 struct list_head fsopts = LIST_HEAD_INIT(fsopts);
188 size_t len = 0;
189 struct mount_opt *opt;
190
191 blobmsg_for_each_attr(cur, msg, rem) {
192 tmp = blobmsg_get_string(cur);
193 if (!strcmp("ro", tmp))
194 mf |= MS_RDONLY;
195 else if (!strcmp("rw", tmp))
196 mf &= ~MS_RDONLY;
197 else if (!strcmp("bind", tmp))
198 mf = MS_BIND;
199 else if (!strcmp("rbind", tmp))
200 mf |= MS_BIND | MS_REC;
201 else if (!strcmp("sync", tmp))
202 mf |= MS_SYNCHRONOUS;
203 else if (!strcmp("async", tmp))
204 mf &= ~MS_SYNCHRONOUS;
205 else if (!strcmp("atime", tmp))
206 mf &= ~MS_NOATIME;
207 else if (!strcmp("noatime", tmp))
208 mf |= MS_NOATIME;
209 else if (!strcmp("defaults", tmp))
210 mf = 0; /* rw, suid, dev, exec, auto, nouser, and async */
211 else if (!strcmp("dev", tmp))
212 mf &= ~MS_NODEV;
213 else if (!strcmp("nodev", tmp))
214 mf |= MS_NODEV;
215 else if (!strcmp("diratime", tmp))
216 mf &= ~MS_NODIRATIME;
217 else if (!strcmp("nodiratime", tmp))
218 mf |= MS_NODIRATIME;
219 else if (!strcmp("dirsync", tmp))
220 mf |= MS_DIRSYNC;
221 else if (!strcmp("exec", tmp))
222 mf &= ~MS_NOEXEC;
223 else if (!strcmp("noexec", tmp))
224 mf |= MS_NOEXEC;
225 else if (!strcmp("mand", tmp))
226 mf |= MS_MANDLOCK;
227 else if (!strcmp("nomand", tmp))
228 mf &= ~MS_MANDLOCK;
229 else if (!strcmp("relatime", tmp))
230 mf |= MS_RELATIME;
231 else if (!strcmp("norelatime", tmp))
232 mf &= ~MS_RELATIME;
233 else if (!strcmp("strictatime", tmp))
234 mf |= MS_STRICTATIME;
235 else if (!strcmp("nostrictatime", tmp))
236 mf &= ~MS_STRICTATIME;
237 else if (!strcmp("lazytime", tmp))
238 mf |= MS_LAZYTIME;
239 else if (!strcmp("nostrictatime", tmp))
240 mf &= ~MS_LAZYTIME;
241 else if (!strcmp("suid", tmp))
242 mf &= ~MS_NOSUID;
243 else if (!strcmp("nosuid", tmp))
244 mf |= MS_NOSUID;
245 else if (!strcmp("remount", tmp))
246 mf |= MS_REMOUNT;
247 else if(!strcmp("nofail", tmp))
248 *error = 0;
249 else if (!strcmp("auto", tmp) ||
250 !strcmp("noauto", tmp) ||
251 !strcmp("user", tmp) ||
252 !strcmp("group", tmp) ||
253 !strcmp("_netdev", tmp))
254 DEBUG("ignoring built-in mount option %s\n", tmp);
255 else {
256 /* filesystem-specific free-form option */
257 opt = calloc(1, sizeof(*opt));
258 opt->optstr = tmp;
259 list_add_tail(&opt->list, &fsopts);
260 }
261 };
262
263 *mount_flags = mf;
264
265 list_for_each_entry(opt, &fsopts, list) {
266 if (len)
267 ++len;
268
269 len += strlen(opt->optstr);
270 };
271
272 if (!len)
273 return 0;
274
275 *mount_data = calloc(len + 1, sizeof(char));
276 if (!mount_data)
277 return ENOMEM;
278
279 len = 0;
280 list_for_each_entry(opt, &fsopts, list) {
281 if (len)
282 strcat(*mount_data, ",");
283
284 strcat(*mount_data, opt->optstr);
285 ++len;
286 };
287
288 list_del(&fsopts);
289
290 DEBUG("mount flags(%08lx) fsopts(\"%s\")\n", mf, *mount_data?:"");
291
292 return 0;
293 };
294
295 int parseOCImount(struct blob_attr *msg)
296 {
297 struct blob_attr *tb[__OCI_MOUNT_MAX];
298 unsigned long mount_flags = 0;
299 char *mount_data = NULL;
300 int ret, err = -1;
301
302 blobmsg_parse(oci_mount_policy, __OCI_MOUNT_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
303
304 if (!tb[OCI_MOUNT_DESTINATION])
305 return EINVAL;
306
307 if (tb[OCI_MOUNT_OPTIONS]) {
308 ret = parseOCImountopts(tb[OCI_MOUNT_OPTIONS], &mount_flags, &mount_data, &err);
309 if (ret)
310 return ret;
311 }
312
313 add_mount(tb[OCI_MOUNT_SOURCE] ? blobmsg_get_string(tb[OCI_MOUNT_SOURCE]) : NULL,
314 blobmsg_get_string(tb[OCI_MOUNT_DESTINATION]),
315 tb[OCI_MOUNT_TYPE] ? blobmsg_get_string(tb[OCI_MOUNT_TYPE]) : NULL,
316 mount_flags, mount_data, err);
317
318 return 0;
319 };
320
321
322 int mount_all(const char *jailroot) {
323 struct library *l;
324 struct mount *m;
325
326 avl_for_each_element(&libraries, l, avl)
327 add_mount_bind(l->path, 1, -1);
328
329 avl_for_each_element(&mounts, m, avl)
330 if (do_mount(jailroot, m->source, m->target, m->filesystemtype, m->mountflags, m->optstr, m->error))
331 return -1;
332
333 return 0;
334 }
335
336 void mount_list_init(void) {
337 avl_init(&mounts, avl_strcmp, false, NULL);
338 }
339
340 static int add_script_interp(const char *path, const char *map, int size)
341 {
342 int start = 2;
343 while (start < size && map[start] != '/') {
344 start++;
345 }
346 if (start >= size) {
347 ERROR("bad script interp (%s)\n", path);
348 return -1;
349 }
350 int stop = start + 1;
351 while (stop < size && map[stop] > 0x20 && map[stop] <= 0x7e) {
352 stop++;
353 }
354 if (stop >= size || (stop-start) > PATH_MAX) {
355 ERROR("bad script interp (%s)\n", path);
356 return -1;
357 }
358 char buf[PATH_MAX];
359 strncpy(buf, map+start, stop-start);
360 return add_path_and_deps(buf, 1, -1, 0);
361 }
362
363 int add_path_and_deps(const char *path, int readonly, int error, int lib)
364 {
365 assert(path != NULL);
366
367 if (lib == 0 && path[0] != '/') {
368 ERROR("%s is not an absolute path\n", path);
369 return error;
370 }
371
372 char *map = NULL;
373 int fd, ret = -1;
374 if (path[0] == '/') {
375 if (avl_find(&mounts, path))
376 return 0;
377 fd = open(path, O_RDONLY|O_CLOEXEC);
378 if (fd == -1)
379 return error;
380 add_mount_bind(path, readonly, error);
381 } else {
382 if (avl_find(&libraries, path))
383 return 0;
384 char *fullpath;
385 fd = lib_open(&fullpath, path);
386 if (fd == -1)
387 return error;
388 if (fullpath) {
389 alloc_library(fullpath, path);
390 free(fullpath);
391 }
392 }
393
394 struct stat s;
395 if (fstat(fd, &s) == -1) {
396 ERROR("fstat(%s) failed: %m\n", path);
397 ret = error;
398 goto out;
399 }
400
401 if (!S_ISREG(s.st_mode)) {
402 ret = 0;
403 goto out;
404 }
405
406 /* too small to be an ELF or a script -> "normal" file */
407 if (s.st_size < 4) {
408 ret = 0;
409 goto out;
410 }
411
412 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
413 if (map == MAP_FAILED) {
414 ERROR("failed to mmap %s: %m\n", path);
415 ret = -1;
416 goto out;
417 }
418
419 if (map[0] == '#' && map[1] == '!') {
420 ret = add_script_interp(path, map, s.st_size);
421 goto out;
422 }
423
424 if (map[0] == ELFMAG0 && map[1] == ELFMAG1 && map[2] == ELFMAG2 && map[3] == ELFMAG3) {
425 ret = elf_load_deps(path, map);
426 goto out;
427 }
428
429 ret = 0;
430
431 out:
432 if (fd >= 0)
433 close(fd);
434 if (map)
435 munmap(map, s.st_size);
436
437 return ret;
438 }