1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * 07/2001 Ported by Wayne Boyer
5 * Copyright (c) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
6 */
7
8 /*\
9 * [Description]
10 *
11 * Verify that if mknod(2) creates a filesystem node in a directory which
12 * does not have the set-group-ID bit set, new node will not inherit the
13 * group ownership from its parent directory and its group ID will be the
14 * effective group ID of the process.
15 */
16
17 #include <pwd.h>
18 #include "tst_test.h"
19
20 #define MODE_DIR 0777
21 #define MODE1 0010777
22 #define MODE_SGID 02000
23
24 #define TEMP_DIR "testdir"
25 #define TEMP_NODE "testnode"
26
27 static struct stat buf;
28 static struct passwd *user_nobody;
29 static gid_t gid_nobody;
30
setup(void)31 static void setup(void)
32 {
33 user_nobody = SAFE_GETPWNAM("nobody");
34 gid_nobody = user_nobody->pw_gid;
35
36 SAFE_MKDIR(TEMP_DIR, MODE_DIR);
37 SAFE_CHOWN(TEMP_DIR, -1, gid_nobody);
38 }
39
run(void)40 static void run(void)
41 {
42 SAFE_CHDIR(TEMP_DIR);
43 TST_EXP_PASS(mknod(TEMP_NODE, MODE1, 0), "mknod(%s, %o, 0)", TEMP_NODE, MODE1);
44
45 SAFE_STAT(TEMP_NODE, &buf);
46 TST_EXP_EQ_LI(buf.st_gid, 0);
47
48 SAFE_UNLINK(TEMP_NODE);
49 SAFE_CHDIR("..");
50 }
51
52 static struct tst_test test = {
53 .setup = setup,
54 .test_all = run,
55 .needs_root = 1,
56 .needs_tmpdir = 1
57 };
58