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