~ubuntu-branches/ubuntu/trusty/eglibc/trusty

« back to all changes in this revision

Viewing changes to .pc/any/local-sunrpc-dos.diff/sunrpc/svc_tcp.c

  • Committer: Package Import Robot
  • Author(s): Adam Conrad
  • Date: 2013-01-10 18:39:35 UTC
  • mfrom: (1.5.2) (4.4.24 experimental)
  • Revision ID: package-import@ubuntu.com-20130110183935-afsgfxkmg7wk5eaj
Tags: 2.17-0ubuntu1
* Merge with Debian, bringing in a new upstream and many small fixes:
  - patches/any/cvs-malloc-deadlock.diff: Dropped, merged upstream.
  - patches/ubuntu/lddebug-scopes.diff: Rebase for upstream changes.
  - patches/ubuntu/local-CVE-2012-3406.diff: Rebased against upstream.
  - patches/ubuntu/no-asm-mtune-i686.diff: Fixed in recent binutils.
* This upstream merge fixes a nasty hang in pulseaudio (LP: #1085342)
* Bump MIN_KERNEL_SUPPORTED to 2.6.32 on ARM, now that we no longer
  have to support shonky 2.6.31 kernels on imx51 babbage builders.
* Drop patches/ubuntu/local-disable-nscd-host-caching.diff, as these
  issues were apparently resolved upstream a while ago (LP: #613662)
* Fix the compiled-in bug URL to point to launchpad.net, not Debian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * svc_tcp.c, Server side for TCP/IP based RPC.
3
 
 *
4
 
 * Copyright (c) 2010, Oracle America, Inc.
5
 
 *
6
 
 * Redistribution and use in source and binary forms, with or without
7
 
 * modification, are permitted provided that the following conditions are
8
 
 * met:
9
 
 *
10
 
 *     * Redistributions of source code must retain the above copyright
11
 
 *       notice, this list of conditions and the following disclaimer.
12
 
 *     * Redistributions in binary form must reproduce the above
13
 
 *       copyright notice, this list of conditions and the following
14
 
 *       disclaimer in the documentation and/or other materials
15
 
 *       provided with the distribution.
16
 
 *     * Neither the name of the "Oracle America, Inc." nor the names of its
17
 
 *       contributors may be used to endorse or promote products derived
18
 
 *       from this software without specific prior written permission.
19
 
 *
20
 
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
 
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
 
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23
 
 *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
 
 *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25
 
 *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
 
 *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27
 
 *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
 
 *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29
 
 *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30
 
 *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
 
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
 
 *
33
 
 * Actually implements two flavors of transporter -
34
 
 * a tcp rendezvouser (a listener and connection establisher)
35
 
 * and a record/tcp stream.
36
 
 */
37
 
 
38
 
#include <stdio.h>
39
 
#include <unistd.h>
40
 
#include <string.h>
41
 
#include <libintl.h>
42
 
#include <rpc/rpc.h>
43
 
#include <sys/socket.h>
44
 
#include <sys/poll.h>
45
 
#include <errno.h>
46
 
#include <stdlib.h>
47
 
 
48
 
#include <wchar.h>
49
 
#include <libio/iolibio.h>
50
 
 
51
 
/*
52
 
 * Ops vector for TCP/IP based rpc service handle
53
 
 */
54
 
static bool_t svctcp_recv (SVCXPRT *, struct rpc_msg *);
55
 
static enum xprt_stat svctcp_stat (SVCXPRT *);
56
 
static bool_t svctcp_getargs (SVCXPRT *, xdrproc_t, caddr_t);
57
 
static bool_t svctcp_reply (SVCXPRT *, struct rpc_msg *);
58
 
static bool_t svctcp_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
59
 
static void svctcp_destroy (SVCXPRT *);
60
 
 
61
 
static const struct xp_ops svctcp_op =
62
 
{
63
 
  svctcp_recv,
64
 
  svctcp_stat,
65
 
  svctcp_getargs,
66
 
  svctcp_reply,
67
 
  svctcp_freeargs,
68
 
  svctcp_destroy
69
 
};
70
 
 
71
 
/*
72
 
 * Ops vector for TCP/IP rendezvous handler
73
 
 */
74
 
static bool_t rendezvous_request (SVCXPRT *, struct rpc_msg *);
75
 
static enum xprt_stat rendezvous_stat (SVCXPRT *);
76
 
static void svctcp_rendezvous_abort (void) __attribute__ ((__noreturn__));
77
 
 
78
 
/* This function makes sure abort() relocation goes through PLT
79
 
   and thus can be lazy bound.  */
80
 
static void
81
 
svctcp_rendezvous_abort (void)
82
 
{
83
 
  abort ();
84
 
};
85
 
 
86
 
static const struct xp_ops svctcp_rendezvous_op =
87
 
{
88
 
  rendezvous_request,
89
 
  rendezvous_stat,
90
 
  (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
91
 
  (bool_t (*) (SVCXPRT *, struct rpc_msg *)) svctcp_rendezvous_abort,
92
 
  (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
93
 
  svctcp_destroy
94
 
};
95
 
 
96
 
static int readtcp (char*, char *, int);
97
 
static int writetcp (char *, char *, int);
98
 
static SVCXPRT *makefd_xprt (int, u_int, u_int) internal_function;
99
 
 
100
 
struct tcp_rendezvous
101
 
  {                             /* kept in xprt->xp_p1 */
102
 
    u_int sendsize;
103
 
    u_int recvsize;
104
 
  };
105
 
 
106
 
struct tcp_conn
107
 
  {                             /* kept in xprt->xp_p1 */
108
 
    enum xprt_stat strm_stat;
109
 
    u_long x_id;
110
 
    XDR xdrs;
111
 
    char verf_body[MAX_AUTH_BYTES];
112
 
  };
113
 
 
114
 
/*
115
 
 * Usage:
116
 
 *      xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
117
 
 *
118
 
 * Creates, registers, and returns a (rpc) tcp based transporter.
119
 
 * Once *xprt is initialized, it is registered as a transporter
120
 
 * see (svc.h, xprt_register).  This routine returns
121
 
 * a NULL if a problem occurred.
122
 
 *
123
 
 * If sock<0 then a socket is created, else sock is used.
124
 
 * If the socket, sock is not bound to a port then svctcp_create
125
 
 * binds it to an arbitrary port.  The routine then starts a tcp
126
 
 * listener on the socket's associated port.  In any (successful) case,
127
 
 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
128
 
 * associated port number.
129
 
 *
130
 
 * Since tcp streams do buffered io similar to stdio, the caller can specify
131
 
 * how big the send and receive buffers are via the second and third parms;
132
 
 * 0 => use the system default.
133
 
 */
134
 
SVCXPRT *
135
 
svctcp_create (int sock, u_int sendsize, u_int recvsize)
136
 
{
137
 
  bool_t madesock = FALSE;
138
 
  SVCXPRT *xprt;
139
 
  struct tcp_rendezvous *r;
140
 
  struct sockaddr_in addr;
141
 
  socklen_t len = sizeof (struct sockaddr_in);
142
 
 
143
 
  if (sock == RPC_ANYSOCK)
144
 
    {
145
 
      if ((sock = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
146
 
        {
147
 
          perror (_("svc_tcp.c - tcp socket creation problem"));
148
 
          return (SVCXPRT *) NULL;
149
 
        }
150
 
      madesock = TRUE;
151
 
    }
152
 
  __bzero ((char *) &addr, sizeof (addr));
153
 
  addr.sin_family = AF_INET;
154
 
  if (bindresvport (sock, &addr))
155
 
    {
156
 
      addr.sin_port = 0;
157
 
      (void) __bind (sock, (struct sockaddr *) &addr, len);
158
 
    }
159
 
  if ((__getsockname (sock, (struct sockaddr *) &addr, &len) != 0) ||
160
 
      (__listen (sock, SOMAXCONN) != 0))
161
 
    {
162
 
      perror (_("svc_tcp.c - cannot getsockname or listen"));
163
 
      if (madesock)
164
 
        (void) __close (sock);
165
 
      return (SVCXPRT *) NULL;
166
 
    }
167
 
  r = (struct tcp_rendezvous *) mem_alloc (sizeof (*r));
168
 
  xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
169
 
  if (r == NULL || xprt == NULL)
170
 
    {
171
 
      (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
172
 
      mem_free (r, sizeof (*r));
173
 
      mem_free (xprt, sizeof (SVCXPRT));
174
 
      return NULL;
175
 
    }
176
 
  r->sendsize = sendsize;
177
 
  r->recvsize = recvsize;
178
 
  xprt->xp_p2 = NULL;
179
 
  xprt->xp_p1 = (caddr_t) r;
180
 
  xprt->xp_verf = _null_auth;
181
 
  xprt->xp_ops = &svctcp_rendezvous_op;
182
 
  xprt->xp_port = ntohs (addr.sin_port);
183
 
  xprt->xp_sock = sock;
184
 
  xprt_register (xprt);
185
 
  return xprt;
186
 
}
187
 
#ifdef EXPORT_RPC_SYMBOLS
188
 
libc_hidden_def (svctcp_create)
189
 
#else
190
 
libc_hidden_nolink_sunrpc (svctcp_create, GLIBC_2_0)
191
 
#endif
192
 
 
193
 
/*
194
 
 * Like svtcp_create(), except the routine takes any *open* UNIX file
195
 
 * descriptor as its first input.
196
 
 */
197
 
SVCXPRT *
198
 
svcfd_create (int fd, u_int sendsize, u_int recvsize)
199
 
{
200
 
  return makefd_xprt (fd, sendsize, recvsize);
201
 
}
202
 
libc_hidden_nolink_sunrpc (svcfd_create, GLIBC_2_0)
203
 
 
204
 
static SVCXPRT *
205
 
internal_function
206
 
makefd_xprt (int fd, u_int sendsize, u_int recvsize)
207
 
{
208
 
  SVCXPRT *xprt;
209
 
  struct tcp_conn *cd;
210
 
 
211
 
  xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
212
 
  cd = (struct tcp_conn *) mem_alloc (sizeof (struct tcp_conn));
213
 
  if (xprt == (SVCXPRT *) NULL || cd == NULL)
214
 
    {
215
 
      (void) __fxprintf (NULL, "%s: %s", "svc_tcp: makefd_xprt",
216
 
                         _("out of memory\n"));
217
 
      mem_free (xprt, sizeof (SVCXPRT));
218
 
      mem_free (cd, sizeof (struct tcp_conn));
219
 
      return NULL;
220
 
    }
221
 
  cd->strm_stat = XPRT_IDLE;
222
 
  xdrrec_create (&(cd->xdrs), sendsize, recvsize,
223
 
                 (caddr_t) xprt, readtcp, writetcp);
224
 
  xprt->xp_p2 = NULL;
225
 
  xprt->xp_p1 = (caddr_t) cd;
226
 
  xprt->xp_verf.oa_base = cd->verf_body;
227
 
  xprt->xp_addrlen = 0;
228
 
  xprt->xp_ops = &svctcp_op;    /* truly deals with calls */
229
 
  xprt->xp_port = 0;            /* this is a connection, not a rendezvouser */
230
 
  xprt->xp_sock = fd;
231
 
  xprt_register (xprt);
232
 
  return xprt;
233
 
}
234
 
 
235
 
static bool_t
236
 
rendezvous_request (SVCXPRT *xprt, struct rpc_msg *errmsg)
237
 
{
238
 
  int sock;
239
 
  struct tcp_rendezvous *r;
240
 
  struct sockaddr_in addr;
241
 
  socklen_t len;
242
 
 
243
 
  r = (struct tcp_rendezvous *) xprt->xp_p1;
244
 
again:
245
 
  len = sizeof (struct sockaddr_in);
246
 
  if ((sock = accept (xprt->xp_sock, (struct sockaddr *) &addr, &len)) < 0)
247
 
    {
248
 
      if (errno == EINTR)
249
 
        goto again;
250
 
      return FALSE;
251
 
    }
252
 
  /*
253
 
   * make a new transporter (re-uses xprt)
254
 
   */
255
 
  xprt = makefd_xprt (sock, r->sendsize, r->recvsize);
256
 
  memcpy (&xprt->xp_raddr, &addr, sizeof (addr));
257
 
  xprt->xp_addrlen = len;
258
 
  return FALSE;         /* there is never an rpc msg to be processed */
259
 
}
260
 
 
261
 
static enum xprt_stat
262
 
rendezvous_stat (SVCXPRT *xprt)
263
 
{
264
 
  return XPRT_IDLE;
265
 
}
266
 
 
267
 
static void
268
 
svctcp_destroy (SVCXPRT *xprt)
269
 
{
270
 
  struct tcp_conn *cd = (struct tcp_conn *) xprt->xp_p1;
271
 
 
272
 
  xprt_unregister (xprt);
273
 
  (void) __close (xprt->xp_sock);
274
 
  if (xprt->xp_port != 0)
275
 
    {
276
 
      /* a rendezvouser socket */
277
 
      xprt->xp_port = 0;
278
 
    }
279
 
  else
280
 
    {
281
 
      /* an actual connection socket */
282
 
      XDR_DESTROY (&(cd->xdrs));
283
 
    }
284
 
  mem_free ((caddr_t) cd, sizeof (struct tcp_conn));
285
 
  mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
286
 
}
287
 
 
288
 
 
289
 
/*
290
 
 * reads data from the tcp connection.
291
 
 * any error is fatal and the connection is closed.
292
 
 * (And a read of zero bytes is a half closed stream => error.)
293
 
 */
294
 
static int
295
 
readtcp (char *xprtptr, char *buf, int len)
296
 
{
297
 
  SVCXPRT *xprt = (SVCXPRT *)xprtptr;
298
 
  int sock = xprt->xp_sock;
299
 
  int milliseconds = 35 * 1000;
300
 
  struct pollfd pollfd;
301
 
 
302
 
  do
303
 
    {
304
 
      pollfd.fd = sock;
305
 
      pollfd.events = POLLIN;
306
 
      switch (__poll (&pollfd, 1, milliseconds))
307
 
        {
308
 
        case -1:
309
 
          if (errno == EINTR)
310
 
            continue;
311
 
          /*FALLTHROUGH*/
312
 
        case 0:
313
 
          goto fatal_err;
314
 
        default:
315
 
          if ((pollfd.revents & POLLERR) || (pollfd.revents & POLLHUP)
316
 
              || (pollfd.revents & POLLNVAL))
317
 
            goto fatal_err;
318
 
          break;
319
 
        }
320
 
    }
321
 
  while ((pollfd.revents & POLLIN) == 0);
322
 
 
323
 
  if ((len = __read (sock, buf, len)) > 0)
324
 
    return len;
325
 
 
326
 
 fatal_err:
327
 
  ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
328
 
  return -1;
329
 
}
330
 
 
331
 
/*
332
 
 * writes data to the tcp connection.
333
 
 * Any error is fatal and the connection is closed.
334
 
 */
335
 
static int
336
 
writetcp (char *xprtptr, char * buf, int len)
337
 
{
338
 
  SVCXPRT *xprt = (SVCXPRT *)xprtptr;
339
 
  int i, cnt;
340
 
 
341
 
  for (cnt = len; cnt > 0; cnt -= i, buf += i)
342
 
    {
343
 
      if ((i = __write (xprt->xp_sock, buf, cnt)) < 0)
344
 
        {
345
 
          ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
346
 
          return -1;
347
 
        }
348
 
    }
349
 
  return len;
350
 
}
351
 
 
352
 
static enum xprt_stat
353
 
svctcp_stat (SVCXPRT *xprt)
354
 
{
355
 
  struct tcp_conn *cd =
356
 
  (struct tcp_conn *) (xprt->xp_p1);
357
 
 
358
 
  if (cd->strm_stat == XPRT_DIED)
359
 
    return XPRT_DIED;
360
 
  if (!xdrrec_eof (&(cd->xdrs)))
361
 
    return XPRT_MOREREQS;
362
 
  return XPRT_IDLE;
363
 
}
364
 
 
365
 
static bool_t
366
 
svctcp_recv (SVCXPRT *xprt, struct rpc_msg *msg)
367
 
{
368
 
  struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
369
 
  XDR *xdrs = &(cd->xdrs);
370
 
 
371
 
  xdrs->x_op = XDR_DECODE;
372
 
  (void) xdrrec_skiprecord (xdrs);
373
 
  if (xdr_callmsg (xdrs, msg))
374
 
    {
375
 
      cd->x_id = msg->rm_xid;
376
 
      return TRUE;
377
 
    }
378
 
  cd->strm_stat = XPRT_DIED;    /* XXXX */
379
 
  return FALSE;
380
 
}
381
 
 
382
 
static bool_t
383
 
svctcp_getargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
384
 
{
385
 
  return ((*xdr_args) (&(((struct tcp_conn *)
386
 
                          (xprt->xp_p1))->xdrs), args_ptr));
387
 
}
388
 
 
389
 
static bool_t
390
 
svctcp_freeargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
391
 
{
392
 
  XDR *xdrs = &(((struct tcp_conn *) (xprt->xp_p1))->xdrs);
393
 
 
394
 
  xdrs->x_op = XDR_FREE;
395
 
  return ((*xdr_args) (xdrs, args_ptr));
396
 
}
397
 
 
398
 
static bool_t
399
 
svctcp_reply (SVCXPRT *xprt, struct rpc_msg *msg)
400
 
{
401
 
  struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
402
 
  XDR *xdrs = &(cd->xdrs);
403
 
  bool_t stat;
404
 
 
405
 
  xdrs->x_op = XDR_ENCODE;
406
 
  msg->rm_xid = cd->x_id;
407
 
  stat = xdr_replymsg (xdrs, msg);
408
 
  (void) xdrrec_endofrecord (xdrs, TRUE);
409
 
  return stat;
410
 
}