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