tst-udp-error.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* Check for use-after-free in clntudp_call (bug 21115).
  2. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <netinet/in.h>
  16. #include <rpc/clnt.h>
  17. #include <rpc/svc.h>
  18. #include <support/check.h>
  19. #include <support/namespace.h>
  20. #include <support/xsocket.h>
  21. #include <unistd.h>
  22. static int
  23. do_test (void)
  24. {
  25. support_become_root ();
  26. support_enter_network_namespace ();
  27. /* Obtain a likely-unused port number. */
  28. struct sockaddr_in sin =
  29. {
  30. .sin_family = AF_INET,
  31. .sin_addr.s_addr = htonl (INADDR_LOOPBACK),
  32. };
  33. {
  34. int fd = xsocket (AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
  35. xbind (fd, (struct sockaddr *) &sin, sizeof (sin));
  36. socklen_t sinlen = sizeof (sin);
  37. xgetsockname (fd, (struct sockaddr *) &sin, &sinlen);
  38. /* Close the socket, so that we will receive an error below. */
  39. close (fd);
  40. }
  41. int sock = RPC_ANYSOCK;
  42. CLIENT *clnt = clntudp_create
  43. (&sin, 1, 2, (struct timeval) { 1, 0 }, &sock);
  44. TEST_VERIFY_EXIT (clnt != NULL);
  45. TEST_VERIFY (clnt_call (clnt, 3,
  46. (xdrproc_t) xdr_void, NULL,
  47. (xdrproc_t) xdr_void, NULL,
  48. ((struct timeval) { 3, 0 }))
  49. == RPC_CANTRECV);
  50. clnt_destroy (clnt);
  51. return 0;
  52. }
  53. #include <support/test-driver.c>