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