xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mknod/mknod02.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) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  *     07/2001 Ported by Wayne Boyer
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 if mknod(2) creates a filesystem node in a directory which
12*49cdfc7eSAndroid Build Coastguard Worker  * does not have the set-group-ID bit set, new node will not inherit the
13*49cdfc7eSAndroid Build Coastguard Worker  * group ownership from its parent directory and its group ID will be the
14*49cdfc7eSAndroid Build Coastguard Worker  * effective group ID of the process.
15*49cdfc7eSAndroid Build Coastguard Worker  */
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #define MODE_DIR 0777
21*49cdfc7eSAndroid Build Coastguard Worker #define MODE1	0010777
22*49cdfc7eSAndroid Build Coastguard Worker #define MODE_SGID	02000
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #define TEMP_DIR "testdir"
25*49cdfc7eSAndroid Build Coastguard Worker #define TEMP_NODE "testnode"
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker static struct stat buf;
28*49cdfc7eSAndroid Build Coastguard Worker static struct passwd *user_nobody;
29*49cdfc7eSAndroid Build Coastguard Worker static gid_t gid_nobody;
30*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)31*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
32*49cdfc7eSAndroid Build Coastguard Worker {
33*49cdfc7eSAndroid Build Coastguard Worker 	user_nobody = SAFE_GETPWNAM("nobody");
34*49cdfc7eSAndroid Build Coastguard Worker 	gid_nobody = user_nobody->pw_gid;
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(TEMP_DIR, MODE_DIR);
37*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHOWN(TEMP_DIR, -1, gid_nobody);
38*49cdfc7eSAndroid Build Coastguard Worker }
39*49cdfc7eSAndroid Build Coastguard Worker 
run(void)40*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHDIR(TEMP_DIR);
43*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(mknod(TEMP_NODE, MODE1, 0), "mknod(%s, %o, 0)", TEMP_NODE, MODE1);
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(TEMP_NODE, &buf);
46*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(buf.st_gid, 0);
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_UNLINK(TEMP_NODE);
49*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHDIR("..");
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
53*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
54*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
55*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
56*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1
57*49cdfc7eSAndroid Build Coastguard Worker };
58