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