]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/nfsserver/nfs_srvkrpc.c
Make sequence numbers checks more strict.
[FreeBSD/FreeBSD.git] / sys / nfsserver / nfs_srvkrpc.c
1 /*-
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *      @(#)nfs_syscalls.c      8.5 (Berkeley) 3/30/95
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include "opt_inet6.h"
39 #include "opt_kgssapi.h"
40
41 #include <sys/param.h>
42 #include <sys/capsicum.h>
43 #include <sys/systm.h>
44 #include <sys/sysproto.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/file.h>
48 #include <sys/filedesc.h>
49 #include <sys/jail.h>
50 #include <sys/vnode.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/priv.h>
54 #include <sys/proc.h>
55 #include <sys/bio.h>
56 #include <sys/buf.h>
57 #include <sys/mbuf.h>
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
60 #include <sys/domain.h>
61 #include <sys/protosw.h>
62 #include <sys/namei.h>
63 #include <sys/fcntl.h>
64 #include <sys/lockf.h>
65 #include <sys/eventhandler.h>
66
67 #include <netinet/in.h>
68 #include <netinet/tcp.h>
69 #ifdef INET6
70 #include <net/if.h>
71 #include <net/if_var.h>                 /* XXX: for in6_var.h */
72 #include <netinet6/in6_var.h>           /* XXX: for ip6_sprintf */
73 #endif
74
75 #include <rpc/rpc.h>
76 #include <rpc/rpcsec_gss.h>
77 #include <rpc/replay.h>
78
79 #include <nfs/xdr_subs.h>
80 #include <nfs/nfsproto.h>
81 #include <nfs/nfs_fha.h>
82 #include <nfsserver/nfs.h>
83 #include <nfsserver/nfsm_subs.h>
84 #include <nfsserver/nfsrvcache.h>
85 #include <nfsserver/nfs_fha_old.h>
86
87 #include <security/mac/mac_framework.h>
88
89 static MALLOC_DEFINE(M_NFSSVC, "nfss_srvsock", "Nfs server structure");
90
91 MALLOC_DEFINE(M_NFSRVDESC, "nfss_srvdesc", "NFS server socket descriptor");
92 MALLOC_DEFINE(M_NFSD, "nfss_daemon", "Nfs server daemon structure");
93
94 #define TRUE    1
95 #define FALSE   0
96
97 SYSCTL_DECL(_vfs_nfsrv);
98
99 SVCPOOL         *nfsrv_pool;
100 int             nfsd_waiting = 0;
101 int             nfsrv_numnfsd = 0;
102 struct callout  nfsrv_callout;
103 static eventhandler_tag nfsrv_nmbclusters_tag;
104
105 static int      nfs_privport = 0;
106 SYSCTL_INT(_vfs_nfsrv, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW,
107     &nfs_privport, 0,
108     "Only allow clients using a privileged port");
109 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay, CTLFLAG_RW,
110     &nfsrvw_procrastinate, 0,
111     "Delay value for write gathering");
112 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay_v3, CTLFLAG_RW,
113     &nfsrvw_procrastinate_v3, 0,
114     "Delay in seconds for NFSv3 write gathering");
115
116 static int      nfssvc_addsock(struct file *, struct thread *);
117 static int      nfssvc_nfsd(struct thread *, struct nfsd_nfsd_args *);
118
119 extern u_long sb_max_adj;
120
121 int32_t (*nfsrv3_procs[NFS_NPROCS])(struct nfsrv_descript *nd,
122     struct nfssvc_sock *slp, struct mbuf **mreqp) = {
123         nfsrv_null,
124         nfsrv_getattr,
125         nfsrv_setattr,
126         nfsrv_lookup,
127         nfsrv3_access,
128         nfsrv_readlink,
129         nfsrv_read,
130         nfsrv_write,
131         nfsrv_create,
132         nfsrv_mkdir,
133         nfsrv_symlink,
134         nfsrv_mknod,
135         nfsrv_remove,
136         nfsrv_rmdir,
137         nfsrv_rename,
138         nfsrv_link,
139         nfsrv_readdir,
140         nfsrv_readdirplus,
141         nfsrv_statfs,
142         nfsrv_fsinfo,
143         nfsrv_pathconf,
144         nfsrv_commit,
145         nfsrv_noop
146 };
147
148 /*
149  * NFS server system calls
150  */
151 /*
152  * This is now called from nfssvc() in nfs/nfs_nfssvc.c.
153  */
154
155 /*
156  * Nfs server psuedo system call for the nfsd's
157  * Based on the flag value it either:
158  * - adds a socket to the selection list
159  * - remains in the kernel as an nfsd
160  * - remains in the kernel as an nfsiod
161  * For INET6 we suppose that nfsd provides only IN6P_IPV6_V6ONLY sockets
162  * and that mountd provides
163  *  - sockaddr with no IPv4-mapped addresses
164  *  - mask for both INET and INET6 families if there is IPv4-mapped overlap
165  */
166 int
167 nfssvc_nfsserver(struct thread *td, struct nfssvc_args *uap)
168 {
169         struct file *fp;
170         struct nfsd_addsock_args addsockarg;
171         struct nfsd_nfsd_args nfsdarg;
172         cap_rights_t rights;
173         int error;
174
175         if (uap->flag & NFSSVC_ADDSOCK) {
176                 error = copyin(uap->argp, (caddr_t)&addsockarg,
177                     sizeof(addsockarg));
178                 if (error)
179                         return (error);
180                 error = fget(td, addsockarg.sock,
181                     cap_rights_init(&rights, CAP_SOCK_SERVER), &fp);
182                 if (error)
183                         return (error);
184                 if (fp->f_type != DTYPE_SOCKET) {
185                         fdrop(fp, td);
186                         return (error); /* XXXRW: Should be EINVAL? */
187                 }
188                 error = nfssvc_addsock(fp, td);
189                 fdrop(fp, td);
190         } else if (uap->flag & NFSSVC_OLDNFSD)
191                 error = nfssvc_nfsd(td, NULL);
192         else if (uap->flag & NFSSVC_NFSD) {
193                 if (!uap->argp)
194                         return (EINVAL);
195                 error = copyin(uap->argp, (caddr_t)&nfsdarg,
196                     sizeof(nfsdarg));
197                 if (error)
198                         return (error);
199                 error = nfssvc_nfsd(td, &nfsdarg);
200         } else
201                 error = ENXIO;
202         return (error);
203 }
204
205 /*
206  * Generate the rpc reply header
207  * siz arg. is used to decide if adding a cluster is worthwhile
208  */
209 struct mbuf *
210 nfs_rephead(int siz, struct nfsrv_descript *nd, int err,
211     struct mbuf **mbp, caddr_t *bposp)
212 {
213         u_int32_t *tl;
214         struct mbuf *mreq;
215         caddr_t bpos;
216         struct mbuf *mb;
217
218         if (err == EBADRPC)
219                 return (NULL);
220
221         nd->nd_repstat = err;
222         if (err && (nd->nd_flag & ND_NFSV3) == 0)       /* XXX recheck */
223                 siz = 0;
224
225         MGET(mreq, M_WAITOK, MT_DATA);
226
227         /*
228          * If this is a big reply, use a cluster
229          */
230         mreq->m_len = 0;
231         if (siz >= MINCLSIZE) {
232                 MCLGET(mreq, M_WAITOK);
233         }
234         mb = mreq;
235         bpos = mtod(mb, caddr_t);
236
237         if (err != NFSERR_RETVOID) {
238                 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
239                 if (err)
240                         *tl = txdr_unsigned(nfsrv_errmap(nd, err));
241                 else
242                         *tl = 0;
243         }
244
245         *mbp = mb;
246         *bposp = bpos;
247         if (err != 0 && err != NFSERR_RETVOID)
248                 nfsrvstats.srvrpc_errs++;
249
250         return (mreq);
251 }
252
253 static void
254 nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt)
255 {
256         rpcproc_t procnum;
257         int32_t (*proc)(struct nfsrv_descript *nd, struct nfssvc_sock *slp,
258             struct mbuf **mreqp);
259         int flag;
260         struct nfsrv_descript nd;
261         struct mbuf *mreq, *mrep;
262         int error;
263
264         if (rqst->rq_vers == NFS_VER2) {
265                 if (rqst->rq_proc > NFSV2PROC_STATFS) {
266                         svcerr_noproc(rqst);
267                         svc_freereq(rqst);
268                         return;
269                 }
270                 procnum = nfsrv_nfsv3_procid[rqst->rq_proc];
271                 flag = 0;
272         } else {
273                 if (rqst->rq_proc >= NFS_NPROCS) {
274                         svcerr_noproc(rqst);
275                         svc_freereq(rqst);
276                         return;
277                 }
278                 procnum = rqst->rq_proc;
279                 flag = ND_NFSV3;
280         }
281         proc = nfsrv3_procs[procnum];
282
283         mreq = mrep = NULL;
284         mreq = rqst->rq_args;
285         rqst->rq_args = NULL;
286         (void)nfs_realign(&mreq, M_WAITOK);
287
288         /*
289          * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 -
290          * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP
291          * mounts.
292          */
293         memset(&nd, 0, sizeof(nd));
294         nd.nd_md = nd.nd_mrep = mreq;
295         nd.nd_dpos = mtod(mreq, caddr_t);
296         nd.nd_nam = svc_getrpccaller(rqst);
297         nd.nd_nam2 = rqst->rq_addr;
298         nd.nd_procnum = procnum;
299         nd.nd_cr = NULL;
300         nd.nd_flag = flag;
301
302         if (nfs_privport) {
303                 /* Check if source port is privileged */
304                 u_short port;
305                 struct sockaddr *nam = nd.nd_nam;
306                 struct sockaddr_in *sin;
307
308                 sin = (struct sockaddr_in *)nam;
309                 /*
310                  * INET/INET6 - same code:
311                  *    sin_port and sin6_port are at same offset
312                  */
313                 port = ntohs(sin->sin_port);
314                 if (port >= IPPORT_RESERVED &&
315                     nd.nd_procnum != NFSPROC_NULL) {
316 #ifdef INET6
317                         char b6[INET6_ADDRSTRLEN];
318 #if defined(KLD_MODULE)
319                         /* Do not use ip6_sprintf: the nfs module should work without INET6. */
320 #define ip6_sprintf(buf, a)                                             \
321                         (sprintf((buf), "%x:%x:%x:%x:%x:%x:%x:%x",      \
322                             (a)->s6_addr16[0], (a)->s6_addr16[1],       \
323                             (a)->s6_addr16[2], (a)->s6_addr16[3],       \
324                             (a)->s6_addr16[4], (a)->s6_addr16[5],       \
325                             (a)->s6_addr16[6], (a)->s6_addr16[7]),      \
326                             (buf))
327 #endif
328 #endif
329                         printf("NFS request from unprivileged port (%s:%d)\n",
330 #ifdef INET6
331                             sin->sin_family == AF_INET6 ?
332                             ip6_sprintf(b6, &satosin6(sin)->sin6_addr) :
333 #if defined(KLD_MODULE)
334 #undef ip6_sprintf
335 #endif
336 #endif
337                             inet_ntoa(sin->sin_addr), port);
338                         m_freem(mreq);
339                         svcerr_weakauth(rqst);
340                         svc_freereq(rqst);
341                         return;
342                 }
343         }
344
345         if (proc != nfsrv_null) {
346                 if (!svc_getcred(rqst, &nd.nd_cr, &nd.nd_credflavor)) {
347                         m_freem(mreq);
348                         svcerr_weakauth(rqst);
349                         svc_freereq(rqst);
350                         return;
351                 }
352 #ifdef MAC
353                 mac_cred_associate_nfsd(nd.nd_cr);
354 #endif
355         }
356         nfsrvstats.srvrpccnt[nd.nd_procnum]++;
357
358         error = proc(&nd, NULL, &mrep);
359
360         if (nd.nd_cr)
361                 crfree(nd.nd_cr);
362
363         if (mrep == NULL) {
364                 svcerr_decode(rqst);
365                 svc_freereq(rqst);
366                 return;
367         }
368         if (error && error != NFSERR_RETVOID) {
369                 svcerr_systemerr(rqst);
370                 svc_freereq(rqst);
371                 return;
372         }
373         if (nd.nd_repstat & NFSERR_AUTHERR) {
374                 svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
375                 m_freem(mrep);
376         } else {
377                 if (!svc_sendreply_mbuf(rqst, mrep))
378                         svcerr_systemerr(rqst);
379         }
380         svc_freereq(rqst);
381 }
382
383 /*
384  * Adds a socket to the list for servicing by nfsds.
385  */
386 static int
387 nfssvc_addsock(struct file *fp, struct thread *td)
388 {
389         int siz;
390         struct socket *so;
391         int error;
392         SVCXPRT *xprt;
393
394         so = fp->f_data;
395
396         siz = sb_max_adj;
397         error = soreserve(so, siz, siz);
398         if (error)
399                 return (error);
400
401         /*
402          * Steal the socket from userland so that it doesn't close
403          * unexpectedly.
404          */
405         if (so->so_type == SOCK_DGRAM)
406                 xprt = svc_dg_create(nfsrv_pool, so, 0, 0);
407         else
408                 xprt = svc_vc_create(nfsrv_pool, so, 0, 0);
409         if (xprt) {
410                 fp->f_ops = &badfileops;
411                 fp->f_data = NULL;
412                 svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program, NULL);
413                 svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program, NULL);
414                 SVC_RELEASE(xprt);
415         }
416
417         return (0);
418 }
419
420 /*
421  * Called by nfssvc() for nfsds.  Just loops around servicing rpc requests
422  * until it is killed by a signal.
423  */
424 static int
425 nfssvc_nfsd(struct thread *td, struct nfsd_nfsd_args *args)
426 {
427         char principal[128];
428         int error;
429
430         if (args) {
431                 error = copyinstr(args->principal, principal,
432                     sizeof(principal), NULL);
433                 if (error)
434                         return (error);
435         } else {
436                 memcpy(principal, "nfs@", 4);
437                 getcredhostname(td->td_ucred, principal + 4,
438                     sizeof(principal) - 4);
439         }
440
441         /*
442          * Only the first nfsd actually does any work.  The RPC code
443          * adds threads to it as needed.  Any extra processes offered
444          * by nfsd just exit.  If nfsd is new enough, it will call us
445          * once with a structure that specifies how many threads to
446          * use.
447          */
448         NFSD_LOCK();
449         if (nfsrv_numnfsd == 0) {
450                 nfsrv_numnfsd++;
451
452                 NFSD_UNLOCK();
453
454                 rpc_gss_set_svc_name_call(principal, "kerberosv5",
455                     GSS_C_INDEFINITE, NFS_PROG, NFS_VER2);
456                 rpc_gss_set_svc_name_call(principal, "kerberosv5",
457                     GSS_C_INDEFINITE, NFS_PROG, NFS_VER3);
458
459                 if (args) {
460                         nfsrv_pool->sp_minthreads = args->minthreads;
461                         nfsrv_pool->sp_maxthreads = args->maxthreads;
462                 } else {
463                         nfsrv_pool->sp_minthreads = 4;
464                         nfsrv_pool->sp_maxthreads = 4;
465                 }
466
467                 svc_run(nfsrv_pool);
468
469                 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2);
470                 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3);
471
472                 NFSD_LOCK();
473                 nfsrv_numnfsd--;
474                 nfsrv_init(TRUE);
475         }
476         NFSD_UNLOCK();
477
478         return (0);
479 }
480
481 /*
482  * Size the NFS server's duplicate request cache at 1/2 the
483  * nmbclusters, floating within a (64, 2048) range.  This is to
484  * prevent all mbuf clusters being tied up in the NFS dupreq
485  * cache for small values of nmbclusters.
486  */
487 static size_t
488 nfsrv_replay_size(void)
489 {
490         size_t replaysiz;
491
492         replaysiz = nmbclusters / 2;
493         if (replaysiz > NFSRVCACHE_MAX_SIZE)
494                 replaysiz = NFSRVCACHE_MAX_SIZE;
495         if (replaysiz < NFSRVCACHE_MIN_SIZE)
496                 replaysiz = NFSRVCACHE_MIN_SIZE;
497         replaysiz *= MCLBYTES;
498
499         return (replaysiz);
500 }
501
502 /*
503  * Called when nmbclusters changes - we resize the replay cache
504  * accordingly.
505  */
506 static void
507 nfsrv_nmbclusters_change(void *tag)
508 {
509
510         if (nfsrv_pool)
511                 replay_setsize(nfsrv_pool->sp_rcache, nfsrv_replay_size());
512 }
513
514 /*
515  * Initialize the data structures for the server.
516  * Handshake with any new nfsds starting up to avoid any chance of
517  * corruption.
518  */
519 void
520 nfsrv_init(int terminating)
521 {
522
523         NFSD_LOCK_ASSERT();
524
525         if (terminating) {
526                 NFSD_UNLOCK();
527                 EVENTHANDLER_DEREGISTER(nmbclusters_change,
528                     nfsrv_nmbclusters_tag);
529                 svcpool_destroy(nfsrv_pool);
530                 nfsrv_pool = NULL;
531                 NFSD_LOCK();
532         } else
533                 nfs_pub.np_valid = 0;
534
535         NFSD_UNLOCK();
536
537         nfsrv_pool = svcpool_create("nfsd", SYSCTL_STATIC_CHILDREN(_vfs_nfsrv));
538         nfsrv_pool->sp_rcache = replay_newcache(nfsrv_replay_size());
539         nfsrv_pool->sp_assign = fhaold_assign;
540         nfsrv_pool->sp_done = fha_nd_complete;
541         nfsrv_nmbclusters_tag = EVENTHANDLER_REGISTER(nmbclusters_change,
542             nfsrv_nmbclusters_change, NULL, EVENTHANDLER_PRI_FIRST);
543
544         NFSD_LOCK();
545 }