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