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