tst-setuid3.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Copyright (C) 2014-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <stdio.h>
  15. #include <errno.h>
  16. #include <pthread.h>
  17. #include <stdbool.h>
  18. #include <unistd.h>
  19. /* The test must run under a non-privileged user ID. */
  20. static const uid_t test_uid = 1;
  21. static pthread_barrier_t barrier1;
  22. static pthread_barrier_t barrier2;
  23. #define FAIL(fmt, ...) \
  24. do { printf ("FAIL: " fmt "\n", __VA_ARGS__); _exit (1); } while (0)
  25. #define FAIL_ERR(fmt, ...) \
  26. do { printf ("FAIL: " fmt ": %m\n", __VA_ARGS__); _exit (1); } while (0)
  27. /* True if x is not a successful return code from pthread_barrier_wait. */
  28. static inline bool
  29. is_invalid_barrier_ret (int x)
  30. {
  31. return x != 0 && x != PTHREAD_BARRIER_SERIAL_THREAD;
  32. }
  33. static void *
  34. thread_func (void *ctx __attribute__ ((unused)))
  35. {
  36. int ret = pthread_barrier_wait (&barrier1);
  37. if (is_invalid_barrier_ret (ret))
  38. FAIL ("pthread_barrier_wait (barrier1) (on thread): %d", ret);
  39. ret = pthread_barrier_wait (&barrier2);
  40. if (is_invalid_barrier_ret (ret))
  41. FAIL ("pthread_barrier_wait (barrier2) (on thread): %d", ret);
  42. return NULL;
  43. }
  44. static void
  45. setuid_failure (int phase)
  46. {
  47. int ret = setuid (0);
  48. switch (ret)
  49. {
  50. case 0:
  51. FAIL ("setuid succeeded unexpectedly in phase %d", phase);
  52. case -1:
  53. if (errno != EPERM)
  54. FAIL_ERR ("setuid phase %d", phase);
  55. break;
  56. default:
  57. FAIL ("invalid setuid return value in phase %d: %d", phase, ret);
  58. }
  59. }
  60. static int
  61. do_test (void)
  62. {
  63. if (getuid () == 0)
  64. if (setuid (test_uid) != 0)
  65. FAIL_ERR ("setuid (%u)", (unsigned) test_uid);
  66. if (setuid (getuid ()))
  67. FAIL_ERR ("setuid (%s)", "getuid ()");
  68. setuid_failure (1);
  69. int ret = pthread_barrier_init (&barrier1, NULL, 2);
  70. if (ret != 0)
  71. FAIL ("pthread_barrier_init (barrier1): %d", ret);
  72. ret = pthread_barrier_init (&barrier2, NULL, 2);
  73. if (ret != 0)
  74. FAIL ("pthread_barrier_init (barrier2): %d", ret);
  75. pthread_t thread;
  76. ret = pthread_create (&thread, NULL, thread_func, NULL);
  77. if (ret != 0)
  78. FAIL ("pthread_create: %d", ret);
  79. /* Ensure that the thread is running properly. */
  80. ret = pthread_barrier_wait (&barrier1);
  81. if (is_invalid_barrier_ret (ret))
  82. FAIL ("pthread_barrier_wait (barrier1): %d", ret);
  83. setuid_failure (2);
  84. /* Check success case. */
  85. if (setuid (getuid ()) != 0)
  86. FAIL_ERR ("setuid (%s)", "getuid ()");
  87. /* Shutdown. */
  88. ret = pthread_barrier_wait (&barrier2);
  89. if (is_invalid_barrier_ret (ret))
  90. FAIL ("pthread_barrier_wait (barrier2): %d", ret);
  91. ret = pthread_join (thread, NULL);
  92. if (ret != 0)
  93. FAIL ("pthread_join: %d", ret);
  94. return 0;
  95. }
  96. #define TEST_FUNCTION do_test ()
  97. #include "../test-skeleton.c"