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