1 /* Copyright (C) 2014-2021 Free Software Foundation, Inc.
2 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.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
23 #include <sys/syscall.h>
26 /* Check that a partial setuid failure aborts the process. */
28 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
29 static pthread_cond_t cond_send
;
30 static void (*func_sent
) (void);
31 static pthread_cond_t cond_recv
;
33 #define FAIL(fmt, ...) \
34 do { printf ("FAIL: " fmt "\n", __VA_ARGS__); _exit (1); } while (0)
37 thread_func (void *ctx
__attribute__ ((unused
)))
39 int ret
= pthread_mutex_lock (&mutex
);
41 FAIL ("pthread_mutex_lock (thread): %d", ret
);
45 if (func_sent
!= NULL
)
47 void (*func
) (void) = func_sent
;
48 ret
= pthread_mutex_unlock (&mutex
);
50 FAIL ("pthread_mutex_unlock (thread): %d", ret
);
52 ret
= pthread_mutex_lock (&mutex
);
54 FAIL ("pthread_mutex_lock (thread): %d", ret
);
56 ret
= pthread_cond_signal (&cond_recv
);
58 FAIL ("pthread_cond_signal (recv): %d", ret
);
60 ret
= pthread_cond_wait (&cond_send
, &mutex
);
62 FAIL ("pthread_cond_wait (send): %d", ret
);
68 run_on_thread (void (*func
) (void))
70 int ret
= pthread_mutex_lock (&mutex
);
72 FAIL ("pthread_mutex_lock (%s): %d", __func__
, ret
);
74 ret
= pthread_mutex_unlock (&mutex
);
76 FAIL ("pthread_mutex_unlock (%s): %d", __func__
, ret
);
78 ret
= pthread_cond_signal (&cond_send
);
80 FAIL ("pthread_mutex_lock (%s): %d", __func__
, ret
);
82 ret
= pthread_mutex_lock (&mutex
);
84 FAIL ("pthread_mutex_lock (%s): %d", __func__
, ret
);
86 while (func_sent
!= NULL
)
88 ret
= pthread_cond_wait (&cond_recv
, &mutex
);
90 FAIL ("pthread_mutex_wait (%s): %d", __func__
, ret
);
92 ret
= pthread_mutex_unlock (&mutex
);
94 FAIL ("pthread_mutex_unlock (%s): %d", __func__
, ret
);
98 change_thread_ids (void)
100 long ret
= syscall (__NR_setresuid
, 2001, 2002, 2003);
102 FAIL ("setresuid (2001, 2002, 2003): %ld", ret
);
105 static uid_t ruid
, euid
, suid
;
108 get_thread_ids (void)
110 if (getresuid (&ruid
, &euid
, &suid
) < 0)
111 FAIL ("getresuid: %m (%d)", errno
);
115 abort_expected (int signal
__attribute__ ((unused
)))
124 int ret
= pthread_create (&thread
, NULL
, thread_func
, NULL
);
126 FAIL ("pthread_create: %d", ret
);
128 run_on_thread (change_thread_ids
);
130 signal (SIGABRT
, &abort_expected
);
131 /* This should abort the process. */
132 if (setresuid (1001, 1002, 1003) < 0)
133 FAIL ("setresuid: %m (%d)", errno
);
134 signal (SIGABRT
, SIG_DFL
);
136 /* If we get here, check that the kernel did the right thing. */
137 run_on_thread (get_thread_ids
);
138 if (ruid
!= 1001 || euid
!= 1002 || suid
!= 1003)
139 FAIL ("unexpected UIDs after setuid: %ld, %ld, %ld",
140 (long) ruid
, (long) euid
, (long) suid
);
144 #define TEST_FUNCTION do_test ()
145 #include "../test-skeleton.c"