Blame sunrpc/tst-udp-error.c

Packit Service 82fcde
/* Check for use-after-free in clntudp_call (bug 21115).
Packit Service 82fcde
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include <netinet/in.h>
Packit Service 82fcde
#include <rpc/clnt.h>
Packit Service 82fcde
#include <rpc/svc.h>
Packit Service 82fcde
#include <support/check.h>
Packit Service 82fcde
#include <support/namespace.h>
Packit Service 82fcde
#include <support/xsocket.h>
Packit Service 82fcde
#include <unistd.h>
Packit Service 82fcde
Packit Service 82fcde
static int
Packit Service 82fcde
do_test (void)
Packit Service 82fcde
{
Packit Service 82fcde
  support_become_root ();
Packit Service 82fcde
  support_enter_network_namespace ();
Packit Service 82fcde
Packit Service 82fcde
  /* Obtain a likely-unused port number.  */
Packit Service 82fcde
  struct sockaddr_in sin =
Packit Service 82fcde
    {
Packit Service 82fcde
      .sin_family = AF_INET,
Packit Service 82fcde
      .sin_addr.s_addr = htonl (INADDR_LOOPBACK),
Packit Service 82fcde
    };
Packit Service 82fcde
  {
Packit Service 82fcde
    int fd = xsocket (AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
Packit Service 82fcde
    xbind (fd, (struct sockaddr *) &sin, sizeof (sin));
Packit Service 82fcde
    socklen_t sinlen = sizeof (sin);
Packit Service 82fcde
    xgetsockname (fd, (struct sockaddr *) &sin, &sinlen);
Packit Service 82fcde
    /* Close the socket, so that we will receive an error below.  */
Packit Service 82fcde
    close (fd);
Packit Service 82fcde
  }
Packit Service 82fcde
Packit Service 82fcde
  int sock = RPC_ANYSOCK;
Packit Service 82fcde
  CLIENT *clnt = clntudp_create
Packit Service 82fcde
    (&sin, 1, 2, (struct timeval) { 1, 0 }, &sock);
Packit Service 82fcde
  TEST_VERIFY_EXIT (clnt != NULL);
Packit Service 82fcde
  TEST_VERIFY (clnt_call (clnt, 3,
Packit Service 82fcde
                          (xdrproc_t) xdr_void, NULL,
Packit Service 82fcde
                          (xdrproc_t) xdr_void, NULL,
Packit Service 82fcde
                          ((struct timeval) { 3, 0 }))
Packit Service 82fcde
               == RPC_CANTRECV);
Packit Service 82fcde
  clnt_destroy (clnt);
Packit Service 82fcde
Packit Service 82fcde
  return 0;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
#include <support/test-driver.c>