xref: /freebsd/sys/fs/fuse/fuse_vfsops.c (revision 123af6ec70016f5556da5972d4d63c7d175c06d3)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2007-2009 Google Inc. and Amit Singh
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  *   copyright notice, this list of conditions and the following disclaimer
15  *   in the documentation and/or other materials provided with the
16  *   distribution.
17  * * Neither the name of Google Inc. nor the names of its
18  *   contributors may be used to endorse or promote products derived from
19  *   this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Copyright (C) 2005 Csaba Henk.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  */
57 
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
60 
61 #include <sys/param.h>
62 #include <sys/buf.h>
63 #include <sys/module.h>
64 #include <sys/systm.h>
65 #include <sys/errno.h>
66 #include <sys/kernel.h>
67 #include <sys/capsicum.h>
68 #include <sys/conf.h>
69 #include <sys/filedesc.h>
70 #include <sys/uio.h>
71 #include <sys/malloc.h>
72 #include <sys/queue.h>
73 #include <sys/lock.h>
74 #include <sys/sx.h>
75 #include <sys/mutex.h>
76 #include <sys/proc.h>
77 #include <sys/vnode.h>
78 #include <sys/namei.h>
79 #include <sys/mount.h>
80 #include <sys/sysctl.h>
81 #include <sys/fcntl.h>
82 
83 #include "fuse.h"
84 #include "fuse_param.h"
85 #include "fuse_node.h"
86 #include "fuse_ipc.h"
87 #include "fuse_internal.h"
88 
89 #include <sys/priv.h>
90 #include <security/mac/mac_framework.h>
91 
92 #define FUSE_DEBUG_MODULE VFSOPS
93 #include "fuse_debug.h"
94 
95 /* This will do for privilege types for now */
96 #ifndef PRIV_VFS_FUSE_ALLOWOTHER
97 #define PRIV_VFS_FUSE_ALLOWOTHER PRIV_VFS_MOUNT_NONUSER
98 #endif
99 #ifndef PRIV_VFS_FUSE_MOUNT_NONUSER
100 #define PRIV_VFS_FUSE_MOUNT_NONUSER PRIV_VFS_MOUNT_NONUSER
101 #endif
102 #ifndef PRIV_VFS_FUSE_SYNC_UNMOUNT
103 #define PRIV_VFS_FUSE_SYNC_UNMOUNT PRIV_VFS_MOUNT_NONUSER
104 #endif
105 
106 static vfs_mount_t fuse_vfsop_mount;
107 static vfs_unmount_t fuse_vfsop_unmount;
108 static vfs_root_t fuse_vfsop_root;
109 static vfs_statfs_t fuse_vfsop_statfs;
110 
111 struct vfsops fuse_vfsops = {
112 	.vfs_mount = fuse_vfsop_mount,
113 	.vfs_unmount = fuse_vfsop_unmount,
114 	.vfs_root = fuse_vfsop_root,
115 	.vfs_statfs = fuse_vfsop_statfs,
116 };
117 
118 SYSCTL_INT(_vfs_fusefs, OID_AUTO, init_backgrounded, CTLFLAG_RD,
119     SYSCTL_NULL_INT_PTR, 1, "indicate async handshake");
120 static int fuse_enforce_dev_perms = 0;
121 
122 SYSCTL_INT(_vfs_fusefs, OID_AUTO, enforce_dev_perms, CTLFLAG_RW,
123     &fuse_enforce_dev_perms, 0,
124     "enforce fuse device permissions for secondary mounts");
125 static unsigned sync_unmount = 1;
126 
127 SYSCTL_UINT(_vfs_fusefs, OID_AUTO, sync_unmount, CTLFLAG_RW,
128     &sync_unmount, 0, "specify when to use synchronous unmount");
129 
130 MALLOC_DEFINE(M_FUSEVFS, "fuse_filesystem", "buffer for fuse vfs layer");
131 
132 static int
133 fuse_getdevice(const char *fspec, struct thread *td, struct cdev **fdevp)
134 {
135 	struct nameidata nd, *ndp = &nd;
136 	struct vnode *devvp;
137 	struct cdev *fdev;
138 	int err;
139 
140 	/*
141 	 * Not an update, or updating the name: look up the name
142 	 * and verify that it refers to a sensible disk device.
143 	 */
144 
145 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, td);
146 	if ((err = namei(ndp)) != 0)
147 		return err;
148 	NDFREE(ndp, NDF_ONLY_PNBUF);
149 	devvp = ndp->ni_vp;
150 
151 	if (devvp->v_type != VCHR) {
152 		vrele(devvp);
153 		return ENXIO;
154 	}
155 	fdev = devvp->v_rdev;
156 	dev_ref(fdev);
157 
158 	if (fuse_enforce_dev_perms) {
159 		/*
160 	         * Check if mounter can open the fuse device.
161 	         *
162 	         * This has significance only if we are doing a secondary mount
163 	         * which doesn't involve actually opening fuse devices, but we
164 	         * still want to enforce the permissions of the device (in
165 	         * order to keep control over the circle of fuse users).
166 	         *
167 	         * (In case of primary mounts, we are either the superuser so
168 	         * we can do anything anyway, or we can mount only if the
169 	         * device is already opened by us, ie. we are permitted to open
170 	         * the device.)
171 	         */
172 #if 0
173 #ifdef MAC
174 		err = mac_check_vnode_open(td->td_ucred, devvp, VREAD | VWRITE);
175 		if (!err)
176 #endif
177 #endif /* 0 */
178 			err = VOP_ACCESS(devvp, VREAD | VWRITE, td->td_ucred, td);
179 		if (err) {
180 			vrele(devvp);
181 			dev_rel(fdev);
182 			return err;
183 		}
184 	}
185 	/*
186 	 * according to coda code, no extra lock is needed --
187 	 * although in sys/vnode.h this field is marked "v"
188 	 */
189 	vrele(devvp);
190 
191 	if (!fdev->si_devsw ||
192 	    strcmp("fuse", fdev->si_devsw->d_name)) {
193 		dev_rel(fdev);
194 		return ENXIO;
195 	}
196 	*fdevp = fdev;
197 
198 	return 0;
199 }
200 
201 #define FUSE_FLAGOPT(fnam, fval) do {				\
202 	vfs_flagopt(opts, #fnam, &mntopts, fval);		\
203 	vfs_flagopt(opts, "__" #fnam, &__mntopts, fval);	\
204 } while (0)
205 
206 static int
207 fuse_vfsop_mount(struct mount *mp)
208 {
209 	int err;
210 
211 	uint64_t mntopts, __mntopts;
212 	uint32_t max_read;
213 	int daemon_timeout;
214 	int fd;
215 
216 	size_t len;
217 
218 	struct cdev *fdev;
219 	struct fuse_data *data;
220 	struct thread *td;
221 	struct file *fp, *fptmp;
222 	char *fspec, *subtype;
223 	struct vfsoptlist *opts;
224 
225 	subtype = NULL;
226 	max_read = ~0;
227 	err = 0;
228 	mntopts = 0;
229 	__mntopts = 0;
230 	td = curthread;
231 
232 	fuse_trace_printf_vfsop();
233 
234 	if (mp->mnt_flag & MNT_UPDATE)
235 		return EOPNOTSUPP;
236 
237 	MNT_ILOCK(mp);
238 	mp->mnt_flag |= MNT_SYNCHRONOUS;
239 	mp->mnt_data = NULL;
240 	MNT_IUNLOCK(mp);
241 	/* Get the new options passed to mount */
242 	opts = mp->mnt_optnew;
243 
244 	if (!opts)
245 		return EINVAL;
246 
247 	/* `fspath' contains the mount point (eg. /mnt/fuse/sshfs); REQUIRED */
248 	if (!vfs_getopts(opts, "fspath", &err))
249 		return err;
250 
251 	/* `from' contains the device name (eg. /dev/fuse0); REQUIRED */
252 	fspec = vfs_getopts(opts, "from", &err);
253 	if (!fspec)
254 		return err;
255 
256 	/* `fd' contains the filedescriptor for this session; REQUIRED */
257 	if (vfs_scanopt(opts, "fd", "%d", &fd) != 1)
258 		return EINVAL;
259 
260 	err = fuse_getdevice(fspec, td, &fdev);
261 	if (err != 0)
262 		return err;
263 
264 	/*
265 	 * With the help of underscored options the mount program
266 	 * can inform us from the flags it sets by default
267 	 */
268 	FUSE_FLAGOPT(allow_other, FSESS_DAEMON_CAN_SPY);
269 	FUSE_FLAGOPT(push_symlinks_in, FSESS_PUSH_SYMLINKS_IN);
270 	FUSE_FLAGOPT(default_permissions, FSESS_DEFAULT_PERMISSIONS);
271 	FUSE_FLAGOPT(no_attrcache, FSESS_NO_ATTRCACHE);
272 	FUSE_FLAGOPT(no_readahed, FSESS_NO_READAHEAD);
273 	FUSE_FLAGOPT(no_datacache, FSESS_NO_DATACACHE);
274 	FUSE_FLAGOPT(no_namecache, FSESS_NO_NAMECACHE);
275 	FUSE_FLAGOPT(no_mmap, FSESS_NO_MMAP);
276 	FUSE_FLAGOPT(brokenio, FSESS_BROKENIO);
277 
278 	(void)vfs_scanopt(opts, "max_read=", "%u", &max_read);
279 	if (vfs_scanopt(opts, "timeout=", "%u", &daemon_timeout) == 1) {
280 		if (daemon_timeout < FUSE_MIN_DAEMON_TIMEOUT)
281 			daemon_timeout = FUSE_MIN_DAEMON_TIMEOUT;
282 		else if (daemon_timeout > FUSE_MAX_DAEMON_TIMEOUT)
283 			daemon_timeout = FUSE_MAX_DAEMON_TIMEOUT;
284 	} else {
285 		daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT;
286 	}
287 	subtype = vfs_getopts(opts, "subtype=", &err);
288 
289 	FS_DEBUG2G("mntopts 0x%jx\n", (uintmax_t)mntopts);
290 
291 	err = fget(td, fd, &cap_read_rights, &fp);
292 	if (err != 0) {
293 		FS_DEBUG("invalid or not opened device: data=%p\n", data);
294 		goto out;
295 	}
296 	fptmp = td->td_fpop;
297 	td->td_fpop = fp;
298 	err = devfs_get_cdevpriv((void **)&data);
299 	td->td_fpop = fptmp;
300 	fdrop(fp, td);
301 	FUSE_LOCK();
302 	if (err != 0 || data == NULL || data->mp != NULL) {
303 		FS_DEBUG("invalid or not opened device: data=%p data.mp=%p\n",
304 		    data, data != NULL ? data->mp : NULL);
305 		err = ENXIO;
306 		FUSE_UNLOCK();
307 		goto out;
308 	}
309 	if (fdata_get_dead(data)) {
310 		FS_DEBUG("device is dead during mount: data=%p\n", data);
311 		err = ENOTCONN;
312 		FUSE_UNLOCK();
313 		goto out;
314 	}
315 	/* Sanity + permission checks */
316 	if (!data->daemoncred)
317 		panic("fuse daemon found, but identity unknown");
318 	if (mntopts & FSESS_DAEMON_CAN_SPY)
319 		err = priv_check(td, PRIV_VFS_FUSE_ALLOWOTHER);
320 	if (err == 0 && td->td_ucred->cr_uid != data->daemoncred->cr_uid)
321 		/* are we allowed to do the first mount? */
322 		err = priv_check(td, PRIV_VFS_FUSE_MOUNT_NONUSER);
323 	if (err) {
324 		FUSE_UNLOCK();
325 		goto out;
326 	}
327 	data->ref++;
328 	data->mp = mp;
329 	data->dataflags |= mntopts;
330 	data->max_read = max_read;
331 	data->daemon_timeout = daemon_timeout;
332 	FUSE_UNLOCK();
333 
334 	vfs_getnewfsid(mp);
335 	MNT_ILOCK(mp);
336 	mp->mnt_data = data;
337 	mp->mnt_flag |= MNT_LOCAL;
338 	mp->mnt_kern_flag |= MNTK_USES_BCACHE;
339 	MNT_IUNLOCK(mp);
340 	/* We need this here as this slot is used by getnewvnode() */
341 	mp->mnt_stat.f_iosize = maxbcachebuf;
342 	if (subtype) {
343 		strlcat(mp->mnt_stat.f_fstypename, ".", MFSNAMELEN);
344 		strlcat(mp->mnt_stat.f_fstypename, subtype, MFSNAMELEN);
345 	}
346 	copystr(fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &len);
347 	bzero(mp->mnt_stat.f_mntfromname + len, MNAMELEN - len);
348 	FS_DEBUG2G("mp %p: %s\n", mp, mp->mnt_stat.f_mntfromname);
349 
350 	/* Now handshaking with daemon */
351 	fuse_internal_send_init(data, td);
352 
353 out:
354 	if (err) {
355 		FUSE_LOCK();
356 		if (data->mp == mp) {
357 			/*
358 			 * Destroy device only if we acquired reference to
359 			 * it
360 			 */
361 			FS_DEBUG("mount failed, destroy device: data=%p mp=%p"
362 			      " err=%d\n",
363 			    data, mp, err);
364 			data->mp = NULL;
365 			fdata_trydestroy(data);
366 		}
367 		FUSE_UNLOCK();
368 		dev_rel(fdev);
369 	}
370 	return err;
371 }
372 
373 static int
374 fuse_vfsop_unmount(struct mount *mp, int mntflags)
375 {
376 	int err = 0;
377 	int flags = 0;
378 
379 	struct cdev *fdev;
380 	struct fuse_data *data;
381 	struct fuse_dispatcher fdi;
382 	struct thread *td = curthread;
383 
384 	fuse_trace_printf_vfsop();
385 
386 	if (mntflags & MNT_FORCE) {
387 		flags |= FORCECLOSE;
388 	}
389 	data = fuse_get_mpdata(mp);
390 	if (!data) {
391 		panic("no private data for mount point?");
392 	}
393 	/* There is 1 extra root vnode reference (mp->mnt_data). */
394 	FUSE_LOCK();
395 	if (data->vroot != NULL) {
396 		struct vnode *vroot = data->vroot;
397 
398 		data->vroot = NULL;
399 		FUSE_UNLOCK();
400 		vrele(vroot);
401 	} else
402 		FUSE_UNLOCK();
403 	err = vflush(mp, 0, flags, td);
404 	if (err) {
405 		debug_printf("vflush failed");
406 		return err;
407 	}
408 	if (fdata_get_dead(data)) {
409 		goto alreadydead;
410 	}
411 	fdisp_init(&fdi, 0);
412 	fdisp_make(&fdi, FUSE_DESTROY, mp, 0, td, NULL);
413 
414 	err = fdisp_wait_answ(&fdi);
415 	fdisp_destroy(&fdi);
416 
417 	fdata_set_dead(data);
418 
419 alreadydead:
420 	FUSE_LOCK();
421 	data->mp = NULL;
422 	fdev = data->fdev;
423 	fdata_trydestroy(data);
424 	FUSE_UNLOCK();
425 
426 	MNT_ILOCK(mp);
427 	mp->mnt_data = NULL;
428 	mp->mnt_flag &= ~MNT_LOCAL;
429 	MNT_IUNLOCK(mp);
430 
431 	dev_rel(fdev);
432 
433 	return 0;
434 }
435 
436 static int
437 fuse_vfsop_root(struct mount *mp, int lkflags, struct vnode **vpp)
438 {
439 	struct fuse_data *data = fuse_get_mpdata(mp);
440 	int err = 0;
441 
442 	if (data->vroot != NULL) {
443 		err = vget(data->vroot, lkflags, curthread);
444 		if (err == 0)
445 			*vpp = data->vroot;
446 	} else {
447 		err = fuse_vnode_get(mp, NULL, FUSE_ROOT_ID, NULL, vpp, NULL,
448 		    VDIR);
449 		if (err == 0) {
450 			FUSE_LOCK();
451 			MPASS(data->vroot == NULL || data->vroot == *vpp);
452 			if (data->vroot == NULL) {
453 				FS_DEBUG("new root vnode\n");
454 				data->vroot = *vpp;
455 				FUSE_UNLOCK();
456 				vref(*vpp);
457 			} else if (data->vroot != *vpp) {
458 				FS_DEBUG("root vnode race\n");
459 				FUSE_UNLOCK();
460 				VOP_UNLOCK(*vpp, 0);
461 				vrele(*vpp);
462 				vrecycle(*vpp);
463 				*vpp = data->vroot;
464 			} else
465 				FUSE_UNLOCK();
466 		}
467 	}
468 	return err;
469 }
470 
471 static int
472 fuse_vfsop_statfs(struct mount *mp, struct statfs *sbp)
473 {
474 	struct fuse_dispatcher fdi;
475 	int err = 0;
476 
477 	struct fuse_statfs_out *fsfo;
478 	struct fuse_data *data;
479 
480 	FS_DEBUG2G("mp %p: %s\n", mp, mp->mnt_stat.f_mntfromname);
481 	data = fuse_get_mpdata(mp);
482 
483 	if (!(data->dataflags & FSESS_INITED))
484 		goto fake;
485 
486 	fdisp_init(&fdi, 0);
487 	fdisp_make(&fdi, FUSE_STATFS, mp, FUSE_ROOT_ID, NULL, NULL);
488 	err = fdisp_wait_answ(&fdi);
489 	if (err) {
490 		fdisp_destroy(&fdi);
491 		if (err == ENOTCONN) {
492 			/*
493 	                 * We want to seem a legitimate fs even if the daemon
494 	                 * is stiff dead... (so that, eg., we can still do path
495 	                 * based unmounting after the daemon dies).
496 	                 */
497 			goto fake;
498 		}
499 		return err;
500 	}
501 	fsfo = fdi.answ;
502 
503 	sbp->f_blocks = fsfo->st.blocks;
504 	sbp->f_bfree = fsfo->st.bfree;
505 	sbp->f_bavail = fsfo->st.bavail;
506 	sbp->f_files = fsfo->st.files;
507 	sbp->f_ffree = fsfo->st.ffree;	/* cast from uint64_t to int64_t */
508 	sbp->f_namemax = fsfo->st.namelen;
509 	sbp->f_bsize = fsfo->st.frsize;	/* cast from uint32_t to uint64_t */
510 
511 	FS_DEBUG("fuse_statfs_out -- blocks: %llu, bfree: %llu, bavail: %llu, "
512 	      "fil	es: %llu, ffree: %llu, bsize: %i, namelen: %i\n",
513 	      (unsigned long long)fsfo->st.blocks,
514 	      (unsigned long long)fsfo->st.bfree,
515 	      (unsigned long long)fsfo->st.bavail,
516 	      (unsigned long long)fsfo->st.files,
517 	      (unsigned long long)fsfo->st.ffree, fsfo->st.bsize,
518 	      fsfo->st.namelen);
519 
520 	fdisp_destroy(&fdi);
521 	return 0;
522 
523 fake:
524 	sbp->f_blocks = 0;
525 	sbp->f_bfree = 0;
526 	sbp->f_bavail = 0;
527 	sbp->f_files = 0;
528 	sbp->f_ffree = 0;
529 	sbp->f_namemax = 0;
530 	sbp->f_bsize = FUSE_DEFAULT_BLOCKSIZE;
531 
532 	return 0;
533 }
534