xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setegid/setegid02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2014 Fujitsu Ltd.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Zeng Linggang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2022 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 setegid() fails with EPERM when the calling process is not
12*49cdfc7eSAndroid Build Coastguard Worker  * privileged and egid does not match the current real group ID,
13*49cdfc7eSAndroid Build Coastguard Worker  * current effective group ID, or current saved set-group-ID.
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker static struct passwd *ltpuser;
20*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)21*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
22*49cdfc7eSAndroid Build Coastguard Worker {
23*49cdfc7eSAndroid Build Coastguard Worker 	ltpuser = SAFE_GETPWNAM("nobody");
24*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(ltpuser->pw_uid);
25*49cdfc7eSAndroid Build Coastguard Worker }
26*49cdfc7eSAndroid Build Coastguard Worker 
setegid_verify(void)27*49cdfc7eSAndroid Build Coastguard Worker static void setegid_verify(void)
28*49cdfc7eSAndroid Build Coastguard Worker {
29*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(setegid(ltpuser->pw_gid),
30*49cdfc7eSAndroid Build Coastguard Worker 				EPERM,
31*49cdfc7eSAndroid Build Coastguard Worker 				"setegid(%d)",
32*49cdfc7eSAndroid Build Coastguard Worker 				ltpuser->pw_gid);
33*49cdfc7eSAndroid Build Coastguard Worker }
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
36*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
37*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = setegid_verify,
38*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1
39*49cdfc7eSAndroid Build Coastguard Worker };
40