xref: /freebsd/sys/fs/nfsserver/nfs_nfsdkrpc.c (revision 964219664dcec4198441910904fb9064569d174d)
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  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
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  * 3. 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  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include "opt_inet6.h"
40 #include "opt_kgssapi.h"
41 
42 #include <fs/nfs/nfsport.h>
43 
44 #include <rpc/rpc.h>
45 #include <rpc/rpcsec_gss.h>
46 
47 #include <nfs/nfs_fha.h>
48 #include <fs/nfsserver/nfs_fha_new.h>
49 
50 #include <security/mac/mac_framework.h>
51 
52 NFSDLOCKMUTEX;
53 NFSV4ROOTLOCKMUTEX;
54 struct nfsv4lock nfsd_suspend_lock;
55 
56 /*
57  * Mapping of old NFS Version 2 RPC numbers to generic numbers.
58  */
59 int newnfs_nfsv3_procid[NFS_V3NPROCS] = {
60 	NFSPROC_NULL,
61 	NFSPROC_GETATTR,
62 	NFSPROC_SETATTR,
63 	NFSPROC_NOOP,
64 	NFSPROC_LOOKUP,
65 	NFSPROC_READLINK,
66 	NFSPROC_READ,
67 	NFSPROC_NOOP,
68 	NFSPROC_WRITE,
69 	NFSPROC_CREATE,
70 	NFSPROC_REMOVE,
71 	NFSPROC_RENAME,
72 	NFSPROC_LINK,
73 	NFSPROC_SYMLINK,
74 	NFSPROC_MKDIR,
75 	NFSPROC_RMDIR,
76 	NFSPROC_READDIR,
77 	NFSPROC_FSSTAT,
78 	NFSPROC_NOOP,
79 	NFSPROC_NOOP,
80 	NFSPROC_NOOP,
81 	NFSPROC_NOOP,
82 };
83 
84 
85 SYSCTL_DECL(_vfs_nfsd);
86 
87 SVCPOOL		*nfsrvd_pool;
88 
89 static int	nfs_privport = 0;
90 SYSCTL_INT(_vfs_nfsd, OID_AUTO, nfs_privport, CTLFLAG_RWTUN,
91     &nfs_privport, 0,
92     "Only allow clients using a privileged port for NFSv2 and 3");
93 
94 static int	nfs_minvers = NFS_VER2;
95 SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_min_nfsvers, CTLFLAG_RWTUN,
96     &nfs_minvers, 0, "The lowest version of NFS handled by the server");
97 
98 static int	nfs_maxvers = NFS_VER4;
99 SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_max_nfsvers, CTLFLAG_RWTUN,
100     &nfs_maxvers, 0, "The highest version of NFS handled by the server");
101 
102 static int nfs_proc(struct nfsrv_descript *, u_int32_t, SVCXPRT *xprt,
103     struct nfsrvcache **);
104 
105 extern u_long sb_max_adj;
106 extern int newnfs_numnfsd;
107 extern struct proc *nfsd_master_proc;
108 extern time_t nfsdev_time;
109 
110 /*
111  * NFS server system calls
112  */
113 
114 static void
115 nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt)
116 {
117 	struct nfsrv_descript nd;
118 	struct nfsrvcache *rp = NULL;
119 	int cacherep, credflavor;
120 
121 	memset(&nd, 0, sizeof(nd));
122 	if (rqst->rq_vers == NFS_VER2) {
123 		if (rqst->rq_proc > NFSV2PROC_STATFS ||
124 		    newnfs_nfsv3_procid[rqst->rq_proc] == NFSPROC_NOOP) {
125 			svcerr_noproc(rqst);
126 			svc_freereq(rqst);
127 			goto out;
128 		}
129 		nd.nd_procnum = newnfs_nfsv3_procid[rqst->rq_proc];
130 		nd.nd_flag = ND_NFSV2;
131 	} else if (rqst->rq_vers == NFS_VER3) {
132 		if (rqst->rq_proc >= NFS_V3NPROCS) {
133 			svcerr_noproc(rqst);
134 			svc_freereq(rqst);
135 			goto out;
136 		}
137 		nd.nd_procnum = rqst->rq_proc;
138 		nd.nd_flag = ND_NFSV3;
139 	} else {
140 		if (rqst->rq_proc != NFSPROC_NULL &&
141 		    rqst->rq_proc != NFSV4PROC_COMPOUND) {
142 			svcerr_noproc(rqst);
143 			svc_freereq(rqst);
144 			goto out;
145 		}
146 		nd.nd_procnum = rqst->rq_proc;
147 		nd.nd_flag = ND_NFSV4;
148 	}
149 
150 	/*
151 	 * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 -
152 	 * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP
153 	 * mounts.
154 	 */
155 	nd.nd_mrep = rqst->rq_args;
156 	rqst->rq_args = NULL;
157 	newnfs_realign(&nd.nd_mrep, M_WAITOK);
158 	nd.nd_md = nd.nd_mrep;
159 	nd.nd_dpos = mtod(nd.nd_md, caddr_t);
160 	nd.nd_nam = svc_getrpccaller(rqst);
161 	nd.nd_nam2 = rqst->rq_addr;
162 	nd.nd_mreq = NULL;
163 	nd.nd_cred = NULL;
164 
165 	if (nfs_privport && (nd.nd_flag & ND_NFSV4) == 0) {
166 		/* Check if source port is privileged */
167 		u_short port;
168 		struct sockaddr *nam = nd.nd_nam;
169 		struct sockaddr_in *sin;
170 
171 		sin = (struct sockaddr_in *)nam;
172 		/*
173 		 * INET/INET6 - same code:
174 		 *    sin_port and sin6_port are at same offset
175 		 */
176 		port = ntohs(sin->sin_port);
177 		if (port >= IPPORT_RESERVED &&
178 		    nd.nd_procnum != NFSPROC_NULL) {
179 #ifdef INET6
180 			char buf[INET6_ADDRSTRLEN];
181 #else
182 			char buf[INET_ADDRSTRLEN];
183 #endif
184 #ifdef INET6
185 #if defined(KLD_MODULE)
186 			/* Do not use ip6_sprintf: the nfs module should work without INET6. */
187 #define	ip6_sprintf(buf, a)						\
188 			(sprintf((buf), "%x:%x:%x:%x:%x:%x:%x:%x",	\
189 			    (a)->s6_addr16[0], (a)->s6_addr16[1],	\
190 			    (a)->s6_addr16[2], (a)->s6_addr16[3],	\
191 			    (a)->s6_addr16[4], (a)->s6_addr16[5],	\
192 			    (a)->s6_addr16[6], (a)->s6_addr16[7]),	\
193 			    (buf))
194 #endif
195 #endif
196 			printf("NFS request from unprivileged port (%s:%d)\n",
197 #ifdef INET6
198 			    sin->sin_family == AF_INET6 ?
199 			    ip6_sprintf(buf, &satosin6(sin)->sin6_addr) :
200 #if defined(KLD_MODULE)
201 #undef ip6_sprintf
202 #endif
203 #endif
204 			    inet_ntoa_r(sin->sin_addr, buf), port);
205 			svcerr_weakauth(rqst);
206 			svc_freereq(rqst);
207 			m_freem(nd.nd_mrep);
208 			goto out;
209 		}
210 	}
211 
212 	if (nd.nd_procnum != NFSPROC_NULL) {
213 		if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) {
214 			svcerr_weakauth(rqst);
215 			svc_freereq(rqst);
216 			m_freem(nd.nd_mrep);
217 			goto out;
218 		}
219 
220 		/* Set the flag based on credflavor */
221 		if (credflavor == RPCSEC_GSS_KRB5) {
222 			nd.nd_flag |= ND_GSS;
223 		} else if (credflavor == RPCSEC_GSS_KRB5I) {
224 			nd.nd_flag |= (ND_GSS | ND_GSSINTEGRITY);
225 		} else if (credflavor == RPCSEC_GSS_KRB5P) {
226 			nd.nd_flag |= (ND_GSS | ND_GSSPRIVACY);
227 		} else if (credflavor != AUTH_SYS) {
228 			svcerr_weakauth(rqst);
229 			svc_freereq(rqst);
230 			m_freem(nd.nd_mrep);
231 			goto out;
232 		}
233 
234 #ifdef MAC
235 		mac_cred_associate_nfsd(nd.nd_cred);
236 #endif
237 		/*
238 		 * Get a refcnt (shared lock) on nfsd_suspend_lock.
239 		 * NFSSVC_SUSPENDNFSD will take an exclusive lock on
240 		 * nfsd_suspend_lock to suspend these threads.
241 		 * The call to nfsv4_lock() that precedes nfsv4_getref()
242 		 * ensures that the acquisition of the exclusive lock
243 		 * takes priority over acquisition of the shared lock by
244 		 * waiting for any exclusive lock request to complete.
245 		 * This must be done here, before the check of
246 		 * nfsv4root exports by nfsvno_v4rootexport().
247 		 */
248 		NFSLOCKV4ROOTMUTEX();
249 		nfsv4_lock(&nfsd_suspend_lock, 0, NULL, NFSV4ROOTLOCKMUTEXPTR,
250 		    NULL);
251 		nfsv4_getref(&nfsd_suspend_lock, NULL, NFSV4ROOTLOCKMUTEXPTR,
252 		    NULL);
253 		NFSUNLOCKV4ROOTMUTEX();
254 
255 		if ((nd.nd_flag & ND_NFSV4) != 0) {
256 			nd.nd_repstat = nfsvno_v4rootexport(&nd);
257 			if (nd.nd_repstat != 0) {
258 				NFSLOCKV4ROOTMUTEX();
259 				nfsv4_relref(&nfsd_suspend_lock);
260 				NFSUNLOCKV4ROOTMUTEX();
261 				svcerr_weakauth(rqst);
262 				svc_freereq(rqst);
263 				m_freem(nd.nd_mrep);
264 				goto out;
265 			}
266 		}
267 
268 		cacherep = nfs_proc(&nd, rqst->rq_xid, xprt, &rp);
269 		NFSLOCKV4ROOTMUTEX();
270 		nfsv4_relref(&nfsd_suspend_lock);
271 		NFSUNLOCKV4ROOTMUTEX();
272 	} else {
273 		NFSMGET(nd.nd_mreq);
274 		nd.nd_mreq->m_len = 0;
275 		cacherep = RC_REPLY;
276 	}
277 	if (nd.nd_mrep != NULL)
278 		m_freem(nd.nd_mrep);
279 
280 	if (nd.nd_cred != NULL)
281 		crfree(nd.nd_cred);
282 
283 	if (cacherep == RC_DROPIT) {
284 		if (nd.nd_mreq != NULL)
285 			m_freem(nd.nd_mreq);
286 		svc_freereq(rqst);
287 		goto out;
288 	}
289 
290 	if (nd.nd_mreq == NULL) {
291 		svcerr_decode(rqst);
292 		svc_freereq(rqst);
293 		goto out;
294 	}
295 
296 	if (nd.nd_repstat & NFSERR_AUTHERR) {
297 		svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
298 		if (nd.nd_mreq != NULL)
299 			m_freem(nd.nd_mreq);
300 	} else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq)) {
301 		svcerr_systemerr(rqst);
302 	}
303 	if (rp != NULL) {
304 		nfsrvd_sentcache(rp, (rqst->rq_reply_seq != 0 ||
305 		    SVC_ACK(xprt, NULL)), rqst->rq_reply_seq);
306 	}
307 	svc_freereq(rqst);
308 
309 out:
310 	td_softdep_cleanup(curthread);
311 	NFSEXITCODE(0);
312 }
313 
314 /*
315  * Check the cache and, optionally, do the RPC.
316  * Return the appropriate cache response.
317  */
318 static int
319 nfs_proc(struct nfsrv_descript *nd, u_int32_t xid, SVCXPRT *xprt,
320     struct nfsrvcache **rpp)
321 {
322 	struct thread *td = curthread;
323 	int cacherep = RC_DOIT, isdgram, taglen = -1;
324 	struct mbuf *m;
325 	u_char tag[NFSV4_SMALLSTR + 1], *tagstr = NULL;
326 	u_int32_t minorvers = 0;
327 	uint32_t ack;
328 
329 	*rpp = NULL;
330 	if (nd->nd_nam2 == NULL) {
331 		nd->nd_flag |= ND_STREAMSOCK;
332 		isdgram = 0;
333 	} else {
334 		isdgram = 1;
335 	}
336 
337 	/*
338 	 * Two cases:
339 	 * 1 - For NFSv2 over UDP, if we are near our malloc/mget
340 	 *     limit, just drop the request. There is no
341 	 *     NFSERR_RESOURCE or NFSERR_DELAY for NFSv2 and the
342 	 *     client will timeout/retry over UDP in a little while.
343 	 * 2 - nd_repstat == 0 && nd_mreq == NULL, which
344 	 *     means a normal nfs rpc, so check the cache
345 	 */
346 	if ((nd->nd_flag & ND_NFSV2) && nd->nd_nam2 != NULL &&
347 	    nfsrv_mallocmget_limit()) {
348 		cacherep = RC_DROPIT;
349 	} else {
350 		/*
351 		 * For NFSv3, play it safe and assume that the client is
352 		 * doing retries on the same TCP connection.
353 		 */
354 		if ((nd->nd_flag & (ND_NFSV4 | ND_STREAMSOCK)) ==
355 		    ND_STREAMSOCK)
356 			nd->nd_flag |= ND_SAMETCPCONN;
357 		nd->nd_retxid = xid;
358 		nd->nd_tcpconntime = NFSD_MONOSEC;
359 		nd->nd_sockref = xprt->xp_sockref;
360 		if ((nd->nd_flag & ND_NFSV4) != 0)
361 			nfsd_getminorvers(nd, tag, &tagstr, &taglen,
362 			    &minorvers);
363 		if ((nd->nd_flag & ND_NFSV41) != 0)
364 			/* NFSv4.1 caches replies in the session slots. */
365 			cacherep = RC_DOIT;
366 		else {
367 			cacherep = nfsrvd_getcache(nd);
368 			ack = 0;
369 			SVC_ACK(xprt, &ack);
370 			nfsrc_trimcache(xprt->xp_sockref, ack, 0);
371 		}
372 	}
373 
374 	/*
375 	 * Handle the request. There are three cases.
376 	 * RC_DOIT - do the RPC
377 	 * RC_REPLY - return the reply already created
378 	 * RC_DROPIT - just throw the request away
379 	 */
380 	if (cacherep == RC_DOIT) {
381 		if ((nd->nd_flag & ND_NFSV41) != 0)
382 			nd->nd_xprt = xprt;
383 		nfsrvd_dorpc(nd, isdgram, tagstr, taglen, minorvers, td);
384 		if ((nd->nd_flag & ND_NFSV41) != 0) {
385 			if (nd->nd_repstat != NFSERR_REPLYFROMCACHE &&
386 			    (nd->nd_flag & ND_SAVEREPLY) != 0) {
387 				/* Cache a copy of the reply. */
388 				m = m_copym(nd->nd_mreq, 0, M_COPYALL,
389 				    M_WAITOK);
390 			} else
391 				m = NULL;
392 			if ((nd->nd_flag & ND_HASSEQUENCE) != 0)
393 				nfsrv_cache_session(nd->nd_sessionid,
394 				    nd->nd_slotid, nd->nd_repstat, &m);
395 			if (nd->nd_repstat == NFSERR_REPLYFROMCACHE)
396 				nd->nd_repstat = 0;
397 			cacherep = RC_REPLY;
398 		} else {
399 			if (nd->nd_repstat == NFSERR_DONTREPLY)
400 				cacherep = RC_DROPIT;
401 			else
402 				cacherep = RC_REPLY;
403 			*rpp = nfsrvd_updatecache(nd);
404 		}
405 	}
406 	if (tagstr != NULL && taglen > NFSV4_SMALLSTR)
407 		free(tagstr, M_TEMP);
408 
409 	NFSEXITCODE2(0, nd);
410 	return (cacherep);
411 }
412 
413 static void
414 nfssvc_loss(SVCXPRT *xprt)
415 {
416 	uint32_t ack;
417 
418 	ack = 0;
419 	SVC_ACK(xprt, &ack);
420 	nfsrc_trimcache(xprt->xp_sockref, ack, 1);
421 }
422 
423 /*
424  * Adds a socket to the list for servicing by nfsds.
425  */
426 int
427 nfsrvd_addsock(struct file *fp)
428 {
429 	int siz;
430 	struct socket *so;
431 	int error = 0;
432 	SVCXPRT *xprt;
433 	static u_int64_t sockref = 0;
434 
435 	so = fp->f_data;
436 
437 	siz = sb_max_adj;
438 	error = soreserve(so, siz, siz);
439 	if (error)
440 		goto out;
441 
442 	/*
443 	 * Steal the socket from userland so that it doesn't close
444 	 * unexpectedly.
445 	 */
446 	if (so->so_type == SOCK_DGRAM)
447 		xprt = svc_dg_create(nfsrvd_pool, so, 0, 0);
448 	else
449 		xprt = svc_vc_create(nfsrvd_pool, so, 0, 0);
450 	if (xprt) {
451 		fp->f_ops = &badfileops;
452 		fp->f_data = NULL;
453 		xprt->xp_sockref = ++sockref;
454 		if (nfs_minvers == NFS_VER2)
455 			svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program,
456 			    NULL);
457 		if (nfs_minvers <= NFS_VER3 && nfs_maxvers >= NFS_VER3)
458 			svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program,
459 			    NULL);
460 		if (nfs_maxvers >= NFS_VER4)
461 			svc_reg(xprt, NFS_PROG, NFS_VER4, nfssvc_program,
462 			    NULL);
463 		if (so->so_type == SOCK_STREAM)
464 			svc_loss_reg(xprt, nfssvc_loss);
465 		SVC_RELEASE(xprt);
466 	}
467 
468 out:
469 	NFSEXITCODE(error);
470 	return (error);
471 }
472 
473 /*
474  * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
475  * until it is killed by a signal.
476  */
477 int
478 nfsrvd_nfsd(struct thread *td, struct nfsd_nfsd_args *args)
479 {
480 	char principal[MAXHOSTNAMELEN + 5];
481 	struct proc *p;
482 	int error = 0;
483 	bool_t ret2, ret3, ret4;
484 
485 	error = copyinstr(args->principal, principal, sizeof (principal),
486 	    NULL);
487 	if (error)
488 		goto out;
489 
490 	/*
491 	 * Only the first nfsd actually does any work. The RPC code
492 	 * adds threads to it as needed. Any extra processes offered
493 	 * by nfsd just exit. If nfsd is new enough, it will call us
494 	 * once with a structure that specifies how many threads to
495 	 * use.
496 	 */
497 	NFSD_LOCK();
498 	if (newnfs_numnfsd == 0) {
499 		nfsdev_time = time_second;
500 		p = td->td_proc;
501 		PROC_LOCK(p);
502 		p->p_flag2 |= P2_AST_SU;
503 		PROC_UNLOCK(p);
504 		newnfs_numnfsd++;
505 
506 		NFSD_UNLOCK();
507 		error = nfsrv_createdevids(args, td);
508 		if (error == 0) {
509 			/* An empty string implies AUTH_SYS only. */
510 			if (principal[0] != '\0') {
511 				ret2 = rpc_gss_set_svc_name_call(principal,
512 				    "kerberosv5", GSS_C_INDEFINITE, NFS_PROG,
513 				    NFS_VER2);
514 				ret3 = rpc_gss_set_svc_name_call(principal,
515 				    "kerberosv5", GSS_C_INDEFINITE, NFS_PROG,
516 				    NFS_VER3);
517 				ret4 = rpc_gss_set_svc_name_call(principal,
518 				    "kerberosv5", GSS_C_INDEFINITE, NFS_PROG,
519 				    NFS_VER4);
520 
521 				if (!ret2 || !ret3 || !ret4)
522 					printf(
523 					    "nfsd: can't register svc name\n");
524 			}
525 
526 			nfsrvd_pool->sp_minthreads = args->minthreads;
527 			nfsrvd_pool->sp_maxthreads = args->maxthreads;
528 
529 			svc_run(nfsrvd_pool);
530 
531 			if (principal[0] != '\0') {
532 				rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2);
533 				rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3);
534 				rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER4);
535 			}
536 		}
537 		NFSD_LOCK();
538 		newnfs_numnfsd--;
539 		nfsrvd_init(1);
540 		PROC_LOCK(p);
541 		p->p_flag2 &= ~P2_AST_SU;
542 		PROC_UNLOCK(p);
543 	}
544 	NFSD_UNLOCK();
545 
546 out:
547 	NFSEXITCODE(error);
548 	return (error);
549 }
550 
551 /*
552  * Initialize the data structures for the server.
553  * Handshake with any new nfsds starting up to avoid any chance of
554  * corruption.
555  */
556 void
557 nfsrvd_init(int terminating)
558 {
559 
560 	NFSD_LOCK_ASSERT();
561 
562 	if (terminating) {
563 		nfsd_master_proc = NULL;
564 		NFSD_UNLOCK();
565 		nfsrv_freealllayoutsanddevids();
566 		nfsrv_freeallbackchannel_xprts();
567 		svcpool_close(nfsrvd_pool);
568 		NFSD_LOCK();
569 	} else {
570 		NFSD_UNLOCK();
571 		nfsrvd_pool = svcpool_create("nfsd",
572 		    SYSCTL_STATIC_CHILDREN(_vfs_nfsd));
573 		nfsrvd_pool->sp_rcache = NULL;
574 		nfsrvd_pool->sp_assign = fhanew_assign;
575 		nfsrvd_pool->sp_done = fha_nd_complete;
576 		NFSD_LOCK();
577 	}
578 }
579 
580