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