• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) International Business Machines  Corp., 2001
3  *
4  * This program is free software;  you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12  * the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program;  if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Ported by John George
19  */
20 
21 /*
22  * Test the setreuid() feature, verifying the role of the saved-set-uid
23  * and setreuid's effect on it.
24  */
25 
26 #include <errno.h>
27 #include <pwd.h>
28 #include <stdlib.h>
29 #include <sys/wait.h>
30 
31 #include "test.h"
32 #include "compat_16.h"
33 
34 TCID_DEFINE(setreuid05);
35 
36 static int fail = -1;
37 static int pass;
38 static uid_t neg_one = -1;
39 
40 static struct passwd nobody, daemonpw, root, bin;
41 
42 struct test_data_t {
43 	uid_t *real_uid;
44 	uid_t *eff_uid;
45 	int *exp_ret;
46 	struct passwd *exp_real_usr;
47 	struct passwd *exp_eff_usr;
48 	char *test_msg;
49 } test_data[] = {
50 	{
51 	&nobody.pw_uid, &root.pw_uid, &pass, &nobody, &root, "Initially"}, {
52 	&neg_one, &nobody.pw_uid, &pass, &nobody, &nobody,
53 		    "After setreuid(-1, nobody),"}, {
54 	&neg_one, &root.pw_uid, &pass, &nobody, &root,
55 		    "After setreuid(-1, root),"}, {
56 	&daemonpw.pw_uid, &neg_one, &pass, &daemonpw, &root,
57 		    "After setreuid(daemon, -1),"}, {
58 	&neg_one, &bin.pw_uid, &pass, &daemonpw, &bin,
59 		    "After setreuid(-1, bin),"}, {
60 	&neg_one, &root.pw_uid, &fail, &daemonpw, &bin,
61 		    "After setreuid(-1, root),"}, {
62 	&neg_one, &nobody.pw_uid, &fail, &daemonpw, &bin,
63 		    "After setreuid(-1, nobody),"}, {
64 	&neg_one, &daemonpw.pw_uid, &pass, &daemonpw, &daemonpw,
65 		    "After setreuid(-1, daemon),"}, {
66 	&neg_one, &bin.pw_uid, &pass, &daemonpw, &bin,
67 		    "After setreuid(-1, bin),"}, {
68 	&bin.pw_uid, &daemonpw.pw_uid, &pass, &bin, &daemonpw,
69 		    "After setreuid(bin, daemon),"}, {
70 	&neg_one, &bin.pw_uid, &pass, &bin, &bin, "After setreuid(-1, bin),"},
71 	{
72 	&neg_one, &daemonpw.pw_uid, &pass, &bin, &daemonpw,
73 		    "After setreuid(-1, daemon),"}, {
74 	&daemonpw.pw_uid, &neg_one, &pass, &daemonpw, &daemonpw,
75 		    "After setreuid(daemon, -1),"}, {
76 	&neg_one, &bin.pw_uid, &fail, &daemonpw, &daemonpw,
77 		    "After setreuid(-1, bin),"},};
78 
79 int TST_TOTAL = ARRAY_SIZE(test_data);
80 
81 static void setup(void);
82 static void cleanup(void);
83 static void uid_verify(struct passwd *, struct passwd *, char *);
84 
main(int argc,char ** argv)85 int main(int argc, char **argv)
86 {
87 	int lc;
88 
89 	tst_parse_opts(argc, argv, NULL, NULL);
90 
91 	setup();
92 
93 	pass = 0;
94 
95 	for (lc = 0; TEST_LOOPING(lc); lc++) {
96 		int i, pid;
97 
98 		tst_count = 0;
99 
100 		if ((pid = FORK_OR_VFORK()) == -1) {
101 			tst_brkm(TBROK, cleanup, "fork failed");
102 		} else if (pid == 0) {	/* child */
103 			for (i = 0; i < TST_TOTAL; i++) {
104 				/* Set the real or effective user id */
105 				TEST(SETREUID(cleanup, *test_data[i].real_uid,
106 					      *test_data[i].eff_uid));
107 
108 				if (TEST_RETURN == *test_data[i].exp_ret) {
109 					if (TEST_RETURN == neg_one) {
110 						if (TEST_ERRNO != EPERM) {
111 							tst_resm(TFAIL,
112 								 "setreuid(%d, %d) "
113 								 "did not set errno "
114 								 "value as expected.",
115 								 *test_data
116 								 [i].real_uid,
117 								 *test_data
118 								 [i].eff_uid);
119 							continue;
120 						}
121 						tst_resm(TPASS,
122 							 "setreuid(%d, %d) "
123 							 "failed as expected.",
124 							 *test_data[i].real_uid,
125 							 *test_data[i].eff_uid);
126 					} else {
127 						tst_resm(TPASS,
128 							 "setreuid(%d, %d) "
129 							 "succeeded as expected.",
130 							 *test_data[i].real_uid,
131 							 *test_data[i].eff_uid);
132 					}
133 				} else {
134 					tst_resm(TFAIL, "setreuid(%d, %d) "
135 						 "did not return as expected.",
136 						 *test_data[i].real_uid,
137 						 *test_data[i].eff_uid);
138 				}
139 
140 				if (TEST_RETURN == -1) {
141 				}
142 				uid_verify(test_data[i].exp_real_usr,
143 					   test_data[i].exp_eff_usr,
144 					   test_data[i].test_msg);
145 			}
146 			tst_exit();
147 		} else {	/* parent */
148 			tst_record_childstatus(cleanup, pid);
149 		}
150 	}
151 	cleanup();
152 	tst_exit();
153 }
154 
setup(void)155 static void setup(void)
156 {
157 	tst_require_root();
158 
159 	tst_sig(FORK, DEF_HANDLER, cleanup);
160 
161 	if (getpwnam("nobody") == NULL)
162 		tst_brkm(TBROK, NULL, "nobody must be a valid user.");
163 
164 	if (getpwnam("daemon") == NULL)
165 		tst_brkm(TBROK, NULL, "daemon must be a valid user.");
166 
167 	if (getpwnam("bin") == NULL)
168 		tst_brkm(TBROK, NULL, "bin must be a valid user.");
169 
170 	nobody = *(getpwnam("nobody"));
171 	UID16_CHECK(nobody.pw_uid, setreuid, cleanup);
172 
173 	daemonpw = *(getpwnam("daemon"));
174 	UID16_CHECK(daemonpw.pw_uid, setreuid, cleanup);
175 
176 	root = *(getpwnam("root"));
177 	UID16_CHECK(root.pw_uid, setreuid, cleanup);
178 
179 	bin = *(getpwnam("bin"));
180 	UID16_CHECK(bin.pw_uid, setreuid, cleanup);
181 
182 	TEST_PAUSE;
183 }
184 
cleanup(void)185 static void cleanup(void)
186 {
187 }
188 
uid_verify(struct passwd * ru,struct passwd * eu,char * when)189 static void uid_verify(struct passwd *ru, struct passwd *eu, char *when)
190 {
191 	if ((getuid() != ru->pw_uid) || (geteuid() != eu->pw_uid)) {
192 		tst_resm(TFAIL, "ERROR: %s real uid = %d; effective uid = %d",
193 			 when, getuid(), geteuid());
194 		tst_resm(TINFO, "Expected: real uid = %d; effective uid = %d",
195 			 ru->pw_uid, eu->pw_uid);
196 	}
197 }
198