1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include "opt_inet.h" 43 #include "opt_inet6.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/dirent.h> 48 #include <sys/jail.h> 49 #include <sys/kernel.h> 50 #include <sys/lock.h> 51 #include <sys/malloc.h> 52 #include <sys/mbuf.h> 53 #include <sys/mount.h> 54 #include <sys/mutex.h> 55 #include <sys/rmlock.h> 56 #include <sys/refcount.h> 57 #include <sys/signalvar.h> 58 #include <sys/socket.h> 59 #include <sys/vnode.h> 60 61 #include <netinet/in.h> 62 #include <net/radix.h> 63 64 static MALLOC_DEFINE(M_NETADDR, "export_host", "Export host address structure"); 65 66 #if defined(INET) || defined(INET6) 67 static struct radix_node_head *vfs_create_addrlist_af( 68 struct radix_node_head **prnh, int off); 69 #endif 70 static void vfs_free_addrlist(struct netexport *nep); 71 static int vfs_free_netcred(struct radix_node *rn, void *w); 72 static void vfs_free_addrlist_af(struct radix_node_head **prnh); 73 static int vfs_hang_addrlist(struct mount *mp, struct netexport *nep, 74 struct export_args *argp); 75 static struct netcred *vfs_export_lookup(struct mount *, struct sockaddr *); 76 77 /* 78 * Network address lookup element 79 */ 80 struct netcred { 81 struct radix_node netc_rnodes[2]; 82 uint64_t netc_exflags; 83 struct ucred *netc_anon; 84 int netc_numsecflavors; 85 int netc_secflavors[MAXSECFLAVORS]; 86 }; 87 88 /* 89 * Network export information 90 */ 91 struct netexport { 92 struct netcred ne_defexported; /* Default export */ 93 struct radix_node_head *ne4; 94 struct radix_node_head *ne6; 95 }; 96 97 /* 98 * Build hash lists of net addresses and hang them off the mount point. 99 * Called by vfs_export() to set up the lists of export addresses. 100 */ 101 static int 102 vfs_hang_addrlist(struct mount *mp, struct netexport *nep, 103 struct export_args *argp) 104 { 105 struct netcred *np; 106 struct radix_node_head *rnh; 107 int i; 108 struct radix_node *rn; 109 struct sockaddr *saddr, *smask = NULL; 110 #if defined(INET6) || defined(INET) 111 int off; 112 #endif 113 int error; 114 115 KASSERT(argp->ex_numsecflavors > 0, 116 ("%s: numsecflavors <= 0", __func__)); 117 KASSERT(argp->ex_numsecflavors < MAXSECFLAVORS, 118 ("%s: numsecflavors >= MAXSECFLAVORS", __func__)); 119 120 /* 121 * XXX: This routine converts from a uid plus gid list 122 * to a `struct ucred' (np->netc_anon). This 123 * operation is questionable; for example, what should be done 124 * with fields like cr_uidinfo and cr_prison? Currently, this 125 * routine does not touch them (leaves them as NULL). 126 */ 127 if (argp->ex_addrlen == 0) { 128 if (mp->mnt_flag & MNT_DEFEXPORTED) { 129 vfs_mount_error(mp, 130 "MNT_DEFEXPORTED already set for mount %p", mp); 131 return (EPERM); 132 } 133 np = &nep->ne_defexported; 134 np->netc_exflags = argp->ex_flags; 135 np->netc_anon = crget(); 136 np->netc_anon->cr_uid = argp->ex_uid; 137 crsetgroups(np->netc_anon, argp->ex_ngroups, 138 argp->ex_groups); 139 np->netc_anon->cr_prison = &prison0; 140 prison_hold(np->netc_anon->cr_prison); 141 np->netc_numsecflavors = argp->ex_numsecflavors; 142 bcopy(argp->ex_secflavors, np->netc_secflavors, 143 sizeof(np->netc_secflavors)); 144 MNT_ILOCK(mp); 145 mp->mnt_flag |= MNT_DEFEXPORTED; 146 MNT_IUNLOCK(mp); 147 return (0); 148 } 149 150 #if MSIZE <= 256 151 if (argp->ex_addrlen > MLEN) { 152 vfs_mount_error(mp, "ex_addrlen %d is greater than %d", 153 argp->ex_addrlen, MLEN); 154 return (EINVAL); 155 } 156 #endif 157 158 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen; 159 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO); 160 saddr = (struct sockaddr *) (np + 1); 161 if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen))) 162 goto out; 163 if (saddr->sa_family == AF_UNSPEC || saddr->sa_family > AF_MAX) { 164 error = EINVAL; 165 vfs_mount_error(mp, "Invalid saddr->sa_family: %d"); 166 goto out; 167 } 168 if (saddr->sa_len > argp->ex_addrlen) 169 saddr->sa_len = argp->ex_addrlen; 170 if (argp->ex_masklen) { 171 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen); 172 error = copyin(argp->ex_mask, smask, argp->ex_masklen); 173 if (error) 174 goto out; 175 if (smask->sa_len > argp->ex_masklen) 176 smask->sa_len = argp->ex_masklen; 177 } 178 rnh = NULL; 179 switch (saddr->sa_family) { 180 #ifdef INET 181 case AF_INET: 182 if ((rnh = nep->ne4) == NULL) { 183 off = offsetof(struct sockaddr_in, sin_addr) << 3; 184 rnh = vfs_create_addrlist_af(&nep->ne4, off); 185 } 186 break; 187 #endif 188 #ifdef INET6 189 case AF_INET6: 190 if ((rnh = nep->ne6) == NULL) { 191 off = offsetof(struct sockaddr_in6, sin6_addr) << 3; 192 rnh = vfs_create_addrlist_af(&nep->ne6, off); 193 } 194 break; 195 #endif 196 } 197 if (rnh == NULL) { 198 error = ENOBUFS; 199 vfs_mount_error(mp, "%s %s %d", 200 "Unable to initialize radix node head ", 201 "for address family", saddr->sa_family); 202 goto out; 203 } 204 RADIX_NODE_HEAD_LOCK(rnh); 205 rn = (*rnh->rnh_addaddr)(saddr, smask, &rnh->rh, np->netc_rnodes); 206 RADIX_NODE_HEAD_UNLOCK(rnh); 207 if (rn == NULL || np != (struct netcred *)rn) { /* already exists */ 208 error = EPERM; 209 vfs_mount_error(mp, 210 "netcred already exists for given addr/mask"); 211 goto out; 212 } 213 np->netc_exflags = argp->ex_flags; 214 np->netc_anon = crget(); 215 np->netc_anon->cr_uid = argp->ex_uid; 216 crsetgroups(np->netc_anon, argp->ex_ngroups, 217 argp->ex_groups); 218 np->netc_anon->cr_prison = &prison0; 219 prison_hold(np->netc_anon->cr_prison); 220 np->netc_numsecflavors = argp->ex_numsecflavors; 221 bcopy(argp->ex_secflavors, np->netc_secflavors, 222 sizeof(np->netc_secflavors)); 223 return (0); 224 out: 225 free(np, M_NETADDR); 226 return (error); 227 } 228 229 /* Helper for vfs_free_addrlist. */ 230 /* ARGSUSED */ 231 static int 232 vfs_free_netcred(struct radix_node *rn, void *w) 233 { 234 struct radix_node_head *rnh = (struct radix_node_head *) w; 235 struct ucred *cred; 236 237 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, &rnh->rh); 238 cred = ((struct netcred *)rn)->netc_anon; 239 if (cred != NULL) 240 crfree(cred); 241 free(rn, M_NETADDR); 242 return (0); 243 } 244 245 #if defined(INET) || defined(INET6) 246 static struct radix_node_head * 247 vfs_create_addrlist_af(struct radix_node_head **prnh, int off) 248 { 249 250 if (rn_inithead((void **)prnh, off) == 0) 251 return (NULL); 252 RADIX_NODE_HEAD_LOCK_INIT(*prnh); 253 return (*prnh); 254 } 255 #endif 256 257 static void 258 vfs_free_addrlist_af(struct radix_node_head **prnh) 259 { 260 struct radix_node_head *rnh; 261 262 rnh = *prnh; 263 RADIX_NODE_HEAD_LOCK(rnh); 264 (*rnh->rnh_walktree)(&rnh->rh, vfs_free_netcred, rnh); 265 RADIX_NODE_HEAD_UNLOCK(rnh); 266 RADIX_NODE_HEAD_DESTROY(rnh); 267 rn_detachhead((void **)prnh); 268 prnh = NULL; 269 } 270 271 /* 272 * Free the net address hash lists that are hanging off the mount points. 273 */ 274 static void 275 vfs_free_addrlist(struct netexport *nep) 276 { 277 struct ucred *cred; 278 279 if (nep->ne4 != NULL) 280 vfs_free_addrlist_af(&nep->ne4); 281 if (nep->ne6 != NULL) 282 vfs_free_addrlist_af(&nep->ne6); 283 284 cred = nep->ne_defexported.netc_anon; 285 if (cred != NULL) 286 crfree(cred); 287 288 } 289 290 /* 291 * High level function to manipulate export options on a mount point 292 * and the passed in netexport. 293 * Struct export_args *argp is the variable used to twiddle options, 294 * the structure is described in sys/mount.h 295 */ 296 int 297 vfs_export(struct mount *mp, struct export_args *argp) 298 { 299 struct netexport *nep; 300 int error; 301 302 if ((argp->ex_flags & (MNT_DELEXPORT | MNT_EXPORTED)) == 0) 303 return (EINVAL); 304 305 if ((argp->ex_flags & MNT_EXPORTED) != 0 && 306 (argp->ex_numsecflavors <= 0 307 || argp->ex_numsecflavors >= MAXSECFLAVORS)) 308 return (EINVAL); 309 310 error = 0; 311 lockmgr(&mp->mnt_explock, LK_EXCLUSIVE, NULL); 312 nep = mp->mnt_export; 313 if (argp->ex_flags & MNT_DELEXPORT) { 314 if (nep == NULL) { 315 error = ENOENT; 316 goto out; 317 } 318 if (mp->mnt_flag & MNT_EXPUBLIC) { 319 vfs_setpublicfs(NULL, NULL, NULL); 320 MNT_ILOCK(mp); 321 mp->mnt_flag &= ~MNT_EXPUBLIC; 322 MNT_IUNLOCK(mp); 323 } 324 vfs_free_addrlist(nep); 325 mp->mnt_export = NULL; 326 free(nep, M_MOUNT); 327 nep = NULL; 328 MNT_ILOCK(mp); 329 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED); 330 MNT_IUNLOCK(mp); 331 } 332 if (argp->ex_flags & MNT_EXPORTED) { 333 if (nep == NULL) { 334 nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO); 335 mp->mnt_export = nep; 336 } 337 if (argp->ex_flags & MNT_EXPUBLIC) { 338 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0) 339 goto out; 340 MNT_ILOCK(mp); 341 mp->mnt_flag |= MNT_EXPUBLIC; 342 MNT_IUNLOCK(mp); 343 } 344 if ((error = vfs_hang_addrlist(mp, nep, argp))) 345 goto out; 346 MNT_ILOCK(mp); 347 mp->mnt_flag |= MNT_EXPORTED; 348 MNT_IUNLOCK(mp); 349 } 350 351 out: 352 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL); 353 /* 354 * Once we have executed the vfs_export() command, we do 355 * not want to keep the "export" option around in the 356 * options list, since that will cause subsequent MNT_UPDATE 357 * calls to fail. The export information is saved in 358 * mp->mnt_export, so we can safely delete the "export" mount option 359 * here. 360 */ 361 vfs_deleteopt(mp->mnt_optnew, "export"); 362 vfs_deleteopt(mp->mnt_opt, "export"); 363 return (error); 364 } 365 366 /* 367 * Set the publicly exported filesystem (WebNFS). Currently, only 368 * one public filesystem is possible in the spec (RFC 2054 and 2055) 369 */ 370 int 371 vfs_setpublicfs(struct mount *mp, struct netexport *nep, 372 struct export_args *argp) 373 { 374 int error; 375 struct vnode *rvp; 376 char *cp; 377 378 /* 379 * mp == NULL -> invalidate the current info, the FS is 380 * no longer exported. May be called from either vfs_export 381 * or unmount, so check if it hasn't already been done. 382 */ 383 if (mp == NULL) { 384 if (nfs_pub.np_valid) { 385 nfs_pub.np_valid = 0; 386 if (nfs_pub.np_index != NULL) { 387 free(nfs_pub.np_index, M_TEMP); 388 nfs_pub.np_index = NULL; 389 } 390 } 391 return (0); 392 } 393 394 /* 395 * Only one allowed at a time. 396 */ 397 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount) 398 return (EBUSY); 399 400 /* 401 * Get real filehandle for root of exported FS. 402 */ 403 bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle)); 404 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid; 405 406 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp))) 407 return (error); 408 409 if ((error = VOP_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid))) 410 return (error); 411 412 vput(rvp); 413 414 /* 415 * If an indexfile was specified, pull it in. 416 */ 417 if (argp->ex_indexfile != NULL) { 418 if (nfs_pub.np_index == NULL) 419 nfs_pub.np_index = malloc(MAXNAMLEN + 1, M_TEMP, 420 M_WAITOK); 421 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index, 422 MAXNAMLEN, (size_t *)0); 423 if (!error) { 424 /* 425 * Check for illegal filenames. 426 */ 427 for (cp = nfs_pub.np_index; *cp; cp++) { 428 if (*cp == '/') { 429 error = EINVAL; 430 break; 431 } 432 } 433 } 434 if (error) { 435 free(nfs_pub.np_index, M_TEMP); 436 nfs_pub.np_index = NULL; 437 return (error); 438 } 439 } 440 441 nfs_pub.np_mount = mp; 442 nfs_pub.np_valid = 1; 443 return (0); 444 } 445 446 /* 447 * Used by the filesystems to determine if a given network address 448 * (passed in 'nam') is present in their exports list, returns a pointer 449 * to struct netcred so that the filesystem can examine it for 450 * access rights (read/write/etc). 451 */ 452 static struct netcred * 453 vfs_export_lookup(struct mount *mp, struct sockaddr *nam) 454 { 455 RADIX_NODE_HEAD_RLOCK_TRACKER; 456 struct netexport *nep; 457 struct netcred *np = NULL; 458 struct radix_node_head *rnh; 459 struct sockaddr *saddr; 460 461 nep = mp->mnt_export; 462 if (nep == NULL) 463 return (NULL); 464 if ((mp->mnt_flag & MNT_EXPORTED) == 0) 465 return (NULL); 466 467 /* 468 * Lookup in the export list 469 */ 470 if (nam != NULL) { 471 saddr = nam; 472 rnh = NULL; 473 switch (saddr->sa_family) { 474 case AF_INET: 475 rnh = nep->ne4; 476 break; 477 case AF_INET6: 478 rnh = nep->ne6; 479 break; 480 } 481 if (rnh != NULL) { 482 RADIX_NODE_HEAD_RLOCK(rnh); 483 np = (struct netcred *) (*rnh->rnh_matchaddr)(saddr, &rnh->rh); 484 RADIX_NODE_HEAD_RUNLOCK(rnh); 485 if (np != NULL && (np->netc_rnodes->rn_flags & RNF_ROOT) != 0) 486 return (NULL); 487 } 488 } 489 490 /* 491 * If no address match, use the default if it exists. 492 */ 493 if (np == NULL && (mp->mnt_flag & MNT_DEFEXPORTED) != 0) 494 return (&nep->ne_defexported); 495 496 return (np); 497 } 498 499 /* 500 * XXX: This comment comes from the deprecated ufs_check_export() 501 * XXX: and may not entirely apply, but lacking something better: 502 * This is the generic part of fhtovp called after the underlying 503 * filesystem has validated the file handle. 504 * 505 * Verify that a host should have access to a filesystem. 506 */ 507 508 int 509 vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, uint64_t *extflagsp, 510 struct ucred **credanonp, int *numsecflavors, int *secflavors) 511 { 512 struct netcred *np; 513 514 lockmgr(&mp->mnt_explock, LK_SHARED, NULL); 515 np = vfs_export_lookup(mp, nam); 516 if (np == NULL) { 517 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL); 518 *credanonp = NULL; 519 return (EACCES); 520 } 521 *extflagsp = np->netc_exflags; 522 if ((*credanonp = np->netc_anon) != NULL) 523 crhold(*credanonp); 524 if (numsecflavors) { 525 *numsecflavors = np->netc_numsecflavors; 526 KASSERT(*numsecflavors > 0, 527 ("%s: numsecflavors <= 0", __func__)); 528 KASSERT(*numsecflavors < MAXSECFLAVORS, 529 ("%s: numsecflavors >= MAXSECFLAVORS", __func__)); 530 } 531 if (secflavors && np->netc_numsecflavors > 0) 532 memcpy(secflavors, np->netc_secflavors, np->netc_numsecflavors * 533 sizeof(int)); 534 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL); 535 return (0); 536 } 537 538