1 /* Test that setuid, pthread_create, thread exit do not deadlock (bug 28361).
2    Copyright (C) 2021-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <support/check.h>
20 #include <support/xthread.h>
21 #include <unistd.h>
22 
23 /* How many threads to launch during each iteration.  */
24 enum { threads = 4 };
25 
26 /* How many iterations to perform.  This value seems to reproduce
27    bug 28361 in a bout one in three runs.  */
28 enum { iterations = 5000 };
29 
30 /* Cache of the real user ID used by setuid_thread.  */
31 static uid_t uid;
32 
33 /* Start routine for the threads.  */
34 static void *
setuid_thread(void * closure)35 setuid_thread (void *closure)
36 {
37   TEST_COMPARE (setuid (uid), 0);
38   return NULL;
39 }
40 
41 static int
do_test(void)42 do_test (void)
43 {
44   /* The setxid machinery is still invoked even if the UID is
45      unchanged.  (The kernel might reset other credentials as part of
46      the system call.)  */
47   uid = getuid ();
48 
49   for (int i = 0; i < iterations; ++i)
50     {
51       pthread_t thread_ids[threads];
52       for (int j = 0; j < threads; ++j)
53         thread_ids[j] = xpthread_create (NULL, setuid_thread, NULL);
54       for (int j = 0; j < threads; ++j)
55         xpthread_join (thread_ids[j]);
56     }
57 
58   return 0;
59 }
60 
61 #include <support/test-driver.c>
62