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