1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (C) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci * Ported by Wayne Boyer
5f08c3bdfSopenharmony_ci * Adapted by Dustin Kirkland <k1rkland@us.ibm.com>
6f08c3bdfSopenharmony_ci * Adapted by Zhao gongyi <zhaogongyi@huawei.com>
7f08c3bdfSopenharmony_ci */
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_ci/*\
10f08c3bdfSopenharmony_ci * [Description]
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * Testcase for setfsgid() syscall to check that
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * - privileged user can change a filesystem group ID different from saved
15f08c3bdfSopenharmony_ci *  value of previous setfsgid() call
16f08c3bdfSopenharmony_ci * - unprivileged user cannot change it
17f08c3bdfSopenharmony_ci */
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci#include <pwd.h>
20f08c3bdfSopenharmony_ci#include "tst_test.h"
21f08c3bdfSopenharmony_ci#include "compat_tst_16.h"
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistatic gid_t gid;
24f08c3bdfSopenharmony_cistatic gid_t pre_gid;
25f08c3bdfSopenharmony_cistatic const char nobody_uid[] = "nobody";
26f08c3bdfSopenharmony_cistatic struct passwd *ltpuser;
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_cistatic void run(unsigned int i)
29f08c3bdfSopenharmony_ci{
30f08c3bdfSopenharmony_ci	int cnt;
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci	GID16_CHECK(gid, setfsgid);
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	if (i == 0) {
35f08c3bdfSopenharmony_ci		ltpuser = SAFE_GETPWNAM(nobody_uid);
36f08c3bdfSopenharmony_ci		SAFE_SETEUID(ltpuser->pw_uid);
37f08c3bdfSopenharmony_ci	}
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	/*
40f08c3bdfSopenharmony_ci	 * Run SETFSGID() twice to check the second running have changed
41f08c3bdfSopenharmony_ci	 * the gid for privileged user, and have not changed the gid
42f08c3bdfSopenharmony_ci	 * for unprivileged user.
43f08c3bdfSopenharmony_ci	 */
44f08c3bdfSopenharmony_ci	for (cnt = 0; cnt < 2; cnt++) {
45f08c3bdfSopenharmony_ci		TEST(SETFSGID(gid));
46f08c3bdfSopenharmony_ci		if ((long)pre_gid != TST_RET) {
47f08c3bdfSopenharmony_ci			tst_res(TFAIL, "EUID %d: setfsgid() returned %ld", geteuid(), TST_RET);
48f08c3bdfSopenharmony_ci		} else {
49f08c3bdfSopenharmony_ci			tst_res(TPASS, "EUID %d: setfsgid() returned expected value: %ld",
50f08c3bdfSopenharmony_ci				geteuid(), TST_RET);
51f08c3bdfSopenharmony_ci			if (i == 1) {
52f08c3bdfSopenharmony_ci				pre_gid = gid;
53f08c3bdfSopenharmony_ci				gid++;
54f08c3bdfSopenharmony_ci			}
55f08c3bdfSopenharmony_ci		}
56f08c3bdfSopenharmony_ci	}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	if (i == 0)
59f08c3bdfSopenharmony_ci		SAFE_SETEUID(0);
60f08c3bdfSopenharmony_ci}
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_cistatic void setup(void)
63f08c3bdfSopenharmony_ci{
64f08c3bdfSopenharmony_ci	pre_gid = 0;
65f08c3bdfSopenharmony_ci	gid = 1;
66f08c3bdfSopenharmony_ci}
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_cistatic struct tst_test test = {
69f08c3bdfSopenharmony_ci	.needs_root = 1,
70f08c3bdfSopenharmony_ci	.setup = setup,
71f08c3bdfSopenharmony_ci	.test = run,
72f08c3bdfSopenharmony_ci	.tcnt = 2,
73f08c3bdfSopenharmony_ci};
74