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 EPERM is set when setreuid is given an invalid user id.
     23  */
     24 
     25 #include <wait.h>
     26 #include <limits.h>
     27 #include <signal.h>
     28 #include <errno.h>
     29 #include <unistd.h>
     30 #include <pwd.h>
     31 #include <sys/param.h>
     32 #include <sys/types.h>
     33 #include <sys/stat.h>
     34 
     35 #include "test.h"
     36 #include "compat_16.h"
     37 
     38 #define INVAL_USER		 (USHRT_MAX-2)
     39 
     40 TCID_DEFINE(setreuid06);
     41 int TST_TOTAL = 1;
     42 
     43 static struct passwd *ltpuser;
     44 
     45 static void setup(void);
     46 static void cleanup(void);
     47 
     48 int main(int argc, char **argv)
     49 {
     50 	int lc;
     51 
     52 	tst_parse_opts(argc, argv, NULL, NULL);
     53 
     54 	setup();
     55 
     56 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     57 		tst_count = 0;
     58 
     59 		TEST(SETREUID(cleanup, -1, INVAL_USER));
     60 		if (TEST_RETURN != -1) {
     61 			tst_resm(TFAIL, "%s did not fail as expected", TCID);
     62 		} else if (TEST_ERRNO == EPERM) {
     63 			tst_resm(TPASS, "setreuid set errno to EPERM as "
     64 				 "expected");
     65 		} else {
     66 			tst_resm(TFAIL, "setreuid FAILED, expected 1 but "
     67 				 "returned %d", TEST_ERRNO);
     68 		}
     69 
     70 	}
     71 	cleanup();
     72 	tst_exit();
     73 }
     74 
     75 static void setup(void)
     76 {
     77 	tst_require_root();
     78 
     79 	tst_sig(FORK, DEF_HANDLER, cleanup);
     80 
     81 	umask(0);
     82 
     83 	ltpuser = getpwnam("nobody");
     84 	if (ltpuser == NULL)
     85 		tst_brkm(TBROK, NULL, "nobody must be a valid user.");
     86 
     87 	if (setuid(ltpuser->pw_uid) == -1)
     88 		tst_brkm(TBROK | TERRNO, NULL, "setuid failed to "
     89 			 "to set the effective uid to %d", ltpuser->pw_uid);
     90 
     91 	TEST_PAUSE;
     92 }
     93 
     94 static void cleanup(void)
     95 {
     96 }
     97