Home | History | Annotate | Download | only in setfsgid
      1 /*
      2  * Copyright (C) International Business Machines  Corp., 2001
      3  * Ported by Wayne Boyer
      4  * Adapted by Dustin Kirkland (k1rkland (at) us.ibm.com)
      5  *
      6  * This program is free software;  you can redistribute it and/or modify
      7  * it under the terms of the GNU General Public License as published by
      8  * the Free Software Foundation; either version 2 of the License, or
      9  * (at your option) any later version.
     10  *
     11  * This program is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     14  * the GNU General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU General Public License
     17  * along with this program;  if not, write to the Free Software
     18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     19  */
     20 
     21 /*
     22  * Testcase to check the basic functionality of setfsgid(2) system
     23  * call fails when called by a non-root user.
     24  */
     25 
     26 #include <stdio.h>
     27 #include <unistd.h>
     28 #include <sys/types.h>
     29 #include <errno.h>
     30 #include <pwd.h>
     31 #include <grp.h>
     32 
     33 #include "test.h"
     34 #include "compat_16.h"
     35 
     36 TCID_DEFINE(setfsgid03);
     37 int TST_TOTAL = 1;
     38 
     39 static char nobody_uid[] = "nobody";
     40 static struct passwd *ltpuser;
     41 
     42 static void setup(void);
     43 static void cleanup(void);
     44 
     45 int main(int ac, char **av)
     46 {
     47 	int lc;
     48 
     49 	gid_t gid;
     50 
     51 	tst_parse_opts(ac, av, NULL, NULL);
     52 
     53 	setup();
     54 
     55 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     56 		tst_count = 0;
     57 
     58 		gid = 1;
     59 		while (!getgrgid(gid))
     60 			gid++;
     61 
     62 		GID16_CHECK(gid, setfsgid, cleanup);
     63 
     64 		TEST(SETFSGID(cleanup, gid));
     65 
     66 		if (TEST_RETURN == -1) {
     67 			tst_resm(TFAIL | TTERRNO,
     68 				"setfsgid() failed unexpectedly");
     69 			continue;
     70 		}
     71 
     72 		if (TEST_RETURN == gid) {
     73 			tst_resm(TFAIL,
     74 				 "setfsgid() returned %ld, expected anything but %d",
     75 				 TEST_RETURN, gid);
     76 		} else {
     77 			tst_resm(TPASS, "setfsgid() returned expected value : "
     78 				 "%ld", TEST_RETURN);
     79 		}
     80 	}
     81 
     82 	cleanup();
     83 	tst_exit();
     84 }
     85 
     86 static void setup(void)
     87 {
     88 	tst_require_root();
     89 
     90 	ltpuser = getpwnam(nobody_uid);
     91 	if (ltpuser == NULL)
     92 		tst_brkm(TBROK, cleanup, "getpwnam failed for user id %s",
     93 			nobody_uid);
     94 
     95 	if (setuid(ltpuser->pw_uid) == -1)
     96 		tst_resm(TINFO | TERRNO,
     97 			"setuid failed to set the effective uid to %d",
     98 			ltpuser->pw_uid);
     99 
    100 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    101 
    102 	TEST_PAUSE;
    103 }
    104 
    105 static void cleanup(void)
    106 {
    107 }
    108