]>
Commit | Line | Data |
---|---|---|
7c6b6602 | 1 | /* |
7387863d DDAG |
2 | * FUSE: Filesystem in Userspace |
3 | * Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu> | |
4 | * | |
5 | * This program can be distributed under the terms of the GNU GPLv2. | |
6 | * See the file COPYING. | |
7 | */ | |
7c6b6602 | 8 | |
7387863d | 9 | /* |
7c6b6602 DDAG |
10 | * |
11 | * This file system mirrors the existing file system hierarchy of the | |
12 | * system, starting at the root file system. This is implemented by | |
13 | * just "passing through" all requests to the corresponding user-space | |
14 | * libc functions. In contrast to passthrough.c and passthrough_fh.c, | |
15 | * this implementation uses the low-level API. Its performance should | |
16 | * be the least bad among the three, but many operations are not | |
17 | * implemented. In particular, it is not possible to remove files (or | |
18 | * directories) because the code necessary to defer actual removal | |
19 | * until the file is not opened anymore would make the example much | |
20 | * more complicated. | |
21 | * | |
22 | * When writeback caching is enabled (-o writeback mount option), it | |
23 | * is only possible to write to files for which the mounting user has | |
24 | * read permissions. This is because the writeback cache requires the | |
25 | * kernel to be able to issue read requests for all files (which the | |
26 | * passthrough filesystem cannot satisfy if it can't read the file in | |
27 | * the underlying filesystem). | |
28 | * | |
29 | * Compile with: | |
30 | * | |
7387863d DDAG |
31 | * gcc -Wall passthrough_ll.c `pkg-config fuse3 --cflags --libs` -o |
32 | * passthrough_ll | |
7c6b6602 DDAG |
33 | * |
34 | * ## Source code ## | |
35 | * \include passthrough_ll.c | |
36 | */ | |
37 | ||
09863ebc | 38 | #include "qemu/osdep.h" |
50fb955a | 39 | #include "qemu/timer.h" |
f6f3573c | 40 | #include "fuse_virtio.h" |
d240314a | 41 | #include "fuse_log.h" |
09863ebc | 42 | #include "fuse_lowlevel.h" |
7c6b6602 | 43 | #include <assert.h> |
2405f3c0 | 44 | #include <cap-ng.h> |
7387863d | 45 | #include <dirent.h> |
7c6b6602 | 46 | #include <errno.h> |
36f38469 | 47 | #include <glib.h> |
7c6b6602 | 48 | #include <inttypes.h> |
7387863d | 49 | #include <limits.h> |
7c6b6602 | 50 | #include <pthread.h> |
7387863d DDAG |
51 | #include <stdbool.h> |
52 | #include <stddef.h> | |
53 | #include <stdio.h> | |
54 | #include <stdlib.h> | |
55 | #include <string.h> | |
7c6b6602 | 56 | #include <sys/file.h> |
5baa3b8e | 57 | #include <sys/mount.h> |
8e1d4ef2 | 58 | #include <sys/prctl.h> |
01a6dc95 | 59 | #include <sys/resource.h> |
929cfb7a | 60 | #include <sys/syscall.h> |
8e1d4ef2 SH |
61 | #include <sys/types.h> |
62 | #include <sys/wait.h> | |
7c6b6602 | 63 | #include <sys/xattr.h> |
f185621d | 64 | #include <syslog.h> |
7387863d | 65 | #include <unistd.h> |
7c6b6602 DDAG |
66 | |
67 | #include "passthrough_helpers.h" | |
4f8bde99 | 68 | #include "seccomp.h" |
7c6b6602 | 69 | |
25c13572 SH |
70 | struct lo_map_elem { |
71 | union { | |
92fb57b8 | 72 | struct lo_inode *inode; |
b39bce12 | 73 | struct lo_dirp *dirp; |
73b4d19d | 74 | int fd; |
25c13572 SH |
75 | ssize_t freelist; |
76 | }; | |
77 | bool in_use; | |
78 | }; | |
79 | ||
80 | /* Maps FUSE fh or ino values to internal objects */ | |
81 | struct lo_map { | |
82 | struct lo_map_elem *elems; | |
83 | size_t nelems; | |
84 | ssize_t freelist; | |
85 | }; | |
86 | ||
7c6b6602 | 87 | struct lo_inode { |
7387863d DDAG |
88 | struct lo_inode *next; /* protected by lo->mutex */ |
89 | struct lo_inode *prev; /* protected by lo->mutex */ | |
90 | int fd; | |
91 | bool is_symlink; | |
92 | ino_t ino; | |
93 | dev_t dev; | |
94 | uint64_t refcount; /* protected by lo->mutex */ | |
92fb57b8 | 95 | fuse_ino_t fuse_ino; |
7c6b6602 DDAG |
96 | }; |
97 | ||
929cfb7a VG |
98 | struct lo_cred { |
99 | uid_t euid; | |
100 | gid_t egid; | |
101 | }; | |
102 | ||
7c6b6602 | 103 | enum { |
7387863d DDAG |
104 | CACHE_NEVER, |
105 | CACHE_NORMAL, | |
106 | CACHE_ALWAYS, | |
7c6b6602 DDAG |
107 | }; |
108 | ||
109 | struct lo_data { | |
7387863d DDAG |
110 | pthread_mutex_t mutex; |
111 | int debug; | |
5fe319a7 | 112 | int norace; |
7387863d DDAG |
113 | int writeback; |
114 | int flock; | |
115 | int xattr; | |
116 | const char *source; | |
117 | double timeout; | |
118 | int cache; | |
119 | int timeout_set; | |
120 | struct lo_inode root; /* protected by lo->mutex */ | |
92fb57b8 | 121 | struct lo_map ino_map; /* protected by lo->mutex */ |
b39bce12 | 122 | struct lo_map dirp_map; /* protected by lo->mutex */ |
73b4d19d | 123 | struct lo_map fd_map; /* protected by lo->mutex */ |
9f59d175 SH |
124 | |
125 | /* An O_PATH file descriptor to /proc/self/fd/ */ | |
126 | int proc_self_fd; | |
7c6b6602 DDAG |
127 | }; |
128 | ||
129 | static const struct fuse_opt lo_opts[] = { | |
7387863d DDAG |
130 | { "writeback", offsetof(struct lo_data, writeback), 1 }, |
131 | { "no_writeback", offsetof(struct lo_data, writeback), 0 }, | |
132 | { "source=%s", offsetof(struct lo_data, source), 0 }, | |
133 | { "flock", offsetof(struct lo_data, flock), 1 }, | |
134 | { "no_flock", offsetof(struct lo_data, flock), 0 }, | |
135 | { "xattr", offsetof(struct lo_data, xattr), 1 }, | |
136 | { "no_xattr", offsetof(struct lo_data, xattr), 0 }, | |
137 | { "timeout=%lf", offsetof(struct lo_data, timeout), 0 }, | |
138 | { "timeout=", offsetof(struct lo_data, timeout_set), 1 }, | |
139 | { "cache=never", offsetof(struct lo_data, cache), CACHE_NEVER }, | |
140 | { "cache=auto", offsetof(struct lo_data, cache), CACHE_NORMAL }, | |
141 | { "cache=always", offsetof(struct lo_data, cache), CACHE_ALWAYS }, | |
5fe319a7 | 142 | { "norace", offsetof(struct lo_data, norace), 1 }, |
7387863d | 143 | FUSE_OPT_END |
7c6b6602 | 144 | }; |
f185621d | 145 | static bool use_syslog = false; |
d240314a | 146 | static int current_log_level; |
7c6b6602 | 147 | |
5fe319a7 MS |
148 | static void unref_inode(struct lo_data *lo, struct lo_inode *inode, uint64_t n); |
149 | ||
2405f3c0 DDAG |
150 | static struct { |
151 | pthread_mutex_t mutex; | |
152 | void *saved; | |
153 | } cap; | |
154 | /* That we loaded cap-ng in the current thread from the saved */ | |
155 | static __thread bool cap_loaded = 0; | |
156 | ||
5fe319a7 MS |
157 | static struct lo_inode *lo_find(struct lo_data *lo, struct stat *st); |
158 | ||
25dae28c SH |
159 | static int is_dot_or_dotdot(const char *name) |
160 | { | |
161 | return name[0] == '.' && | |
162 | (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')); | |
163 | } | |
164 | ||
165 | /* Is `path` a single path component that is not "." or ".."? */ | |
166 | static int is_safe_path_component(const char *path) | |
167 | { | |
168 | if (strchr(path, '/')) { | |
169 | return 0; | |
170 | } | |
171 | ||
172 | return !is_dot_or_dotdot(path); | |
173 | } | |
5fe319a7 | 174 | |
7c6b6602 DDAG |
175 | static struct lo_data *lo_data(fuse_req_t req) |
176 | { | |
7387863d | 177 | return (struct lo_data *)fuse_req_userdata(req); |
7c6b6602 DDAG |
178 | } |
179 | ||
2405f3c0 DDAG |
180 | /* |
181 | * Load capng's state from our saved state if the current thread | |
182 | * hadn't previously been loaded. | |
183 | * returns 0 on success | |
184 | */ | |
185 | static int load_capng(void) | |
186 | { | |
187 | if (!cap_loaded) { | |
188 | pthread_mutex_lock(&cap.mutex); | |
189 | capng_restore_state(&cap.saved); | |
190 | /* | |
191 | * restore_state free's the saved copy | |
192 | * so make another. | |
193 | */ | |
194 | cap.saved = capng_save_state(); | |
195 | if (!cap.saved) { | |
196 | fuse_log(FUSE_LOG_ERR, "capng_save_state (thread)\n"); | |
197 | return -EINVAL; | |
198 | } | |
199 | pthread_mutex_unlock(&cap.mutex); | |
200 | ||
201 | /* | |
202 | * We want to use the loaded state for our pid, | |
203 | * not the original | |
204 | */ | |
205 | capng_setpid(syscall(SYS_gettid)); | |
206 | cap_loaded = true; | |
207 | } | |
208 | return 0; | |
209 | } | |
210 | ||
ee884652 VG |
211 | /* |
212 | * Helpers for dropping and regaining effective capabilities. Returns 0 | |
213 | * on success, error otherwise | |
214 | */ | |
215 | static int drop_effective_cap(const char *cap_name, bool *cap_dropped) | |
216 | { | |
217 | int cap, ret; | |
218 | ||
219 | cap = capng_name_to_capability(cap_name); | |
220 | if (cap < 0) { | |
221 | ret = errno; | |
222 | fuse_log(FUSE_LOG_ERR, "capng_name_to_capability(%s) failed:%s\n", | |
223 | cap_name, strerror(errno)); | |
224 | goto out; | |
225 | } | |
226 | ||
227 | if (load_capng()) { | |
228 | ret = errno; | |
229 | fuse_log(FUSE_LOG_ERR, "load_capng() failed\n"); | |
230 | goto out; | |
231 | } | |
232 | ||
233 | /* We dont have this capability in effective set already. */ | |
234 | if (!capng_have_capability(CAPNG_EFFECTIVE, cap)) { | |
235 | ret = 0; | |
236 | goto out; | |
237 | } | |
238 | ||
239 | if (capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, cap)) { | |
240 | ret = errno; | |
241 | fuse_log(FUSE_LOG_ERR, "capng_update(DROP,) failed\n"); | |
242 | goto out; | |
243 | } | |
244 | ||
245 | if (capng_apply(CAPNG_SELECT_CAPS)) { | |
246 | ret = errno; | |
247 | fuse_log(FUSE_LOG_ERR, "drop:capng_apply() failed\n"); | |
248 | goto out; | |
249 | } | |
250 | ||
251 | ret = 0; | |
252 | if (cap_dropped) { | |
253 | *cap_dropped = true; | |
254 | } | |
255 | ||
256 | out: | |
257 | return ret; | |
258 | } | |
259 | ||
260 | static int gain_effective_cap(const char *cap_name) | |
261 | { | |
262 | int cap; | |
263 | int ret = 0; | |
264 | ||
265 | cap = capng_name_to_capability(cap_name); | |
266 | if (cap < 0) { | |
267 | ret = errno; | |
268 | fuse_log(FUSE_LOG_ERR, "capng_name_to_capability(%s) failed:%s\n", | |
269 | cap_name, strerror(errno)); | |
270 | goto out; | |
271 | } | |
272 | ||
273 | if (load_capng()) { | |
274 | ret = errno; | |
275 | fuse_log(FUSE_LOG_ERR, "load_capng() failed\n"); | |
276 | goto out; | |
277 | } | |
278 | ||
279 | if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, cap)) { | |
280 | ret = errno; | |
281 | fuse_log(FUSE_LOG_ERR, "capng_update(ADD,) failed\n"); | |
282 | goto out; | |
283 | } | |
284 | ||
285 | if (capng_apply(CAPNG_SELECT_CAPS)) { | |
286 | ret = errno; | |
287 | fuse_log(FUSE_LOG_ERR, "gain:capng_apply() failed\n"); | |
288 | goto out; | |
289 | } | |
290 | ret = 0; | |
291 | ||
292 | out: | |
293 | return ret; | |
294 | } | |
295 | ||
92fb57b8 | 296 | static void lo_map_init(struct lo_map *map) |
25c13572 SH |
297 | { |
298 | map->elems = NULL; | |
299 | map->nelems = 0; | |
300 | map->freelist = -1; | |
301 | } | |
302 | ||
92fb57b8 | 303 | static void lo_map_destroy(struct lo_map *map) |
25c13572 SH |
304 | { |
305 | free(map->elems); | |
306 | } | |
307 | ||
308 | static int lo_map_grow(struct lo_map *map, size_t new_nelems) | |
309 | { | |
310 | struct lo_map_elem *new_elems; | |
311 | size_t i; | |
312 | ||
313 | if (new_nelems <= map->nelems) { | |
314 | return 1; | |
315 | } | |
316 | ||
317 | new_elems = realloc(map->elems, sizeof(map->elems[0]) * new_nelems); | |
318 | if (!new_elems) { | |
319 | return 0; | |
320 | } | |
321 | ||
322 | for (i = map->nelems; i < new_nelems; i++) { | |
323 | new_elems[i].freelist = i + 1; | |
324 | new_elems[i].in_use = false; | |
325 | } | |
326 | new_elems[new_nelems - 1].freelist = -1; | |
327 | ||
328 | map->elems = new_elems; | |
329 | map->freelist = map->nelems; | |
330 | map->nelems = new_nelems; | |
331 | return 1; | |
332 | } | |
333 | ||
92fb57b8 | 334 | static struct lo_map_elem *lo_map_alloc_elem(struct lo_map *map) |
25c13572 SH |
335 | { |
336 | struct lo_map_elem *elem; | |
337 | ||
338 | if (map->freelist == -1 && !lo_map_grow(map, map->nelems + 256)) { | |
339 | return NULL; | |
340 | } | |
341 | ||
342 | elem = &map->elems[map->freelist]; | |
343 | map->freelist = elem->freelist; | |
344 | ||
345 | elem->in_use = true; | |
346 | ||
347 | return elem; | |
348 | } | |
349 | ||
92fb57b8 | 350 | static struct lo_map_elem *lo_map_reserve(struct lo_map *map, size_t key) |
25c13572 SH |
351 | { |
352 | ssize_t *prev; | |
353 | ||
354 | if (!lo_map_grow(map, key + 1)) { | |
355 | return NULL; | |
356 | } | |
357 | ||
358 | for (prev = &map->freelist; *prev != -1; | |
359 | prev = &map->elems[*prev].freelist) { | |
360 | if (*prev == key) { | |
361 | struct lo_map_elem *elem = &map->elems[key]; | |
362 | ||
363 | *prev = elem->freelist; | |
364 | elem->in_use = true; | |
365 | return elem; | |
366 | } | |
367 | } | |
368 | return NULL; | |
369 | } | |
370 | ||
92fb57b8 | 371 | static struct lo_map_elem *lo_map_get(struct lo_map *map, size_t key) |
25c13572 SH |
372 | { |
373 | if (key >= map->nelems) { | |
374 | return NULL; | |
375 | } | |
376 | if (!map->elems[key].in_use) { | |
377 | return NULL; | |
378 | } | |
379 | return &map->elems[key]; | |
380 | } | |
381 | ||
92fb57b8 | 382 | static void lo_map_remove(struct lo_map *map, size_t key) |
25c13572 SH |
383 | { |
384 | struct lo_map_elem *elem; | |
385 | ||
386 | if (key >= map->nelems) { | |
387 | return; | |
388 | } | |
389 | ||
390 | elem = &map->elems[key]; | |
391 | if (!elem->in_use) { | |
392 | return; | |
393 | } | |
394 | ||
395 | elem->in_use = false; | |
396 | ||
397 | elem->freelist = map->freelist; | |
398 | map->freelist = key; | |
399 | } | |
400 | ||
73b4d19d SH |
401 | /* Assumes lo->mutex is held */ |
402 | static ssize_t lo_add_fd_mapping(fuse_req_t req, int fd) | |
403 | { | |
404 | struct lo_map_elem *elem; | |
405 | ||
406 | elem = lo_map_alloc_elem(&lo_data(req)->fd_map); | |
407 | if (!elem) { | |
408 | return -1; | |
409 | } | |
410 | ||
411 | elem->fd = fd; | |
412 | return elem - lo_data(req)->fd_map.elems; | |
413 | } | |
414 | ||
b39bce12 SH |
415 | /* Assumes lo->mutex is held */ |
416 | static ssize_t lo_add_dirp_mapping(fuse_req_t req, struct lo_dirp *dirp) | |
417 | { | |
418 | struct lo_map_elem *elem; | |
419 | ||
420 | elem = lo_map_alloc_elem(&lo_data(req)->dirp_map); | |
421 | if (!elem) { | |
422 | return -1; | |
423 | } | |
424 | ||
425 | elem->dirp = dirp; | |
426 | return elem - lo_data(req)->dirp_map.elems; | |
427 | } | |
428 | ||
92fb57b8 SH |
429 | /* Assumes lo->mutex is held */ |
430 | static ssize_t lo_add_inode_mapping(fuse_req_t req, struct lo_inode *inode) | |
431 | { | |
432 | struct lo_map_elem *elem; | |
433 | ||
434 | elem = lo_map_alloc_elem(&lo_data(req)->ino_map); | |
435 | if (!elem) { | |
436 | return -1; | |
437 | } | |
438 | ||
439 | elem->inode = inode; | |
440 | return elem - lo_data(req)->ino_map.elems; | |
441 | } | |
442 | ||
7c6b6602 DDAG |
443 | static struct lo_inode *lo_inode(fuse_req_t req, fuse_ino_t ino) |
444 | { | |
92fb57b8 SH |
445 | struct lo_data *lo = lo_data(req); |
446 | struct lo_map_elem *elem; | |
447 | ||
448 | pthread_mutex_lock(&lo->mutex); | |
449 | elem = lo_map_get(&lo->ino_map, ino); | |
450 | pthread_mutex_unlock(&lo->mutex); | |
451 | ||
452 | if (!elem) { | |
453 | return NULL; | |
7387863d | 454 | } |
92fb57b8 SH |
455 | |
456 | return elem->inode; | |
7c6b6602 DDAG |
457 | } |
458 | ||
459 | static int lo_fd(fuse_req_t req, fuse_ino_t ino) | |
460 | { | |
92fb57b8 SH |
461 | struct lo_inode *inode = lo_inode(req, ino); |
462 | return inode ? inode->fd : -1; | |
7c6b6602 DDAG |
463 | } |
464 | ||
7387863d | 465 | static void lo_init(void *userdata, struct fuse_conn_info *conn) |
7c6b6602 | 466 | { |
7387863d DDAG |
467 | struct lo_data *lo = (struct lo_data *)userdata; |
468 | ||
469 | if (conn->capable & FUSE_CAP_EXPORT_SUPPORT) { | |
470 | conn->want |= FUSE_CAP_EXPORT_SUPPORT; | |
471 | } | |
472 | ||
473 | if (lo->writeback && conn->capable & FUSE_CAP_WRITEBACK_CACHE) { | |
d240314a | 474 | fuse_log(FUSE_LOG_DEBUG, "lo_init: activating writeback\n"); |
7387863d DDAG |
475 | conn->want |= FUSE_CAP_WRITEBACK_CACHE; |
476 | } | |
477 | if (lo->flock && conn->capable & FUSE_CAP_FLOCK_LOCKS) { | |
d240314a | 478 | fuse_log(FUSE_LOG_DEBUG, "lo_init: activating flock locks\n"); |
7387863d DDAG |
479 | conn->want |= FUSE_CAP_FLOCK_LOCKS; |
480 | } | |
7c6b6602 DDAG |
481 | } |
482 | ||
483 | static void lo_getattr(fuse_req_t req, fuse_ino_t ino, | |
7387863d | 484 | struct fuse_file_info *fi) |
7c6b6602 | 485 | { |
7387863d DDAG |
486 | int res; |
487 | struct stat buf; | |
488 | struct lo_data *lo = lo_data(req); | |
7c6b6602 | 489 | |
7387863d | 490 | (void)fi; |
7c6b6602 | 491 | |
7387863d DDAG |
492 | res = |
493 | fstatat(lo_fd(req, ino), "", &buf, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW); | |
494 | if (res == -1) { | |
495 | return (void)fuse_reply_err(req, errno); | |
496 | } | |
7c6b6602 | 497 | |
7387863d | 498 | fuse_reply_attr(req, &buf, lo->timeout); |
7c6b6602 DDAG |
499 | } |
500 | ||
5fe319a7 MS |
501 | static int lo_parent_and_name(struct lo_data *lo, struct lo_inode *inode, |
502 | char path[PATH_MAX], struct lo_inode **parent) | |
7c6b6602 | 503 | { |
7387863d | 504 | char procname[64]; |
5fe319a7 MS |
505 | char *last; |
506 | struct stat stat; | |
507 | struct lo_inode *p; | |
508 | int retries = 2; | |
509 | int res; | |
510 | ||
511 | retry: | |
9f59d175 | 512 | sprintf(procname, "%i", inode->fd); |
5fe319a7 | 513 | |
9f59d175 | 514 | res = readlinkat(lo->proc_self_fd, procname, path, PATH_MAX); |
5fe319a7 MS |
515 | if (res < 0) { |
516 | fuse_log(FUSE_LOG_WARNING, "%s: readlink failed: %m\n", __func__); | |
517 | goto fail_noretry; | |
518 | } | |
519 | ||
520 | if (res >= PATH_MAX) { | |
521 | fuse_log(FUSE_LOG_WARNING, "%s: readlink overflowed\n", __func__); | |
522 | goto fail_noretry; | |
523 | } | |
524 | path[res] = '\0'; | |
525 | ||
526 | last = strrchr(path, '/'); | |
527 | if (last == NULL) { | |
528 | /* Shouldn't happen */ | |
529 | fuse_log( | |
530 | FUSE_LOG_WARNING, | |
531 | "%s: INTERNAL ERROR: bad path read from proc\n", __func__); | |
532 | goto fail_noretry; | |
533 | } | |
534 | if (last == path) { | |
535 | p = &lo->root; | |
536 | pthread_mutex_lock(&lo->mutex); | |
537 | p->refcount++; | |
538 | pthread_mutex_unlock(&lo->mutex); | |
539 | } else { | |
540 | *last = '\0'; | |
541 | res = fstatat(AT_FDCWD, last == path ? "/" : path, &stat, 0); | |
542 | if (res == -1) { | |
543 | if (!retries) { | |
544 | fuse_log(FUSE_LOG_WARNING, | |
545 | "%s: failed to stat parent: %m\n", __func__); | |
546 | } | |
547 | goto fail; | |
548 | } | |
549 | p = lo_find(lo, &stat); | |
550 | if (p == NULL) { | |
551 | if (!retries) { | |
552 | fuse_log(FUSE_LOG_WARNING, | |
553 | "%s: failed to find parent\n", __func__); | |
554 | } | |
555 | goto fail; | |
556 | } | |
557 | } | |
558 | last++; | |
559 | res = fstatat(p->fd, last, &stat, AT_SYMLINK_NOFOLLOW); | |
560 | if (res == -1) { | |
561 | if (!retries) { | |
562 | fuse_log(FUSE_LOG_WARNING, | |
563 | "%s: failed to stat last\n", __func__); | |
564 | } | |
565 | goto fail_unref; | |
566 | } | |
567 | if (stat.st_dev != inode->dev || stat.st_ino != inode->ino) { | |
568 | if (!retries) { | |
569 | fuse_log(FUSE_LOG_WARNING, | |
570 | "%s: failed to match last\n", __func__); | |
571 | } | |
572 | goto fail_unref; | |
573 | } | |
574 | *parent = p; | |
575 | memmove(path, last, strlen(last) + 1); | |
576 | ||
577 | return 0; | |
578 | ||
579 | fail_unref: | |
580 | unref_inode(lo, p, 1); | |
581 | fail: | |
582 | if (retries) { | |
583 | retries--; | |
584 | goto retry; | |
585 | } | |
586 | fail_noretry: | |
587 | errno = EIO; | |
588 | return -1; | |
589 | } | |
590 | ||
591 | static int utimensat_empty(struct lo_data *lo, struct lo_inode *inode, | |
592 | const struct timespec *tv) | |
593 | { | |
594 | int res; | |
595 | struct lo_inode *parent; | |
596 | char path[PATH_MAX]; | |
7387863d DDAG |
597 | |
598 | if (inode->is_symlink) { | |
5fe319a7 | 599 | res = utimensat(inode->fd, "", tv, AT_EMPTY_PATH); |
7387863d DDAG |
600 | if (res == -1 && errno == EINVAL) { |
601 | /* Sorry, no race free way to set times on symlink. */ | |
5fe319a7 MS |
602 | if (lo->norace) { |
603 | errno = EPERM; | |
604 | } else { | |
605 | goto fallback; | |
606 | } | |
7387863d DDAG |
607 | } |
608 | return res; | |
609 | } | |
9f59d175 | 610 | sprintf(path, "%i", inode->fd); |
5fe319a7 | 611 | |
9f59d175 | 612 | return utimensat(lo->proc_self_fd, path, tv, 0); |
7387863d | 613 | |
5fe319a7 MS |
614 | fallback: |
615 | res = lo_parent_and_name(lo, inode, path, &parent); | |
616 | if (res != -1) { | |
617 | res = utimensat(parent->fd, path, tv, AT_SYMLINK_NOFOLLOW); | |
618 | unref_inode(lo, parent, 1); | |
619 | } | |
620 | ||
621 | return res; | |
7c6b6602 DDAG |
622 | } |
623 | ||
73b4d19d SH |
624 | static int lo_fi_fd(fuse_req_t req, struct fuse_file_info *fi) |
625 | { | |
626 | struct lo_data *lo = lo_data(req); | |
627 | struct lo_map_elem *elem; | |
628 | ||
629 | pthread_mutex_lock(&lo->mutex); | |
630 | elem = lo_map_get(&lo->fd_map, fi->fh); | |
631 | pthread_mutex_unlock(&lo->mutex); | |
632 | ||
633 | if (!elem) { | |
634 | return -1; | |
635 | } | |
636 | ||
637 | return elem->fd; | |
638 | } | |
639 | ||
7c6b6602 | 640 | static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr, |
7387863d | 641 | int valid, struct fuse_file_info *fi) |
7c6b6602 | 642 | { |
7387863d DDAG |
643 | int saverr; |
644 | char procname[64]; | |
5fe319a7 | 645 | struct lo_data *lo = lo_data(req); |
92fb57b8 SH |
646 | struct lo_inode *inode; |
647 | int ifd; | |
7387863d | 648 | int res; |
73b4d19d | 649 | int fd; |
7387863d | 650 | |
92fb57b8 SH |
651 | inode = lo_inode(req, ino); |
652 | if (!inode) { | |
653 | fuse_reply_err(req, EBADF); | |
654 | return; | |
655 | } | |
656 | ||
657 | ifd = inode->fd; | |
658 | ||
73b4d19d SH |
659 | /* If fi->fh is invalid we'll report EBADF later */ |
660 | if (fi) { | |
661 | fd = lo_fi_fd(req, fi); | |
662 | } | |
663 | ||
7387863d DDAG |
664 | if (valid & FUSE_SET_ATTR_MODE) { |
665 | if (fi) { | |
73b4d19d | 666 | res = fchmod(fd, attr->st_mode); |
7387863d | 667 | } else { |
9f59d175 SH |
668 | sprintf(procname, "%i", ifd); |
669 | res = fchmodat(lo->proc_self_fd, procname, attr->st_mode, 0); | |
7387863d DDAG |
670 | } |
671 | if (res == -1) { | |
672 | goto out_err; | |
673 | } | |
674 | } | |
675 | if (valid & (FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID)) { | |
676 | uid_t uid = (valid & FUSE_SET_ATTR_UID) ? attr->st_uid : (uid_t)-1; | |
677 | gid_t gid = (valid & FUSE_SET_ATTR_GID) ? attr->st_gid : (gid_t)-1; | |
678 | ||
679 | res = fchownat(ifd, "", uid, gid, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW); | |
680 | if (res == -1) { | |
681 | goto out_err; | |
682 | } | |
683 | } | |
684 | if (valid & FUSE_SET_ATTR_SIZE) { | |
9f59d175 SH |
685 | int truncfd; |
686 | ||
7387863d | 687 | if (fi) { |
9f59d175 | 688 | truncfd = fd; |
7387863d | 689 | } else { |
9f59d175 SH |
690 | sprintf(procname, "%i", ifd); |
691 | truncfd = openat(lo->proc_self_fd, procname, O_RDWR); | |
692 | if (truncfd < 0) { | |
693 | goto out_err; | |
694 | } | |
695 | } | |
696 | ||
697 | res = ftruncate(truncfd, attr->st_size); | |
698 | if (!fi) { | |
699 | saverr = errno; | |
700 | close(truncfd); | |
701 | errno = saverr; | |
7387863d DDAG |
702 | } |
703 | if (res == -1) { | |
704 | goto out_err; | |
705 | } | |
706 | } | |
707 | if (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) { | |
708 | struct timespec tv[2]; | |
709 | ||
710 | tv[0].tv_sec = 0; | |
711 | tv[1].tv_sec = 0; | |
712 | tv[0].tv_nsec = UTIME_OMIT; | |
713 | tv[1].tv_nsec = UTIME_OMIT; | |
714 | ||
715 | if (valid & FUSE_SET_ATTR_ATIME_NOW) { | |
716 | tv[0].tv_nsec = UTIME_NOW; | |
717 | } else if (valid & FUSE_SET_ATTR_ATIME) { | |
718 | tv[0] = attr->st_atim; | |
719 | } | |
720 | ||
721 | if (valid & FUSE_SET_ATTR_MTIME_NOW) { | |
722 | tv[1].tv_nsec = UTIME_NOW; | |
723 | } else if (valid & FUSE_SET_ATTR_MTIME) { | |
724 | tv[1] = attr->st_mtim; | |
725 | } | |
726 | ||
727 | if (fi) { | |
73b4d19d | 728 | res = futimens(fd, tv); |
7387863d | 729 | } else { |
5fe319a7 | 730 | res = utimensat_empty(lo, inode, tv); |
7387863d DDAG |
731 | } |
732 | if (res == -1) { | |
733 | goto out_err; | |
734 | } | |
735 | } | |
736 | ||
737 | return lo_getattr(req, ino, fi); | |
7c6b6602 DDAG |
738 | |
739 | out_err: | |
7387863d DDAG |
740 | saverr = errno; |
741 | fuse_reply_err(req, saverr); | |
7c6b6602 DDAG |
742 | } |
743 | ||
744 | static struct lo_inode *lo_find(struct lo_data *lo, struct stat *st) | |
745 | { | |
7387863d DDAG |
746 | struct lo_inode *p; |
747 | struct lo_inode *ret = NULL; | |
748 | ||
749 | pthread_mutex_lock(&lo->mutex); | |
750 | for (p = lo->root.next; p != &lo->root; p = p->next) { | |
751 | if (p->ino == st->st_ino && p->dev == st->st_dev) { | |
752 | assert(p->refcount > 0); | |
753 | ret = p; | |
754 | ret->refcount++; | |
755 | break; | |
756 | } | |
757 | } | |
758 | pthread_mutex_unlock(&lo->mutex); | |
759 | return ret; | |
7c6b6602 DDAG |
760 | } |
761 | ||
762 | static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name, | |
7387863d | 763 | struct fuse_entry_param *e) |
7c6b6602 | 764 | { |
7387863d DDAG |
765 | int newfd; |
766 | int res; | |
767 | int saverr; | |
768 | struct lo_data *lo = lo_data(req); | |
854684bc | 769 | struct lo_inode *inode, *dir = lo_inode(req, parent); |
7387863d DDAG |
770 | |
771 | memset(e, 0, sizeof(*e)); | |
772 | e->attr_timeout = lo->timeout; | |
773 | e->entry_timeout = lo->timeout; | |
774 | ||
854684bc SH |
775 | /* Do not allow escaping root directory */ |
776 | if (dir == &lo->root && strcmp(name, "..") == 0) { | |
777 | name = "."; | |
778 | } | |
779 | ||
7387863d DDAG |
780 | newfd = openat(lo_fd(req, parent), name, O_PATH | O_NOFOLLOW); |
781 | if (newfd == -1) { | |
782 | goto out_err; | |
783 | } | |
784 | ||
785 | res = fstatat(newfd, "", &e->attr, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW); | |
786 | if (res == -1) { | |
787 | goto out_err; | |
788 | } | |
789 | ||
790 | inode = lo_find(lo_data(req), &e->attr); | |
791 | if (inode) { | |
792 | close(newfd); | |
793 | newfd = -1; | |
794 | } else { | |
795 | struct lo_inode *prev, *next; | |
796 | ||
797 | saverr = ENOMEM; | |
798 | inode = calloc(1, sizeof(struct lo_inode)); | |
799 | if (!inode) { | |
800 | goto out_err; | |
801 | } | |
802 | ||
803 | inode->is_symlink = S_ISLNK(e->attr.st_mode); | |
804 | inode->refcount = 1; | |
805 | inode->fd = newfd; | |
806 | inode->ino = e->attr.st_ino; | |
807 | inode->dev = e->attr.st_dev; | |
808 | ||
809 | pthread_mutex_lock(&lo->mutex); | |
92fb57b8 | 810 | inode->fuse_ino = lo_add_inode_mapping(req, inode); |
7387863d DDAG |
811 | prev = &lo->root; |
812 | next = prev->next; | |
813 | next->prev = inode; | |
814 | inode->next = next; | |
815 | inode->prev = prev; | |
816 | prev->next = inode; | |
817 | pthread_mutex_unlock(&lo->mutex); | |
818 | } | |
92fb57b8 | 819 | e->ino = inode->fuse_ino; |
7387863d | 820 | |
d240314a EG |
821 | fuse_log(FUSE_LOG_DEBUG, " %lli/%s -> %lli\n", (unsigned long long)parent, |
822 | name, (unsigned long long)e->ino); | |
7387863d DDAG |
823 | |
824 | return 0; | |
7c6b6602 DDAG |
825 | |
826 | out_err: | |
7387863d DDAG |
827 | saverr = errno; |
828 | if (newfd != -1) { | |
829 | close(newfd); | |
830 | } | |
831 | return saverr; | |
7c6b6602 DDAG |
832 | } |
833 | ||
834 | static void lo_lookup(fuse_req_t req, fuse_ino_t parent, const char *name) | |
835 | { | |
7387863d DDAG |
836 | struct fuse_entry_param e; |
837 | int err; | |
838 | ||
d240314a EG |
839 | fuse_log(FUSE_LOG_DEBUG, "lo_lookup(parent=%" PRIu64 ", name=%s)\n", parent, |
840 | name); | |
7387863d | 841 | |
25dae28c SH |
842 | /* |
843 | * Don't use is_safe_path_component(), allow "." and ".." for NFS export | |
844 | * support. | |
845 | */ | |
846 | if (strchr(name, '/')) { | |
847 | fuse_reply_err(req, EINVAL); | |
848 | return; | |
849 | } | |
850 | ||
7387863d DDAG |
851 | err = lo_do_lookup(req, parent, name, &e); |
852 | if (err) { | |
853 | fuse_reply_err(req, err); | |
854 | } else { | |
855 | fuse_reply_entry(req, &e); | |
856 | } | |
7c6b6602 DDAG |
857 | } |
858 | ||
929cfb7a VG |
859 | /* |
860 | * On some archs, setres*id is limited to 2^16 but they | |
861 | * provide setres*id32 variants that allow 2^32. | |
862 | * Others just let setres*id do 2^32 anyway. | |
863 | */ | |
864 | #ifdef SYS_setresgid32 | |
865 | #define OURSYS_setresgid SYS_setresgid32 | |
866 | #else | |
867 | #define OURSYS_setresgid SYS_setresgid | |
868 | #endif | |
869 | ||
870 | #ifdef SYS_setresuid32 | |
871 | #define OURSYS_setresuid SYS_setresuid32 | |
872 | #else | |
873 | #define OURSYS_setresuid SYS_setresuid | |
874 | #endif | |
875 | ||
876 | /* | |
877 | * Change to uid/gid of caller so that file is created with | |
878 | * ownership of caller. | |
879 | * TODO: What about selinux context? | |
880 | */ | |
881 | static int lo_change_cred(fuse_req_t req, struct lo_cred *old) | |
882 | { | |
883 | int res; | |
884 | ||
885 | old->euid = geteuid(); | |
886 | old->egid = getegid(); | |
887 | ||
888 | res = syscall(OURSYS_setresgid, -1, fuse_req_ctx(req)->gid, -1); | |
889 | if (res == -1) { | |
890 | return errno; | |
891 | } | |
892 | ||
893 | res = syscall(OURSYS_setresuid, -1, fuse_req_ctx(req)->uid, -1); | |
894 | if (res == -1) { | |
895 | int errno_save = errno; | |
896 | ||
897 | syscall(OURSYS_setresgid, -1, old->egid, -1); | |
898 | return errno_save; | |
899 | } | |
900 | ||
901 | return 0; | |
902 | } | |
903 | ||
904 | /* Regain Privileges */ | |
905 | static void lo_restore_cred(struct lo_cred *old) | |
906 | { | |
907 | int res; | |
908 | ||
909 | res = syscall(OURSYS_setresuid, -1, old->euid, -1); | |
910 | if (res == -1) { | |
911 | fuse_log(FUSE_LOG_ERR, "seteuid(%u): %m\n", old->euid); | |
912 | exit(1); | |
913 | } | |
914 | ||
915 | res = syscall(OURSYS_setresgid, -1, old->egid, -1); | |
916 | if (res == -1) { | |
917 | fuse_log(FUSE_LOG_ERR, "setegid(%u): %m\n", old->egid); | |
918 | exit(1); | |
919 | } | |
920 | } | |
921 | ||
7c6b6602 | 922 | static void lo_mknod_symlink(fuse_req_t req, fuse_ino_t parent, |
7387863d DDAG |
923 | const char *name, mode_t mode, dev_t rdev, |
924 | const char *link) | |
7c6b6602 | 925 | { |
7387863d DDAG |
926 | int res; |
927 | int saverr; | |
92fb57b8 | 928 | struct lo_inode *dir; |
7387863d | 929 | struct fuse_entry_param e; |
929cfb7a | 930 | struct lo_cred old = {}; |
7c6b6602 | 931 | |
25dae28c SH |
932 | if (!is_safe_path_component(name)) { |
933 | fuse_reply_err(req, EINVAL); | |
934 | return; | |
935 | } | |
936 | ||
92fb57b8 SH |
937 | dir = lo_inode(req, parent); |
938 | if (!dir) { | |
939 | fuse_reply_err(req, EBADF); | |
940 | return; | |
941 | } | |
942 | ||
7387863d | 943 | saverr = ENOMEM; |
7c6b6602 | 944 | |
929cfb7a VG |
945 | saverr = lo_change_cred(req, &old); |
946 | if (saverr) { | |
947 | goto out; | |
948 | } | |
949 | ||
7387863d | 950 | res = mknod_wrapper(dir->fd, name, link, mode, rdev); |
7c6b6602 | 951 | |
7387863d | 952 | saverr = errno; |
929cfb7a VG |
953 | |
954 | lo_restore_cred(&old); | |
955 | ||
7387863d DDAG |
956 | if (res == -1) { |
957 | goto out; | |
958 | } | |
7c6b6602 | 959 | |
7387863d DDAG |
960 | saverr = lo_do_lookup(req, parent, name, &e); |
961 | if (saverr) { | |
962 | goto out; | |
963 | } | |
7c6b6602 | 964 | |
d240314a EG |
965 | fuse_log(FUSE_LOG_DEBUG, " %lli/%s -> %lli\n", (unsigned long long)parent, |
966 | name, (unsigned long long)e.ino); | |
7c6b6602 | 967 | |
7387863d DDAG |
968 | fuse_reply_entry(req, &e); |
969 | return; | |
7c6b6602 DDAG |
970 | |
971 | out: | |
7387863d | 972 | fuse_reply_err(req, saverr); |
7c6b6602 DDAG |
973 | } |
974 | ||
7387863d DDAG |
975 | static void lo_mknod(fuse_req_t req, fuse_ino_t parent, const char *name, |
976 | mode_t mode, dev_t rdev) | |
7c6b6602 | 977 | { |
7387863d | 978 | lo_mknod_symlink(req, parent, name, mode, rdev, NULL); |
7c6b6602 DDAG |
979 | } |
980 | ||
981 | static void lo_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name, | |
7387863d | 982 | mode_t mode) |
7c6b6602 | 983 | { |
7387863d | 984 | lo_mknod_symlink(req, parent, name, S_IFDIR | mode, 0, NULL); |
7c6b6602 DDAG |
985 | } |
986 | ||
7387863d DDAG |
987 | static void lo_symlink(fuse_req_t req, const char *link, fuse_ino_t parent, |
988 | const char *name) | |
7c6b6602 | 989 | { |
7387863d | 990 | lo_mknod_symlink(req, parent, name, S_IFLNK, 0, link); |
7c6b6602 DDAG |
991 | } |
992 | ||
5fe319a7 MS |
993 | static int linkat_empty_nofollow(struct lo_data *lo, struct lo_inode *inode, |
994 | int dfd, const char *name) | |
7c6b6602 | 995 | { |
7387863d | 996 | int res; |
5fe319a7 MS |
997 | struct lo_inode *parent; |
998 | char path[PATH_MAX]; | |
7c6b6602 | 999 | |
7387863d DDAG |
1000 | if (inode->is_symlink) { |
1001 | res = linkat(inode->fd, "", dfd, name, AT_EMPTY_PATH); | |
1002 | if (res == -1 && (errno == ENOENT || errno == EINVAL)) { | |
1003 | /* Sorry, no race free way to hard-link a symlink. */ | |
5fe319a7 MS |
1004 | if (lo->norace) { |
1005 | errno = EPERM; | |
1006 | } else { | |
1007 | goto fallback; | |
1008 | } | |
7387863d DDAG |
1009 | } |
1010 | return res; | |
1011 | } | |
7c6b6602 | 1012 | |
9f59d175 | 1013 | sprintf(path, "%i", inode->fd); |
5fe319a7 | 1014 | |
9f59d175 | 1015 | return linkat(lo->proc_self_fd, path, dfd, name, AT_SYMLINK_FOLLOW); |
5fe319a7 MS |
1016 | |
1017 | fallback: | |
1018 | res = lo_parent_and_name(lo, inode, path, &parent); | |
1019 | if (res != -1) { | |
1020 | res = linkat(parent->fd, path, dfd, name, 0); | |
1021 | unref_inode(lo, parent, 1); | |
1022 | } | |
7c6b6602 | 1023 | |
5fe319a7 | 1024 | return res; |
7c6b6602 DDAG |
1025 | } |
1026 | ||
1027 | static void lo_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t parent, | |
7387863d | 1028 | const char *name) |
7c6b6602 | 1029 | { |
7387863d DDAG |
1030 | int res; |
1031 | struct lo_data *lo = lo_data(req); | |
92fb57b8 | 1032 | struct lo_inode *inode; |
7387863d DDAG |
1033 | struct fuse_entry_param e; |
1034 | int saverr; | |
1035 | ||
25dae28c SH |
1036 | if (!is_safe_path_component(name)) { |
1037 | fuse_reply_err(req, EINVAL); | |
1038 | return; | |
1039 | } | |
1040 | ||
92fb57b8 SH |
1041 | inode = lo_inode(req, ino); |
1042 | if (!inode) { | |
1043 | fuse_reply_err(req, EBADF); | |
1044 | return; | |
1045 | } | |
1046 | ||
7387863d DDAG |
1047 | memset(&e, 0, sizeof(struct fuse_entry_param)); |
1048 | e.attr_timeout = lo->timeout; | |
1049 | e.entry_timeout = lo->timeout; | |
1050 | ||
5fe319a7 | 1051 | res = linkat_empty_nofollow(lo, inode, lo_fd(req, parent), name); |
7387863d DDAG |
1052 | if (res == -1) { |
1053 | goto out_err; | |
1054 | } | |
1055 | ||
1056 | res = fstatat(inode->fd, "", &e.attr, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW); | |
1057 | if (res == -1) { | |
1058 | goto out_err; | |
1059 | } | |
1060 | ||
1061 | pthread_mutex_lock(&lo->mutex); | |
1062 | inode->refcount++; | |
1063 | pthread_mutex_unlock(&lo->mutex); | |
92fb57b8 | 1064 | e.ino = inode->fuse_ino; |
7387863d | 1065 | |
d240314a EG |
1066 | fuse_log(FUSE_LOG_DEBUG, " %lli/%s -> %lli\n", (unsigned long long)parent, |
1067 | name, (unsigned long long)e.ino); | |
7387863d DDAG |
1068 | |
1069 | fuse_reply_entry(req, &e); | |
1070 | return; | |
7c6b6602 DDAG |
1071 | |
1072 | out_err: | |
7387863d DDAG |
1073 | saverr = errno; |
1074 | fuse_reply_err(req, saverr); | |
7c6b6602 DDAG |
1075 | } |
1076 | ||
1077 | static void lo_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name) | |
1078 | { | |
7387863d | 1079 | int res; |
25dae28c SH |
1080 | if (!is_safe_path_component(name)) { |
1081 | fuse_reply_err(req, EINVAL); | |
1082 | return; | |
1083 | } | |
7c6b6602 | 1084 | |
7387863d | 1085 | res = unlinkat(lo_fd(req, parent), name, AT_REMOVEDIR); |
7c6b6602 | 1086 | |
7387863d | 1087 | fuse_reply_err(req, res == -1 ? errno : 0); |
7c6b6602 DDAG |
1088 | } |
1089 | ||
1090 | static void lo_rename(fuse_req_t req, fuse_ino_t parent, const char *name, | |
7387863d DDAG |
1091 | fuse_ino_t newparent, const char *newname, |
1092 | unsigned int flags) | |
7c6b6602 | 1093 | { |
7387863d | 1094 | int res; |
7c6b6602 | 1095 | |
25dae28c SH |
1096 | if (!is_safe_path_component(name) || !is_safe_path_component(newname)) { |
1097 | fuse_reply_err(req, EINVAL); | |
1098 | return; | |
1099 | } | |
1100 | ||
7387863d DDAG |
1101 | if (flags) { |
1102 | fuse_reply_err(req, EINVAL); | |
1103 | return; | |
1104 | } | |
7c6b6602 | 1105 | |
7387863d | 1106 | res = renameat(lo_fd(req, parent), name, lo_fd(req, newparent), newname); |
7c6b6602 | 1107 | |
7387863d | 1108 | fuse_reply_err(req, res == -1 ? errno : 0); |
7c6b6602 DDAG |
1109 | } |
1110 | ||
1111 | static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name) | |
1112 | { | |
7387863d | 1113 | int res; |
7c6b6602 | 1114 | |
25dae28c SH |
1115 | if (!is_safe_path_component(name)) { |
1116 | fuse_reply_err(req, EINVAL); | |
1117 | return; | |
1118 | } | |
1119 | ||
7387863d | 1120 | res = unlinkat(lo_fd(req, parent), name, 0); |
7c6b6602 | 1121 | |
7387863d | 1122 | fuse_reply_err(req, res == -1 ? errno : 0); |
7c6b6602 DDAG |
1123 | } |
1124 | ||
1125 | static void unref_inode(struct lo_data *lo, struct lo_inode *inode, uint64_t n) | |
1126 | { | |
7387863d DDAG |
1127 | if (!inode) { |
1128 | return; | |
1129 | } | |
1130 | ||
1131 | pthread_mutex_lock(&lo->mutex); | |
1132 | assert(inode->refcount >= n); | |
1133 | inode->refcount -= n; | |
1134 | if (!inode->refcount) { | |
1135 | struct lo_inode *prev, *next; | |
1136 | ||
1137 | prev = inode->prev; | |
1138 | next = inode->next; | |
1139 | next->prev = prev; | |
1140 | prev->next = next; | |
1141 | ||
92fb57b8 | 1142 | lo_map_remove(&lo->ino_map, inode->fuse_ino); |
7387863d DDAG |
1143 | pthread_mutex_unlock(&lo->mutex); |
1144 | close(inode->fd); | |
1145 | free(inode); | |
7387863d DDAG |
1146 | } else { |
1147 | pthread_mutex_unlock(&lo->mutex); | |
1148 | } | |
7c6b6602 DDAG |
1149 | } |
1150 | ||
1151 | static void lo_forget_one(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup) | |
1152 | { | |
7387863d | 1153 | struct lo_data *lo = lo_data(req); |
92fb57b8 SH |
1154 | struct lo_inode *inode; |
1155 | ||
1156 | inode = lo_inode(req, ino); | |
1157 | if (!inode) { | |
1158 | return; | |
1159 | } | |
7c6b6602 | 1160 | |
d240314a EG |
1161 | fuse_log(FUSE_LOG_DEBUG, " forget %lli %lli -%lli\n", |
1162 | (unsigned long long)ino, (unsigned long long)inode->refcount, | |
1163 | (unsigned long long)nlookup); | |
7c6b6602 | 1164 | |
7387863d | 1165 | unref_inode(lo, inode, nlookup); |
7c6b6602 DDAG |
1166 | } |
1167 | ||
1168 | static void lo_forget(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup) | |
1169 | { | |
7387863d DDAG |
1170 | lo_forget_one(req, ino, nlookup); |
1171 | fuse_reply_none(req); | |
7c6b6602 DDAG |
1172 | } |
1173 | ||
1174 | static void lo_forget_multi(fuse_req_t req, size_t count, | |
7387863d | 1175 | struct fuse_forget_data *forgets) |
7c6b6602 | 1176 | { |
7387863d | 1177 | int i; |
7c6b6602 | 1178 | |
7387863d DDAG |
1179 | for (i = 0; i < count; i++) { |
1180 | lo_forget_one(req, forgets[i].ino, forgets[i].nlookup); | |
1181 | } | |
1182 | fuse_reply_none(req); | |
7c6b6602 DDAG |
1183 | } |
1184 | ||
1185 | static void lo_readlink(fuse_req_t req, fuse_ino_t ino) | |
1186 | { | |
7387863d DDAG |
1187 | char buf[PATH_MAX + 1]; |
1188 | int res; | |
7c6b6602 | 1189 | |
7387863d DDAG |
1190 | res = readlinkat(lo_fd(req, ino), "", buf, sizeof(buf)); |
1191 | if (res == -1) { | |
1192 | return (void)fuse_reply_err(req, errno); | |
1193 | } | |
7c6b6602 | 1194 | |
7387863d DDAG |
1195 | if (res == sizeof(buf)) { |
1196 | return (void)fuse_reply_err(req, ENAMETOOLONG); | |
1197 | } | |
7c6b6602 | 1198 | |
7387863d | 1199 | buf[res] = '\0'; |
7c6b6602 | 1200 | |
7387863d | 1201 | fuse_reply_readlink(req, buf); |
7c6b6602 DDAG |
1202 | } |
1203 | ||
1204 | struct lo_dirp { | |
7387863d DDAG |
1205 | DIR *dp; |
1206 | struct dirent *entry; | |
1207 | off_t offset; | |
7c6b6602 DDAG |
1208 | }; |
1209 | ||
b39bce12 | 1210 | static struct lo_dirp *lo_dirp(fuse_req_t req, struct fuse_file_info *fi) |
7c6b6602 | 1211 | { |
b39bce12 SH |
1212 | struct lo_data *lo = lo_data(req); |
1213 | struct lo_map_elem *elem; | |
1214 | ||
1215 | pthread_mutex_lock(&lo->mutex); | |
1216 | elem = lo_map_get(&lo->dirp_map, fi->fh); | |
1217 | pthread_mutex_unlock(&lo->mutex); | |
1218 | if (!elem) { | |
1219 | return NULL; | |
1220 | } | |
1221 | ||
1222 | return elem->dirp; | |
7c6b6602 DDAG |
1223 | } |
1224 | ||
7387863d DDAG |
1225 | static void lo_opendir(fuse_req_t req, fuse_ino_t ino, |
1226 | struct fuse_file_info *fi) | |
7c6b6602 | 1227 | { |
7387863d DDAG |
1228 | int error = ENOMEM; |
1229 | struct lo_data *lo = lo_data(req); | |
1230 | struct lo_dirp *d; | |
1231 | int fd; | |
b39bce12 | 1232 | ssize_t fh; |
7387863d DDAG |
1233 | |
1234 | d = calloc(1, sizeof(struct lo_dirp)); | |
1235 | if (d == NULL) { | |
1236 | goto out_err; | |
1237 | } | |
1238 | ||
1239 | fd = openat(lo_fd(req, ino), ".", O_RDONLY); | |
1240 | if (fd == -1) { | |
1241 | goto out_errno; | |
1242 | } | |
1243 | ||
1244 | d->dp = fdopendir(fd); | |
1245 | if (d->dp == NULL) { | |
1246 | goto out_errno; | |
1247 | } | |
1248 | ||
1249 | d->offset = 0; | |
1250 | d->entry = NULL; | |
1251 | ||
b39bce12 SH |
1252 | pthread_mutex_lock(&lo->mutex); |
1253 | fh = lo_add_dirp_mapping(req, d); | |
1254 | pthread_mutex_unlock(&lo->mutex); | |
1255 | if (fh == -1) { | |
1256 | goto out_err; | |
1257 | } | |
1258 | ||
1259 | fi->fh = fh; | |
7387863d DDAG |
1260 | if (lo->cache == CACHE_ALWAYS) { |
1261 | fi->keep_cache = 1; | |
1262 | } | |
1263 | fuse_reply_open(req, fi); | |
1264 | return; | |
7c6b6602 DDAG |
1265 | |
1266 | out_errno: | |
7387863d | 1267 | error = errno; |
7c6b6602 | 1268 | out_err: |
7387863d | 1269 | if (d) { |
b39bce12 SH |
1270 | if (d->dp) { |
1271 | closedir(d->dp); | |
1272 | } | |
7387863d DDAG |
1273 | if (fd != -1) { |
1274 | close(fd); | |
1275 | } | |
1276 | free(d); | |
1277 | } | |
1278 | fuse_reply_err(req, error); | |
7c6b6602 DDAG |
1279 | } |
1280 | ||
7c6b6602 | 1281 | static void lo_do_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, |
7387863d | 1282 | off_t offset, struct fuse_file_info *fi, int plus) |
7c6b6602 | 1283 | { |
752272da | 1284 | struct lo_data *lo = lo_data(req); |
b39bce12 | 1285 | struct lo_dirp *d; |
752272da | 1286 | struct lo_inode *dinode; |
b39bce12 | 1287 | char *buf = NULL; |
7387863d DDAG |
1288 | char *p; |
1289 | size_t rem = size; | |
752272da | 1290 | int err = EBADF; |
7387863d | 1291 | |
752272da SH |
1292 | dinode = lo_inode(req, ino); |
1293 | if (!dinode) { | |
1294 | goto error; | |
1295 | } | |
7387863d | 1296 | |
b39bce12 SH |
1297 | d = lo_dirp(req, fi); |
1298 | if (!d) { | |
1299 | goto error; | |
1300 | } | |
1301 | ||
752272da | 1302 | err = ENOMEM; |
7387863d DDAG |
1303 | buf = calloc(1, size); |
1304 | if (!buf) { | |
7387863d DDAG |
1305 | goto error; |
1306 | } | |
1307 | p = buf; | |
1308 | ||
1309 | if (offset != d->offset) { | |
1310 | seekdir(d->dp, offset); | |
1311 | d->entry = NULL; | |
1312 | d->offset = offset; | |
1313 | } | |
1314 | while (1) { | |
1315 | size_t entsize; | |
1316 | off_t nextoff; | |
1317 | const char *name; | |
1318 | ||
1319 | if (!d->entry) { | |
1320 | errno = 0; | |
1321 | d->entry = readdir(d->dp); | |
1322 | if (!d->entry) { | |
1323 | if (errno) { /* Error */ | |
1324 | err = errno; | |
1325 | goto error; | |
1326 | } else { /* End of stream */ | |
1327 | break; | |
1328 | } | |
1329 | } | |
1330 | } | |
1331 | nextoff = d->entry->d_off; | |
1332 | name = d->entry->d_name; | |
752272da | 1333 | |
7387863d | 1334 | fuse_ino_t entry_ino = 0; |
752272da SH |
1335 | struct fuse_entry_param e = (struct fuse_entry_param){ |
1336 | .attr.st_ino = d->entry->d_ino, | |
1337 | .attr.st_mode = d->entry->d_type << 12, | |
1338 | }; | |
1339 | ||
1340 | /* Hide root's parent directory */ | |
1341 | if (dinode == &lo->root && strcmp(name, "..") == 0) { | |
1342 | e.attr.st_ino = lo->root.ino; | |
1343 | e.attr.st_mode = DT_DIR << 12; | |
1344 | } | |
1345 | ||
7387863d | 1346 | if (plus) { |
752272da | 1347 | if (!is_dot_or_dotdot(name)) { |
7387863d DDAG |
1348 | err = lo_do_lookup(req, ino, name, &e); |
1349 | if (err) { | |
1350 | goto error; | |
1351 | } | |
1352 | entry_ino = e.ino; | |
1353 | } | |
1354 | ||
1355 | entsize = fuse_add_direntry_plus(req, p, rem, name, &e, nextoff); | |
1356 | } else { | |
752272da | 1357 | entsize = fuse_add_direntry(req, p, rem, name, &e.attr, nextoff); |
7387863d DDAG |
1358 | } |
1359 | if (entsize > rem) { | |
1360 | if (entry_ino != 0) { | |
1361 | lo_forget_one(req, entry_ino, 1); | |
1362 | } | |
1363 | break; | |
1364 | } | |
1365 | ||
1366 | p += entsize; | |
1367 | rem -= entsize; | |
1368 | ||
1369 | d->entry = NULL; | |
1370 | d->offset = nextoff; | |
1371 | } | |
7c6b6602 DDAG |
1372 | |
1373 | err = 0; | |
1374 | error: | |
7387863d DDAG |
1375 | /* |
1376 | * If there's an error, we can only signal it if we haven't stored | |
1377 | * any entries yet - otherwise we'd end up with wrong lookup | |
1378 | * counts for the entries that are already in the buffer. So we | |
1379 | * return what we've collected until that point. | |
1380 | */ | |
1381 | if (err && rem == size) { | |
1382 | fuse_reply_err(req, err); | |
1383 | } else { | |
1384 | fuse_reply_buf(req, buf, size - rem); | |
1385 | } | |
7c6b6602 DDAG |
1386 | free(buf); |
1387 | } | |
1388 | ||
1389 | static void lo_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, | |
7387863d | 1390 | off_t offset, struct fuse_file_info *fi) |
7c6b6602 | 1391 | { |
7387863d | 1392 | lo_do_readdir(req, ino, size, offset, fi, 0); |
7c6b6602 DDAG |
1393 | } |
1394 | ||
1395 | static void lo_readdirplus(fuse_req_t req, fuse_ino_t ino, size_t size, | |
7387863d | 1396 | off_t offset, struct fuse_file_info *fi) |
7c6b6602 | 1397 | { |
7387863d | 1398 | lo_do_readdir(req, ino, size, offset, fi, 1); |
7c6b6602 DDAG |
1399 | } |
1400 | ||
7387863d DDAG |
1401 | static void lo_releasedir(fuse_req_t req, fuse_ino_t ino, |
1402 | struct fuse_file_info *fi) | |
7c6b6602 | 1403 | { |
b39bce12 SH |
1404 | struct lo_data *lo = lo_data(req); |
1405 | struct lo_dirp *d; | |
1406 | ||
7387863d | 1407 | (void)ino; |
b39bce12 SH |
1408 | |
1409 | d = lo_dirp(req, fi); | |
1410 | if (!d) { | |
1411 | fuse_reply_err(req, EBADF); | |
1412 | return; | |
1413 | } | |
1414 | ||
1415 | pthread_mutex_lock(&lo->mutex); | |
1416 | lo_map_remove(&lo->dirp_map, fi->fh); | |
1417 | pthread_mutex_unlock(&lo->mutex); | |
1418 | ||
7387863d DDAG |
1419 | closedir(d->dp); |
1420 | free(d); | |
1421 | fuse_reply_err(req, 0); | |
7c6b6602 DDAG |
1422 | } |
1423 | ||
1424 | static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name, | |
7387863d | 1425 | mode_t mode, struct fuse_file_info *fi) |
7c6b6602 | 1426 | { |
7387863d DDAG |
1427 | int fd; |
1428 | struct lo_data *lo = lo_data(req); | |
1429 | struct fuse_entry_param e; | |
1430 | int err; | |
929cfb7a | 1431 | struct lo_cred old = {}; |
7387863d | 1432 | |
d240314a EG |
1433 | fuse_log(FUSE_LOG_DEBUG, "lo_create(parent=%" PRIu64 ", name=%s)\n", parent, |
1434 | name); | |
7387863d | 1435 | |
25dae28c SH |
1436 | if (!is_safe_path_component(name)) { |
1437 | fuse_reply_err(req, EINVAL); | |
1438 | return; | |
1439 | } | |
1440 | ||
929cfb7a VG |
1441 | err = lo_change_cred(req, &old); |
1442 | if (err) { | |
1443 | goto out; | |
1444 | } | |
1445 | ||
7387863d DDAG |
1446 | fd = openat(lo_fd(req, parent), name, (fi->flags | O_CREAT) & ~O_NOFOLLOW, |
1447 | mode); | |
929cfb7a VG |
1448 | err = fd == -1 ? errno : 0; |
1449 | lo_restore_cred(&old); | |
7387863d | 1450 | |
929cfb7a | 1451 | if (!err) { |
73b4d19d SH |
1452 | ssize_t fh; |
1453 | ||
1454 | pthread_mutex_lock(&lo->mutex); | |
1455 | fh = lo_add_fd_mapping(req, fd); | |
1456 | pthread_mutex_unlock(&lo->mutex); | |
1457 | if (fh == -1) { | |
1458 | close(fd); | |
1459 | fuse_reply_err(req, ENOMEM); | |
1460 | return; | |
1461 | } | |
1462 | ||
1463 | fi->fh = fh; | |
929cfb7a VG |
1464 | err = lo_do_lookup(req, parent, name, &e); |
1465 | } | |
7387863d DDAG |
1466 | if (lo->cache == CACHE_NEVER) { |
1467 | fi->direct_io = 1; | |
1468 | } else if (lo->cache == CACHE_ALWAYS) { | |
1469 | fi->keep_cache = 1; | |
1470 | } | |
1471 | ||
929cfb7a | 1472 | out: |
7387863d DDAG |
1473 | if (err) { |
1474 | fuse_reply_err(req, err); | |
1475 | } else { | |
1476 | fuse_reply_create(req, &e, fi); | |
1477 | } | |
7c6b6602 DDAG |
1478 | } |
1479 | ||
1480 | static void lo_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync, | |
7387863d | 1481 | struct fuse_file_info *fi) |
7c6b6602 | 1482 | { |
7387863d | 1483 | int res; |
b39bce12 SH |
1484 | struct lo_dirp *d; |
1485 | int fd; | |
1486 | ||
7387863d | 1487 | (void)ino; |
b39bce12 SH |
1488 | |
1489 | d = lo_dirp(req, fi); | |
1490 | if (!d) { | |
1491 | fuse_reply_err(req, EBADF); | |
1492 | return; | |
1493 | } | |
1494 | ||
1495 | fd = dirfd(d->dp); | |
7387863d DDAG |
1496 | if (datasync) { |
1497 | res = fdatasync(fd); | |
1498 | } else { | |
1499 | res = fsync(fd); | |
1500 | } | |
1501 | fuse_reply_err(req, res == -1 ? errno : 0); | |
7c6b6602 DDAG |
1502 | } |
1503 | ||
1504 | static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) | |
1505 | { | |
7387863d | 1506 | int fd; |
73b4d19d | 1507 | ssize_t fh; |
7387863d DDAG |
1508 | char buf[64]; |
1509 | struct lo_data *lo = lo_data(req); | |
1510 | ||
d240314a EG |
1511 | fuse_log(FUSE_LOG_DEBUG, "lo_open(ino=%" PRIu64 ", flags=%d)\n", ino, |
1512 | fi->flags); | |
7387863d DDAG |
1513 | |
1514 | /* | |
1515 | * With writeback cache, kernel may send read requests even | |
1516 | * when userspace opened write-only | |
1517 | */ | |
1518 | if (lo->writeback && (fi->flags & O_ACCMODE) == O_WRONLY) { | |
1519 | fi->flags &= ~O_ACCMODE; | |
1520 | fi->flags |= O_RDWR; | |
1521 | } | |
1522 | ||
1523 | /* | |
1524 | * With writeback cache, O_APPEND is handled by the kernel. | |
1525 | * This breaks atomicity (since the file may change in the | |
1526 | * underlying filesystem, so that the kernel's idea of the | |
1527 | * end of the file isn't accurate anymore). In this example, | |
1528 | * we just accept that. A more rigorous filesystem may want | |
1529 | * to return an error here | |
1530 | */ | |
1531 | if (lo->writeback && (fi->flags & O_APPEND)) { | |
1532 | fi->flags &= ~O_APPEND; | |
1533 | } | |
1534 | ||
9f59d175 SH |
1535 | sprintf(buf, "%i", lo_fd(req, ino)); |
1536 | fd = openat(lo->proc_self_fd, buf, fi->flags & ~O_NOFOLLOW); | |
7387863d DDAG |
1537 | if (fd == -1) { |
1538 | return (void)fuse_reply_err(req, errno); | |
1539 | } | |
1540 | ||
73b4d19d SH |
1541 | pthread_mutex_lock(&lo->mutex); |
1542 | fh = lo_add_fd_mapping(req, fd); | |
1543 | pthread_mutex_unlock(&lo->mutex); | |
1544 | if (fh == -1) { | |
1545 | close(fd); | |
1546 | fuse_reply_err(req, ENOMEM); | |
1547 | return; | |
1548 | } | |
1549 | ||
1550 | fi->fh = fh; | |
7387863d DDAG |
1551 | if (lo->cache == CACHE_NEVER) { |
1552 | fi->direct_io = 1; | |
1553 | } else if (lo->cache == CACHE_ALWAYS) { | |
1554 | fi->keep_cache = 1; | |
1555 | } | |
1556 | fuse_reply_open(req, fi); | |
7c6b6602 DDAG |
1557 | } |
1558 | ||
7387863d DDAG |
1559 | static void lo_release(fuse_req_t req, fuse_ino_t ino, |
1560 | struct fuse_file_info *fi) | |
7c6b6602 | 1561 | { |
73b4d19d SH |
1562 | struct lo_data *lo = lo_data(req); |
1563 | int fd; | |
1564 | ||
7387863d | 1565 | (void)ino; |
7c6b6602 | 1566 | |
73b4d19d SH |
1567 | fd = lo_fi_fd(req, fi); |
1568 | ||
1569 | pthread_mutex_lock(&lo->mutex); | |
1570 | lo_map_remove(&lo->fd_map, fi->fh); | |
1571 | pthread_mutex_unlock(&lo->mutex); | |
1572 | ||
1573 | close(fd); | |
7387863d | 1574 | fuse_reply_err(req, 0); |
7c6b6602 DDAG |
1575 | } |
1576 | ||
1577 | static void lo_flush(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) | |
1578 | { | |
7387863d DDAG |
1579 | int res; |
1580 | (void)ino; | |
73b4d19d | 1581 | res = close(dup(lo_fi_fd(req, fi))); |
7387863d | 1582 | fuse_reply_err(req, res == -1 ? errno : 0); |
7c6b6602 DDAG |
1583 | } |
1584 | ||
1585 | static void lo_fsync(fuse_req_t req, fuse_ino_t ino, int datasync, | |
7387863d | 1586 | struct fuse_file_info *fi) |
7c6b6602 | 1587 | { |
7387863d | 1588 | int res; |
1b209805 VG |
1589 | int fd; |
1590 | char *buf; | |
1591 | ||
1592 | fuse_log(FUSE_LOG_DEBUG, "lo_fsync(ino=%" PRIu64 ", fi=0x%p)\n", ino, | |
1593 | (void *)fi); | |
1594 | ||
1595 | if (!fi) { | |
9f59d175 SH |
1596 | struct lo_data *lo = lo_data(req); |
1597 | ||
1598 | res = asprintf(&buf, "%i", lo_fd(req, ino)); | |
1b209805 VG |
1599 | if (res == -1) { |
1600 | return (void)fuse_reply_err(req, errno); | |
1601 | } | |
1602 | ||
9f59d175 | 1603 | fd = openat(lo->proc_self_fd, buf, O_RDWR); |
1b209805 VG |
1604 | free(buf); |
1605 | if (fd == -1) { | |
1606 | return (void)fuse_reply_err(req, errno); | |
1607 | } | |
1608 | } else { | |
73b4d19d | 1609 | fd = lo_fi_fd(req, fi); |
1b209805 VG |
1610 | } |
1611 | ||
7387863d | 1612 | if (datasync) { |
1b209805 | 1613 | res = fdatasync(fd); |
7387863d | 1614 | } else { |
1b209805 VG |
1615 | res = fsync(fd); |
1616 | } | |
1617 | if (!fi) { | |
1618 | close(fd); | |
7387863d DDAG |
1619 | } |
1620 | fuse_reply_err(req, res == -1 ? errno : 0); | |
7c6b6602 DDAG |
1621 | } |
1622 | ||
7387863d DDAG |
1623 | static void lo_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t offset, |
1624 | struct fuse_file_info *fi) | |
7c6b6602 | 1625 | { |
7387863d | 1626 | struct fuse_bufvec buf = FUSE_BUFVEC_INIT(size); |
7c6b6602 | 1627 | |
d240314a EG |
1628 | fuse_log(FUSE_LOG_DEBUG, |
1629 | "lo_read(ino=%" PRIu64 ", size=%zd, " | |
1630 | "off=%lu)\n", | |
1631 | ino, size, (unsigned long)offset); | |
7c6b6602 | 1632 | |
7387863d | 1633 | buf.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK; |
73b4d19d | 1634 | buf.buf[0].fd = lo_fi_fd(req, fi); |
7387863d | 1635 | buf.buf[0].pos = offset; |
7c6b6602 | 1636 | |
8c3fe75e | 1637 | fuse_reply_data(req, &buf); |
7c6b6602 DDAG |
1638 | } |
1639 | ||
1640 | static void lo_write_buf(fuse_req_t req, fuse_ino_t ino, | |
7387863d DDAG |
1641 | struct fuse_bufvec *in_buf, off_t off, |
1642 | struct fuse_file_info *fi) | |
7c6b6602 | 1643 | { |
7387863d DDAG |
1644 | (void)ino; |
1645 | ssize_t res; | |
1646 | struct fuse_bufvec out_buf = FUSE_BUFVEC_INIT(fuse_buf_size(in_buf)); | |
ee884652 | 1647 | bool cap_fsetid_dropped = false; |
7387863d DDAG |
1648 | |
1649 | out_buf.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK; | |
73b4d19d | 1650 | out_buf.buf[0].fd = lo_fi_fd(req, fi); |
7387863d DDAG |
1651 | out_buf.buf[0].pos = off; |
1652 | ||
d240314a EG |
1653 | fuse_log(FUSE_LOG_DEBUG, |
1654 | "lo_write_buf(ino=%" PRIu64 ", size=%zd, off=%lu)\n", ino, | |
1655 | out_buf.buf[0].size, (unsigned long)off); | |
7387863d | 1656 | |
ee884652 VG |
1657 | /* |
1658 | * If kill_priv is set, drop CAP_FSETID which should lead to kernel | |
1659 | * clearing setuid/setgid on file. | |
1660 | */ | |
1661 | if (fi->kill_priv) { | |
1662 | res = drop_effective_cap("FSETID", &cap_fsetid_dropped); | |
1663 | if (res != 0) { | |
1664 | fuse_reply_err(req, res); | |
1665 | return; | |
1666 | } | |
1667 | } | |
1668 | ||
8c3fe75e | 1669 | res = fuse_buf_copy(&out_buf, in_buf); |
7387863d DDAG |
1670 | if (res < 0) { |
1671 | fuse_reply_err(req, -res); | |
1672 | } else { | |
1673 | fuse_reply_write(req, (size_t)res); | |
1674 | } | |
ee884652 VG |
1675 | |
1676 | if (cap_fsetid_dropped) { | |
1677 | res = gain_effective_cap("FSETID"); | |
1678 | if (res) { | |
1679 | fuse_log(FUSE_LOG_ERR, "Failed to gain CAP_FSETID\n"); | |
1680 | } | |
1681 | } | |
7c6b6602 DDAG |
1682 | } |
1683 | ||
1684 | static void lo_statfs(fuse_req_t req, fuse_ino_t ino) | |
1685 | { | |
7387863d DDAG |
1686 | int res; |
1687 | struct statvfs stbuf; | |
1688 | ||
1689 | res = fstatvfs(lo_fd(req, ino), &stbuf); | |
1690 | if (res == -1) { | |
1691 | fuse_reply_err(req, errno); | |
1692 | } else { | |
1693 | fuse_reply_statfs(req, &stbuf); | |
1694 | } | |
7c6b6602 DDAG |
1695 | } |
1696 | ||
7387863d DDAG |
1697 | static void lo_fallocate(fuse_req_t req, fuse_ino_t ino, int mode, off_t offset, |
1698 | off_t length, struct fuse_file_info *fi) | |
7c6b6602 | 1699 | { |
7387863d DDAG |
1700 | int err = EOPNOTSUPP; |
1701 | (void)ino; | |
7c6b6602 | 1702 | |
9776457c | 1703 | #ifdef CONFIG_FALLOCATE |
73b4d19d | 1704 | err = fallocate(lo_fi_fd(req, fi), mode, offset, length); |
7387863d DDAG |
1705 | if (err < 0) { |
1706 | err = errno; | |
1707 | } | |
7c6b6602 | 1708 | |
9776457c | 1709 | #elif defined(CONFIG_POSIX_FALLOCATE) |
7387863d DDAG |
1710 | if (mode) { |
1711 | fuse_reply_err(req, EOPNOTSUPP); | |
1712 | return; | |
1713 | } | |
7c6b6602 | 1714 | |
73b4d19d | 1715 | err = posix_fallocate(lo_fi_fd(req, fi), offset, length); |
7c6b6602 DDAG |
1716 | #endif |
1717 | ||
7387863d | 1718 | fuse_reply_err(req, err); |
7c6b6602 DDAG |
1719 | } |
1720 | ||
1721 | static void lo_flock(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi, | |
7387863d | 1722 | int op) |
7c6b6602 | 1723 | { |
7387863d DDAG |
1724 | int res; |
1725 | (void)ino; | |
7c6b6602 | 1726 | |
73b4d19d | 1727 | res = flock(lo_fi_fd(req, fi), op); |
7c6b6602 | 1728 | |
7387863d | 1729 | fuse_reply_err(req, res == -1 ? errno : 0); |
7c6b6602 DDAG |
1730 | } |
1731 | ||
1732 | static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name, | |
7387863d | 1733 | size_t size) |
7c6b6602 | 1734 | { |
9f59d175 | 1735 | struct lo_data *lo = lo_data(req); |
7387863d DDAG |
1736 | char *value = NULL; |
1737 | char procname[64]; | |
92fb57b8 | 1738 | struct lo_inode *inode; |
7387863d DDAG |
1739 | ssize_t ret; |
1740 | int saverr; | |
9f59d175 | 1741 | int fd = -1; |
7387863d | 1742 | |
92fb57b8 SH |
1743 | inode = lo_inode(req, ino); |
1744 | if (!inode) { | |
1745 | fuse_reply_err(req, EBADF); | |
1746 | return; | |
1747 | } | |
1748 | ||
7387863d DDAG |
1749 | saverr = ENOSYS; |
1750 | if (!lo_data(req)->xattr) { | |
1751 | goto out; | |
1752 | } | |
1753 | ||
d240314a EG |
1754 | fuse_log(FUSE_LOG_DEBUG, "lo_getxattr(ino=%" PRIu64 ", name=%s size=%zd)\n", |
1755 | ino, name, size); | |
7387863d DDAG |
1756 | |
1757 | if (inode->is_symlink) { | |
1758 | /* Sorry, no race free way to getxattr on symlink. */ | |
1759 | saverr = EPERM; | |
1760 | goto out; | |
1761 | } | |
1762 | ||
9f59d175 SH |
1763 | sprintf(procname, "%i", inode->fd); |
1764 | fd = openat(lo->proc_self_fd, procname, O_RDONLY); | |
1765 | if (fd < 0) { | |
1766 | goto out_err; | |
1767 | } | |
7387863d DDAG |
1768 | |
1769 | if (size) { | |
1770 | value = malloc(size); | |
1771 | if (!value) { | |
1772 | goto out_err; | |
1773 | } | |
1774 | ||
9f59d175 | 1775 | ret = fgetxattr(fd, name, value, size); |
7387863d DDAG |
1776 | if (ret == -1) { |
1777 | goto out_err; | |
1778 | } | |
1779 | saverr = 0; | |
1780 | if (ret == 0) { | |
1781 | goto out; | |
1782 | } | |
1783 | ||
1784 | fuse_reply_buf(req, value, ret); | |
1785 | } else { | |
9f59d175 | 1786 | ret = fgetxattr(fd, name, NULL, 0); |
7387863d DDAG |
1787 | if (ret == -1) { |
1788 | goto out_err; | |
1789 | } | |
1790 | ||
1791 | fuse_reply_xattr(req, ret); | |
1792 | } | |
7c6b6602 | 1793 | out_free: |
7387863d | 1794 | free(value); |
9f59d175 SH |
1795 | |
1796 | if (fd >= 0) { | |
1797 | close(fd); | |
1798 | } | |
7387863d | 1799 | return; |
7c6b6602 DDAG |
1800 | |
1801 | out_err: | |
7387863d | 1802 | saverr = errno; |
7c6b6602 | 1803 | out: |
7387863d DDAG |
1804 | fuse_reply_err(req, saverr); |
1805 | goto out_free; | |
7c6b6602 DDAG |
1806 | } |
1807 | ||
1808 | static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) | |
1809 | { | |
9f59d175 | 1810 | struct lo_data *lo = lo_data(req); |
7387863d DDAG |
1811 | char *value = NULL; |
1812 | char procname[64]; | |
92fb57b8 | 1813 | struct lo_inode *inode; |
7387863d DDAG |
1814 | ssize_t ret; |
1815 | int saverr; | |
9f59d175 | 1816 | int fd = -1; |
7387863d | 1817 | |
92fb57b8 SH |
1818 | inode = lo_inode(req, ino); |
1819 | if (!inode) { | |
1820 | fuse_reply_err(req, EBADF); | |
1821 | return; | |
1822 | } | |
1823 | ||
7387863d DDAG |
1824 | saverr = ENOSYS; |
1825 | if (!lo_data(req)->xattr) { | |
1826 | goto out; | |
1827 | } | |
1828 | ||
d240314a EG |
1829 | fuse_log(FUSE_LOG_DEBUG, "lo_listxattr(ino=%" PRIu64 ", size=%zd)\n", ino, |
1830 | size); | |
7387863d DDAG |
1831 | |
1832 | if (inode->is_symlink) { | |
1833 | /* Sorry, no race free way to listxattr on symlink. */ | |
1834 | saverr = EPERM; | |
1835 | goto out; | |
1836 | } | |
1837 | ||
9f59d175 SH |
1838 | sprintf(procname, "%i", inode->fd); |
1839 | fd = openat(lo->proc_self_fd, procname, O_RDONLY); | |
1840 | if (fd < 0) { | |
1841 | goto out_err; | |
1842 | } | |
7387863d DDAG |
1843 | |
1844 | if (size) { | |
1845 | value = malloc(size); | |
1846 | if (!value) { | |
1847 | goto out_err; | |
1848 | } | |
1849 | ||
9f59d175 | 1850 | ret = flistxattr(fd, value, size); |
7387863d DDAG |
1851 | if (ret == -1) { |
1852 | goto out_err; | |
1853 | } | |
1854 | saverr = 0; | |
1855 | if (ret == 0) { | |
1856 | goto out; | |
1857 | } | |
1858 | ||
1859 | fuse_reply_buf(req, value, ret); | |
1860 | } else { | |
9f59d175 | 1861 | ret = flistxattr(fd, NULL, 0); |
7387863d DDAG |
1862 | if (ret == -1) { |
1863 | goto out_err; | |
1864 | } | |
1865 | ||
1866 | fuse_reply_xattr(req, ret); | |
1867 | } | |
7c6b6602 | 1868 | out_free: |
7387863d | 1869 | free(value); |
9f59d175 SH |
1870 | |
1871 | if (fd >= 0) { | |
1872 | close(fd); | |
1873 | } | |
7387863d | 1874 | return; |
7c6b6602 DDAG |
1875 | |
1876 | out_err: | |
7387863d | 1877 | saverr = errno; |
7c6b6602 | 1878 | out: |
7387863d DDAG |
1879 | fuse_reply_err(req, saverr); |
1880 | goto out_free; | |
7c6b6602 DDAG |
1881 | } |
1882 | ||
1883 | static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name, | |
7387863d | 1884 | const char *value, size_t size, int flags) |
7c6b6602 | 1885 | { |
7387863d | 1886 | char procname[64]; |
9f59d175 | 1887 | struct lo_data *lo = lo_data(req); |
92fb57b8 | 1888 | struct lo_inode *inode; |
7387863d DDAG |
1889 | ssize_t ret; |
1890 | int saverr; | |
9f59d175 | 1891 | int fd = -1; |
7c6b6602 | 1892 | |
92fb57b8 SH |
1893 | inode = lo_inode(req, ino); |
1894 | if (!inode) { | |
1895 | fuse_reply_err(req, EBADF); | |
1896 | return; | |
1897 | } | |
1898 | ||
7387863d DDAG |
1899 | saverr = ENOSYS; |
1900 | if (!lo_data(req)->xattr) { | |
1901 | goto out; | |
1902 | } | |
7c6b6602 | 1903 | |
d240314a EG |
1904 | fuse_log(FUSE_LOG_DEBUG, "lo_setxattr(ino=%" PRIu64 |
1905 | ", name=%s value=%s size=%zd)\n", ino, name, value, size); | |
7c6b6602 | 1906 | |
7387863d DDAG |
1907 | if (inode->is_symlink) { |
1908 | /* Sorry, no race free way to setxattr on symlink. */ | |
1909 | saverr = EPERM; | |
1910 | goto out; | |
1911 | } | |
7c6b6602 | 1912 | |
9f59d175 SH |
1913 | sprintf(procname, "%i", inode->fd); |
1914 | fd = openat(lo->proc_self_fd, procname, O_RDWR); | |
1915 | if (fd < 0) { | |
1916 | saverr = errno; | |
1917 | goto out; | |
1918 | } | |
7c6b6602 | 1919 | |
9f59d175 | 1920 | ret = fsetxattr(fd, name, value, size, flags); |
7387863d | 1921 | saverr = ret == -1 ? errno : 0; |
7c6b6602 DDAG |
1922 | |
1923 | out: | |
9f59d175 SH |
1924 | if (fd >= 0) { |
1925 | close(fd); | |
1926 | } | |
7387863d | 1927 | fuse_reply_err(req, saverr); |
7c6b6602 DDAG |
1928 | } |
1929 | ||
1930 | static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name) | |
1931 | { | |
7387863d | 1932 | char procname[64]; |
9f59d175 | 1933 | struct lo_data *lo = lo_data(req); |
92fb57b8 | 1934 | struct lo_inode *inode; |
7387863d DDAG |
1935 | ssize_t ret; |
1936 | int saverr; | |
9f59d175 | 1937 | int fd = -1; |
7c6b6602 | 1938 | |
92fb57b8 SH |
1939 | inode = lo_inode(req, ino); |
1940 | if (!inode) { | |
1941 | fuse_reply_err(req, EBADF); | |
1942 | return; | |
1943 | } | |
1944 | ||
7387863d DDAG |
1945 | saverr = ENOSYS; |
1946 | if (!lo_data(req)->xattr) { | |
1947 | goto out; | |
1948 | } | |
7c6b6602 | 1949 | |
d240314a EG |
1950 | fuse_log(FUSE_LOG_DEBUG, "lo_removexattr(ino=%" PRIu64 ", name=%s)\n", ino, |
1951 | name); | |
7c6b6602 | 1952 | |
7387863d DDAG |
1953 | if (inode->is_symlink) { |
1954 | /* Sorry, no race free way to setxattr on symlink. */ | |
1955 | saverr = EPERM; | |
1956 | goto out; | |
1957 | } | |
7c6b6602 | 1958 | |
9f59d175 SH |
1959 | sprintf(procname, "%i", inode->fd); |
1960 | fd = openat(lo->proc_self_fd, procname, O_RDWR); | |
1961 | if (fd < 0) { | |
1962 | saverr = errno; | |
1963 | goto out; | |
1964 | } | |
7c6b6602 | 1965 | |
9f59d175 | 1966 | ret = fremovexattr(fd, name); |
7387863d | 1967 | saverr = ret == -1 ? errno : 0; |
7c6b6602 DDAG |
1968 | |
1969 | out: | |
9f59d175 SH |
1970 | if (fd >= 0) { |
1971 | close(fd); | |
1972 | } | |
7387863d | 1973 | fuse_reply_err(req, saverr); |
7c6b6602 DDAG |
1974 | } |
1975 | ||
1976 | #ifdef HAVE_COPY_FILE_RANGE | |
1977 | static void lo_copy_file_range(fuse_req_t req, fuse_ino_t ino_in, off_t off_in, | |
7387863d DDAG |
1978 | struct fuse_file_info *fi_in, fuse_ino_t ino_out, |
1979 | off_t off_out, struct fuse_file_info *fi_out, | |
1980 | size_t len, int flags) | |
7c6b6602 | 1981 | { |
73b4d19d | 1982 | int in_fd, out_fd; |
7387863d DDAG |
1983 | ssize_t res; |
1984 | ||
73b4d19d SH |
1985 | in_fd = lo_fi_fd(req, fi_in); |
1986 | out_fd = lo_fi_fd(req, fi_out); | |
1987 | ||
1988 | fuse_log(FUSE_LOG_DEBUG, | |
1989 | "lo_copy_file_range(ino=%" PRIu64 "/fd=%d, " | |
1990 | "off=%lu, ino=%" PRIu64 "/fd=%d, " | |
1991 | "off=%lu, size=%zd, flags=0x%x)\n", | |
1992 | ino_in, in_fd, off_in, ino_out, out_fd, off_out, len, flags); | |
7387863d | 1993 | |
73b4d19d | 1994 | res = copy_file_range(in_fd, &off_in, out_fd, &off_out, len, flags); |
7387863d DDAG |
1995 | if (res < 0) { |
1996 | fuse_reply_err(req, -errno); | |
1997 | } else { | |
1998 | fuse_reply_write(req, res); | |
1999 | } | |
7c6b6602 DDAG |
2000 | } |
2001 | #endif | |
2002 | ||
2003 | static void lo_lseek(fuse_req_t req, fuse_ino_t ino, off_t off, int whence, | |
7387863d | 2004 | struct fuse_file_info *fi) |
7c6b6602 | 2005 | { |
7387863d DDAG |
2006 | off_t res; |
2007 | ||
2008 | (void)ino; | |
73b4d19d | 2009 | res = lseek(lo_fi_fd(req, fi), off, whence); |
7387863d DDAG |
2010 | if (res != -1) { |
2011 | fuse_reply_lseek(req, res); | |
2012 | } else { | |
2013 | fuse_reply_err(req, errno); | |
2014 | } | |
7c6b6602 DDAG |
2015 | } |
2016 | ||
2017 | static struct fuse_lowlevel_ops lo_oper = { | |
7387863d DDAG |
2018 | .init = lo_init, |
2019 | .lookup = lo_lookup, | |
2020 | .mkdir = lo_mkdir, | |
2021 | .mknod = lo_mknod, | |
2022 | .symlink = lo_symlink, | |
2023 | .link = lo_link, | |
2024 | .unlink = lo_unlink, | |
2025 | .rmdir = lo_rmdir, | |
2026 | .rename = lo_rename, | |
2027 | .forget = lo_forget, | |
2028 | .forget_multi = lo_forget_multi, | |
2029 | .getattr = lo_getattr, | |
2030 | .setattr = lo_setattr, | |
2031 | .readlink = lo_readlink, | |
2032 | .opendir = lo_opendir, | |
2033 | .readdir = lo_readdir, | |
2034 | .readdirplus = lo_readdirplus, | |
2035 | .releasedir = lo_releasedir, | |
2036 | .fsyncdir = lo_fsyncdir, | |
2037 | .create = lo_create, | |
2038 | .open = lo_open, | |
2039 | .release = lo_release, | |
2040 | .flush = lo_flush, | |
2041 | .fsync = lo_fsync, | |
2042 | .read = lo_read, | |
2043 | .write_buf = lo_write_buf, | |
2044 | .statfs = lo_statfs, | |
2045 | .fallocate = lo_fallocate, | |
2046 | .flock = lo_flock, | |
2047 | .getxattr = lo_getxattr, | |
2048 | .listxattr = lo_listxattr, | |
2049 | .setxattr = lo_setxattr, | |
2050 | .removexattr = lo_removexattr, | |
7c6b6602 | 2051 | #ifdef HAVE_COPY_FILE_RANGE |
7387863d | 2052 | .copy_file_range = lo_copy_file_range, |
7c6b6602 | 2053 | #endif |
7387863d | 2054 | .lseek = lo_lseek, |
7c6b6602 DDAG |
2055 | }; |
2056 | ||
45018fbb SH |
2057 | /* Print vhost-user.json backend program capabilities */ |
2058 | static void print_capabilities(void) | |
2059 | { | |
2060 | printf("{\n"); | |
2061 | printf(" \"type\": \"fs\"\n"); | |
2062 | printf("}\n"); | |
2063 | } | |
2064 | ||
d74830d1 | 2065 | /* |
8e1d4ef2 | 2066 | * Move to a new mount, net, and pid namespaces to isolate this process. |
d74830d1 | 2067 | */ |
8e1d4ef2 | 2068 | static void setup_namespaces(struct lo_data *lo, struct fuse_session *se) |
d74830d1 | 2069 | { |
8e1d4ef2 SH |
2070 | pid_t child; |
2071 | ||
2072 | /* | |
2073 | * Create a new pid namespace for *child* processes. We'll have to | |
2074 | * fork in order to enter the new pid namespace. A new mount namespace | |
2075 | * is also needed so that we can remount /proc for the new pid | |
2076 | * namespace. | |
2077 | * | |
2078 | * Our UNIX domain sockets have been created. Now we can move to | |
2079 | * an empty network namespace to prevent TCP/IP and other network | |
2080 | * activity in case this process is compromised. | |
2081 | */ | |
2082 | if (unshare(CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWNET) != 0) { | |
2083 | fuse_log(FUSE_LOG_ERR, "unshare(CLONE_NEWPID | CLONE_NEWNS): %m\n"); | |
2084 | exit(1); | |
2085 | } | |
2086 | ||
2087 | child = fork(); | |
2088 | if (child < 0) { | |
2089 | fuse_log(FUSE_LOG_ERR, "fork() failed: %m\n"); | |
2090 | exit(1); | |
2091 | } | |
2092 | if (child > 0) { | |
2093 | pid_t waited; | |
2094 | int wstatus; | |
2095 | ||
2096 | /* The parent waits for the child */ | |
2097 | do { | |
2098 | waited = waitpid(child, &wstatus, 0); | |
2099 | } while (waited < 0 && errno == EINTR && !se->exited); | |
2100 | ||
2101 | /* We were terminated by a signal, see fuse_signals.c */ | |
2102 | if (se->exited) { | |
2103 | exit(0); | |
2104 | } | |
2105 | ||
2106 | if (WIFEXITED(wstatus)) { | |
2107 | exit(WEXITSTATUS(wstatus)); | |
2108 | } | |
2109 | ||
2110 | exit(1); | |
2111 | } | |
2112 | ||
2113 | /* Send us SIGTERM when the parent thread terminates, see prctl(2) */ | |
2114 | prctl(PR_SET_PDEATHSIG, SIGTERM); | |
2115 | ||
2116 | /* | |
2117 | * If the mounts have shared propagation then we want to opt out so our | |
2118 | * mount changes don't affect the parent mount namespace. | |
2119 | */ | |
2120 | if (mount(NULL, "/", NULL, MS_REC | MS_SLAVE, NULL) < 0) { | |
2121 | fuse_log(FUSE_LOG_ERR, "mount(/, MS_REC|MS_SLAVE): %m\n"); | |
2122 | exit(1); | |
2123 | } | |
2124 | ||
2125 | /* The child must remount /proc to use the new pid namespace */ | |
2126 | if (mount("proc", "/proc", "proc", | |
2127 | MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) { | |
2128 | fuse_log(FUSE_LOG_ERR, "mount(/proc): %m\n"); | |
2129 | exit(1); | |
2130 | } | |
2131 | ||
2132 | /* Now we can get our /proc/self/fd directory file descriptor */ | |
2133 | lo->proc_self_fd = open("/proc/self/fd", O_PATH); | |
2134 | if (lo->proc_self_fd == -1) { | |
2135 | fuse_log(FUSE_LOG_ERR, "open(/proc/self/fd, O_PATH): %m\n"); | |
d74830d1 SH |
2136 | exit(1); |
2137 | } | |
2138 | } | |
2139 | ||
2405f3c0 DDAG |
2140 | /* |
2141 | * Capture the capability state, we'll need to restore this for individual | |
2142 | * threads later; see load_capng. | |
2143 | */ | |
2144 | static void setup_capng(void) | |
2145 | { | |
2146 | /* Note this accesses /proc so has to happen before the sandbox */ | |
2147 | if (capng_get_caps_process()) { | |
2148 | fuse_log(FUSE_LOG_ERR, "capng_get_caps_process\n"); | |
2149 | exit(1); | |
2150 | } | |
2151 | pthread_mutex_init(&cap.mutex, NULL); | |
2152 | pthread_mutex_lock(&cap.mutex); | |
2153 | cap.saved = capng_save_state(); | |
2154 | if (!cap.saved) { | |
2155 | fuse_log(FUSE_LOG_ERR, "capng_save_state\n"); | |
2156 | exit(1); | |
2157 | } | |
2158 | pthread_mutex_unlock(&cap.mutex); | |
2159 | } | |
2160 | ||
2161 | static void cleanup_capng(void) | |
2162 | { | |
2163 | free(cap.saved); | |
2164 | cap.saved = NULL; | |
2165 | pthread_mutex_destroy(&cap.mutex); | |
2166 | } | |
2167 | ||
2168 | ||
8e1d4ef2 SH |
2169 | /* |
2170 | * Make the source directory our root so symlinks cannot escape and no other | |
2171 | * files are accessible. Assumes unshare(CLONE_NEWNS) was already called. | |
2172 | */ | |
2173 | static void setup_mounts(const char *source) | |
5baa3b8e SH |
2174 | { |
2175 | int oldroot; | |
2176 | int newroot; | |
2177 | ||
8e1d4ef2 SH |
2178 | if (mount(source, source, NULL, MS_BIND, NULL) < 0) { |
2179 | fuse_log(FUSE_LOG_ERR, "mount(%s, %s, MS_BIND): %m\n", source, source); | |
2180 | exit(1); | |
2181 | } | |
2182 | ||
2183 | /* This magic is based on lxc's lxc_pivot_root() */ | |
5baa3b8e SH |
2184 | oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC); |
2185 | if (oldroot < 0) { | |
2186 | fuse_log(FUSE_LOG_ERR, "open(/): %m\n"); | |
2187 | exit(1); | |
2188 | } | |
2189 | ||
2190 | newroot = open(source, O_DIRECTORY | O_RDONLY | O_CLOEXEC); | |
2191 | if (newroot < 0) { | |
2192 | fuse_log(FUSE_LOG_ERR, "open(%s): %m\n", source); | |
2193 | exit(1); | |
2194 | } | |
2195 | ||
2196 | if (fchdir(newroot) < 0) { | |
2197 | fuse_log(FUSE_LOG_ERR, "fchdir(newroot): %m\n"); | |
2198 | exit(1); | |
2199 | } | |
2200 | ||
2201 | if (syscall(__NR_pivot_root, ".", ".") < 0) { | |
2202 | fuse_log(FUSE_LOG_ERR, "pivot_root(., .): %m\n"); | |
2203 | exit(1); | |
2204 | } | |
2205 | ||
2206 | if (fchdir(oldroot) < 0) { | |
2207 | fuse_log(FUSE_LOG_ERR, "fchdir(oldroot): %m\n"); | |
2208 | exit(1); | |
2209 | } | |
2210 | ||
2211 | if (mount("", ".", "", MS_SLAVE | MS_REC, NULL) < 0) { | |
2212 | fuse_log(FUSE_LOG_ERR, "mount(., MS_SLAVE | MS_REC): %m\n"); | |
2213 | exit(1); | |
2214 | } | |
2215 | ||
2216 | if (umount2(".", MNT_DETACH) < 0) { | |
2217 | fuse_log(FUSE_LOG_ERR, "umount2(., MNT_DETACH): %m\n"); | |
2218 | exit(1); | |
2219 | } | |
2220 | ||
2221 | if (fchdir(newroot) < 0) { | |
2222 | fuse_log(FUSE_LOG_ERR, "fchdir(newroot): %m\n"); | |
2223 | exit(1); | |
2224 | } | |
2225 | ||
2226 | close(newroot); | |
2227 | close(oldroot); | |
2228 | } | |
2229 | ||
5baa3b8e SH |
2230 | /* |
2231 | * Lock down this process to prevent access to other processes or files outside | |
2232 | * source directory. This reduces the impact of arbitrary code execution bugs. | |
2233 | */ | |
f185621d SH |
2234 | static void setup_sandbox(struct lo_data *lo, struct fuse_session *se, |
2235 | bool enable_syslog) | |
5baa3b8e | 2236 | { |
8e1d4ef2 SH |
2237 | setup_namespaces(lo, se); |
2238 | setup_mounts(lo->source); | |
f185621d | 2239 | setup_seccomp(enable_syslog); |
5baa3b8e SH |
2240 | } |
2241 | ||
01a6dc95 SH |
2242 | /* Raise the maximum number of open file descriptors */ |
2243 | static void setup_nofile_rlimit(void) | |
2244 | { | |
2245 | const rlim_t max_fds = 1000000; | |
2246 | struct rlimit rlim; | |
2247 | ||
2248 | if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) { | |
2249 | fuse_log(FUSE_LOG_ERR, "getrlimit(RLIMIT_NOFILE): %m\n"); | |
2250 | exit(1); | |
2251 | } | |
2252 | ||
2253 | if (rlim.rlim_cur >= max_fds) { | |
2254 | return; /* nothing to do */ | |
2255 | } | |
2256 | ||
2257 | rlim.rlim_cur = max_fds; | |
2258 | rlim.rlim_max = max_fds; | |
2259 | ||
2260 | if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) { | |
2261 | /* Ignore SELinux denials */ | |
2262 | if (errno == EPERM) { | |
2263 | return; | |
2264 | } | |
2265 | ||
2266 | fuse_log(FUSE_LOG_ERR, "setrlimit(RLIMIT_NOFILE): %m\n"); | |
2267 | exit(1); | |
2268 | } | |
2269 | } | |
2270 | ||
f185621d SH |
2271 | static void log_func(enum fuse_log_level level, const char *fmt, va_list ap) |
2272 | { | |
36f38469 MM |
2273 | g_autofree char *localfmt = NULL; |
2274 | ||
d240314a EG |
2275 | if (current_log_level < level) { |
2276 | return; | |
2277 | } | |
2278 | ||
36f38469 | 2279 | if (current_log_level == FUSE_LOG_DEBUG) { |
50fb955a MM |
2280 | if (!use_syslog) { |
2281 | localfmt = g_strdup_printf("[%" PRId64 "] [ID: %08ld] %s", | |
2282 | get_clock(), syscall(__NR_gettid), fmt); | |
2283 | } else { | |
2284 | localfmt = g_strdup_printf("[ID: %08ld] %s", syscall(__NR_gettid), | |
2285 | fmt); | |
2286 | } | |
36f38469 MM |
2287 | fmt = localfmt; |
2288 | } | |
2289 | ||
f185621d SH |
2290 | if (use_syslog) { |
2291 | int priority = LOG_ERR; | |
2292 | switch (level) { | |
2293 | case FUSE_LOG_EMERG: | |
2294 | priority = LOG_EMERG; | |
2295 | break; | |
2296 | case FUSE_LOG_ALERT: | |
2297 | priority = LOG_ALERT; | |
2298 | break; | |
2299 | case FUSE_LOG_CRIT: | |
2300 | priority = LOG_CRIT; | |
2301 | break; | |
2302 | case FUSE_LOG_ERR: | |
2303 | priority = LOG_ERR; | |
2304 | break; | |
2305 | case FUSE_LOG_WARNING: | |
2306 | priority = LOG_WARNING; | |
2307 | break; | |
2308 | case FUSE_LOG_NOTICE: | |
2309 | priority = LOG_NOTICE; | |
2310 | break; | |
2311 | case FUSE_LOG_INFO: | |
2312 | priority = LOG_INFO; | |
2313 | break; | |
2314 | case FUSE_LOG_DEBUG: | |
2315 | priority = LOG_DEBUG; | |
2316 | break; | |
2317 | } | |
2318 | vsyslog(priority, fmt, ap); | |
2319 | } else { | |
2320 | vfprintf(stderr, fmt, ap); | |
2321 | } | |
2322 | } | |
2323 | ||
7c6b6602 DDAG |
2324 | int main(int argc, char *argv[]) |
2325 | { | |
7387863d DDAG |
2326 | struct fuse_args args = FUSE_ARGS_INIT(argc, argv); |
2327 | struct fuse_session *se; | |
2328 | struct fuse_cmdline_opts opts; | |
9f59d175 SH |
2329 | struct lo_data lo = { |
2330 | .debug = 0, | |
2331 | .writeback = 0, | |
2332 | .proc_self_fd = -1, | |
2333 | }; | |
92fb57b8 | 2334 | struct lo_map_elem *root_elem; |
7387863d DDAG |
2335 | int ret = -1; |
2336 | ||
2337 | /* Don't mask creation mode, kernel already did that */ | |
2338 | umask(0); | |
2339 | ||
2340 | pthread_mutex_init(&lo.mutex, NULL); | |
2341 | lo.root.next = lo.root.prev = &lo.root; | |
2342 | lo.root.fd = -1; | |
92fb57b8 | 2343 | lo.root.fuse_ino = FUSE_ROOT_ID; |
7387863d DDAG |
2344 | lo.cache = CACHE_NORMAL; |
2345 | ||
92fb57b8 SH |
2346 | /* |
2347 | * Set up the ino map like this: | |
2348 | * [0] Reserved (will not be used) | |
2349 | * [1] Root inode | |
2350 | */ | |
2351 | lo_map_init(&lo.ino_map); | |
2352 | lo_map_reserve(&lo.ino_map, 0)->in_use = false; | |
2353 | root_elem = lo_map_reserve(&lo.ino_map, lo.root.fuse_ino); | |
2354 | root_elem->inode = &lo.root; | |
2355 | ||
b39bce12 | 2356 | lo_map_init(&lo.dirp_map); |
73b4d19d | 2357 | lo_map_init(&lo.fd_map); |
b39bce12 | 2358 | |
7387863d DDAG |
2359 | if (fuse_parse_cmdline(&args, &opts) != 0) { |
2360 | return 1; | |
2361 | } | |
f185621d SH |
2362 | fuse_set_log_func(log_func); |
2363 | use_syslog = opts.syslog; | |
2364 | if (use_syslog) { | |
2365 | openlog("virtiofsd", LOG_PID, LOG_DAEMON); | |
2366 | } | |
7387863d | 2367 | if (opts.show_help) { |
67aab022 | 2368 | printf("usage: %s [options]\n\n", argv[0]); |
7387863d | 2369 | fuse_cmdline_help(); |
4ff075f7 | 2370 | printf(" -o source=PATH shared directory tree\n"); |
7387863d DDAG |
2371 | fuse_lowlevel_help(); |
2372 | ret = 0; | |
2373 | goto err_out1; | |
2374 | } else if (opts.show_version) { | |
2375 | fuse_lowlevel_version(); | |
2376 | ret = 0; | |
2377 | goto err_out1; | |
45018fbb SH |
2378 | } else if (opts.print_capabilities) { |
2379 | print_capabilities(); | |
2380 | ret = 0; | |
2381 | goto err_out1; | |
7387863d DDAG |
2382 | } |
2383 | ||
7387863d DDAG |
2384 | if (fuse_opt_parse(&args, &lo, lo_opts, NULL) == -1) { |
2385 | return 1; | |
2386 | } | |
2387 | ||
d240314a EG |
2388 | /* |
2389 | * log_level is 0 if not configured via cmd options (0 is LOG_EMERG, | |
2390 | * and we don't use this log level). | |
2391 | */ | |
2392 | if (opts.log_level != 0) { | |
2393 | current_log_level = opts.log_level; | |
2394 | } | |
7387863d | 2395 | lo.debug = opts.debug; |
d240314a EG |
2396 | if (lo.debug) { |
2397 | current_log_level = FUSE_LOG_DEBUG; | |
2398 | } | |
7387863d | 2399 | lo.root.refcount = 2; |
d240314a | 2400 | |
7387863d DDAG |
2401 | if (lo.source) { |
2402 | struct stat stat; | |
2403 | int res; | |
2404 | ||
2405 | res = lstat(lo.source, &stat); | |
2406 | if (res == -1) { | |
2407 | fuse_log(FUSE_LOG_ERR, "failed to stat source (\"%s\"): %m\n", | |
2408 | lo.source); | |
2409 | exit(1); | |
2410 | } | |
2411 | if (!S_ISDIR(stat.st_mode)) { | |
2412 | fuse_log(FUSE_LOG_ERR, "source is not a directory\n"); | |
2413 | exit(1); | |
2414 | } | |
2415 | ||
2416 | } else { | |
2417 | lo.source = "/"; | |
2418 | } | |
2419 | lo.root.is_symlink = false; | |
2420 | if (!lo.timeout_set) { | |
2421 | switch (lo.cache) { | |
2422 | case CACHE_NEVER: | |
2423 | lo.timeout = 0.0; | |
2424 | break; | |
2425 | ||
2426 | case CACHE_NORMAL: | |
2427 | lo.timeout = 1.0; | |
2428 | break; | |
2429 | ||
2430 | case CACHE_ALWAYS: | |
2431 | lo.timeout = 86400.0; | |
2432 | break; | |
2433 | } | |
2434 | } else if (lo.timeout < 0) { | |
2435 | fuse_log(FUSE_LOG_ERR, "timeout is negative (%lf)\n", lo.timeout); | |
2436 | exit(1); | |
2437 | } | |
2438 | ||
2439 | lo.root.fd = open(lo.source, O_PATH); | |
5baa3b8e | 2440 | |
7387863d DDAG |
2441 | if (lo.root.fd == -1) { |
2442 | fuse_log(FUSE_LOG_ERR, "open(\"%s\", O_PATH): %m\n", lo.source); | |
2443 | exit(1); | |
2444 | } | |
2445 | ||
2446 | se = fuse_session_new(&args, &lo_oper, sizeof(lo_oper), &lo); | |
2447 | if (se == NULL) { | |
2448 | goto err_out1; | |
2449 | } | |
2450 | ||
2451 | if (fuse_set_signal_handlers(se) != 0) { | |
2452 | goto err_out2; | |
2453 | } | |
2454 | ||
67aab022 | 2455 | if (fuse_session_mount(se) != 0) { |
7387863d DDAG |
2456 | goto err_out3; |
2457 | } | |
2458 | ||
2459 | fuse_daemonize(opts.foreground); | |
2460 | ||
01a6dc95 SH |
2461 | setup_nofile_rlimit(); |
2462 | ||
2405f3c0 DDAG |
2463 | /* Must be before sandbox since it wants /proc */ |
2464 | setup_capng(); | |
2465 | ||
f185621d | 2466 | setup_sandbox(&lo, se, opts.syslog); |
5baa3b8e | 2467 | |
7387863d | 2468 | /* Block until ctrl+c or fusermount -u */ |
f6f3573c | 2469 | ret = virtio_loop(se); |
7387863d DDAG |
2470 | |
2471 | fuse_session_unmount(se); | |
2405f3c0 | 2472 | cleanup_capng(); |
7c6b6602 | 2473 | err_out3: |
7387863d | 2474 | fuse_remove_signal_handlers(se); |
7c6b6602 | 2475 | err_out2: |
7387863d | 2476 | fuse_session_destroy(se); |
7c6b6602 | 2477 | err_out1: |
7387863d | 2478 | fuse_opt_free_args(&args); |
7c6b6602 | 2479 | |
73b4d19d | 2480 | lo_map_destroy(&lo.fd_map); |
b39bce12 | 2481 | lo_map_destroy(&lo.dirp_map); |
92fb57b8 SH |
2482 | lo_map_destroy(&lo.ino_map); |
2483 | ||
9f59d175 SH |
2484 | if (lo.proc_self_fd >= 0) { |
2485 | close(lo.proc_self_fd); | |
2486 | } | |
2487 | ||
7387863d DDAG |
2488 | if (lo.root.fd >= 0) { |
2489 | close(lo.root.fd); | |
2490 | } | |
7c6b6602 | 2491 | |
7387863d | 2492 | return ret ? 1 : 0; |
7c6b6602 | 2493 | } |