xref: /freebsd/sys/kern/vfs_export.c (revision d65cd7a57bf0600b722afc770838a5d0c1c3a8e1)
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 	int	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 `struct xucred'
122 	 * (argp->ex_anon) 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_anon.cr_version != XUCRED_VERSION) {
128 		vfs_mount_error(mp, "ex_anon.cr_version: %d != %d",
129 		    argp->ex_anon.cr_version, XUCRED_VERSION);
130 		return (EINVAL);
131 	}
132 
133 	if (argp->ex_addrlen == 0) {
134 		if (mp->mnt_flag & MNT_DEFEXPORTED) {
135 			vfs_mount_error(mp,
136 			    "MNT_DEFEXPORTED already set for mount %p", mp);
137 			return (EPERM);
138 		}
139 		np = &nep->ne_defexported;
140 		np->netc_exflags = argp->ex_flags;
141 		np->netc_anon = crget();
142 		np->netc_anon->cr_uid = argp->ex_anon.cr_uid;
143 		crsetgroups(np->netc_anon, argp->ex_anon.cr_ngroups,
144 		    argp->ex_anon.cr_groups);
145 		np->netc_anon->cr_prison = &prison0;
146 		prison_hold(np->netc_anon->cr_prison);
147 		np->netc_numsecflavors = argp->ex_numsecflavors;
148 		bcopy(argp->ex_secflavors, np->netc_secflavors,
149 		    sizeof(np->netc_secflavors));
150 		MNT_ILOCK(mp);
151 		mp->mnt_flag |= MNT_DEFEXPORTED;
152 		MNT_IUNLOCK(mp);
153 		return (0);
154 	}
155 
156 #if MSIZE <= 256
157 	if (argp->ex_addrlen > MLEN) {
158 		vfs_mount_error(mp, "ex_addrlen %d is greater than %d",
159 		    argp->ex_addrlen, MLEN);
160 		return (EINVAL);
161 	}
162 #endif
163 
164 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
165 	np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
166 	saddr = (struct sockaddr *) (np + 1);
167 	if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen)))
168 		goto out;
169 	if (saddr->sa_family == AF_UNSPEC || saddr->sa_family > AF_MAX) {
170 		error = EINVAL;
171 		vfs_mount_error(mp, "Invalid saddr->sa_family: %d");
172 		goto out;
173 	}
174 	if (saddr->sa_len > argp->ex_addrlen)
175 		saddr->sa_len = argp->ex_addrlen;
176 	if (argp->ex_masklen) {
177 		smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
178 		error = copyin(argp->ex_mask, smask, argp->ex_masklen);
179 		if (error)
180 			goto out;
181 		if (smask->sa_len > argp->ex_masklen)
182 			smask->sa_len = argp->ex_masklen;
183 	}
184 	rnh = NULL;
185 	switch (saddr->sa_family) {
186 #ifdef INET
187 	case AF_INET:
188 		if ((rnh = nep->ne4) == NULL) {
189 			off = offsetof(struct sockaddr_in, sin_addr) << 3;
190 			rnh = vfs_create_addrlist_af(&nep->ne4, off);
191 		}
192 		break;
193 #endif
194 #ifdef INET6
195 	case AF_INET6:
196 		if ((rnh = nep->ne6) == NULL) {
197 			off = offsetof(struct sockaddr_in6, sin6_addr) << 3;
198 			rnh = vfs_create_addrlist_af(&nep->ne6, off);
199 		}
200 		break;
201 #endif
202 	}
203 	if (rnh == NULL) {
204 		error = ENOBUFS;
205 		vfs_mount_error(mp, "%s %s %d",
206 		    "Unable to initialize radix node head ",
207 		    "for address family", saddr->sa_family);
208 		goto out;
209 	}
210 	RADIX_NODE_HEAD_LOCK(rnh);
211 	rn = (*rnh->rnh_addaddr)(saddr, smask, &rnh->rh, np->netc_rnodes);
212 	RADIX_NODE_HEAD_UNLOCK(rnh);
213 	if (rn == NULL || np != (struct netcred *)rn) {	/* already exists */
214 		error = EPERM;
215 		vfs_mount_error(mp,
216 		    "netcred already exists for given addr/mask");
217 		goto out;
218 	}
219 	np->netc_exflags = argp->ex_flags;
220 	np->netc_anon = crget();
221 	np->netc_anon->cr_uid = argp->ex_anon.cr_uid;
222 	crsetgroups(np->netc_anon, argp->ex_anon.cr_ngroups,
223 	    argp->ex_anon.cr_groups);
224 	np->netc_anon->cr_prison = &prison0;
225 	prison_hold(np->netc_anon->cr_prison);
226 	np->netc_numsecflavors = argp->ex_numsecflavors;
227 	bcopy(argp->ex_secflavors, np->netc_secflavors,
228 	    sizeof(np->netc_secflavors));
229 	return (0);
230 out:
231 	free(np, M_NETADDR);
232 	return (error);
233 }
234 
235 /* Helper for vfs_free_addrlist. */
236 /* ARGSUSED */
237 static int
238 vfs_free_netcred(struct radix_node *rn, void *w)
239 {
240 	struct radix_node_head *rnh = (struct radix_node_head *) w;
241 	struct ucred *cred;
242 
243 	(*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, &rnh->rh);
244 	cred = ((struct netcred *)rn)->netc_anon;
245 	if (cred != NULL)
246 		crfree(cred);
247 	free(rn, M_NETADDR);
248 	return (0);
249 }
250 
251 #if defined(INET) || defined(INET6)
252 static struct radix_node_head *
253 vfs_create_addrlist_af(struct radix_node_head **prnh, int off)
254 {
255 
256 	if (rn_inithead((void **)prnh, off) == 0)
257 		return (NULL);
258 	RADIX_NODE_HEAD_LOCK_INIT(*prnh);
259 	return (*prnh);
260 }
261 #endif
262 
263 static void
264 vfs_free_addrlist_af(struct radix_node_head **prnh)
265 {
266 	struct radix_node_head *rnh;
267 
268 	rnh = *prnh;
269 	RADIX_NODE_HEAD_LOCK(rnh);
270 	(*rnh->rnh_walktree)(&rnh->rh, vfs_free_netcred, rnh);
271 	RADIX_NODE_HEAD_UNLOCK(rnh);
272 	RADIX_NODE_HEAD_DESTROY(rnh);
273 	rn_detachhead((void **)prnh);
274 	prnh = NULL;
275 }
276 
277 /*
278  * Free the net address hash lists that are hanging off the mount points.
279  */
280 static void
281 vfs_free_addrlist(struct netexport *nep)
282 {
283 	struct ucred *cred;
284 
285 	if (nep->ne4 != NULL)
286 		vfs_free_addrlist_af(&nep->ne4);
287 	if (nep->ne6 != NULL)
288 		vfs_free_addrlist_af(&nep->ne6);
289 
290 	cred = nep->ne_defexported.netc_anon;
291 	if (cred != NULL)
292 		crfree(cred);
293 
294 }
295 
296 /*
297  * High level function to manipulate export options on a mount point
298  * and the passed in netexport.
299  * Struct export_args *argp is the variable used to twiddle options,
300  * the structure is described in sys/mount.h
301  */
302 int
303 vfs_export(struct mount *mp, struct export_args *argp)
304 {
305 	struct netexport *nep;
306 	int error;
307 
308 	if ((argp->ex_flags & (MNT_DELEXPORT | MNT_EXPORTED)) == 0)
309 		return (EINVAL);
310 
311 	if ((argp->ex_flags & MNT_EXPORTED) != 0 &&
312 	    (argp->ex_numsecflavors <= 0
313 	    || argp->ex_numsecflavors >= MAXSECFLAVORS))
314 		return (EINVAL);
315 
316 	error = 0;
317 	lockmgr(&mp->mnt_explock, LK_EXCLUSIVE, NULL);
318 	nep = mp->mnt_export;
319 	if (argp->ex_flags & MNT_DELEXPORT) {
320 		if (nep == NULL) {
321 			error = ENOENT;
322 			goto out;
323 		}
324 		if (mp->mnt_flag & MNT_EXPUBLIC) {
325 			vfs_setpublicfs(NULL, NULL, NULL);
326 			MNT_ILOCK(mp);
327 			mp->mnt_flag &= ~MNT_EXPUBLIC;
328 			MNT_IUNLOCK(mp);
329 		}
330 		vfs_free_addrlist(nep);
331 		mp->mnt_export = NULL;
332 		free(nep, M_MOUNT);
333 		nep = NULL;
334 		MNT_ILOCK(mp);
335 		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
336 		MNT_IUNLOCK(mp);
337 	}
338 	if (argp->ex_flags & MNT_EXPORTED) {
339 		if (nep == NULL) {
340 			nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO);
341 			mp->mnt_export = nep;
342 		}
343 		if (argp->ex_flags & MNT_EXPUBLIC) {
344 			if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
345 				goto out;
346 			MNT_ILOCK(mp);
347 			mp->mnt_flag |= MNT_EXPUBLIC;
348 			MNT_IUNLOCK(mp);
349 		}
350 		if ((error = vfs_hang_addrlist(mp, nep, argp)))
351 			goto out;
352 		MNT_ILOCK(mp);
353 		mp->mnt_flag |= MNT_EXPORTED;
354 		MNT_IUNLOCK(mp);
355 	}
356 
357 out:
358 	lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
359 	/*
360 	 * Once we have executed the vfs_export() command, we do
361 	 * not want to keep the "export" option around in the
362 	 * options list, since that will cause subsequent MNT_UPDATE
363 	 * calls to fail.  The export information is saved in
364 	 * mp->mnt_export, so we can safely delete the "export" mount option
365 	 * here.
366 	 */
367 	vfs_deleteopt(mp->mnt_optnew, "export");
368 	vfs_deleteopt(mp->mnt_opt, "export");
369 	return (error);
370 }
371 
372 /*
373  * Set the publicly exported filesystem (WebNFS). Currently, only
374  * one public filesystem is possible in the spec (RFC 2054 and 2055)
375  */
376 int
377 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
378     struct export_args *argp)
379 {
380 	int error;
381 	struct vnode *rvp;
382 	char *cp;
383 
384 	/*
385 	 * mp == NULL -> invalidate the current info, the FS is
386 	 * no longer exported. May be called from either vfs_export
387 	 * or unmount, so check if it hasn't already been done.
388 	 */
389 	if (mp == NULL) {
390 		if (nfs_pub.np_valid) {
391 			nfs_pub.np_valid = 0;
392 			if (nfs_pub.np_index != NULL) {
393 				free(nfs_pub.np_index, M_TEMP);
394 				nfs_pub.np_index = NULL;
395 			}
396 		}
397 		return (0);
398 	}
399 
400 	/*
401 	 * Only one allowed at a time.
402 	 */
403 	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
404 		return (EBUSY);
405 
406 	/*
407 	 * Get real filehandle for root of exported FS.
408 	 */
409 	bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
410 	nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
411 
412 	if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp)))
413 		return (error);
414 
415 	if ((error = VOP_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
416 		return (error);
417 
418 	vput(rvp);
419 
420 	/*
421 	 * If an indexfile was specified, pull it in.
422 	 */
423 	if (argp->ex_indexfile != NULL) {
424 		if (nfs_pub.np_index == NULL)
425 			nfs_pub.np_index = malloc(MAXNAMLEN + 1, M_TEMP,
426 			    M_WAITOK);
427 		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
428 		    MAXNAMLEN, (size_t *)0);
429 		if (!error) {
430 			/*
431 			 * Check for illegal filenames.
432 			 */
433 			for (cp = nfs_pub.np_index; *cp; cp++) {
434 				if (*cp == '/') {
435 					error = EINVAL;
436 					break;
437 				}
438 			}
439 		}
440 		if (error) {
441 			free(nfs_pub.np_index, M_TEMP);
442 			nfs_pub.np_index = NULL;
443 			return (error);
444 		}
445 	}
446 
447 	nfs_pub.np_mount = mp;
448 	nfs_pub.np_valid = 1;
449 	return (0);
450 }
451 
452 /*
453  * Used by the filesystems to determine if a given network address
454  * (passed in 'nam') is present in their exports list, returns a pointer
455  * to struct netcred so that the filesystem can examine it for
456  * access rights (read/write/etc).
457  */
458 static struct netcred *
459 vfs_export_lookup(struct mount *mp, struct sockaddr *nam)
460 {
461 	RADIX_NODE_HEAD_RLOCK_TRACKER;
462 	struct netexport *nep;
463 	struct netcred *np = NULL;
464 	struct radix_node_head *rnh;
465 	struct sockaddr *saddr;
466 
467 	nep = mp->mnt_export;
468 	if (nep == NULL)
469 		return (NULL);
470 	if ((mp->mnt_flag & MNT_EXPORTED) == 0)
471 		return (NULL);
472 
473 	/*
474 	 * Lookup in the export list
475 	 */
476 	if (nam != NULL) {
477 		saddr = nam;
478 		rnh = NULL;
479 		switch (saddr->sa_family) {
480 		case AF_INET:
481 			rnh = nep->ne4;
482 			break;
483 		case AF_INET6:
484 			rnh = nep->ne6;
485 			break;
486 		}
487 		if (rnh != NULL) {
488 			RADIX_NODE_HEAD_RLOCK(rnh);
489 			np = (struct netcred *) (*rnh->rnh_matchaddr)(saddr, &rnh->rh);
490 			RADIX_NODE_HEAD_RUNLOCK(rnh);
491 			if (np != NULL && (np->netc_rnodes->rn_flags & RNF_ROOT) != 0)
492 				return (NULL);
493 		}
494 	}
495 
496 	/*
497 	 * If no address match, use the default if it exists.
498 	 */
499 	if (np == NULL && (mp->mnt_flag & MNT_DEFEXPORTED) != 0)
500 		return (&nep->ne_defexported);
501 
502 	return (np);
503 }
504 
505 /*
506  * XXX: This comment comes from the deprecated ufs_check_export()
507  * XXX: and may not entirely apply, but lacking something better:
508  * This is the generic part of fhtovp called after the underlying
509  * filesystem has validated the file handle.
510  *
511  * Verify that a host should have access to a filesystem.
512  */
513 
514 int
515 vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
516     struct ucred **credanonp, int *numsecflavors, int **secflavors)
517 {
518 	struct netcred *np;
519 
520 	lockmgr(&mp->mnt_explock, LK_SHARED, NULL);
521 	np = vfs_export_lookup(mp, nam);
522 	if (np == NULL) {
523 		lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
524 		*credanonp = NULL;
525 		return (EACCES);
526 	}
527 	*extflagsp = np->netc_exflags;
528 	if ((*credanonp = np->netc_anon) != NULL)
529 		crhold(*credanonp);
530 	if (numsecflavors) {
531 		*numsecflavors = np->netc_numsecflavors;
532 		KASSERT(*numsecflavors > 0,
533 		    ("%s: numsecflavors <= 0", __func__));
534 		KASSERT(*numsecflavors < MAXSECFLAVORS,
535 		    ("%s: numsecflavors >= MAXSECFLAVORS", __func__));
536 	}
537 	if (secflavors)
538 		*secflavors = np->netc_secflavors;
539 	lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
540 	return (0);
541 }
542 
543