OSDN Git Service

svc.c: svc_getreqset() buffer overflow
[uclinux-h8/uclibc-ng.git] / libc / inet / rpc / svc.c
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  */
29 /*
30  * svc.c, Server-side remote procedure call interface.
31  *
32  * There are two sets of procedures here.  The xprt routines are
33  * for handling transport handles.  The svc routines handle the
34  * list of service routines.
35  *
36  * Copyright (C) 1984, Sun Microsystems, Inc.
37  */
38
39 #include <errno.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include "rpc_private.h"
43 #include <rpc/svc.h>
44 #include <rpc/pmap_clnt.h>
45 #include <sys/poll.h>
46
47 /* used by svc_[max_]pollfd */
48 /* used by svc_fdset */
49
50 #ifdef __UCLIBC_HAS_THREADS__
51 #define xports (*(SVCXPRT ***)&RPC_THREAD_VARIABLE(svc_xports_s))
52 #else
53 static SVCXPRT **xports;
54 #endif
55
56 #define NULL_SVC ((struct svc_callout *)0)
57 #define RQCRED_SIZE     400     /* this size is excessive */
58
59 /* The services list
60    Each entry represents a set of procedures (an rpc program).
61    The dispatch routine takes request structs and runs the
62    appropriate procedure. */
63 struct svc_callout {
64   struct svc_callout *sc_next;
65   rpcprog_t sc_prog;
66   rpcvers_t sc_vers;
67   void (*sc_dispatch) (struct svc_req *, SVCXPRT *);
68 };
69 #ifdef __UCLIBC_HAS_THREADS__
70 #define svc_head (*(struct svc_callout **)&RPC_THREAD_VARIABLE(svc_head_s))
71 #else
72 static struct svc_callout *svc_head;
73 #endif
74
75 /* ***************  SVCXPRT related stuff **************** */
76
77 /* Activate a transport handle. */
78 void
79 xprt_register (SVCXPRT *xprt)
80 {
81   register int sock = xprt->xp_sock;
82   register int i;
83
84   if (xports == NULL)
85     {
86       xports = (SVCXPRT **) malloc (_rpc_dtablesize () * sizeof (SVCXPRT *));
87       if (xports == NULL) /* DonĀ“t add handle */
88         return;
89     }
90
91   if (sock < _rpc_dtablesize ())
92     {
93       xports[sock] = xprt;
94       if (sock < FD_SETSIZE)
95         FD_SET (sock, &svc_fdset);
96
97       /* Check if we have an empty slot */
98       for (i = 0; i < svc_max_pollfd; ++i)
99         if (svc_pollfd[i].fd == -1)
100           {
101             svc_pollfd[i].fd = sock;
102             svc_pollfd[i].events = (POLLIN | POLLPRI |
103                                     POLLRDNORM | POLLRDBAND);
104             return;
105           }
106
107       ++svc_max_pollfd;
108       svc_pollfd = realloc (svc_pollfd,
109                             sizeof (struct pollfd) * svc_max_pollfd);
110       if (svc_pollfd == NULL) /* Out of memory */
111         return;
112
113       svc_pollfd[svc_max_pollfd - 1].fd = sock;
114       svc_pollfd[svc_max_pollfd - 1].events = (POLLIN | POLLPRI |
115                                                POLLRDNORM | POLLRDBAND);
116     }
117 }
118 libc_hidden_def(xprt_register)
119
120 /* De-activate a transport handle. */
121 void
122 xprt_unregister (SVCXPRT *xprt)
123 {
124   register int sock = xprt->xp_sock;
125   register int i;
126
127   if ((sock < _rpc_dtablesize ()) && (xports[sock] == xprt))
128     {
129       xports[sock] = (SVCXPRT *) 0;
130
131       if (sock < FD_SETSIZE)
132         FD_CLR (sock, &svc_fdset);
133
134       for (i = 0; i < svc_max_pollfd; ++i)
135         if (svc_pollfd[i].fd == sock)
136           svc_pollfd[i].fd = -1;
137     }
138 }
139 libc_hidden_def(xprt_unregister)
140
141
142 /* ********************** CALLOUT list related stuff ************* */
143
144 /* Search the callout list for a program number, return the callout
145    struct. */
146 static struct svc_callout *
147 svc_find (rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev)
148 {
149   register struct svc_callout *s, *p;
150
151   p = NULL_SVC;
152   for (s = svc_head; s != NULL_SVC; s = s->sc_next)
153     {
154       if ((s->sc_prog == prog) && (s->sc_vers == vers))
155         goto done;
156       p = s;
157     }
158 done:
159   *prev = p;
160   return s;
161 }
162
163 /* Add a service program to the callout list.
164    The dispatch routine will be called when a rpc request for this
165    program number comes in. */
166 bool_t
167 svc_register (SVCXPRT * xprt, rpcprog_t prog, rpcvers_t vers,
168               void (*dispatch) (struct svc_req *, SVCXPRT *),
169               rpcproc_t protocol)
170 {
171   struct svc_callout *prev;
172   register struct svc_callout *s;
173
174   if ((s = svc_find (prog, vers, &prev)) != NULL_SVC)
175     {
176       if (s->sc_dispatch == dispatch)
177         goto pmap_it;           /* he is registering another xptr */
178       return FALSE;
179     }
180   s = (struct svc_callout *) mem_alloc (sizeof (struct svc_callout));
181   if (s == (struct svc_callout *) 0)
182     return FALSE;
183
184   s->sc_prog = prog;
185   s->sc_vers = vers;
186   s->sc_dispatch = dispatch;
187   s->sc_next = svc_head;
188   svc_head = s;
189
190 pmap_it:
191   /* now register the information with the local binder service */
192   if (protocol)
193     return pmap_set (prog, vers, protocol, xprt->xp_port);
194
195   return TRUE;
196 }
197 libc_hidden_def(svc_register)
198
199 /* Remove a service program from the callout list. */
200 void
201 svc_unregister (rpcprog_t prog, rpcvers_t vers)
202 {
203   struct svc_callout *prev;
204   register struct svc_callout *s;
205
206   if ((s = svc_find (prog, vers, &prev)) == NULL_SVC)
207     return;
208
209   if (prev == NULL_SVC)
210     svc_head = s->sc_next;
211   else
212     prev->sc_next = s->sc_next;
213
214   s->sc_next = NULL_SVC;
215   mem_free ((char *) s, (u_int) sizeof (struct svc_callout));
216   /* now unregister the information with the local binder service */
217   pmap_unset (prog, vers);
218 }
219 libc_hidden_def(svc_unregister)
220
221 /* ******************* REPLY GENERATION ROUTINES  ************ */
222
223 /* Send a reply to an rpc request */
224 bool_t
225 svc_sendreply (register SVCXPRT *xprt, xdrproc_t xdr_results,
226                caddr_t xdr_location)
227 {
228   struct rpc_msg rply;
229
230   rply.rm_direction = REPLY;
231   rply.rm_reply.rp_stat = MSG_ACCEPTED;
232   rply.acpted_rply.ar_verf = xprt->xp_verf;
233   rply.acpted_rply.ar_stat = SUCCESS;
234   rply.acpted_rply.ar_results.where = xdr_location;
235   rply.acpted_rply.ar_results.proc = xdr_results;
236   return SVC_REPLY (xprt, &rply);
237 }
238 libc_hidden_def(svc_sendreply)
239
240 /* No procedure error reply */
241 void
242 svcerr_noproc (register SVCXPRT *xprt)
243 {
244   struct rpc_msg rply;
245
246   rply.rm_direction = REPLY;
247   rply.rm_reply.rp_stat = MSG_ACCEPTED;
248   rply.acpted_rply.ar_verf = xprt->xp_verf;
249   rply.acpted_rply.ar_stat = PROC_UNAVAIL;
250   SVC_REPLY (xprt, &rply);
251 }
252
253 /* Can't decode args error reply */
254 void
255 svcerr_decode (register SVCXPRT *xprt)
256 {
257   struct rpc_msg rply;
258
259   rply.rm_direction = REPLY;
260   rply.rm_reply.rp_stat = MSG_ACCEPTED;
261   rply.acpted_rply.ar_verf = xprt->xp_verf;
262   rply.acpted_rply.ar_stat = GARBAGE_ARGS;
263   SVC_REPLY (xprt, &rply);
264 }
265 libc_hidden_def(svcerr_decode)
266
267 /* Some system error */
268 void
269 svcerr_systemerr (register SVCXPRT *xprt)
270 {
271   struct rpc_msg rply;
272
273   rply.rm_direction = REPLY;
274   rply.rm_reply.rp_stat = MSG_ACCEPTED;
275   rply.acpted_rply.ar_verf = xprt->xp_verf;
276   rply.acpted_rply.ar_stat = SYSTEM_ERR;
277   SVC_REPLY (xprt, &rply);
278 }
279
280 /* Authentication error reply */
281 void
282 svcerr_auth (SVCXPRT *xprt, enum auth_stat why)
283 {
284   struct rpc_msg rply;
285
286   rply.rm_direction = REPLY;
287   rply.rm_reply.rp_stat = MSG_DENIED;
288   rply.rjcted_rply.rj_stat = AUTH_ERROR;
289   rply.rjcted_rply.rj_why = why;
290   SVC_REPLY (xprt, &rply);
291 }
292 libc_hidden_def(svcerr_auth)
293
294 /* Auth too weak error reply */
295 void
296 svcerr_weakauth (SVCXPRT *xprt)
297 {
298   svcerr_auth (xprt, AUTH_TOOWEAK);
299 }
300
301 /* Program unavailable error reply */
302 void
303 svcerr_noprog (register SVCXPRT *xprt)
304 {
305   struct rpc_msg rply;
306
307   rply.rm_direction = REPLY;
308   rply.rm_reply.rp_stat = MSG_ACCEPTED;
309   rply.acpted_rply.ar_verf = xprt->xp_verf;
310   rply.acpted_rply.ar_stat = PROG_UNAVAIL;
311   SVC_REPLY (xprt, &rply);
312 }
313 libc_hidden_def(svcerr_noprog)
314
315 /* Program version mismatch error reply */
316 void
317 svcerr_progvers (register SVCXPRT *xprt, rpcvers_t low_vers,
318                  rpcvers_t high_vers)
319 {
320   struct rpc_msg rply;
321
322   rply.rm_direction = REPLY;
323   rply.rm_reply.rp_stat = MSG_ACCEPTED;
324   rply.acpted_rply.ar_verf = xprt->xp_verf;
325   rply.acpted_rply.ar_stat = PROG_MISMATCH;
326   rply.acpted_rply.ar_vers.low = low_vers;
327   rply.acpted_rply.ar_vers.high = high_vers;
328   SVC_REPLY (xprt, &rply);
329 }
330 libc_hidden_def(svcerr_progvers)
331
332 /* ******************* SERVER INPUT STUFF ******************* */
333
334 /*
335  * Get server side input from some transport.
336  *
337  * Statement of authentication parameters management:
338  * This function owns and manages all authentication parameters, specifically
339  * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
340  * the "cooked" credentials (rqst->rq_clntcred).
341  * However, this function does not know the structure of the cooked
342  * credentials, so it make the following assumptions:
343  *   a) the structure is contiguous (no pointers), and
344  *   b) the cred structure size does not exceed RQCRED_SIZE bytes.
345  * In all events, all three parameters are freed upon exit from this routine.
346  * The storage is trivially management on the call stack in user land, but
347  * is mallocated in kernel land.
348  */
349
350 void
351 svc_getreq_common (const int fd)
352 {
353   enum xprt_stat stat;
354   struct rpc_msg msg;
355   register SVCXPRT *xprt;
356   char cred_area[2 * MAX_AUTH_BYTES + RQCRED_SIZE];
357   msg.rm_call.cb_cred.oa_base = cred_area;
358   msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
359
360   xprt = xports[fd];
361   /* Do we control fd? */
362   if (xprt == NULL)
363      return;
364
365   /* now receive msgs from xprtprt (support batch calls) */
366   do
367     {
368       if (SVC_RECV (xprt, &msg))
369         {
370           /* now find the exported program and call it */
371           struct svc_callout *s;
372           struct svc_req r;
373           enum auth_stat why;
374           rpcvers_t low_vers;
375           rpcvers_t high_vers;
376           int prog_found;
377
378           r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
379           r.rq_xprt = xprt;
380           r.rq_prog = msg.rm_call.cb_prog;
381           r.rq_vers = msg.rm_call.cb_vers;
382           r.rq_proc = msg.rm_call.cb_proc;
383           r.rq_cred = msg.rm_call.cb_cred;
384
385           /* first authenticate the message */
386           /* Check for null flavor and bypass these calls if possible */
387
388           if (msg.rm_call.cb_cred.oa_flavor == AUTH_NULL)
389             {
390               r.rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
391               r.rq_xprt->xp_verf.oa_length = 0;
392             }
393           else if ((why = _authenticate (&r, &msg)) != AUTH_OK)
394             {
395               svcerr_auth (xprt, why);
396               goto call_done;
397             }
398
399           /* now match message with a registered service */
400           prog_found = FALSE;
401           low_vers = 0 - 1;
402           high_vers = 0;
403
404           for (s = svc_head; s != NULL_SVC; s = s->sc_next)
405             {
406               if (s->sc_prog == r.rq_prog)
407                 {
408                   if (s->sc_vers == r.rq_vers)
409                     {
410                       (*s->sc_dispatch) (&r, xprt);
411                       goto call_done;
412                     }
413                   /* found correct version */
414                   prog_found = TRUE;
415                   if (s->sc_vers < low_vers)
416                     low_vers = s->sc_vers;
417                   if (s->sc_vers > high_vers)
418                     high_vers = s->sc_vers;
419                 }
420               /* found correct program */
421             }
422           /* if we got here, the program or version
423              is not served ... */
424           if (prog_found)
425             svcerr_progvers (xprt, low_vers, high_vers);
426           else
427             svcerr_noprog (xprt);
428           /* Fall through to ... */
429         }
430     call_done:
431       if ((stat = SVC_STAT (xprt)) == XPRT_DIED)
432         {
433           SVC_DESTROY (xprt);
434           break;
435         }
436     }
437   while (stat == XPRT_MOREREQS);
438 }
439 libc_hidden_def(svc_getreq_common)
440
441 void
442 svc_getreqset (fd_set *readfds)
443 {
444   register u_int32_t mask;
445   register u_int32_t *maskp;
446   register int setsize;
447   register int sock;
448   register int bit;
449
450   setsize = _rpc_dtablesize ();
451   if (setsize > FD_SETSIZE)
452     setsize = FD_SETSIZE;
453   maskp = (u_int32_t *) readfds->fds_bits;
454   for (sock = 0; sock < setsize; sock += 32)
455     for (mask = *maskp++; (bit = ffs (mask)); mask ^= (1 << (bit - 1)))
456       svc_getreq_common (sock + bit - 1);
457 }
458 libc_hidden_def(svc_getreqset)
459
460 void
461 svc_getreq (int rdfds)
462 {
463   fd_set readfds;
464
465   FD_ZERO (&readfds);
466   readfds.fds_bits[0] = rdfds;
467   svc_getreqset (&readfds);
468 }
469 libc_hidden_def(svc_getreq)
470
471 void
472 svc_getreq_poll (struct pollfd *pfdp, int pollretval)
473 {
474   register int i;
475   register int fds_found;
476
477   for (i = fds_found = 0; i < svc_max_pollfd && fds_found < pollretval; ++i)
478     {
479       register struct pollfd *p = &pfdp[i];
480
481       if (p->fd != -1 && p->revents)
482         {
483           /* fd has input waiting */
484           ++fds_found;
485
486           if (p->revents & POLLNVAL)
487             xprt_unregister (xports[p->fd]);
488           else
489             svc_getreq_common (p->fd);
490         }
491     }
492 }
493 libc_hidden_def(svc_getreq_poll)
494
495 #ifdef __UCLIBC_HAS_THREADS__
496
497 void attribute_hidden __rpc_thread_svc_cleanup (void)
498 {
499   struct svc_callout *svcp;
500
501   while ((svcp = svc_head) != NULL)
502     svc_unregister (svcp->sc_prog, svcp->sc_vers);
503 }
504
505 #endif /* __UCLIBC_HAS_THREADS__ */