xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setreuid/setreuid06.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  * Ported by John George
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2023 SUSE LLC Avinesh Kumar <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Verify that setreuid(2) syscall fails with EPERM errno when the calling
12*49cdfc7eSAndroid Build Coastguard Worker  * process is not privileged and a change other than
13*49cdfc7eSAndroid Build Coastguard Worker  * (i) swapping the effective user ID with the real user ID, or
14*49cdfc7eSAndroid Build Coastguard Worker  * (ii) setting one to the value of the other or
15*49cdfc7eSAndroid Build Coastguard Worker  * (iii) setting the effective user ID to the value of the saved set-user-ID
16*49cdfc7eSAndroid Build Coastguard Worker  * was specified.
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_uid.h"
22*49cdfc7eSAndroid Build Coastguard Worker #include "compat_tst_16.h"
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker static struct passwd *ltpuser;
25*49cdfc7eSAndroid Build Coastguard Worker static uid_t other_uid;
26*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)27*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
28*49cdfc7eSAndroid Build Coastguard Worker {
29*49cdfc7eSAndroid Build Coastguard Worker 	tst_get_uids(&other_uid, 0, 1);
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker 	UID16_CHECK(other_uid, setreuid);
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	ltpuser = SAFE_GETPWNAM("nobody");
34*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETUID(ltpuser->pw_uid);
35*49cdfc7eSAndroid Build Coastguard Worker }
36*49cdfc7eSAndroid Build Coastguard Worker 
run(void)37*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
38*49cdfc7eSAndroid Build Coastguard Worker {
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(SETREUID(-1, other_uid), EPERM,
41*49cdfc7eSAndroid Build Coastguard Worker 				"setreuid(%d, %d)", -1, other_uid);
42*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(SETREUID(other_uid, -1), EPERM,
43*49cdfc7eSAndroid Build Coastguard Worker 				"setreuid(%d, %d)", other_uid, -1);
44*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(SETREUID(other_uid, other_uid), EPERM,
45*49cdfc7eSAndroid Build Coastguard Worker 				"setreuid(%d, %d)", other_uid, other_uid);
46*49cdfc7eSAndroid Build Coastguard Worker }
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
49*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
50*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
51*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1
52*49cdfc7eSAndroid Build Coastguard Worker };
53