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/types.h> 62 #include <sys/module.h> 63 #include <sys/systm.h> 64 #include <sys/errno.h> 65 #include <sys/param.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_fuse, 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_fuse, 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_fuse, 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 int max_read_set; 213 uint32_t max_read; 214 int daemon_timeout; 215 int fd; 216 217 size_t len; 218 219 struct cdev *fdev; 220 struct fuse_data *data; 221 struct thread *td; 222 struct file *fp, *fptmp; 223 char *fspec, *subtype; 224 struct vfsoptlist *opts; 225 226 subtype = NULL; 227 max_read_set = 0; 228 max_read = ~0; 229 err = 0; 230 mntopts = 0; 231 __mntopts = 0; 232 td = curthread; 233 234 fuse_trace_printf_vfsop(); 235 236 if (mp->mnt_flag & MNT_UPDATE) 237 return EOPNOTSUPP; 238 239 MNT_ILOCK(mp); 240 mp->mnt_flag |= MNT_SYNCHRONOUS; 241 mp->mnt_data = NULL; 242 MNT_IUNLOCK(mp); 243 /* Get the new options passed to mount */ 244 opts = mp->mnt_optnew; 245 246 if (!opts) 247 return EINVAL; 248 249 /* `fspath' contains the mount point (eg. /mnt/fuse/sshfs); REQUIRED */ 250 if (!vfs_getopts(opts, "fspath", &err)) 251 return err; 252 253 /* `from' contains the device name (eg. /dev/fuse0); REQUIRED */ 254 fspec = vfs_getopts(opts, "from", &err); 255 if (!fspec) 256 return err; 257 258 /* `fd' contains the filedescriptor for this session; REQUIRED */ 259 if (vfs_scanopt(opts, "fd", "%d", &fd) != 1) 260 return EINVAL; 261 262 err = fuse_getdevice(fspec, td, &fdev); 263 if (err != 0) 264 return err; 265 266 /* 267 * With the help of underscored options the mount program 268 * can inform us from the flags it sets by default 269 */ 270 FUSE_FLAGOPT(allow_other, FSESS_DAEMON_CAN_SPY); 271 FUSE_FLAGOPT(push_symlinks_in, FSESS_PUSH_SYMLINKS_IN); 272 FUSE_FLAGOPT(default_permissions, FSESS_DEFAULT_PERMISSIONS); 273 FUSE_FLAGOPT(no_attrcache, FSESS_NO_ATTRCACHE); 274 FUSE_FLAGOPT(no_readahed, FSESS_NO_READAHEAD); 275 FUSE_FLAGOPT(no_datacache, FSESS_NO_DATACACHE); 276 FUSE_FLAGOPT(no_namecache, FSESS_NO_NAMECACHE); 277 FUSE_FLAGOPT(no_mmap, FSESS_NO_MMAP); 278 FUSE_FLAGOPT(brokenio, FSESS_BROKENIO); 279 280 if (vfs_scanopt(opts, "max_read=", "%u", &max_read) == 1) 281 max_read_set = 1; 282 if (vfs_scanopt(opts, "timeout=", "%u", &daemon_timeout) == 1) { 283 if (daemon_timeout < FUSE_MIN_DAEMON_TIMEOUT) 284 daemon_timeout = FUSE_MIN_DAEMON_TIMEOUT; 285 else if (daemon_timeout > FUSE_MAX_DAEMON_TIMEOUT) 286 daemon_timeout = FUSE_MAX_DAEMON_TIMEOUT; 287 } else { 288 daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT; 289 } 290 subtype = vfs_getopts(opts, "subtype=", &err); 291 292 FS_DEBUG2G("mntopts 0x%jx\n", (uintmax_t)mntopts); 293 294 err = fget(td, fd, &cap_read_rights, &fp); 295 if (err != 0) { 296 FS_DEBUG("invalid or not opened device: data=%p\n", data); 297 goto out; 298 } 299 fptmp = td->td_fpop; 300 td->td_fpop = fp; 301 err = devfs_get_cdevpriv((void **)&data); 302 td->td_fpop = fptmp; 303 fdrop(fp, td); 304 FUSE_LOCK(); 305 if (err != 0 || data == NULL || data->mp != NULL) { 306 FS_DEBUG("invalid or not opened device: data=%p data.mp=%p\n", 307 data, data != NULL ? data->mp : NULL); 308 err = ENXIO; 309 FUSE_UNLOCK(); 310 goto out; 311 } 312 if (fdata_get_dead(data)) { 313 FS_DEBUG("device is dead during mount: data=%p\n", data); 314 err = ENOTCONN; 315 FUSE_UNLOCK(); 316 goto out; 317 } 318 /* Sanity + permission checks */ 319 if (!data->daemoncred) 320 panic("fuse daemon found, but identity unknown"); 321 if (mntopts & FSESS_DAEMON_CAN_SPY) 322 err = priv_check(td, PRIV_VFS_FUSE_ALLOWOTHER); 323 if (err == 0 && td->td_ucred->cr_uid != data->daemoncred->cr_uid) 324 /* are we allowed to do the first mount? */ 325 err = priv_check(td, PRIV_VFS_FUSE_MOUNT_NONUSER); 326 if (err) { 327 FUSE_UNLOCK(); 328 goto out; 329 } 330 data->ref++; 331 data->mp = mp; 332 data->dataflags |= mntopts; 333 data->max_read = max_read; 334 data->daemon_timeout = daemon_timeout; 335 FUSE_UNLOCK(); 336 337 vfs_getnewfsid(mp); 338 MNT_ILOCK(mp); 339 mp->mnt_data = data; 340 mp->mnt_flag |= MNT_LOCAL; 341 mp->mnt_kern_flag |= MNTK_USES_BCACHE; 342 MNT_IUNLOCK(mp); 343 /* We need this here as this slot is used by getnewvnode() */ 344 mp->mnt_stat.f_iosize = PAGE_SIZE; 345 if (subtype) { 346 strlcat(mp->mnt_stat.f_fstypename, ".", MFSNAMELEN); 347 strlcat(mp->mnt_stat.f_fstypename, subtype, MFSNAMELEN); 348 } 349 copystr(fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &len); 350 bzero(mp->mnt_stat.f_mntfromname + len, MNAMELEN - len); 351 FS_DEBUG2G("mp %p: %s\n", mp, mp->mnt_stat.f_mntfromname); 352 353 /* Now handshaking with daemon */ 354 fuse_internal_send_init(data, td); 355 356 out: 357 if (err) { 358 FUSE_LOCK(); 359 if (data->mp == mp) { 360 /* 361 * Destroy device only if we acquired reference to 362 * it 363 */ 364 FS_DEBUG("mount failed, destroy device: data=%p mp=%p" 365 " err=%d\n", 366 data, mp, err); 367 data->mp = NULL; 368 fdata_trydestroy(data); 369 } 370 FUSE_UNLOCK(); 371 dev_rel(fdev); 372 } 373 return err; 374 } 375 376 static int 377 fuse_vfsop_unmount(struct mount *mp, int mntflags) 378 { 379 int err = 0; 380 int flags = 0; 381 382 struct cdev *fdev; 383 struct fuse_data *data; 384 struct fuse_dispatcher fdi; 385 struct thread *td = curthread; 386 387 fuse_trace_printf_vfsop(); 388 389 if (mntflags & MNT_FORCE) { 390 flags |= FORCECLOSE; 391 } 392 data = fuse_get_mpdata(mp); 393 if (!data) { 394 panic("no private data for mount point?"); 395 } 396 /* There is 1 extra root vnode reference (mp->mnt_data). */ 397 FUSE_LOCK(); 398 if (data->vroot != NULL) { 399 struct vnode *vroot = data->vroot; 400 401 data->vroot = NULL; 402 FUSE_UNLOCK(); 403 vrele(vroot); 404 } else 405 FUSE_UNLOCK(); 406 err = vflush(mp, 0, flags, td); 407 if (err) { 408 debug_printf("vflush failed"); 409 return err; 410 } 411 if (fdata_get_dead(data)) { 412 goto alreadydead; 413 } 414 fdisp_init(&fdi, 0); 415 fdisp_make(&fdi, FUSE_DESTROY, mp, 0, td, NULL); 416 417 err = fdisp_wait_answ(&fdi); 418 fdisp_destroy(&fdi); 419 420 fdata_set_dead(data); 421 422 alreadydead: 423 FUSE_LOCK(); 424 data->mp = NULL; 425 fdev = data->fdev; 426 fdata_trydestroy(data); 427 FUSE_UNLOCK(); 428 429 MNT_ILOCK(mp); 430 mp->mnt_data = NULL; 431 mp->mnt_flag &= ~MNT_LOCAL; 432 MNT_IUNLOCK(mp); 433 434 dev_rel(fdev); 435 436 return 0; 437 } 438 439 static int 440 fuse_vfsop_root(struct mount *mp, int lkflags, struct vnode **vpp) 441 { 442 struct fuse_data *data = fuse_get_mpdata(mp); 443 int err = 0; 444 445 if (data->vroot != NULL) { 446 err = vget(data->vroot, lkflags, curthread); 447 if (err == 0) 448 *vpp = data->vroot; 449 } else { 450 err = fuse_vnode_get(mp, FUSE_ROOT_ID, NULL, vpp, NULL, VDIR); 451 if (err == 0) { 452 FUSE_LOCK(); 453 MPASS(data->vroot == NULL || data->vroot == *vpp); 454 if (data->vroot == NULL) { 455 FS_DEBUG("new root vnode\n"); 456 data->vroot = *vpp; 457 FUSE_UNLOCK(); 458 vref(*vpp); 459 } else if (data->vroot != *vpp) { 460 FS_DEBUG("root vnode race\n"); 461 FUSE_UNLOCK(); 462 VOP_UNLOCK(*vpp, 0); 463 vrele(*vpp); 464 vrecycle(*vpp); 465 *vpp = data->vroot; 466 } else 467 FUSE_UNLOCK(); 468 } 469 } 470 return err; 471 } 472 473 static int 474 fuse_vfsop_statfs(struct mount *mp, struct statfs *sbp) 475 { 476 struct fuse_dispatcher fdi; 477 int err = 0; 478 479 struct fuse_statfs_out *fsfo; 480 struct fuse_data *data; 481 482 FS_DEBUG2G("mp %p: %s\n", mp, mp->mnt_stat.f_mntfromname); 483 data = fuse_get_mpdata(mp); 484 485 if (!(data->dataflags & FSESS_INITED)) 486 goto fake; 487 488 fdisp_init(&fdi, 0); 489 fdisp_make(&fdi, FUSE_STATFS, mp, FUSE_ROOT_ID, NULL, NULL); 490 err = fdisp_wait_answ(&fdi); 491 if (err) { 492 fdisp_destroy(&fdi); 493 if (err == ENOTCONN) { 494 /* 495 * We want to seem a legitimate fs even if the daemon 496 * is stiff dead... (so that, eg., we can still do path 497 * based unmounting after the daemon dies). 498 */ 499 goto fake; 500 } 501 return err; 502 } 503 fsfo = fdi.answ; 504 505 sbp->f_blocks = fsfo->st.blocks; 506 sbp->f_bfree = fsfo->st.bfree; 507 sbp->f_bavail = fsfo->st.bavail; 508 sbp->f_files = fsfo->st.files; 509 sbp->f_ffree = fsfo->st.ffree; /* cast from uint64_t to int64_t */ 510 sbp->f_namemax = fsfo->st.namelen; 511 sbp->f_bsize = fsfo->st.frsize; /* cast from uint32_t to uint64_t */ 512 513 FS_DEBUG("fuse_statfs_out -- blocks: %llu, bfree: %llu, bavail: %llu, " 514 "fil es: %llu, ffree: %llu, bsize: %i, namelen: %i\n", 515 (unsigned long long)fsfo->st.blocks, 516 (unsigned long long)fsfo->st.bfree, 517 (unsigned long long)fsfo->st.bavail, 518 (unsigned long long)fsfo->st.files, 519 (unsigned long long)fsfo->st.ffree, fsfo->st.bsize, 520 fsfo->st.namelen); 521 522 fdisp_destroy(&fdi); 523 return 0; 524 525 fake: 526 sbp->f_blocks = 0; 527 sbp->f_bfree = 0; 528 sbp->f_bavail = 0; 529 sbp->f_files = 0; 530 sbp->f_ffree = 0; 531 sbp->f_namemax = 0; 532 sbp->f_bsize = FUSE_DEFAULT_BLOCKSIZE; 533 534 return 0; 535 } 536