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 *
5*49cdfc7eSAndroid Build Coastguard Worker * Ported by John George
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker /*
9*49cdfc7eSAndroid Build Coastguard Worker * Test setregid() when executed by root.
10*49cdfc7eSAndroid Build Coastguard Worker */
11*49cdfc7eSAndroid Build Coastguard Worker
12*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
13*49cdfc7eSAndroid Build Coastguard Worker #include "tst_uid.h"
14*49cdfc7eSAndroid Build Coastguard Worker #include "compat_tst_16.h"
15*49cdfc7eSAndroid Build Coastguard Worker
16*49cdfc7eSAndroid Build Coastguard Worker static gid_t first_gid, second_gid, root_gid, neg_one = -1;
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker /*
19*49cdfc7eSAndroid Build Coastguard Worker * The following structure contains all test data. Each structure in the array
20*49cdfc7eSAndroid Build Coastguard Worker * is used for a separate test. The tests are executed in the for loop below.
21*49cdfc7eSAndroid Build Coastguard Worker */
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker struct test_data_t {
24*49cdfc7eSAndroid Build Coastguard Worker gid_t *real_gid;
25*49cdfc7eSAndroid Build Coastguard Worker gid_t *eff_gid;
26*49cdfc7eSAndroid Build Coastguard Worker gid_t *exp_real_usr;
27*49cdfc7eSAndroid Build Coastguard Worker gid_t *exp_eff_usr;
28*49cdfc7eSAndroid Build Coastguard Worker const char *test_msg;
29*49cdfc7eSAndroid Build Coastguard Worker } test_data[] = {
30*49cdfc7eSAndroid Build Coastguard Worker {
31*49cdfc7eSAndroid Build Coastguard Worker &root_gid, &root_gid, &root_gid, &root_gid,
32*49cdfc7eSAndroid Build Coastguard Worker "After setregid(root, root),"}, {
33*49cdfc7eSAndroid Build Coastguard Worker &first_gid, &neg_one, &first_gid, &root_gid,
34*49cdfc7eSAndroid Build Coastguard Worker "After setregid(first, -1)"}, {
35*49cdfc7eSAndroid Build Coastguard Worker &root_gid, &neg_one, &root_gid, &root_gid,
36*49cdfc7eSAndroid Build Coastguard Worker "After setregid(root,-1),"}, {
37*49cdfc7eSAndroid Build Coastguard Worker &neg_one, &neg_one, &root_gid, &root_gid,
38*49cdfc7eSAndroid Build Coastguard Worker "After setregid(-1, -1),"}, {
39*49cdfc7eSAndroid Build Coastguard Worker &neg_one, &root_gid, &root_gid, &root_gid,
40*49cdfc7eSAndroid Build Coastguard Worker "After setregid(-1, root)"}, {
41*49cdfc7eSAndroid Build Coastguard Worker &root_gid, &neg_one, &root_gid, &root_gid,
42*49cdfc7eSAndroid Build Coastguard Worker "After setregid(root, -1),"}, {
43*49cdfc7eSAndroid Build Coastguard Worker &second_gid, &first_gid, &second_gid, &first_gid,
44*49cdfc7eSAndroid Build Coastguard Worker "After setregid(second, first)"}, {
45*49cdfc7eSAndroid Build Coastguard Worker &neg_one, &neg_one, &second_gid, &first_gid,
46*49cdfc7eSAndroid Build Coastguard Worker "After setregid(-1, -1)"}, {
47*49cdfc7eSAndroid Build Coastguard Worker &neg_one, &first_gid, &second_gid, &first_gid,
48*49cdfc7eSAndroid Build Coastguard Worker "After setregid(-1, first)"}
49*49cdfc7eSAndroid Build Coastguard Worker };
50*49cdfc7eSAndroid Build Coastguard Worker
gid_verify(gid_t rg,gid_t eg,const char * when)51*49cdfc7eSAndroid Build Coastguard Worker static void gid_verify(gid_t rg, gid_t eg, const char *when)
52*49cdfc7eSAndroid Build Coastguard Worker {
53*49cdfc7eSAndroid Build Coastguard Worker if ((getgid() != rg) || (getegid() != eg)) {
54*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "ERROR: %s real gid = %d; effective gid = %d",
55*49cdfc7eSAndroid Build Coastguard Worker when, getgid(), getegid());
56*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Expected: real gid = %d; effective gid = %d",
57*49cdfc7eSAndroid Build Coastguard Worker rg, eg);
58*49cdfc7eSAndroid Build Coastguard Worker } else {
59*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS,
60*49cdfc7eSAndroid Build Coastguard Worker "real or effective gid was modified as expected");
61*49cdfc7eSAndroid Build Coastguard Worker }
62*49cdfc7eSAndroid Build Coastguard Worker }
63*49cdfc7eSAndroid Build Coastguard Worker
run(unsigned int i)64*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int i)
65*49cdfc7eSAndroid Build Coastguard Worker {
66*49cdfc7eSAndroid Build Coastguard Worker /* Set the real or effective group id */
67*49cdfc7eSAndroid Build Coastguard Worker TEST(SETREGID(*test_data[i].real_gid, *test_data[i].eff_gid));
68*49cdfc7eSAndroid Build Coastguard Worker
69*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == -1) {
70*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "setregid(%d, %d) failed",
71*49cdfc7eSAndroid Build Coastguard Worker *test_data[i].real_gid, *test_data[i].eff_gid);
72*49cdfc7eSAndroid Build Coastguard Worker return;
73*49cdfc7eSAndroid Build Coastguard Worker }
74*49cdfc7eSAndroid Build Coastguard Worker
75*49cdfc7eSAndroid Build Coastguard Worker gid_verify(*test_data[i].exp_real_usr, *test_data[i].exp_eff_usr,
76*49cdfc7eSAndroid Build Coastguard Worker test_data[i].test_msg);
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker
setup(void)79*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
80*49cdfc7eSAndroid Build Coastguard Worker {
81*49cdfc7eSAndroid Build Coastguard Worker gid_t test_groups[3];
82*49cdfc7eSAndroid Build Coastguard Worker
83*49cdfc7eSAndroid Build Coastguard Worker root_gid = test_groups[0] = getgid();
84*49cdfc7eSAndroid Build Coastguard Worker tst_get_gids(test_groups, 1, 3);
85*49cdfc7eSAndroid Build Coastguard Worker first_gid = test_groups[1];
86*49cdfc7eSAndroid Build Coastguard Worker second_gid = test_groups[2];
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
90*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(test_data),
91*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
92*49cdfc7eSAndroid Build Coastguard Worker .test = run,
93*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
94*49cdfc7eSAndroid Build Coastguard Worker };
95