Home | History | Annotate | Download | only in setgid
      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  * Testcase to ensure that the setgid() system call sets errno to EPERM
     22  *
     23  * ALGORITHM
     24  *	Call setgid() to set the gid to that of root. Run this test as
     25  *	ltpuser1, and expect to get EPERM
     26  */
     27 #include <pwd.h>
     28 #include <errno.h>
     29 
     30 #include "test.h"
     31 #include "compat_16.h"
     32 
     33 TCID_DEFINE(setgid02);
     34 int TST_TOTAL = 1;
     35 
     36 static char root[] = "root";
     37 static char nobody_uid[] = "nobody";
     38 static char nobody_gid[] = "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 	struct passwd *getpwnam(), *rootpwent;
     47 	int lc;
     48 
     49 	tst_parse_opts(ac, av, NULL, NULL);
     50 
     51 	setup();
     52 
     53 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     54 		tst_count = 0;
     55 
     56 		if ((rootpwent = getpwnam(root)) == NULL) {
     57 			tst_brkm(TBROK, cleanup, "getpwnam failed for user id "
     58 				 "%s", root);
     59 		}
     60 
     61 		GID16_CHECK(rootpwent->pw_gid, setgid, cleanup);
     62 
     63 		TEST(SETGID(cleanup, rootpwent->pw_gid));
     64 
     65 		if (TEST_RETURN != -1) {
     66 			tst_resm(TFAIL, "call succeeded unexpectedly");
     67 			continue;
     68 		}
     69 
     70 		if (TEST_ERRNO != EPERM) {
     71 			tst_resm(TFAIL, "setgid set invalid errno, expected: "
     72 				 "EPERM, got: %d\n", TEST_ERRNO);
     73 		} else {
     74 			tst_resm(TPASS, "setgid returned EPERM");
     75 		}
     76 	}
     77 
     78 	cleanup();
     79 	tst_exit();
     80 }
     81 
     82 static void setup(void)
     83 {
     84 	tst_require_root();
     85 
     86 	/* Switch to nobody user for correct error code collection */
     87 	ltpuser = getpwnam(nobody_uid);
     88 	if (ltpuser == NULL)
     89 		tst_brkm(TBROK, cleanup, "getpwnam failed for user id %s",
     90 			nobody_uid);
     91 
     92 	if (setgid(ltpuser->pw_gid) == -1) {
     93 		tst_resm(TINFO, "setgid failed to "
     94 			 "to set the effective gid to %d", ltpuser->pw_gid);
     95 		perror("setgid");
     96 	}
     97 
     98 	if (setuid(ltpuser->pw_uid) == -1) {
     99 		tst_resm(TINFO, "setuid failed to "
    100 			 "to set the effective uid to %d", ltpuser->pw_uid);
    101 		perror("setuid");
    102 	}
    103 
    104 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    105 
    106 	TEST_PAUSE;
    107 }
    108 
    109 static void cleanup(void)
    110 {
    111 }
    112