• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2001
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  *   Ported by John George
20  */
21 
22 /*
23  * Test that setregid() fails and sets the proper errno values when a
24  * non-root user attemps to change the real or effective group id to a
25  * value other than the current gid or the current effective gid.
26  */
27 
28 #include <errno.h>
29 #include <pwd.h>
30 #include <grp.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include "test.h"
35 #include "compat_16.h"
36 
37 TCID_DEFINE(setregid02);
38 
39 static gid_t neg_one = -1;
40 
41 static struct passwd *ltpuser;
42 
43 static struct group ltpgroup, root, bin;
44 
45 /*
46  * The following structure contains all test data.  Each structure in the array
47  * is used for a separate test.  The tests are executed in the for loop below.
48  */
49 
50 struct test_data_t {
51 	gid_t *real_gid;
52 	gid_t *eff_gid;
53 	int exp_errno;
54 	struct group *exp_real_usr;
55 	struct group *exp_eff_usr;
56 	char *test_msg;
57 } test_data[] = {
58 	{
59 	&neg_one, &root.gr_gid, EPERM, &ltpgroup, &ltpgroup,
60 		    "After setregid(-1, root),"}, {
61 	&neg_one, &bin.gr_gid, EPERM, &ltpgroup, &ltpgroup,
62 		    "After setregid(-1, bin)"}, {
63 	&root.gr_gid, &neg_one, EPERM, &ltpgroup, &ltpgroup,
64 		    "After setregid(root,-1),"}, {
65 	&bin.gr_gid, &neg_one, EPERM, &ltpgroup, &ltpgroup,
66 		    "After setregid(bin, -1),"}, {
67 	&root.gr_gid, &bin.gr_gid, EPERM, &ltpgroup, &ltpgroup,
68 		    "After setregid(root, bin)"}, {
69 	&bin.gr_gid, &root.gr_gid, EPERM, &ltpgroup, &ltpgroup,
70 		    "After setregid(bin, root),"}
71 };
72 
73 int TST_TOTAL = ARRAY_SIZE(test_data);
74 
75 static void setup(void);
76 static void gid_verify(struct group *ru, struct group *eu, char *when);
77 static struct group get_group_by_name(const char *name);
78 static struct group get_group_by_gid(gid_t gid);
79 
main(int ac,char ** av)80 int main(int ac, char **av)
81 {
82 	int lc;
83 
84 	tst_parse_opts(ac, av, NULL, NULL);
85 
86 	setup();
87 
88 	for (lc = 0; TEST_LOOPING(lc); lc++) {
89 		int i;
90 
91 		tst_count = 0;
92 
93 		for (i = 0; i < TST_TOTAL; i++) {
94 			/* Set the real or effective group id */
95 			TEST(SETREGID(NULL, *test_data[i].real_gid,
96 				      *test_data[i].eff_gid));
97 
98 			if (TEST_RETURN == -1) {
99 				if (TEST_ERRNO == test_data[i].exp_errno) {
100 					tst_resm(TPASS, "setregid(%d, %d) "
101 						 "failed as expected.",
102 						 *test_data[i].real_gid,
103 						 *test_data[i].eff_gid);
104 				} else {
105 					tst_resm(TFAIL, "setregid(%d, %d) "
106 						 "failed (%d) but did not set the "
107 						 "expected errno (%d).",
108 						 *test_data[i].real_gid,
109 						 *test_data[i].eff_gid,
110 						 TEST_ERRNO,
111 						 test_data[i].exp_errno);
112 				}
113 			} else {
114 				tst_resm(TFAIL, "setregid(%d, %d) "
115 					 "did not fail (ret: %ld) as expected (ret: -1).",
116 					 *test_data[i].real_gid,
117 					 *test_data[i].eff_gid, TEST_RETURN);
118 			}
119 			gid_verify(test_data[i].exp_real_usr,
120 				   test_data[i].exp_eff_usr,
121 				   test_data[i].test_msg);
122 		}
123 	}
124 
125 	tst_exit();
126 }
127 
setup(void)128 static void setup(void)
129 {
130 	tst_require_root();
131 
132 	tst_sig(FORK, DEF_HANDLER, NULL);
133 
134 	ltpuser = getpwnam("nobody");
135 	if (ltpuser == NULL)
136 		tst_brkm(TBROK, NULL, "getpwnam(\"nobody\") failed");
137 
138 	if (setgid(ltpuser->pw_gid) == -1) {
139 		tst_brkm(TBROK | TERRNO, NULL,
140 			 "setgid failed to set the effective gid to %d",
141 			 ltpuser->pw_gid);
142 	}
143 	if (setuid(ltpuser->pw_uid) == -1) {
144 		tst_brkm(TBROK | TERRNO, NULL,
145 			 "setuid failed to to set the effective uid to %d",
146 			 ltpuser->pw_uid);
147 	}
148 
149 	root = get_group_by_name("root");
150 	ltpgroup = get_group_by_gid(ltpuser->pw_gid);
151 	bin = get_group_by_name("bin");
152 
153 	TEST_PAUSE;
154 }
155 
get_group_by_name(const char * name)156 static struct group get_group_by_name(const char *name)
157 {
158 	struct group *ret = getgrnam(name);
159 
160 	if (ret == NULL)
161 		tst_brkm(TBROK|TERRNO, NULL, "getgrnam(\"%s\") failed", name);
162 
163 	GID16_CHECK(ret->gr_gid, setregid, NULL);
164 
165 	return *ret;
166 }
167 
get_group_by_gid(gid_t gid)168 static struct group get_group_by_gid(gid_t gid)
169 {
170 	struct group *ret = getgrgid(gid);
171 
172 	if (ret == NULL)
173 		tst_brkm(TBROK|TERRNO, NULL, "getgrgid(\"%d\") failed", gid);
174 
175 	GID16_CHECK(ret->gr_gid, setregid, NULL);
176 
177 	return *ret;
178 }
179 
gid_verify(struct group * rg,struct group * eg,char * when)180 void gid_verify(struct group *rg, struct group *eg, char *when)
181 {
182 	if ((getgid() != rg->gr_gid) || (getegid() != eg->gr_gid)) {
183 		tst_resm(TFAIL, "ERROR: %s real gid = %d; effective gid = %d",
184 			 when, getgid(), getegid());
185 		tst_resm(TINFO, "Expected: real gid = %d; effective gid = %d",
186 			 rg->gr_gid, eg->gr_gid);
187 	} else {
188 		tst_resm(TPASS, "real or effective gid was modified as expected");
189 	}
190 }
191