xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setfsgid/setfsgid01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2001
4  * Ported by Wayne Boyer
5  * Copyright (c) 2022 SUSE LLC Avinesh Kumar <[email protected]>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Verify that setfsgid() correctly updates the filesystem group ID
12  * to the value given in fsgid argument.
13  */
14 
15 #include <pwd.h>
16 #include <unistd.h>
17 
18 #include "tst_test.h"
19 #include "compat_tst_16.h"
20 
21 static gid_t nobody_gid;
22 
setup(void)23 static void setup(void)
24 {
25 	struct passwd *nobody;
26 
27 	nobody = SAFE_GETPWNAM("nobody");
28 	nobody_gid = nobody->pw_gid;
29 }
30 
run(void)31 static void run(void)
32 {
33 	gid_t gid;
34 
35 	gid = getegid();
36 	GID16_CHECK(gid, setfsgid);
37 
38 	SAFE_SETEUID(0);
39 	TST_EXP_VAL(SETFSGID(nobody_gid), gid);
40 	TST_EXP_VAL(SETFSGID(-1), nobody_gid);
41 	TST_EXP_VAL_SILENT(SETFSGID(gid), nobody_gid);
42 }
43 
44 static struct tst_test test = {
45 	.setup = setup,
46 	.test_all = run,
47 	.needs_root = 1
48 };
49