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