tst-cancel-getpwuid_r.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* Test cancellation of getpwuid_r.
  2. Copyright (C) 2016-2026 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. <https://www.gnu.org/licenses/>. */
  15. /* Test if cancellation of getpwuid_r incorrectly leaves internal
  16. function state locked resulting in hang of subsequent calls to
  17. getpwuid_r. The main thread creates a second thread which will do
  18. the calls to getpwuid_r. A semaphore is used by the second thread to
  19. signal to the main thread that it is as close as it can be to the
  20. call site of getpwuid_r. The goal of the semaphore is to avoid any
  21. cancellable function calls between the sem_post and the call to
  22. getpwuid_r. The main thread then attempts to cancel the second
  23. thread. Without the fixes the cancellation happens at any number of
  24. calls to cancellable functions in getpuid_r, but with the fix the
  25. cancellation either does not happen or happens only at expected
  26. points where the internal state is consistent. We use an explicit
  27. pthread_testcancel call to terminate the loop in a timely fashion
  28. if the implementation does not have a cancellation point. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <pthread.h>
  32. #include <pwd.h>
  33. #include <nss.h>
  34. #include <sys/types.h>
  35. #include <unistd.h>
  36. #include <semaphore.h>
  37. #include <errno.h>
  38. #include <support/support.h>
  39. sem_t started;
  40. char *wbuf;
  41. long wbufsz;
  42. void
  43. worker_free (void *arg)
  44. {
  45. free (arg);
  46. }
  47. static void *
  48. worker (void *arg)
  49. {
  50. int ret;
  51. unsigned int iter = 0;
  52. struct passwd pwbuf, *pw;
  53. uid_t uid;
  54. uid = geteuid ();
  55. /* Use a reasonable sized buffer. Note that _SC_GETPW_R_SIZE_MAX is
  56. just a hint and not any kind of maximum value. */
  57. wbufsz = sysconf (_SC_GETPW_R_SIZE_MAX);
  58. if (wbufsz == -1)
  59. wbufsz = 1024;
  60. wbuf = xmalloc (wbufsz);
  61. pthread_cleanup_push (worker_free, wbuf);
  62. sem_post (&started);
  63. while (1)
  64. {
  65. iter++;
  66. ret = getpwuid_r (uid, &pwbuf, wbuf, wbufsz, &pw);
  67. /* The call to getpwuid_r may not cancel so we need to test
  68. for cancellation after some number of iterations of the
  69. function. Choose an arbitrary 100,000 iterations of running
  70. getpwuid_r in a tight cancellation loop before testing for
  71. cancellation. */
  72. if (iter > 100000)
  73. pthread_testcancel ();
  74. if (ret == ERANGE)
  75. {
  76. /* Increase the buffer size. */
  77. free (wbuf);
  78. wbufsz = wbufsz * 2;
  79. wbuf = xmalloc (wbufsz);
  80. }
  81. }
  82. pthread_cleanup_pop (1);
  83. return NULL;
  84. }
  85. static int
  86. do_test (void)
  87. {
  88. int ret;
  89. char *buf;
  90. long bufsz;
  91. void *retval;
  92. struct passwd pwbuf, *pw;
  93. pthread_t thread;
  94. /* Configure the test to only use files. We control the files plugin
  95. as part of glibc so we assert that it should be deferred
  96. cancellation safe. */
  97. __nss_configure_lookup ("passwd", "files");
  98. /* Use a reasonable sized buffer. Note that _SC_GETPW_R_SIZE_MAX is
  99. just a hint and not any kind of maximum value. */
  100. bufsz = sysconf (_SC_GETPW_R_SIZE_MAX);
  101. if (bufsz == -1)
  102. bufsz = 1024;
  103. buf = xmalloc (bufsz);
  104. sem_init (&started, 0, 0);
  105. pthread_create (&thread, NULL, worker, NULL);
  106. do
  107. {
  108. ret = sem_wait (&started);
  109. if (ret == -1 && errno != EINTR)
  110. {
  111. printf ("FAIL: Failed to wait for second thread to start.\n");
  112. exit (EXIT_FAILURE);
  113. }
  114. }
  115. while (ret != 0);
  116. printf ("INFO: Cancelling thread\n");
  117. if ((ret = pthread_cancel (thread)) != 0)
  118. {
  119. printf ("FAIL: Failed to cancel thread. Returned %d\n", ret);
  120. exit (EXIT_FAILURE);
  121. }
  122. printf ("INFO: Joining...\n");
  123. pthread_join (thread, &retval);
  124. if (retval != PTHREAD_CANCELED)
  125. {
  126. printf ("FAIL: Thread was not cancelled.\n");
  127. exit (EXIT_FAILURE);
  128. }
  129. printf ("INFO: Joined, trying getpwuid_r call\n");
  130. /* Before the fix in 312be3f9f5eab1643d7dcc7728c76d413d4f2640 for this
  131. issue the cancellation point could happen in any number of internal
  132. calls, and therefore locks would be left held and the following
  133. call to getpwuid_r would block and the test would time out. */
  134. do
  135. {
  136. ret = getpwuid_r (geteuid (), &pwbuf, buf, bufsz, &pw);
  137. if (ret == ERANGE)
  138. {
  139. /* Increase the buffer size. */
  140. free (buf);
  141. bufsz = bufsz * 2;
  142. buf = xmalloc (bufsz);
  143. }
  144. }
  145. while (ret == ERANGE);
  146. free (buf);
  147. /* Before the fix we would never get here. */
  148. printf ("PASS: Canceled getpwuid_r successfully"
  149. " and called it again without blocking.\n");
  150. return 0;
  151. }
  152. #define TIMEOUT 900
  153. #include <support/test-driver.c>