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 setreuid() when executed by root.
23  */
24 
25 #include <errno.h>
26 #include <pwd.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include "test.h"
31 #include "compat_16.h"
32 
33 TCID_DEFINE(setreuid02);
34 
35 static uid_t neg_one = -1;
36 static struct passwd nobody, daemonpw, root, bin;
37 
38 /*
39  * The following structure contains all test data.  Each structure in the array
40  * is used for a separate test.  The tests are executed in the for loop below.
41  */
42 
43 static struct test_data_t {
44 	uid_t *real_uid;
45 	uid_t *eff_uid;
46 	struct passwd *exp_real_usr;
47 	struct passwd *exp_eff_usr;
48 	char *test_msg;
49 } test_data[] = {
50 	{
51 	&neg_one, &neg_one, &root, &root, "After setreuid(-1, -1),"}, {
52 	&nobody.pw_uid, &neg_one, &nobody, &root, "After setreuid(nobody, -1)"},
53 	{
54 	&root.pw_uid, &neg_one, &root, &root, "After setreuid(root,-1),"}, {
55 	&neg_one, &daemonpw.pw_uid, &root, &daemonpw,
56 		    "After setreuid(-1, daemon)"}, {
57 	&neg_one, &root.pw_uid, &root, &root, "After setreuid(-1,root),"}, {
58 	&bin.pw_uid, &neg_one, &bin, &root, "After setreuid(bin, -1)"}, {
59 &root.pw_uid, &neg_one, &root, &root, "After setreuid(-1, root)"},};
60 
61 int TST_TOTAL = ARRAY_SIZE(test_data);
62 
63 static void setup(void);
64 static void cleanup(void);
65 static void uid_verify(struct passwd *ru, struct passwd *eu, char *when);
66 
main(int ac,char ** av)67 int main(int ac, char **av)
68 {
69 	int lc;
70 
71 	tst_parse_opts(ac, av, NULL, NULL);
72 
73 	setup();
74 
75 	for (lc = 0; TEST_LOOPING(lc); lc++) {
76 		int i;
77 
78 		tst_count = 0;
79 
80 		for (i = 0; i < TST_TOTAL; i++) {
81 			/* Set the real or effective user id */
82 			TEST(SETREUID(cleanup, *test_data[i].real_uid,
83 				      *test_data[i].eff_uid));
84 
85 			if (TEST_RETURN == -1) {
86 				tst_resm(TBROK, "setreuid(%d, %d) failed",
87 					 *test_data[i].real_uid,
88 					 *test_data[i].eff_uid);
89 			} else {
90 				uid_verify(test_data[i].exp_real_usr,
91 					   test_data[i].exp_eff_usr,
92 					   test_data[i].test_msg);
93 			}
94 		}
95 	}
96 	cleanup();
97 	tst_exit();
98 }
99 
setup(void)100 static void setup(void)
101 {
102 	tst_require_root();
103 
104 	tst_sig(FORK, DEF_HANDLER, cleanup);
105 
106 	if (getpwnam("nobody") == NULL)
107 		tst_brkm(TBROK, NULL, "nobody must be a valid user.");
108 
109 	if (getpwnam("daemon") == NULL)
110 		tst_brkm(TBROK, NULL, "daemon must be a valid user.");
111 
112 	if (getpwnam("bin") == NULL)
113 		tst_brkm(TBROK, NULL, "bin must be a valid user.");
114 
115 	root = *(getpwnam("root"));
116 	UID16_CHECK(root.pw_uid, setreuid, cleanup);
117 
118 	nobody = *(getpwnam("nobody"));
119 	UID16_CHECK(nobody.pw_uid, setreuid, cleanup);
120 
121 	daemonpw = *(getpwnam("daemon"));
122 	UID16_CHECK(daemonpw.pw_uid, setreuid, cleanup);
123 
124 	bin = *(getpwnam("bin"));
125 	UID16_CHECK(bin.pw_uid, setreuid, cleanup);
126 
127 	TEST_PAUSE;
128 }
129 
cleanup(void)130 static void cleanup(void)
131 {
132 }
133 
uid_verify(struct passwd * ru,struct passwd * eu,char * when)134 static void uid_verify(struct passwd *ru, struct passwd *eu, char *when)
135 {
136 	if ((getuid() != ru->pw_uid) || (geteuid() != eu->pw_uid)) {
137 		tst_resm(TFAIL, "ERROR: %s real uid = %d; effective uid = %d",
138 			 when, getuid(), geteuid());
139 		tst_resm(TFAIL, "Expected: real uid = %d; effective uid = %d",
140 			 ru->pw_uid, eu->pw_uid);
141 	} else {
142 		tst_resm(TPASS, "real or effective uid was modified as "
143 			 "expected");
144 	}
145 }
146