1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*\
7f08c3bdfSopenharmony_ci * [Description]
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * Verify that new directory created by mkdir(2) inherites the group ID from
10f08c3bdfSopenharmony_ci * the parent directory and S_ISGID bit, if the S_ISGID bit is set in the
11f08c3bdfSopenharmony_ci * parent directory.
12f08c3bdfSopenharmony_ci */
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include <sys/stat.h>
15f08c3bdfSopenharmony_ci#include <sys/types.h>
16f08c3bdfSopenharmony_ci#include <pwd.h>
17f08c3bdfSopenharmony_ci#include "tst_test.h"
18f08c3bdfSopenharmony_ci#include "tst_uid.h"
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#define TESTDIR1	"testdir1"
21f08c3bdfSopenharmony_ci#define TESTDIR2	"testdir1/testdir2"
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistatic gid_t free_gid;
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic void verify_mkdir(void)
26f08c3bdfSopenharmony_ci{
27f08c3bdfSopenharmony_ci	struct stat statbuf;
28f08c3bdfSopenharmony_ci	int fail = 0;
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	SAFE_MKDIR(TESTDIR2, 0777);
31f08c3bdfSopenharmony_ci	SAFE_STAT(TESTDIR2, &statbuf);
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	if (statbuf.st_gid != free_gid) {
34f08c3bdfSopenharmony_ci		tst_res(TFAIL,
35f08c3bdfSopenharmony_ci			"New dir FAILED to inherit GID: has %d, expected %d",
36f08c3bdfSopenharmony_ci			statbuf.st_gid, free_gid);
37f08c3bdfSopenharmony_ci		fail = 1;
38f08c3bdfSopenharmony_ci	}
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci	if (!(statbuf.st_mode & S_ISGID)) {
41f08c3bdfSopenharmony_ci		tst_res(TFAIL, "New dir FAILED to inherit S_ISGID");
42f08c3bdfSopenharmony_ci		fail = 1;
43f08c3bdfSopenharmony_ci	}
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	if (!fail)
46f08c3bdfSopenharmony_ci		tst_res(TPASS, "New dir inherited GID and S_ISGID");
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	SAFE_RMDIR(TESTDIR2);
49f08c3bdfSopenharmony_ci}
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_cistatic void setup(void)
53f08c3bdfSopenharmony_ci{
54f08c3bdfSopenharmony_ci	struct passwd *pw = SAFE_GETPWNAM("nobody");
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci	free_gid = tst_get_free_gid(pw->pw_gid);
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	umask(0);
59f08c3bdfSopenharmony_ci	SAFE_MKDIR(TESTDIR1, 0777);
60f08c3bdfSopenharmony_ci	SAFE_CHMOD(TESTDIR1, 0777 | S_ISGID);
61f08c3bdfSopenharmony_ci	SAFE_CHOWN(TESTDIR1, getuid(), free_gid);
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	SAFE_SETREGID(pw->pw_gid, pw->pw_gid);
64f08c3bdfSopenharmony_ci	SAFE_SETREUID(pw->pw_uid, pw->pw_uid);
65f08c3bdfSopenharmony_ci}
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_cistatic struct tst_test test = {
68f08c3bdfSopenharmony_ci	.setup = setup,
69f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
70f08c3bdfSopenharmony_ci	.needs_root = 1,
71f08c3bdfSopenharmony_ci	.test_all = verify_mkdir,
72f08c3bdfSopenharmony_ci};
73