Home | History | Annotate | Download | only in setreuid
      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 setreuid() when executed by an unpriviledged user.
     24  */
     25 
     26 #include <errno.h>
     27 #include <pwd.h>
     28 #include <stdlib.h>
     29 
     30 #include "test.h"
     31 #include "compat_16.h"
     32 
     33 #define FAILED  1
     34 
     35 TCID_DEFINE(setreuid03);
     36 
     37 static int fail = -1;
     38 static int pass;
     39 static uid_t neg_one = -1;
     40 
     41 static struct passwd nobody, bin, root;
     42 
     43 /*
     44  * The following structure contains all test data.  Each structure in the array
     45  * is used for a separate test.  The tests are executed in the for loop below.
     46  */
     47 
     48 static struct test_data_t {
     49 	uid_t *real_uid;
     50 	uid_t *eff_uid;
     51 	int *exp_ret;
     52 	struct passwd *exp_real_usr;
     53 	struct passwd *exp_eff_usr;
     54 	char *test_msg;
     55 } test_data[] = {
     56 	{
     57 	&nobody.pw_uid, &nobody.pw_uid, &pass, &nobody, &nobody,
     58 		    "After setreuid(nobody, nobody),"}, {
     59 	&neg_one, &nobody.pw_uid, &pass, &nobody, &nobody,
     60 		    "After setreuid(-1, nobody),"}, {
     61 	&nobody.pw_uid, &neg_one, &pass, &nobody, &nobody,
     62 		    "After setreuid(nobody, -1),"}, {
     63 	&neg_one, &neg_one, &pass, &nobody, &nobody, "After setreuid(-1, -1),"},
     64 	{
     65 	&neg_one, &root.pw_uid, &fail, &nobody, &nobody,
     66 		    "After setreuid(-1, root),"}, {
     67 	&root.pw_uid, &neg_one, &fail, &nobody, &nobody,
     68 		    "After setreuid(root, -1),"}, {
     69 	&root.pw_uid, &root.pw_uid, &fail, &nobody, &nobody,
     70 		    "After setreuid(root, root),"}, {
     71 	&root.pw_uid, &nobody.pw_uid, &fail, &nobody, &nobody,
     72 		    "After setreuid(root, nobody),"}, {
     73 	&root.pw_uid, &bin.pw_uid, &fail, &nobody, &nobody,
     74 		    "After setreuid(root, nobody),"}, {
     75 	&bin.pw_uid, &root.pw_uid, &fail, &nobody, &nobody,
     76 		    "After setreuid(bin, root),"}, {
     77 	&bin.pw_uid, &neg_one, &fail, &nobody, &nobody,
     78 		    "After setreuid(bin, -1),"}, {
     79 	&bin.pw_uid, &bin.pw_uid, &fail, &nobody, &nobody,
     80 		    "After setreuid(bin, bin,),"}, {
     81 	&bin.pw_uid, &nobody.pw_uid, &fail, &nobody, &nobody,
     82 		    "After setreuid(bin, nobody),"}, {
     83 	&nobody.pw_uid, &bin.pw_uid, &fail, &nobody, &nobody,
     84 		    "After setreuid(nobody, bin),"},};
     85 
     86 int TST_TOTAL = ARRAY_SIZE(test_data);
     87 
     88 static void setup(void);
     89 static void cleanup(void);
     90 static void uid_verify(struct passwd *, struct passwd *, char *);
     91 
     92 int main(int ac, char **av)
     93 {
     94 	int lc;
     95 
     96 	tst_parse_opts(ac, av, NULL, NULL);
     97 
     98 	setup();
     99 
    100 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    101 		int i;
    102 
    103 		tst_count = 0;
    104 
    105 		for (i = 0; i < TST_TOTAL; i++) {
    106 
    107 			/* Set the real or effective user id */
    108 			TEST(SETREUID(cleanup, *test_data[i].real_uid,
    109 				      *test_data[i].eff_uid));
    110 
    111 			if (TEST_RETURN == *test_data[i].exp_ret) {
    112 				if (TEST_RETURN == neg_one) {
    113 					if (TEST_ERRNO != EPERM) {
    114 						tst_resm(TFAIL,
    115 							 "setreuid(%d, %d) "
    116 							 "did not set errno "
    117 							 "value as expected.",
    118 							 *test_data[i].real_uid,
    119 							 *test_data[i].eff_uid);
    120 						continue;
    121 					}
    122 					tst_resm(TPASS, "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, "setreuid(%d, %d) "
    128 						 "succeeded as expected.",
    129 						 *test_data[i].real_uid,
    130 						 *test_data[i].eff_uid);
    131 				}
    132 			} else {
    133 				tst_resm(TFAIL, "setreuid(%d, %d) "
    134 					 "did not return as expected.",
    135 					 *test_data[i].real_uid,
    136 					 *test_data[i].eff_uid);
    137 			}
    138 
    139 			if (TEST_RETURN == -1) {
    140 			}
    141 			uid_verify(test_data[i].exp_real_usr,
    142 				   test_data[i].exp_eff_usr,
    143 				   test_data[i].test_msg);
    144 		}
    145 	}
    146 
    147 	cleanup();
    148 	tst_exit();
    149 }
    150 
    151 static void setup(void)
    152 {
    153 	tst_require_root();
    154 
    155 	tst_sig(FORK, DEF_HANDLER, cleanup);
    156 
    157 	if (getpwnam("nobody") == NULL)
    158 		tst_brkm(TBROK, NULL, "nobody must be a valid user.");
    159 
    160 	if (getpwnam("bin") == NULL)
    161 		tst_brkm(TBROK, NULL, "bin must be a valid user.");
    162 
    163 	root = *(getpwnam("root"));
    164 	UID16_CHECK(root.pw_uid, setreuid, cleanup);
    165 
    166 	nobody = *(getpwnam("nobody"));
    167 	UID16_CHECK(nobody.pw_uid, setreuid, cleanup);
    168 
    169 	bin = *(getpwnam("bin"));
    170 	UID16_CHECK(bin.pw_uid, setreuid, cleanup);
    171 
    172 	if (setuid(nobody.pw_uid) < 0)
    173 		tst_brkm(TBROK | TERRNO, NULL, "setuid() to nobody failed");
    174 
    175 	TEST_PAUSE;
    176 }
    177 
    178 static void cleanup(void)
    179 {
    180 }
    181 
    182 static void uid_verify(struct passwd *ru, struct passwd *eu, char *when)
    183 {
    184 	if ((getuid() != ru->pw_uid) || (geteuid() != eu->pw_uid)) {
    185 		tst_resm(TFAIL, "ERROR: %s real uid = %d; effective uid = %d",
    186 			 when, getuid(), geteuid());
    187 		tst_resm(TINFO, "Expected: real uid = %d; effective uid = %d",
    188 			 ru->pw_uid, eu->pw_uid);
    189 	}
    190 }
    191