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 <fs/nfsserver/nfs_fha_new.h> 48 49 #include <security/mac/mac_framework.h> 50 51 NFSDLOCKMUTEX; 52 NFSV4ROOTLOCKMUTEX; 53 struct nfsv4lock nfsd_suspend_lock; 54 char *nfsrv_zeropnfsdat = NULL; 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, 3 and 4"); 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 extern int nfsrv_writerpc[NFS_NPROCS]; 110 extern volatile int nfsrv_devidcnt; 111 extern struct nfsv4_opflag nfsv4_opflag[NFSV42_NOPS]; 112 113 /* 114 * NFS server system calls 115 */ 116 117 static void 118 nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt) 119 { 120 struct nfsrv_descript nd; 121 struct nfsrvcache *rp = NULL; 122 int cacherep, credflavor; 123 124 memset(&nd, 0, sizeof(nd)); 125 if (rqst->rq_vers == NFS_VER2) { 126 if (rqst->rq_proc > NFSV2PROC_STATFS || 127 newnfs_nfsv3_procid[rqst->rq_proc] == NFSPROC_NOOP) { 128 svcerr_noproc(rqst); 129 svc_freereq(rqst); 130 goto out; 131 } 132 nd.nd_procnum = newnfs_nfsv3_procid[rqst->rq_proc]; 133 nd.nd_flag = ND_NFSV2; 134 } else if (rqst->rq_vers == NFS_VER3) { 135 if (rqst->rq_proc >= NFS_V3NPROCS) { 136 svcerr_noproc(rqst); 137 svc_freereq(rqst); 138 goto out; 139 } 140 nd.nd_procnum = rqst->rq_proc; 141 nd.nd_flag = ND_NFSV3; 142 } else { 143 if (rqst->rq_proc != NFSPROC_NULL && 144 rqst->rq_proc != NFSV4PROC_COMPOUND) { 145 svcerr_noproc(rqst); 146 svc_freereq(rqst); 147 goto out; 148 } 149 nd.nd_procnum = rqst->rq_proc; 150 nd.nd_flag = ND_NFSV4; 151 } 152 153 /* 154 * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 - 155 * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP 156 * mounts. 157 */ 158 nd.nd_mrep = rqst->rq_args; 159 rqst->rq_args = NULL; 160 newnfs_realign(&nd.nd_mrep, M_WAITOK); 161 nd.nd_md = nd.nd_mrep; 162 nd.nd_dpos = mtod(nd.nd_md, caddr_t); 163 nd.nd_nam = svc_getrpccaller(rqst); 164 nd.nd_nam2 = rqst->rq_addr; 165 nd.nd_mreq = NULL; 166 nd.nd_cred = NULL; 167 168 if (nfs_privport != 0) { 169 /* Check if source port is privileged */ 170 u_short port; 171 struct sockaddr *nam = nd.nd_nam; 172 struct sockaddr_in *sin; 173 174 sin = (struct sockaddr_in *)nam; 175 /* 176 * INET/INET6 - same code: 177 * sin_port and sin6_port are at same offset 178 */ 179 port = ntohs(sin->sin_port); 180 if (port >= IPPORT_RESERVED && 181 nd.nd_procnum != NFSPROC_NULL) { 182 #ifdef INET6 183 char buf[INET6_ADDRSTRLEN]; 184 #else 185 char buf[INET_ADDRSTRLEN]; 186 #endif 187 #ifdef INET6 188 #if defined(KLD_MODULE) 189 /* Do not use ip6_sprintf: the nfs module should work without INET6. */ 190 #define ip6_sprintf(buf, a) \ 191 (sprintf((buf), "%x:%x:%x:%x:%x:%x:%x:%x", \ 192 (a)->s6_addr16[0], (a)->s6_addr16[1], \ 193 (a)->s6_addr16[2], (a)->s6_addr16[3], \ 194 (a)->s6_addr16[4], (a)->s6_addr16[5], \ 195 (a)->s6_addr16[6], (a)->s6_addr16[7]), \ 196 (buf)) 197 #endif 198 #endif 199 printf("NFS request from unprivileged port (%s:%d)\n", 200 #ifdef INET6 201 sin->sin_family == AF_INET6 ? 202 ip6_sprintf(buf, &satosin6(sin)->sin6_addr) : 203 #if defined(KLD_MODULE) 204 #undef ip6_sprintf 205 #endif 206 #endif 207 inet_ntoa_r(sin->sin_addr, buf), port); 208 svcerr_weakauth(rqst); 209 svc_freereq(rqst); 210 m_freem(nd.nd_mrep); 211 goto out; 212 } 213 } 214 215 if (nd.nd_procnum != NFSPROC_NULL) { 216 if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) { 217 svcerr_weakauth(rqst); 218 svc_freereq(rqst); 219 m_freem(nd.nd_mrep); 220 goto out; 221 } 222 223 /* Set the flag based on credflavor */ 224 if (credflavor == RPCSEC_GSS_KRB5) { 225 nd.nd_flag |= ND_GSS; 226 } else if (credflavor == RPCSEC_GSS_KRB5I) { 227 nd.nd_flag |= (ND_GSS | ND_GSSINTEGRITY); 228 } else if (credflavor == RPCSEC_GSS_KRB5P) { 229 nd.nd_flag |= (ND_GSS | ND_GSSPRIVACY); 230 } else if (credflavor != AUTH_SYS) { 231 svcerr_weakauth(rqst); 232 svc_freereq(rqst); 233 m_freem(nd.nd_mrep); 234 goto out; 235 } 236 237 #ifdef MAC 238 mac_cred_associate_nfsd(nd.nd_cred); 239 #endif 240 /* 241 * Get a refcnt (shared lock) on nfsd_suspend_lock. 242 * NFSSVC_SUSPENDNFSD will take an exclusive lock on 243 * nfsd_suspend_lock to suspend these threads. 244 * The call to nfsv4_lock() that precedes nfsv4_getref() 245 * ensures that the acquisition of the exclusive lock 246 * takes priority over acquisition of the shared lock by 247 * waiting for any exclusive lock request to complete. 248 * This must be done here, before the check of 249 * nfsv4root exports by nfsvno_v4rootexport(). 250 */ 251 NFSLOCKV4ROOTMUTEX(); 252 nfsv4_lock(&nfsd_suspend_lock, 0, NULL, NFSV4ROOTLOCKMUTEXPTR, 253 NULL); 254 nfsv4_getref(&nfsd_suspend_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, 255 NULL); 256 NFSUNLOCKV4ROOTMUTEX(); 257 258 if ((nd.nd_flag & ND_NFSV4) != 0) { 259 nd.nd_repstat = nfsvno_v4rootexport(&nd); 260 if (nd.nd_repstat != 0) { 261 NFSLOCKV4ROOTMUTEX(); 262 nfsv4_relref(&nfsd_suspend_lock); 263 NFSUNLOCKV4ROOTMUTEX(); 264 svcerr_weakauth(rqst); 265 svc_freereq(rqst); 266 m_freem(nd.nd_mrep); 267 goto out; 268 } 269 } 270 271 cacherep = nfs_proc(&nd, rqst->rq_xid, xprt, &rp); 272 NFSLOCKV4ROOTMUTEX(); 273 nfsv4_relref(&nfsd_suspend_lock); 274 NFSUNLOCKV4ROOTMUTEX(); 275 } else { 276 NFSMGET(nd.nd_mreq); 277 nd.nd_mreq->m_len = 0; 278 cacherep = RC_REPLY; 279 } 280 if (nd.nd_mrep != NULL) 281 m_freem(nd.nd_mrep); 282 283 if (nd.nd_cred != NULL) 284 crfree(nd.nd_cred); 285 286 if (cacherep == RC_DROPIT) { 287 if (nd.nd_mreq != NULL) 288 m_freem(nd.nd_mreq); 289 svc_freereq(rqst); 290 goto out; 291 } 292 293 if (nd.nd_mreq == NULL) { 294 svcerr_decode(rqst); 295 svc_freereq(rqst); 296 goto out; 297 } 298 299 if (nd.nd_repstat & NFSERR_AUTHERR) { 300 svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR); 301 if (nd.nd_mreq != NULL) 302 m_freem(nd.nd_mreq); 303 } else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq)) { 304 svcerr_systemerr(rqst); 305 } 306 if (rp != NULL) { 307 nfsrvd_sentcache(rp, (rqst->rq_reply_seq != 0 || 308 SVC_ACK(xprt, NULL)), rqst->rq_reply_seq); 309 } 310 svc_freereq(rqst); 311 312 out: 313 td_softdep_cleanup(curthread); 314 NFSEXITCODE(0); 315 } 316 317 /* 318 * Check the cache and, optionally, do the RPC. 319 * Return the appropriate cache response. 320 */ 321 static int 322 nfs_proc(struct nfsrv_descript *nd, u_int32_t xid, SVCXPRT *xprt, 323 struct nfsrvcache **rpp) 324 { 325 int cacherep = RC_DOIT, isdgram, taglen = -1; 326 struct mbuf *m; 327 u_char tag[NFSV4_SMALLSTR + 1], *tagstr = NULL; 328 u_int32_t minorvers = 0; 329 uint32_t ack; 330 331 *rpp = NULL; 332 if (nd->nd_nam2 == NULL) { 333 nd->nd_flag |= ND_STREAMSOCK; 334 isdgram = 0; 335 } else { 336 isdgram = 1; 337 } 338 339 /* 340 * Two cases: 341 * 1 - For NFSv2 over UDP, if we are near our malloc/mget 342 * limit, just drop the request. There is no 343 * NFSERR_RESOURCE or NFSERR_DELAY for NFSv2 and the 344 * client will timeout/retry over UDP in a little while. 345 * 2 - nd_repstat == 0 && nd_mreq == NULL, which 346 * means a normal nfs rpc, so check the cache 347 */ 348 if ((nd->nd_flag & ND_NFSV2) && nd->nd_nam2 != NULL && 349 nfsrv_mallocmget_limit()) { 350 cacherep = RC_DROPIT; 351 } else { 352 /* 353 * For NFSv3, play it safe and assume that the client is 354 * doing retries on the same TCP connection. 355 */ 356 if ((nd->nd_flag & (ND_NFSV4 | ND_STREAMSOCK)) == 357 ND_STREAMSOCK) 358 nd->nd_flag |= ND_SAMETCPCONN; 359 nd->nd_retxid = xid; 360 nd->nd_tcpconntime = NFSD_MONOSEC; 361 nd->nd_sockref = xprt->xp_sockref; 362 if ((nd->nd_flag & ND_NFSV4) != 0) 363 nfsd_getminorvers(nd, tag, &tagstr, &taglen, 364 &minorvers); 365 if ((nd->nd_flag & ND_NFSV41) != 0) 366 /* NFSv4.1 caches replies in the session slots. */ 367 cacherep = RC_DOIT; 368 else { 369 cacherep = nfsrvd_getcache(nd); 370 ack = 0; 371 SVC_ACK(xprt, &ack); 372 nfsrc_trimcache(xprt->xp_sockref, ack, 0); 373 } 374 } 375 376 /* 377 * Handle the request. There are three cases. 378 * RC_DOIT - do the RPC 379 * RC_REPLY - return the reply already created 380 * RC_DROPIT - just throw the request away 381 */ 382 if (cacherep == RC_DOIT) { 383 if ((nd->nd_flag & ND_NFSV41) != 0) 384 nd->nd_xprt = xprt; 385 nfsrvd_dorpc(nd, isdgram, tagstr, taglen, minorvers); 386 if ((nd->nd_flag & ND_NFSV41) != 0) { 387 if (nd->nd_repstat != NFSERR_REPLYFROMCACHE && 388 (nd->nd_flag & ND_SAVEREPLY) != 0) { 389 /* Cache a copy of the reply. */ 390 m = m_copym(nd->nd_mreq, 0, M_COPYALL, 391 M_WAITOK); 392 } else 393 m = NULL; 394 if ((nd->nd_flag & ND_HASSEQUENCE) != 0) 395 nfsrv_cache_session(nd, &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 /* 531 * If this is a pNFS service, make Getattr do a 532 * vn_start_write(), so it can do a vn_set_extattr(). 533 */ 534 if (nfsrv_devidcnt > 0) { 535 nfsrv_writerpc[NFSPROC_GETATTR] = 1; 536 nfsv4_opflag[NFSV4OP_GETATTR].modifyfs = 1; 537 } 538 539 svc_run(nfsrvd_pool); 540 541 /* Reset Getattr to not do a vn_start_write(). */ 542 nfsrv_writerpc[NFSPROC_GETATTR] = 0; 543 nfsv4_opflag[NFSV4OP_GETATTR].modifyfs = 0; 544 545 if (principal[0] != '\0') { 546 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2); 547 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3); 548 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER4); 549 } 550 } 551 NFSD_LOCK(); 552 newnfs_numnfsd--; 553 nfsrvd_init(1); 554 PROC_LOCK(p); 555 p->p_flag2 &= ~P2_AST_SU; 556 PROC_UNLOCK(p); 557 } 558 NFSD_UNLOCK(); 559 560 out: 561 NFSEXITCODE(error); 562 return (error); 563 } 564 565 /* 566 * Initialize the data structures for the server. 567 * Handshake with any new nfsds starting up to avoid any chance of 568 * corruption. 569 */ 570 void 571 nfsrvd_init(int terminating) 572 { 573 574 NFSD_LOCK_ASSERT(); 575 576 if (terminating) { 577 nfsd_master_proc = NULL; 578 NFSD_UNLOCK(); 579 nfsrv_freealllayoutsanddevids(); 580 nfsrv_freeallbackchannel_xprts(); 581 svcpool_close(nfsrvd_pool); 582 free(nfsrv_zeropnfsdat, M_TEMP); 583 nfsrv_zeropnfsdat = NULL; 584 NFSD_LOCK(); 585 } else { 586 NFSD_UNLOCK(); 587 nfsrvd_pool = svcpool_create("nfsd", 588 SYSCTL_STATIC_CHILDREN(_vfs_nfsd)); 589 nfsrvd_pool->sp_rcache = NULL; 590 nfsrvd_pool->sp_assign = fhanew_assign; 591 nfsrvd_pool->sp_done = fhanew_nd_complete; 592 NFSD_LOCK(); 593 } 594 } 595 596