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