Home | History | Annotate | Download | only in setuid
      1 /*
      2  * Copyright (c) International Business Machines  Corp., 2001
      3  *  Ported by Wayne Boyer
      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 
     20 /*
     21  * Test to check the error and trivial conditions in setuid
     22  */
     23 
     24 #include <errno.h>
     25 #include <pwd.h>
     26 #include <sys/types.h>
     27 #include <sys/stat.h>
     28 #include <unistd.h>
     29 
     30 #include "test.h"
     31 #include "compat_16.h"
     32 
     33 #define ROOT_USER	0
     34 
     35 char *TCID = "setuid03";
     36 int TST_TOTAL = 1;
     37 
     38 static char nobody_uid[] = "nobody";
     39 static struct passwd *ltpuser;
     40 
     41 static void setup(void);
     42 static void cleanup(void);
     43 
     44 int main(int ac, char **av)
     45 {
     46 	int lc;
     47 
     48 	tst_parse_opts(ac, av, NULL, NULL);
     49 
     50 	setup();
     51 
     52 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     53 		tst_count = 0;
     54 
     55 		TEST(SETUID(cleanup, ROOT_USER));
     56 
     57 		if (TEST_RETURN != -1) {
     58 			tst_resm(TFAIL, "call succeeded unexpectedly");
     59 			continue;
     60 		}
     61 
     62 		if (TEST_ERRNO == EPERM) {
     63 			tst_resm(TPASS, "setuid returned errno EPERM");
     64 		} else {
     65 			tst_resm(TFAIL, "setuid returned unexpected errno - %d",
     66 				 TEST_ERRNO);
     67 		}
     68 	}
     69 
     70 	cleanup();
     71 	tst_exit();
     72 }
     73 
     74 static void setup(void)
     75 {
     76 	tst_require_root();
     77 
     78 	/* Switch to nobody user for correct error code collection */
     79 	ltpuser = getpwnam(nobody_uid);
     80 	if (ltpuser == NULL)
     81 		tst_brkm(TBROK, cleanup, "getpwnam failed for user id %s",
     82 			nobody_uid);
     83 
     84 	if (setuid(ltpuser->pw_uid) == -1) {
     85 		tst_resm(TINFO, "setuid failed to "
     86 			 "to set the effective uid to %d", ltpuser->pw_uid);
     87 		perror("setuid");
     88 	}
     89 
     90 	tst_sig(FORK, DEF_HANDLER, cleanup);
     91 
     92 	umask(0);
     93 
     94 	TEST_PAUSE;
     95 }
     96 
     97 static void cleanup(void)
     98 {
     99 }
    100