]>
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 | 1101 | if (flags) { |
f0ab7d6f | 1102 | #ifndef SYS_renameat2 |
7387863d | 1103 | fuse_reply_err(req, EINVAL); |
f0ab7d6f MS |
1104 | #else |
1105 | res = syscall(SYS_renameat2, lo_fd(req, parent), name, | |
1106 | lo_fd(req, newparent), newname, flags); | |
1107 | if (res == -1 && errno == ENOSYS) { | |
1108 | fuse_reply_err(req, EINVAL); | |
1109 | } else { | |
1110 | fuse_reply_err(req, res == -1 ? errno : 0); | |
1111 | } | |
1112 | #endif | |
7387863d DDAG |
1113 | return; |
1114 | } | |
7c6b6602 | 1115 | |
7387863d | 1116 | res = renameat(lo_fd(req, parent), name, lo_fd(req, newparent), newname); |
7c6b6602 | 1117 | |
7387863d | 1118 | fuse_reply_err(req, res == -1 ? errno : 0); |
7c6b6602 DDAG |
1119 | } |
1120 | ||
1121 | static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name) | |
1122 | { | |
7387863d | 1123 | int res; |
7c6b6602 | 1124 | |
25dae28c SH |
1125 | if (!is_safe_path_component(name)) { |
1126 | fuse_reply_err(req, EINVAL); | |
1127 | return; | |
1128 | } | |
1129 | ||
7387863d | 1130 | res = unlinkat(lo_fd(req, parent), name, 0); |
7c6b6602 | 1131 | |
7387863d | 1132 | fuse_reply_err(req, res == -1 ? errno : 0); |
7c6b6602 DDAG |
1133 | } |
1134 | ||
1135 | static void unref_inode(struct lo_data *lo, struct lo_inode *inode, uint64_t n) | |
1136 | { | |
7387863d DDAG |
1137 | if (!inode) { |
1138 | return; | |
1139 | } | |
1140 | ||
1141 | pthread_mutex_lock(&lo->mutex); | |
1142 | assert(inode->refcount >= n); | |
1143 | inode->refcount -= n; | |
1144 | if (!inode->refcount) { | |
1145 | struct lo_inode *prev, *next; | |
1146 | ||
1147 | prev = inode->prev; | |
1148 | next = inode->next; | |
1149 | next->prev = prev; | |
1150 | prev->next = next; | |
1151 | ||
92fb57b8 | 1152 | lo_map_remove(&lo->ino_map, inode->fuse_ino); |
7387863d DDAG |
1153 | pthread_mutex_unlock(&lo->mutex); |
1154 | close(inode->fd); | |
1155 | free(inode); | |
7387863d DDAG |
1156 | } else { |
1157 | pthread_mutex_unlock(&lo->mutex); | |
1158 | } | |
7c6b6602 DDAG |
1159 | } |
1160 | ||
1161 | static void lo_forget_one(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup) | |
1162 | { | |
7387863d | 1163 | struct lo_data *lo = lo_data(req); |
92fb57b8 SH |
1164 | struct lo_inode *inode; |
1165 | ||
1166 | inode = lo_inode(req, ino); | |
1167 | if (!inode) { | |
1168 | return; | |
1169 | } | |
7c6b6602 | 1170 | |
d240314a EG |
1171 | fuse_log(FUSE_LOG_DEBUG, " forget %lli %lli -%lli\n", |
1172 | (unsigned long long)ino, (unsigned long long)inode->refcount, | |
1173 | (unsigned long long)nlookup); | |
7c6b6602 | 1174 | |
7387863d | 1175 | unref_inode(lo, inode, nlookup); |
7c6b6602 DDAG |
1176 | } |
1177 | ||
1178 | static void lo_forget(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup) | |
1179 | { | |
7387863d DDAG |
1180 | lo_forget_one(req, ino, nlookup); |
1181 | fuse_reply_none(req); | |
7c6b6602 DDAG |
1182 | } |
1183 | ||
1184 | static void lo_forget_multi(fuse_req_t req, size_t count, | |
7387863d | 1185 | struct fuse_forget_data *forgets) |
7c6b6602 | 1186 | { |
7387863d | 1187 | int i; |
7c6b6602 | 1188 | |
7387863d DDAG |
1189 | for (i = 0; i < count; i++) { |
1190 | lo_forget_one(req, forgets[i].ino, forgets[i].nlookup); | |
1191 | } | |
1192 | fuse_reply_none(req); | |
7c6b6602 DDAG |
1193 | } |
1194 | ||
1195 | static void lo_readlink(fuse_req_t req, fuse_ino_t ino) | |
1196 | { | |
7387863d DDAG |
1197 | char buf[PATH_MAX + 1]; |
1198 | int res; | |
7c6b6602 | 1199 | |
7387863d DDAG |
1200 | res = readlinkat(lo_fd(req, ino), "", buf, sizeof(buf)); |
1201 | if (res == -1) { | |
1202 | return (void)fuse_reply_err(req, errno); | |
1203 | } | |
7c6b6602 | 1204 | |
7387863d DDAG |
1205 | if (res == sizeof(buf)) { |
1206 | return (void)fuse_reply_err(req, ENAMETOOLONG); | |
1207 | } | |
7c6b6602 | 1208 | |
7387863d | 1209 | buf[res] = '\0'; |
7c6b6602 | 1210 | |
7387863d | 1211 | fuse_reply_readlink(req, buf); |
7c6b6602 DDAG |
1212 | } |
1213 | ||
1214 | struct lo_dirp { | |
7387863d DDAG |
1215 | DIR *dp; |
1216 | struct dirent *entry; | |
1217 | off_t offset; | |
7c6b6602 DDAG |
1218 | }; |
1219 | ||
b39bce12 | 1220 | static struct lo_dirp *lo_dirp(fuse_req_t req, struct fuse_file_info *fi) |
7c6b6602 | 1221 | { |
b39bce12 SH |
1222 | struct lo_data *lo = lo_data(req); |
1223 | struct lo_map_elem *elem; | |
1224 | ||
1225 | pthread_mutex_lock(&lo->mutex); | |
1226 | elem = lo_map_get(&lo->dirp_map, fi->fh); | |
1227 | pthread_mutex_unlock(&lo->mutex); | |
1228 | if (!elem) { | |
1229 | return NULL; | |
1230 | } | |
1231 | ||
1232 | return elem->dirp; | |
7c6b6602 DDAG |
1233 | } |
1234 | ||
7387863d DDAG |
1235 | static void lo_opendir(fuse_req_t req, fuse_ino_t ino, |
1236 | struct fuse_file_info *fi) | |
7c6b6602 | 1237 | { |
7387863d DDAG |
1238 | int error = ENOMEM; |
1239 | struct lo_data *lo = lo_data(req); | |
1240 | struct lo_dirp *d; | |
1241 | int fd; | |
b39bce12 | 1242 | ssize_t fh; |
7387863d DDAG |
1243 | |
1244 | d = calloc(1, sizeof(struct lo_dirp)); | |
1245 | if (d == NULL) { | |
1246 | goto out_err; | |
1247 | } | |
1248 | ||
1249 | fd = openat(lo_fd(req, ino), ".", O_RDONLY); | |
1250 | if (fd == -1) { | |
1251 | goto out_errno; | |
1252 | } | |
1253 | ||
1254 | d->dp = fdopendir(fd); | |
1255 | if (d->dp == NULL) { | |
1256 | goto out_errno; | |
1257 | } | |
1258 | ||
1259 | d->offset = 0; | |
1260 | d->entry = NULL; | |
1261 | ||
b39bce12 SH |
1262 | pthread_mutex_lock(&lo->mutex); |
1263 | fh = lo_add_dirp_mapping(req, d); | |
1264 | pthread_mutex_unlock(&lo->mutex); | |
1265 | if (fh == -1) { | |
1266 | goto out_err; | |
1267 | } | |
1268 | ||
1269 | fi->fh = fh; | |
7387863d DDAG |
1270 | if (lo->cache == CACHE_ALWAYS) { |
1271 | fi->keep_cache = 1; | |
1272 | } | |
1273 | fuse_reply_open(req, fi); | |
1274 | return; | |
7c6b6602 DDAG |
1275 | |
1276 | out_errno: | |
7387863d | 1277 | error = errno; |
7c6b6602 | 1278 | out_err: |
7387863d | 1279 | if (d) { |
b39bce12 SH |
1280 | if (d->dp) { |
1281 | closedir(d->dp); | |
1282 | } | |
7387863d DDAG |
1283 | if (fd != -1) { |
1284 | close(fd); | |
1285 | } | |
1286 | free(d); | |
1287 | } | |
1288 | fuse_reply_err(req, error); | |
7c6b6602 DDAG |
1289 | } |
1290 | ||
7c6b6602 | 1291 | static void lo_do_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, |
7387863d | 1292 | off_t offset, struct fuse_file_info *fi, int plus) |
7c6b6602 | 1293 | { |
752272da | 1294 | struct lo_data *lo = lo_data(req); |
b39bce12 | 1295 | struct lo_dirp *d; |
752272da | 1296 | struct lo_inode *dinode; |
b39bce12 | 1297 | char *buf = NULL; |
7387863d DDAG |
1298 | char *p; |
1299 | size_t rem = size; | |
752272da | 1300 | int err = EBADF; |
7387863d | 1301 | |
752272da SH |
1302 | dinode = lo_inode(req, ino); |
1303 | if (!dinode) { | |
1304 | goto error; | |
1305 | } | |
7387863d | 1306 | |
b39bce12 SH |
1307 | d = lo_dirp(req, fi); |
1308 | if (!d) { | |
1309 | goto error; | |
1310 | } | |
1311 | ||
752272da | 1312 | err = ENOMEM; |
7387863d DDAG |
1313 | buf = calloc(1, size); |
1314 | if (!buf) { | |
7387863d DDAG |
1315 | goto error; |
1316 | } | |
1317 | p = buf; | |
1318 | ||
1319 | if (offset != d->offset) { | |
1320 | seekdir(d->dp, offset); | |
1321 | d->entry = NULL; | |
1322 | d->offset = offset; | |
1323 | } | |
1324 | while (1) { | |
1325 | size_t entsize; | |
1326 | off_t nextoff; | |
1327 | const char *name; | |
1328 | ||
1329 | if (!d->entry) { | |
1330 | errno = 0; | |
1331 | d->entry = readdir(d->dp); | |
1332 | if (!d->entry) { | |
1333 | if (errno) { /* Error */ | |
1334 | err = errno; | |
1335 | goto error; | |
1336 | } else { /* End of stream */ | |
1337 | break; | |
1338 | } | |
1339 | } | |
1340 | } | |
1341 | nextoff = d->entry->d_off; | |
1342 | name = d->entry->d_name; | |
752272da | 1343 | |
7387863d | 1344 | fuse_ino_t entry_ino = 0; |
752272da SH |
1345 | struct fuse_entry_param e = (struct fuse_entry_param){ |
1346 | .attr.st_ino = d->entry->d_ino, | |
1347 | .attr.st_mode = d->entry->d_type << 12, | |
1348 | }; | |
1349 | ||
1350 | /* Hide root's parent directory */ | |
1351 | if (dinode == &lo->root && strcmp(name, "..") == 0) { | |
1352 | e.attr.st_ino = lo->root.ino; | |
1353 | e.attr.st_mode = DT_DIR << 12; | |
1354 | } | |
1355 | ||
7387863d | 1356 | if (plus) { |
752272da | 1357 | if (!is_dot_or_dotdot(name)) { |
7387863d DDAG |
1358 | err = lo_do_lookup(req, ino, name, &e); |
1359 | if (err) { | |
1360 | goto error; | |
1361 | } | |
1362 | entry_ino = e.ino; | |
1363 | } | |
1364 | ||
1365 | entsize = fuse_add_direntry_plus(req, p, rem, name, &e, nextoff); | |
1366 | } else { | |
752272da | 1367 | entsize = fuse_add_direntry(req, p, rem, name, &e.attr, nextoff); |
7387863d DDAG |
1368 | } |
1369 | if (entsize > rem) { | |
1370 | if (entry_ino != 0) { | |
1371 | lo_forget_one(req, entry_ino, 1); | |
1372 | } | |
1373 | break; | |
1374 | } | |
1375 | ||
1376 | p += entsize; | |
1377 | rem -= entsize; | |
1378 | ||
1379 | d->entry = NULL; | |
1380 | d->offset = nextoff; | |
1381 | } | |
7c6b6602 DDAG |
1382 | |
1383 | err = 0; | |
1384 | error: | |
7387863d DDAG |
1385 | /* |
1386 | * If there's an error, we can only signal it if we haven't stored | |
1387 | * any entries yet - otherwise we'd end up with wrong lookup | |
1388 | * counts for the entries that are already in the buffer. So we | |
1389 | * return what we've collected until that point. | |
1390 | */ | |
1391 | if (err && rem == size) { | |
1392 | fuse_reply_err(req, err); | |
1393 | } else { | |
1394 | fuse_reply_buf(req, buf, size - rem); | |
1395 | } | |
7c6b6602 DDAG |
1396 | free(buf); |
1397 | } | |
1398 | ||
1399 | static void lo_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, | |
7387863d | 1400 | off_t offset, struct fuse_file_info *fi) |
7c6b6602 | 1401 | { |
7387863d | 1402 | lo_do_readdir(req, ino, size, offset, fi, 0); |
7c6b6602 DDAG |
1403 | } |
1404 | ||
1405 | static void lo_readdirplus(fuse_req_t req, fuse_ino_t ino, size_t size, | |
7387863d | 1406 | off_t offset, struct fuse_file_info *fi) |
7c6b6602 | 1407 | { |
7387863d | 1408 | lo_do_readdir(req, ino, size, offset, fi, 1); |
7c6b6602 DDAG |
1409 | } |
1410 | ||
7387863d DDAG |
1411 | static void lo_releasedir(fuse_req_t req, fuse_ino_t ino, |
1412 | struct fuse_file_info *fi) | |
7c6b6602 | 1413 | { |
b39bce12 SH |
1414 | struct lo_data *lo = lo_data(req); |
1415 | struct lo_dirp *d; | |
1416 | ||
7387863d | 1417 | (void)ino; |
b39bce12 SH |
1418 | |
1419 | d = lo_dirp(req, fi); | |
1420 | if (!d) { | |
1421 | fuse_reply_err(req, EBADF); | |
1422 | return; | |
1423 | } | |
1424 | ||
1425 | pthread_mutex_lock(&lo->mutex); | |
1426 | lo_map_remove(&lo->dirp_map, fi->fh); | |
1427 | pthread_mutex_unlock(&lo->mutex); | |
1428 | ||
7387863d DDAG |
1429 | closedir(d->dp); |
1430 | free(d); | |
1431 | fuse_reply_err(req, 0); | |
7c6b6602 DDAG |
1432 | } |
1433 | ||
1434 | static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name, | |
7387863d | 1435 | mode_t mode, struct fuse_file_info *fi) |
7c6b6602 | 1436 | { |
7387863d DDAG |
1437 | int fd; |
1438 | struct lo_data *lo = lo_data(req); | |
1439 | struct fuse_entry_param e; | |
1440 | int err; | |
929cfb7a | 1441 | struct lo_cred old = {}; |
7387863d | 1442 | |
d240314a EG |
1443 | fuse_log(FUSE_LOG_DEBUG, "lo_create(parent=%" PRIu64 ", name=%s)\n", parent, |
1444 | name); | |
7387863d | 1445 | |
25dae28c SH |
1446 | if (!is_safe_path_component(name)) { |
1447 | fuse_reply_err(req, EINVAL); | |
1448 | return; | |
1449 | } | |
1450 | ||
929cfb7a VG |
1451 | err = lo_change_cred(req, &old); |
1452 | if (err) { | |
1453 | goto out; | |
1454 | } | |
1455 | ||
7387863d DDAG |
1456 | fd = openat(lo_fd(req, parent), name, (fi->flags | O_CREAT) & ~O_NOFOLLOW, |
1457 | mode); | |
929cfb7a VG |
1458 | err = fd == -1 ? errno : 0; |
1459 | lo_restore_cred(&old); | |
7387863d | 1460 | |
929cfb7a | 1461 | if (!err) { |
73b4d19d SH |
1462 | ssize_t fh; |
1463 | ||
1464 | pthread_mutex_lock(&lo->mutex); | |
1465 | fh = lo_add_fd_mapping(req, fd); | |
1466 | pthread_mutex_unlock(&lo->mutex); | |
1467 | if (fh == -1) { | |
1468 | close(fd); | |
1469 | fuse_reply_err(req, ENOMEM); | |
1470 | return; | |
1471 | } | |
1472 | ||
1473 | fi->fh = fh; | |
929cfb7a VG |
1474 | err = lo_do_lookup(req, parent, name, &e); |
1475 | } | |
7387863d DDAG |
1476 | if (lo->cache == CACHE_NEVER) { |
1477 | fi->direct_io = 1; | |
1478 | } else if (lo->cache == CACHE_ALWAYS) { | |
1479 | fi->keep_cache = 1; | |
1480 | } | |
1481 | ||
929cfb7a | 1482 | out: |
7387863d DDAG |
1483 | if (err) { |
1484 | fuse_reply_err(req, err); | |
1485 | } else { | |
1486 | fuse_reply_create(req, &e, fi); | |
1487 | } | |
7c6b6602 DDAG |
1488 | } |
1489 | ||
1490 | static void lo_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync, | |
7387863d | 1491 | struct fuse_file_info *fi) |
7c6b6602 | 1492 | { |
7387863d | 1493 | int res; |
b39bce12 SH |
1494 | struct lo_dirp *d; |
1495 | int fd; | |
1496 | ||
7387863d | 1497 | (void)ino; |
b39bce12 SH |
1498 | |
1499 | d = lo_dirp(req, fi); | |
1500 | if (!d) { | |
1501 | fuse_reply_err(req, EBADF); | |
1502 | return; | |
1503 | } | |
1504 | ||
1505 | fd = dirfd(d->dp); | |
7387863d DDAG |
1506 | if (datasync) { |
1507 | res = fdatasync(fd); | |
1508 | } else { | |
1509 | res = fsync(fd); | |
1510 | } | |
1511 | fuse_reply_err(req, res == -1 ? errno : 0); | |
7c6b6602 DDAG |
1512 | } |
1513 | ||
1514 | static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) | |
1515 | { | |
7387863d | 1516 | int fd; |
73b4d19d | 1517 | ssize_t fh; |
7387863d DDAG |
1518 | char buf[64]; |
1519 | struct lo_data *lo = lo_data(req); | |
1520 | ||
d240314a EG |
1521 | fuse_log(FUSE_LOG_DEBUG, "lo_open(ino=%" PRIu64 ", flags=%d)\n", ino, |
1522 | fi->flags); | |
7387863d DDAG |
1523 | |
1524 | /* | |
1525 | * With writeback cache, kernel may send read requests even | |
1526 | * when userspace opened write-only | |
1527 | */ | |
1528 | if (lo->writeback && (fi->flags & O_ACCMODE) == O_WRONLY) { | |
1529 | fi->flags &= ~O_ACCMODE; | |
1530 | fi->flags |= O_RDWR; | |
1531 | } | |
1532 | ||
1533 | /* | |
1534 | * With writeback cache, O_APPEND is handled by the kernel. | |
1535 | * This breaks atomicity (since the file may change in the | |
1536 | * underlying filesystem, so that the kernel's idea of the | |
1537 | * end of the file isn't accurate anymore). In this example, | |
1538 | * we just accept that. A more rigorous filesystem may want | |
1539 | * to return an error here | |
1540 | */ | |
1541 | if (lo->writeback && (fi->flags & O_APPEND)) { | |
1542 | fi->flags &= ~O_APPEND; | |
1543 | } | |
1544 | ||
9f59d175 SH |
1545 | sprintf(buf, "%i", lo_fd(req, ino)); |
1546 | fd = openat(lo->proc_self_fd, buf, fi->flags & ~O_NOFOLLOW); | |
7387863d DDAG |
1547 | if (fd == -1) { |
1548 | return (void)fuse_reply_err(req, errno); | |
1549 | } | |
1550 | ||
73b4d19d SH |
1551 | pthread_mutex_lock(&lo->mutex); |
1552 | fh = lo_add_fd_mapping(req, fd); | |
1553 | pthread_mutex_unlock(&lo->mutex); | |
1554 | if (fh == -1) { | |
1555 | close(fd); | |
1556 | fuse_reply_err(req, ENOMEM); | |
1557 | return; | |
1558 | } | |
1559 | ||
1560 | fi->fh = fh; | |
7387863d DDAG |
1561 | if (lo->cache == CACHE_NEVER) { |
1562 | fi->direct_io = 1; | |
1563 | } else if (lo->cache == CACHE_ALWAYS) { | |
1564 | fi->keep_cache = 1; | |
1565 | } | |
1566 | fuse_reply_open(req, fi); | |
7c6b6602 DDAG |
1567 | } |
1568 | ||
7387863d DDAG |
1569 | static void lo_release(fuse_req_t req, fuse_ino_t ino, |
1570 | struct fuse_file_info *fi) | |
7c6b6602 | 1571 | { |
73b4d19d SH |
1572 | struct lo_data *lo = lo_data(req); |
1573 | int fd; | |
1574 | ||
7387863d | 1575 | (void)ino; |
7c6b6602 | 1576 | |
73b4d19d SH |
1577 | fd = lo_fi_fd(req, fi); |
1578 | ||
1579 | pthread_mutex_lock(&lo->mutex); | |
1580 | lo_map_remove(&lo->fd_map, fi->fh); | |
1581 | pthread_mutex_unlock(&lo->mutex); | |
1582 | ||
1583 | close(fd); | |
7387863d | 1584 | fuse_reply_err(req, 0); |
7c6b6602 DDAG |
1585 | } |
1586 | ||
1587 | static void lo_flush(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) | |
1588 | { | |
7387863d DDAG |
1589 | int res; |
1590 | (void)ino; | |
73b4d19d | 1591 | res = close(dup(lo_fi_fd(req, fi))); |
7387863d | 1592 | fuse_reply_err(req, res == -1 ? errno : 0); |
7c6b6602 DDAG |
1593 | } |
1594 | ||
1595 | static void lo_fsync(fuse_req_t req, fuse_ino_t ino, int datasync, | |
7387863d | 1596 | struct fuse_file_info *fi) |
7c6b6602 | 1597 | { |
7387863d | 1598 | int res; |
1b209805 VG |
1599 | int fd; |
1600 | char *buf; | |
1601 | ||
1602 | fuse_log(FUSE_LOG_DEBUG, "lo_fsync(ino=%" PRIu64 ", fi=0x%p)\n", ino, | |
1603 | (void *)fi); | |
1604 | ||
1605 | if (!fi) { | |
9f59d175 SH |
1606 | struct lo_data *lo = lo_data(req); |
1607 | ||
1608 | res = asprintf(&buf, "%i", lo_fd(req, ino)); | |
1b209805 VG |
1609 | if (res == -1) { |
1610 | return (void)fuse_reply_err(req, errno); | |
1611 | } | |
1612 | ||
9f59d175 | 1613 | fd = openat(lo->proc_self_fd, buf, O_RDWR); |
1b209805 VG |
1614 | free(buf); |
1615 | if (fd == -1) { | |
1616 | return (void)fuse_reply_err(req, errno); | |
1617 | } | |
1618 | } else { | |
73b4d19d | 1619 | fd = lo_fi_fd(req, fi); |
1b209805 VG |
1620 | } |
1621 | ||
7387863d | 1622 | if (datasync) { |
1b209805 | 1623 | res = fdatasync(fd); |
7387863d | 1624 | } else { |
1b209805 VG |
1625 | res = fsync(fd); |
1626 | } | |
1627 | if (!fi) { | |
1628 | close(fd); | |
7387863d DDAG |
1629 | } |
1630 | fuse_reply_err(req, res == -1 ? errno : 0); | |
7c6b6602 DDAG |
1631 | } |
1632 | ||
7387863d DDAG |
1633 | static void lo_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t offset, |
1634 | struct fuse_file_info *fi) | |
7c6b6602 | 1635 | { |
7387863d | 1636 | struct fuse_bufvec buf = FUSE_BUFVEC_INIT(size); |
7c6b6602 | 1637 | |
d240314a EG |
1638 | fuse_log(FUSE_LOG_DEBUG, |
1639 | "lo_read(ino=%" PRIu64 ", size=%zd, " | |
1640 | "off=%lu)\n", | |
1641 | ino, size, (unsigned long)offset); | |
7c6b6602 | 1642 | |
7387863d | 1643 | buf.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK; |
73b4d19d | 1644 | buf.buf[0].fd = lo_fi_fd(req, fi); |
7387863d | 1645 | buf.buf[0].pos = offset; |
7c6b6602 | 1646 | |
8c3fe75e | 1647 | fuse_reply_data(req, &buf); |
7c6b6602 DDAG |
1648 | } |
1649 | ||
1650 | static void lo_write_buf(fuse_req_t req, fuse_ino_t ino, | |
7387863d DDAG |
1651 | struct fuse_bufvec *in_buf, off_t off, |
1652 | struct fuse_file_info *fi) | |
7c6b6602 | 1653 | { |
7387863d DDAG |
1654 | (void)ino; |
1655 | ssize_t res; | |
1656 | struct fuse_bufvec out_buf = FUSE_BUFVEC_INIT(fuse_buf_size(in_buf)); | |
ee884652 | 1657 | bool cap_fsetid_dropped = false; |
7387863d DDAG |
1658 | |
1659 | out_buf.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK; | |
73b4d19d | 1660 | out_buf.buf[0].fd = lo_fi_fd(req, fi); |
7387863d DDAG |
1661 | out_buf.buf[0].pos = off; |
1662 | ||
d240314a EG |
1663 | fuse_log(FUSE_LOG_DEBUG, |
1664 | "lo_write_buf(ino=%" PRIu64 ", size=%zd, off=%lu)\n", ino, | |
1665 | out_buf.buf[0].size, (unsigned long)off); | |
7387863d | 1666 | |
ee884652 VG |
1667 | /* |
1668 | * If kill_priv is set, drop CAP_FSETID which should lead to kernel | |
1669 | * clearing setuid/setgid on file. | |
1670 | */ | |
1671 | if (fi->kill_priv) { | |
1672 | res = drop_effective_cap("FSETID", &cap_fsetid_dropped); | |
1673 | if (res != 0) { | |
1674 | fuse_reply_err(req, res); | |
1675 | return; | |
1676 | } | |
1677 | } | |
1678 | ||
8c3fe75e | 1679 | res = fuse_buf_copy(&out_buf, in_buf); |
7387863d DDAG |
1680 | if (res < 0) { |
1681 | fuse_reply_err(req, -res); | |
1682 | } else { | |
1683 | fuse_reply_write(req, (size_t)res); | |
1684 | } | |
ee884652 VG |
1685 | |
1686 | if (cap_fsetid_dropped) { | |
1687 | res = gain_effective_cap("FSETID"); | |
1688 | if (res) { | |
1689 | fuse_log(FUSE_LOG_ERR, "Failed to gain CAP_FSETID\n"); | |
1690 | } | |
1691 | } | |
7c6b6602 DDAG |
1692 | } |
1693 | ||
1694 | static void lo_statfs(fuse_req_t req, fuse_ino_t ino) | |
1695 | { | |
7387863d DDAG |
1696 | int res; |
1697 | struct statvfs stbuf; | |
1698 | ||
1699 | res = fstatvfs(lo_fd(req, ino), &stbuf); | |
1700 | if (res == -1) { | |
1701 | fuse_reply_err(req, errno); | |
1702 | } else { | |
1703 | fuse_reply_statfs(req, &stbuf); | |
1704 | } | |
7c6b6602 DDAG |
1705 | } |
1706 | ||
7387863d DDAG |
1707 | static void lo_fallocate(fuse_req_t req, fuse_ino_t ino, int mode, off_t offset, |
1708 | off_t length, struct fuse_file_info *fi) | |
7c6b6602 | 1709 | { |
7387863d DDAG |
1710 | int err = EOPNOTSUPP; |
1711 | (void)ino; | |
7c6b6602 | 1712 | |
9776457c | 1713 | #ifdef CONFIG_FALLOCATE |
73b4d19d | 1714 | err = fallocate(lo_fi_fd(req, fi), mode, offset, length); |
7387863d DDAG |
1715 | if (err < 0) { |
1716 | err = errno; | |
1717 | } | |
7c6b6602 | 1718 | |
9776457c | 1719 | #elif defined(CONFIG_POSIX_FALLOCATE) |
7387863d DDAG |
1720 | if (mode) { |
1721 | fuse_reply_err(req, EOPNOTSUPP); | |
1722 | return; | |
1723 | } | |
7c6b6602 | 1724 | |
73b4d19d | 1725 | err = posix_fallocate(lo_fi_fd(req, fi), offset, length); |
7c6b6602 DDAG |
1726 | #endif |
1727 | ||
7387863d | 1728 | fuse_reply_err(req, err); |
7c6b6602 DDAG |
1729 | } |
1730 | ||
1731 | static void lo_flock(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi, | |
7387863d | 1732 | int op) |
7c6b6602 | 1733 | { |
7387863d DDAG |
1734 | int res; |
1735 | (void)ino; | |
7c6b6602 | 1736 | |
73b4d19d | 1737 | res = flock(lo_fi_fd(req, fi), op); |
7c6b6602 | 1738 | |
7387863d | 1739 | fuse_reply_err(req, res == -1 ? errno : 0); |
7c6b6602 DDAG |
1740 | } |
1741 | ||
1742 | static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name, | |
7387863d | 1743 | size_t size) |
7c6b6602 | 1744 | { |
9f59d175 | 1745 | struct lo_data *lo = lo_data(req); |
7387863d DDAG |
1746 | char *value = NULL; |
1747 | char procname[64]; | |
92fb57b8 | 1748 | struct lo_inode *inode; |
7387863d DDAG |
1749 | ssize_t ret; |
1750 | int saverr; | |
9f59d175 | 1751 | int fd = -1; |
7387863d | 1752 | |
92fb57b8 SH |
1753 | inode = lo_inode(req, ino); |
1754 | if (!inode) { | |
1755 | fuse_reply_err(req, EBADF); | |
1756 | return; | |
1757 | } | |
1758 | ||
7387863d DDAG |
1759 | saverr = ENOSYS; |
1760 | if (!lo_data(req)->xattr) { | |
1761 | goto out; | |
1762 | } | |
1763 | ||
d240314a EG |
1764 | fuse_log(FUSE_LOG_DEBUG, "lo_getxattr(ino=%" PRIu64 ", name=%s size=%zd)\n", |
1765 | ino, name, size); | |
7387863d DDAG |
1766 | |
1767 | if (inode->is_symlink) { | |
1768 | /* Sorry, no race free way to getxattr on symlink. */ | |
1769 | saverr = EPERM; | |
1770 | goto out; | |
1771 | } | |
1772 | ||
9f59d175 SH |
1773 | sprintf(procname, "%i", inode->fd); |
1774 | fd = openat(lo->proc_self_fd, procname, O_RDONLY); | |
1775 | if (fd < 0) { | |
1776 | goto out_err; | |
1777 | } | |
7387863d DDAG |
1778 | |
1779 | if (size) { | |
1780 | value = malloc(size); | |
1781 | if (!value) { | |
1782 | goto out_err; | |
1783 | } | |
1784 | ||
9f59d175 | 1785 | ret = fgetxattr(fd, name, value, size); |
7387863d DDAG |
1786 | if (ret == -1) { |
1787 | goto out_err; | |
1788 | } | |
1789 | saverr = 0; | |
1790 | if (ret == 0) { | |
1791 | goto out; | |
1792 | } | |
1793 | ||
1794 | fuse_reply_buf(req, value, ret); | |
1795 | } else { | |
9f59d175 | 1796 | ret = fgetxattr(fd, name, NULL, 0); |
7387863d DDAG |
1797 | if (ret == -1) { |
1798 | goto out_err; | |
1799 | } | |
1800 | ||
1801 | fuse_reply_xattr(req, ret); | |
1802 | } | |
7c6b6602 | 1803 | out_free: |
7387863d | 1804 | free(value); |
9f59d175 SH |
1805 | |
1806 | if (fd >= 0) { | |
1807 | close(fd); | |
1808 | } | |
7387863d | 1809 | return; |
7c6b6602 DDAG |
1810 | |
1811 | out_err: | |
7387863d | 1812 | saverr = errno; |
7c6b6602 | 1813 | out: |
7387863d DDAG |
1814 | fuse_reply_err(req, saverr); |
1815 | goto out_free; | |
7c6b6602 DDAG |
1816 | } |
1817 | ||
1818 | static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) | |
1819 | { | |
9f59d175 | 1820 | struct lo_data *lo = lo_data(req); |
7387863d DDAG |
1821 | char *value = NULL; |
1822 | char procname[64]; | |
92fb57b8 | 1823 | struct lo_inode *inode; |
7387863d DDAG |
1824 | ssize_t ret; |
1825 | int saverr; | |
9f59d175 | 1826 | int fd = -1; |
7387863d | 1827 | |
92fb57b8 SH |
1828 | inode = lo_inode(req, ino); |
1829 | if (!inode) { | |
1830 | fuse_reply_err(req, EBADF); | |
1831 | return; | |
1832 | } | |
1833 | ||
7387863d DDAG |
1834 | saverr = ENOSYS; |
1835 | if (!lo_data(req)->xattr) { | |
1836 | goto out; | |
1837 | } | |
1838 | ||
d240314a EG |
1839 | fuse_log(FUSE_LOG_DEBUG, "lo_listxattr(ino=%" PRIu64 ", size=%zd)\n", ino, |
1840 | size); | |
7387863d DDAG |
1841 | |
1842 | if (inode->is_symlink) { | |
1843 | /* Sorry, no race free way to listxattr on symlink. */ | |
1844 | saverr = EPERM; | |
1845 | goto out; | |
1846 | } | |
1847 | ||
9f59d175 SH |
1848 | sprintf(procname, "%i", inode->fd); |
1849 | fd = openat(lo->proc_self_fd, procname, O_RDONLY); | |
1850 | if (fd < 0) { | |
1851 | goto out_err; | |
1852 | } | |
7387863d DDAG |
1853 | |
1854 | if (size) { | |
1855 | value = malloc(size); | |
1856 | if (!value) { | |
1857 | goto out_err; | |
1858 | } | |
1859 | ||
9f59d175 | 1860 | ret = flistxattr(fd, value, size); |
7387863d DDAG |
1861 | if (ret == -1) { |
1862 | goto out_err; | |
1863 | } | |
1864 | saverr = 0; | |
1865 | if (ret == 0) { | |
1866 | goto out; | |
1867 | } | |
1868 | ||
1869 | fuse_reply_buf(req, value, ret); | |
1870 | } else { | |
9f59d175 | 1871 | ret = flistxattr(fd, NULL, 0); |
7387863d DDAG |
1872 | if (ret == -1) { |
1873 | goto out_err; | |
1874 | } | |
1875 | ||
1876 | fuse_reply_xattr(req, ret); | |
1877 | } | |
7c6b6602 | 1878 | out_free: |
7387863d | 1879 | free(value); |
9f59d175 SH |
1880 | |
1881 | if (fd >= 0) { | |
1882 | close(fd); | |
1883 | } | |
7387863d | 1884 | return; |
7c6b6602 DDAG |
1885 | |
1886 | out_err: | |
7387863d | 1887 | saverr = errno; |
7c6b6602 | 1888 | out: |
7387863d DDAG |
1889 | fuse_reply_err(req, saverr); |
1890 | goto out_free; | |
7c6b6602 DDAG |
1891 | } |
1892 | ||
1893 | static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name, | |
7387863d | 1894 | const char *value, size_t size, int flags) |
7c6b6602 | 1895 | { |
7387863d | 1896 | char procname[64]; |
9f59d175 | 1897 | struct lo_data *lo = lo_data(req); |
92fb57b8 | 1898 | struct lo_inode *inode; |
7387863d DDAG |
1899 | ssize_t ret; |
1900 | int saverr; | |
9f59d175 | 1901 | int fd = -1; |
7c6b6602 | 1902 | |
92fb57b8 SH |
1903 | inode = lo_inode(req, ino); |
1904 | if (!inode) { | |
1905 | fuse_reply_err(req, EBADF); | |
1906 | return; | |
1907 | } | |
1908 | ||
7387863d DDAG |
1909 | saverr = ENOSYS; |
1910 | if (!lo_data(req)->xattr) { | |
1911 | goto out; | |
1912 | } | |
7c6b6602 | 1913 | |
d240314a EG |
1914 | fuse_log(FUSE_LOG_DEBUG, "lo_setxattr(ino=%" PRIu64 |
1915 | ", name=%s value=%s size=%zd)\n", ino, name, value, size); | |
7c6b6602 | 1916 | |
7387863d DDAG |
1917 | if (inode->is_symlink) { |
1918 | /* Sorry, no race free way to setxattr on symlink. */ | |
1919 | saverr = EPERM; | |
1920 | goto out; | |
1921 | } | |
7c6b6602 | 1922 | |
9f59d175 SH |
1923 | sprintf(procname, "%i", inode->fd); |
1924 | fd = openat(lo->proc_self_fd, procname, O_RDWR); | |
1925 | if (fd < 0) { | |
1926 | saverr = errno; | |
1927 | goto out; | |
1928 | } | |
7c6b6602 | 1929 | |
9f59d175 | 1930 | ret = fsetxattr(fd, name, value, size, flags); |
7387863d | 1931 | saverr = ret == -1 ? errno : 0; |
7c6b6602 DDAG |
1932 | |
1933 | out: | |
9f59d175 SH |
1934 | if (fd >= 0) { |
1935 | close(fd); | |
1936 | } | |
7387863d | 1937 | fuse_reply_err(req, saverr); |
7c6b6602 DDAG |
1938 | } |
1939 | ||
1940 | static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name) | |
1941 | { | |
7387863d | 1942 | char procname[64]; |
9f59d175 | 1943 | struct lo_data *lo = lo_data(req); |
92fb57b8 | 1944 | struct lo_inode *inode; |
7387863d DDAG |
1945 | ssize_t ret; |
1946 | int saverr; | |
9f59d175 | 1947 | int fd = -1; |
7c6b6602 | 1948 | |
92fb57b8 SH |
1949 | inode = lo_inode(req, ino); |
1950 | if (!inode) { | |
1951 | fuse_reply_err(req, EBADF); | |
1952 | return; | |
1953 | } | |
1954 | ||
7387863d DDAG |
1955 | saverr = ENOSYS; |
1956 | if (!lo_data(req)->xattr) { | |
1957 | goto out; | |
1958 | } | |
7c6b6602 | 1959 | |
d240314a EG |
1960 | fuse_log(FUSE_LOG_DEBUG, "lo_removexattr(ino=%" PRIu64 ", name=%s)\n", ino, |
1961 | name); | |
7c6b6602 | 1962 | |
7387863d DDAG |
1963 | if (inode->is_symlink) { |
1964 | /* Sorry, no race free way to setxattr on symlink. */ | |
1965 | saverr = EPERM; | |
1966 | goto out; | |
1967 | } | |
7c6b6602 | 1968 | |
9f59d175 SH |
1969 | sprintf(procname, "%i", inode->fd); |
1970 | fd = openat(lo->proc_self_fd, procname, O_RDWR); | |
1971 | if (fd < 0) { | |
1972 | saverr = errno; | |
1973 | goto out; | |
1974 | } | |
7c6b6602 | 1975 | |
9f59d175 | 1976 | ret = fremovexattr(fd, name); |
7387863d | 1977 | saverr = ret == -1 ? errno : 0; |
7c6b6602 DDAG |
1978 | |
1979 | out: | |
9f59d175 SH |
1980 | if (fd >= 0) { |
1981 | close(fd); | |
1982 | } | |
7387863d | 1983 | fuse_reply_err(req, saverr); |
7c6b6602 DDAG |
1984 | } |
1985 | ||
1986 | #ifdef HAVE_COPY_FILE_RANGE | |
1987 | static void lo_copy_file_range(fuse_req_t req, fuse_ino_t ino_in, off_t off_in, | |
7387863d DDAG |
1988 | struct fuse_file_info *fi_in, fuse_ino_t ino_out, |
1989 | off_t off_out, struct fuse_file_info *fi_out, | |
1990 | size_t len, int flags) | |
7c6b6602 | 1991 | { |
73b4d19d | 1992 | int in_fd, out_fd; |
7387863d DDAG |
1993 | ssize_t res; |
1994 | ||
73b4d19d SH |
1995 | in_fd = lo_fi_fd(req, fi_in); |
1996 | out_fd = lo_fi_fd(req, fi_out); | |
1997 | ||
1998 | fuse_log(FUSE_LOG_DEBUG, | |
1999 | "lo_copy_file_range(ino=%" PRIu64 "/fd=%d, " | |
2000 | "off=%lu, ino=%" PRIu64 "/fd=%d, " | |
2001 | "off=%lu, size=%zd, flags=0x%x)\n", | |
2002 | ino_in, in_fd, off_in, ino_out, out_fd, off_out, len, flags); | |
7387863d | 2003 | |
73b4d19d | 2004 | res = copy_file_range(in_fd, &off_in, out_fd, &off_out, len, flags); |
7387863d DDAG |
2005 | if (res < 0) { |
2006 | fuse_reply_err(req, -errno); | |
2007 | } else { | |
2008 | fuse_reply_write(req, res); | |
2009 | } | |
7c6b6602 DDAG |
2010 | } |
2011 | #endif | |
2012 | ||
2013 | static void lo_lseek(fuse_req_t req, fuse_ino_t ino, off_t off, int whence, | |
7387863d | 2014 | struct fuse_file_info *fi) |
7c6b6602 | 2015 | { |
7387863d DDAG |
2016 | off_t res; |
2017 | ||
2018 | (void)ino; | |
73b4d19d | 2019 | res = lseek(lo_fi_fd(req, fi), off, whence); |
7387863d DDAG |
2020 | if (res != -1) { |
2021 | fuse_reply_lseek(req, res); | |
2022 | } else { | |
2023 | fuse_reply_err(req, errno); | |
2024 | } | |
7c6b6602 DDAG |
2025 | } |
2026 | ||
2027 | static struct fuse_lowlevel_ops lo_oper = { | |
7387863d DDAG |
2028 | .init = lo_init, |
2029 | .lookup = lo_lookup, | |
2030 | .mkdir = lo_mkdir, | |
2031 | .mknod = lo_mknod, | |
2032 | .symlink = lo_symlink, | |
2033 | .link = lo_link, | |
2034 | .unlink = lo_unlink, | |
2035 | .rmdir = lo_rmdir, | |
2036 | .rename = lo_rename, | |
2037 | .forget = lo_forget, | |
2038 | .forget_multi = lo_forget_multi, | |
2039 | .getattr = lo_getattr, | |
2040 | .setattr = lo_setattr, | |
2041 | .readlink = lo_readlink, | |
2042 | .opendir = lo_opendir, | |
2043 | .readdir = lo_readdir, | |
2044 | .readdirplus = lo_readdirplus, | |
2045 | .releasedir = lo_releasedir, | |
2046 | .fsyncdir = lo_fsyncdir, | |
2047 | .create = lo_create, | |
2048 | .open = lo_open, | |
2049 | .release = lo_release, | |
2050 | .flush = lo_flush, | |
2051 | .fsync = lo_fsync, | |
2052 | .read = lo_read, | |
2053 | .write_buf = lo_write_buf, | |
2054 | .statfs = lo_statfs, | |
2055 | .fallocate = lo_fallocate, | |
2056 | .flock = lo_flock, | |
2057 | .getxattr = lo_getxattr, | |
2058 | .listxattr = lo_listxattr, | |
2059 | .setxattr = lo_setxattr, | |
2060 | .removexattr = lo_removexattr, | |
7c6b6602 | 2061 | #ifdef HAVE_COPY_FILE_RANGE |
7387863d | 2062 | .copy_file_range = lo_copy_file_range, |
7c6b6602 | 2063 | #endif |
7387863d | 2064 | .lseek = lo_lseek, |
7c6b6602 DDAG |
2065 | }; |
2066 | ||
45018fbb SH |
2067 | /* Print vhost-user.json backend program capabilities */ |
2068 | static void print_capabilities(void) | |
2069 | { | |
2070 | printf("{\n"); | |
2071 | printf(" \"type\": \"fs\"\n"); | |
2072 | printf("}\n"); | |
2073 | } | |
2074 | ||
d74830d1 | 2075 | /* |
8e1d4ef2 | 2076 | * Move to a new mount, net, and pid namespaces to isolate this process. |
d74830d1 | 2077 | */ |
8e1d4ef2 | 2078 | static void setup_namespaces(struct lo_data *lo, struct fuse_session *se) |
d74830d1 | 2079 | { |
8e1d4ef2 SH |
2080 | pid_t child; |
2081 | ||
2082 | /* | |
2083 | * Create a new pid namespace for *child* processes. We'll have to | |
2084 | * fork in order to enter the new pid namespace. A new mount namespace | |
2085 | * is also needed so that we can remount /proc for the new pid | |
2086 | * namespace. | |
2087 | * | |
2088 | * Our UNIX domain sockets have been created. Now we can move to | |
2089 | * an empty network namespace to prevent TCP/IP and other network | |
2090 | * activity in case this process is compromised. | |
2091 | */ | |
2092 | if (unshare(CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWNET) != 0) { | |
2093 | fuse_log(FUSE_LOG_ERR, "unshare(CLONE_NEWPID | CLONE_NEWNS): %m\n"); | |
2094 | exit(1); | |
2095 | } | |
2096 | ||
2097 | child = fork(); | |
2098 | if (child < 0) { | |
2099 | fuse_log(FUSE_LOG_ERR, "fork() failed: %m\n"); | |
2100 | exit(1); | |
2101 | } | |
2102 | if (child > 0) { | |
2103 | pid_t waited; | |
2104 | int wstatus; | |
2105 | ||
2106 | /* The parent waits for the child */ | |
2107 | do { | |
2108 | waited = waitpid(child, &wstatus, 0); | |
2109 | } while (waited < 0 && errno == EINTR && !se->exited); | |
2110 | ||
2111 | /* We were terminated by a signal, see fuse_signals.c */ | |
2112 | if (se->exited) { | |
2113 | exit(0); | |
2114 | } | |
2115 | ||
2116 | if (WIFEXITED(wstatus)) { | |
2117 | exit(WEXITSTATUS(wstatus)); | |
2118 | } | |
2119 | ||
2120 | exit(1); | |
2121 | } | |
2122 | ||
2123 | /* Send us SIGTERM when the parent thread terminates, see prctl(2) */ | |
2124 | prctl(PR_SET_PDEATHSIG, SIGTERM); | |
2125 | ||
2126 | /* | |
2127 | * If the mounts have shared propagation then we want to opt out so our | |
2128 | * mount changes don't affect the parent mount namespace. | |
2129 | */ | |
2130 | if (mount(NULL, "/", NULL, MS_REC | MS_SLAVE, NULL) < 0) { | |
2131 | fuse_log(FUSE_LOG_ERR, "mount(/, MS_REC|MS_SLAVE): %m\n"); | |
2132 | exit(1); | |
2133 | } | |
2134 | ||
2135 | /* The child must remount /proc to use the new pid namespace */ | |
2136 | if (mount("proc", "/proc", "proc", | |
2137 | MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) { | |
2138 | fuse_log(FUSE_LOG_ERR, "mount(/proc): %m\n"); | |
2139 | exit(1); | |
2140 | } | |
2141 | ||
2142 | /* Now we can get our /proc/self/fd directory file descriptor */ | |
2143 | lo->proc_self_fd = open("/proc/self/fd", O_PATH); | |
2144 | if (lo->proc_self_fd == -1) { | |
2145 | fuse_log(FUSE_LOG_ERR, "open(/proc/self/fd, O_PATH): %m\n"); | |
d74830d1 SH |
2146 | exit(1); |
2147 | } | |
2148 | } | |
2149 | ||
2405f3c0 DDAG |
2150 | /* |
2151 | * Capture the capability state, we'll need to restore this for individual | |
2152 | * threads later; see load_capng. | |
2153 | */ | |
2154 | static void setup_capng(void) | |
2155 | { | |
2156 | /* Note this accesses /proc so has to happen before the sandbox */ | |
2157 | if (capng_get_caps_process()) { | |
2158 | fuse_log(FUSE_LOG_ERR, "capng_get_caps_process\n"); | |
2159 | exit(1); | |
2160 | } | |
2161 | pthread_mutex_init(&cap.mutex, NULL); | |
2162 | pthread_mutex_lock(&cap.mutex); | |
2163 | cap.saved = capng_save_state(); | |
2164 | if (!cap.saved) { | |
2165 | fuse_log(FUSE_LOG_ERR, "capng_save_state\n"); | |
2166 | exit(1); | |
2167 | } | |
2168 | pthread_mutex_unlock(&cap.mutex); | |
2169 | } | |
2170 | ||
2171 | static void cleanup_capng(void) | |
2172 | { | |
2173 | free(cap.saved); | |
2174 | cap.saved = NULL; | |
2175 | pthread_mutex_destroy(&cap.mutex); | |
2176 | } | |
2177 | ||
2178 | ||
8e1d4ef2 SH |
2179 | /* |
2180 | * Make the source directory our root so symlinks cannot escape and no other | |
2181 | * files are accessible. Assumes unshare(CLONE_NEWNS) was already called. | |
2182 | */ | |
2183 | static void setup_mounts(const char *source) | |
5baa3b8e SH |
2184 | { |
2185 | int oldroot; | |
2186 | int newroot; | |
2187 | ||
8e1d4ef2 SH |
2188 | if (mount(source, source, NULL, MS_BIND, NULL) < 0) { |
2189 | fuse_log(FUSE_LOG_ERR, "mount(%s, %s, MS_BIND): %m\n", source, source); | |
2190 | exit(1); | |
2191 | } | |
2192 | ||
2193 | /* This magic is based on lxc's lxc_pivot_root() */ | |
5baa3b8e SH |
2194 | oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC); |
2195 | if (oldroot < 0) { | |
2196 | fuse_log(FUSE_LOG_ERR, "open(/): %m\n"); | |
2197 | exit(1); | |
2198 | } | |
2199 | ||
2200 | newroot = open(source, O_DIRECTORY | O_RDONLY | O_CLOEXEC); | |
2201 | if (newroot < 0) { | |
2202 | fuse_log(FUSE_LOG_ERR, "open(%s): %m\n", source); | |
2203 | exit(1); | |
2204 | } | |
2205 | ||
2206 | if (fchdir(newroot) < 0) { | |
2207 | fuse_log(FUSE_LOG_ERR, "fchdir(newroot): %m\n"); | |
2208 | exit(1); | |
2209 | } | |
2210 | ||
2211 | if (syscall(__NR_pivot_root, ".", ".") < 0) { | |
2212 | fuse_log(FUSE_LOG_ERR, "pivot_root(., .): %m\n"); | |
2213 | exit(1); | |
2214 | } | |
2215 | ||
2216 | if (fchdir(oldroot) < 0) { | |
2217 | fuse_log(FUSE_LOG_ERR, "fchdir(oldroot): %m\n"); | |
2218 | exit(1); | |
2219 | } | |
2220 | ||
2221 | if (mount("", ".", "", MS_SLAVE | MS_REC, NULL) < 0) { | |
2222 | fuse_log(FUSE_LOG_ERR, "mount(., MS_SLAVE | MS_REC): %m\n"); | |
2223 | exit(1); | |
2224 | } | |
2225 | ||
2226 | if (umount2(".", MNT_DETACH) < 0) { | |
2227 | fuse_log(FUSE_LOG_ERR, "umount2(., MNT_DETACH): %m\n"); | |
2228 | exit(1); | |
2229 | } | |
2230 | ||
2231 | if (fchdir(newroot) < 0) { | |
2232 | fuse_log(FUSE_LOG_ERR, "fchdir(newroot): %m\n"); | |
2233 | exit(1); | |
2234 | } | |
2235 | ||
2236 | close(newroot); | |
2237 | close(oldroot); | |
2238 | } | |
2239 | ||
5baa3b8e SH |
2240 | /* |
2241 | * Lock down this process to prevent access to other processes or files outside | |
2242 | * source directory. This reduces the impact of arbitrary code execution bugs. | |
2243 | */ | |
f185621d SH |
2244 | static void setup_sandbox(struct lo_data *lo, struct fuse_session *se, |
2245 | bool enable_syslog) | |
5baa3b8e | 2246 | { |
8e1d4ef2 SH |
2247 | setup_namespaces(lo, se); |
2248 | setup_mounts(lo->source); | |
f185621d | 2249 | setup_seccomp(enable_syslog); |
5baa3b8e SH |
2250 | } |
2251 | ||
01a6dc95 SH |
2252 | /* Raise the maximum number of open file descriptors */ |
2253 | static void setup_nofile_rlimit(void) | |
2254 | { | |
2255 | const rlim_t max_fds = 1000000; | |
2256 | struct rlimit rlim; | |
2257 | ||
2258 | if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) { | |
2259 | fuse_log(FUSE_LOG_ERR, "getrlimit(RLIMIT_NOFILE): %m\n"); | |
2260 | exit(1); | |
2261 | } | |
2262 | ||
2263 | if (rlim.rlim_cur >= max_fds) { | |
2264 | return; /* nothing to do */ | |
2265 | } | |
2266 | ||
2267 | rlim.rlim_cur = max_fds; | |
2268 | rlim.rlim_max = max_fds; | |
2269 | ||
2270 | if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) { | |
2271 | /* Ignore SELinux denials */ | |
2272 | if (errno == EPERM) { | |
2273 | return; | |
2274 | } | |
2275 | ||
2276 | fuse_log(FUSE_LOG_ERR, "setrlimit(RLIMIT_NOFILE): %m\n"); | |
2277 | exit(1); | |
2278 | } | |
2279 | } | |
2280 | ||
f185621d SH |
2281 | static void log_func(enum fuse_log_level level, const char *fmt, va_list ap) |
2282 | { | |
36f38469 MM |
2283 | g_autofree char *localfmt = NULL; |
2284 | ||
d240314a EG |
2285 | if (current_log_level < level) { |
2286 | return; | |
2287 | } | |
2288 | ||
36f38469 | 2289 | if (current_log_level == FUSE_LOG_DEBUG) { |
50fb955a MM |
2290 | if (!use_syslog) { |
2291 | localfmt = g_strdup_printf("[%" PRId64 "] [ID: %08ld] %s", | |
2292 | get_clock(), syscall(__NR_gettid), fmt); | |
2293 | } else { | |
2294 | localfmt = g_strdup_printf("[ID: %08ld] %s", syscall(__NR_gettid), | |
2295 | fmt); | |
2296 | } | |
36f38469 MM |
2297 | fmt = localfmt; |
2298 | } | |
2299 | ||
f185621d SH |
2300 | if (use_syslog) { |
2301 | int priority = LOG_ERR; | |
2302 | switch (level) { | |
2303 | case FUSE_LOG_EMERG: | |
2304 | priority = LOG_EMERG; | |
2305 | break; | |
2306 | case FUSE_LOG_ALERT: | |
2307 | priority = LOG_ALERT; | |
2308 | break; | |
2309 | case FUSE_LOG_CRIT: | |
2310 | priority = LOG_CRIT; | |
2311 | break; | |
2312 | case FUSE_LOG_ERR: | |
2313 | priority = LOG_ERR; | |
2314 | break; | |
2315 | case FUSE_LOG_WARNING: | |
2316 | priority = LOG_WARNING; | |
2317 | break; | |
2318 | case FUSE_LOG_NOTICE: | |
2319 | priority = LOG_NOTICE; | |
2320 | break; | |
2321 | case FUSE_LOG_INFO: | |
2322 | priority = LOG_INFO; | |
2323 | break; | |
2324 | case FUSE_LOG_DEBUG: | |
2325 | priority = LOG_DEBUG; | |
2326 | break; | |
2327 | } | |
2328 | vsyslog(priority, fmt, ap); | |
2329 | } else { | |
2330 | vfprintf(stderr, fmt, ap); | |
2331 | } | |
2332 | } | |
2333 | ||
7c6b6602 DDAG |
2334 | int main(int argc, char *argv[]) |
2335 | { | |
7387863d DDAG |
2336 | struct fuse_args args = FUSE_ARGS_INIT(argc, argv); |
2337 | struct fuse_session *se; | |
2338 | struct fuse_cmdline_opts opts; | |
9f59d175 SH |
2339 | struct lo_data lo = { |
2340 | .debug = 0, | |
2341 | .writeback = 0, | |
2342 | .proc_self_fd = -1, | |
2343 | }; | |
92fb57b8 | 2344 | struct lo_map_elem *root_elem; |
7387863d DDAG |
2345 | int ret = -1; |
2346 | ||
2347 | /* Don't mask creation mode, kernel already did that */ | |
2348 | umask(0); | |
2349 | ||
2350 | pthread_mutex_init(&lo.mutex, NULL); | |
2351 | lo.root.next = lo.root.prev = &lo.root; | |
2352 | lo.root.fd = -1; | |
92fb57b8 | 2353 | lo.root.fuse_ino = FUSE_ROOT_ID; |
7387863d DDAG |
2354 | lo.cache = CACHE_NORMAL; |
2355 | ||
92fb57b8 SH |
2356 | /* |
2357 | * Set up the ino map like this: | |
2358 | * [0] Reserved (will not be used) | |
2359 | * [1] Root inode | |
2360 | */ | |
2361 | lo_map_init(&lo.ino_map); | |
2362 | lo_map_reserve(&lo.ino_map, 0)->in_use = false; | |
2363 | root_elem = lo_map_reserve(&lo.ino_map, lo.root.fuse_ino); | |
2364 | root_elem->inode = &lo.root; | |
2365 | ||
b39bce12 | 2366 | lo_map_init(&lo.dirp_map); |
73b4d19d | 2367 | lo_map_init(&lo.fd_map); |
b39bce12 | 2368 | |
7387863d DDAG |
2369 | if (fuse_parse_cmdline(&args, &opts) != 0) { |
2370 | return 1; | |
2371 | } | |
f185621d SH |
2372 | fuse_set_log_func(log_func); |
2373 | use_syslog = opts.syslog; | |
2374 | if (use_syslog) { | |
2375 | openlog("virtiofsd", LOG_PID, LOG_DAEMON); | |
2376 | } | |
7387863d | 2377 | if (opts.show_help) { |
67aab022 | 2378 | printf("usage: %s [options]\n\n", argv[0]); |
7387863d | 2379 | fuse_cmdline_help(); |
4ff075f7 | 2380 | printf(" -o source=PATH shared directory tree\n"); |
7387863d DDAG |
2381 | fuse_lowlevel_help(); |
2382 | ret = 0; | |
2383 | goto err_out1; | |
2384 | } else if (opts.show_version) { | |
2385 | fuse_lowlevel_version(); | |
2386 | ret = 0; | |
2387 | goto err_out1; | |
45018fbb SH |
2388 | } else if (opts.print_capabilities) { |
2389 | print_capabilities(); | |
2390 | ret = 0; | |
2391 | goto err_out1; | |
7387863d DDAG |
2392 | } |
2393 | ||
7387863d DDAG |
2394 | if (fuse_opt_parse(&args, &lo, lo_opts, NULL) == -1) { |
2395 | return 1; | |
2396 | } | |
2397 | ||
d240314a EG |
2398 | /* |
2399 | * log_level is 0 if not configured via cmd options (0 is LOG_EMERG, | |
2400 | * and we don't use this log level). | |
2401 | */ | |
2402 | if (opts.log_level != 0) { | |
2403 | current_log_level = opts.log_level; | |
2404 | } | |
7387863d | 2405 | lo.debug = opts.debug; |
d240314a EG |
2406 | if (lo.debug) { |
2407 | current_log_level = FUSE_LOG_DEBUG; | |
2408 | } | |
7387863d | 2409 | lo.root.refcount = 2; |
d240314a | 2410 | |
7387863d DDAG |
2411 | if (lo.source) { |
2412 | struct stat stat; | |
2413 | int res; | |
2414 | ||
2415 | res = lstat(lo.source, &stat); | |
2416 | if (res == -1) { | |
2417 | fuse_log(FUSE_LOG_ERR, "failed to stat source (\"%s\"): %m\n", | |
2418 | lo.source); | |
2419 | exit(1); | |
2420 | } | |
2421 | if (!S_ISDIR(stat.st_mode)) { | |
2422 | fuse_log(FUSE_LOG_ERR, "source is not a directory\n"); | |
2423 | exit(1); | |
2424 | } | |
2425 | ||
2426 | } else { | |
2427 | lo.source = "/"; | |
2428 | } | |
2429 | lo.root.is_symlink = false; | |
2430 | if (!lo.timeout_set) { | |
2431 | switch (lo.cache) { | |
2432 | case CACHE_NEVER: | |
2433 | lo.timeout = 0.0; | |
2434 | break; | |
2435 | ||
2436 | case CACHE_NORMAL: | |
2437 | lo.timeout = 1.0; | |
2438 | break; | |
2439 | ||
2440 | case CACHE_ALWAYS: | |
2441 | lo.timeout = 86400.0; | |
2442 | break; | |
2443 | } | |
2444 | } else if (lo.timeout < 0) { | |
2445 | fuse_log(FUSE_LOG_ERR, "timeout is negative (%lf)\n", lo.timeout); | |
2446 | exit(1); | |
2447 | } | |
2448 | ||
2449 | lo.root.fd = open(lo.source, O_PATH); | |
5baa3b8e | 2450 | |
7387863d DDAG |
2451 | if (lo.root.fd == -1) { |
2452 | fuse_log(FUSE_LOG_ERR, "open(\"%s\", O_PATH): %m\n", lo.source); | |
2453 | exit(1); | |
2454 | } | |
2455 | ||
2456 | se = fuse_session_new(&args, &lo_oper, sizeof(lo_oper), &lo); | |
2457 | if (se == NULL) { | |
2458 | goto err_out1; | |
2459 | } | |
2460 | ||
2461 | if (fuse_set_signal_handlers(se) != 0) { | |
2462 | goto err_out2; | |
2463 | } | |
2464 | ||
67aab022 | 2465 | if (fuse_session_mount(se) != 0) { |
7387863d DDAG |
2466 | goto err_out3; |
2467 | } | |
2468 | ||
2469 | fuse_daemonize(opts.foreground); | |
2470 | ||
01a6dc95 SH |
2471 | setup_nofile_rlimit(); |
2472 | ||
2405f3c0 DDAG |
2473 | /* Must be before sandbox since it wants /proc */ |
2474 | setup_capng(); | |
2475 | ||
f185621d | 2476 | setup_sandbox(&lo, se, opts.syslog); |
5baa3b8e | 2477 | |
7387863d | 2478 | /* Block until ctrl+c or fusermount -u */ |
f6f3573c | 2479 | ret = virtio_loop(se); |
7387863d DDAG |
2480 | |
2481 | fuse_session_unmount(se); | |
2405f3c0 | 2482 | cleanup_capng(); |
7c6b6602 | 2483 | err_out3: |
7387863d | 2484 | fuse_remove_signal_handlers(se); |
7c6b6602 | 2485 | err_out2: |
7387863d | 2486 | fuse_session_destroy(se); |
7c6b6602 | 2487 | err_out1: |
7387863d | 2488 | fuse_opt_free_args(&args); |
7c6b6602 | 2489 | |
73b4d19d | 2490 | lo_map_destroy(&lo.fd_map); |
b39bce12 | 2491 | lo_map_destroy(&lo.dirp_map); |
92fb57b8 SH |
2492 | lo_map_destroy(&lo.ino_map); |
2493 | ||
9f59d175 SH |
2494 | if (lo.proc_self_fd >= 0) { |
2495 | close(lo.proc_self_fd); | |
2496 | } | |
2497 | ||
7387863d DDAG |
2498 | if (lo.root.fd >= 0) { |
2499 | close(lo.root.fd); | |
2500 | } | |
7c6b6602 | 2501 | |
7387863d | 2502 | return ret ? 1 : 0; |
7c6b6602 | 2503 | } |