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