xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/getgroups/getgroups03.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  * Copyright (c) Cyril Hrubis <[email protected]> 2013
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  * Test Description:
23*49cdfc7eSAndroid Build Coastguard Worker  *  Verify that, getgroups() system call gets the supplementary group IDs
24*49cdfc7eSAndroid Build Coastguard Worker  *  of the calling process.
25*49cdfc7eSAndroid Build Coastguard Worker  *
26*49cdfc7eSAndroid Build Coastguard Worker  * Expected Result:
27*49cdfc7eSAndroid Build Coastguard Worker  *  The call succeeds in getting all the supplementary group IDs of the
28*49cdfc7eSAndroid Build Coastguard Worker  *  calling process. The effective group ID may or may not be returned.
29*49cdfc7eSAndroid Build Coastguard Worker  */
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <grp.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <sys/param.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker /*
45*49cdfc7eSAndroid Build Coastguard Worker  * Don't forget to remove USE_LEGACY_COMPAT_16_H from Makefile after
46*49cdfc7eSAndroid Build Coastguard Worker  * rewriting all tests to the new API.
47*49cdfc7eSAndroid Build Coastguard Worker  */
48*49cdfc7eSAndroid Build Coastguard Worker #include "compat_16.h"
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker #define TESTUSER "root"
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker TCID_DEFINE(getgroups03);
53*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker static int ngroups;
56*49cdfc7eSAndroid Build Coastguard Worker static GID_T groups_list[NGROUPS];
57*49cdfc7eSAndroid Build Coastguard Worker static GID_T groups[NGROUPS];
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker static void verify_groups(int ret_ngroups);
60*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
61*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
62*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)63*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
64*49cdfc7eSAndroid Build Coastguard Worker {
65*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
66*49cdfc7eSAndroid Build Coastguard Worker 	int gidsetsize = NGROUPS;
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker 	setup();
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker 		TEST(GETGROUPS(cleanup, gidsetsize, groups_list));
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN == -1) {
79*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL | TTERRNO, "getgroups failed");
80*49cdfc7eSAndroid Build Coastguard Worker 			continue;
81*49cdfc7eSAndroid Build Coastguard Worker 		}
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 		verify_groups(TEST_RETURN);
84*49cdfc7eSAndroid Build Coastguard Worker 	}
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
87*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
88*49cdfc7eSAndroid Build Coastguard Worker }
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker /*
91*49cdfc7eSAndroid Build Coastguard Worker  * readgroups(GID_T *)  - Read supplimentary group ids of "root" user
92*49cdfc7eSAndroid Build Coastguard Worker  * Scans the /etc/group file to get IDs of all the groups to which TESTUSER
93*49cdfc7eSAndroid Build Coastguard Worker  * belongs and puts them into the array passed.
94*49cdfc7eSAndroid Build Coastguard Worker  * Returns the no of gids read.
95*49cdfc7eSAndroid Build Coastguard Worker  */
readgroups(GID_T groups[NGROUPS])96*49cdfc7eSAndroid Build Coastguard Worker static int readgroups(GID_T groups[NGROUPS])
97*49cdfc7eSAndroid Build Coastguard Worker {
98*49cdfc7eSAndroid Build Coastguard Worker 	struct group *grp;
99*49cdfc7eSAndroid Build Coastguard Worker 	int ngrps = 0;
100*49cdfc7eSAndroid Build Coastguard Worker 	int i;
101*49cdfc7eSAndroid Build Coastguard Worker 	int found;
102*49cdfc7eSAndroid Build Coastguard Worker 	GID_T g;
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker 	setgrent();
105*49cdfc7eSAndroid Build Coastguard Worker 
106*49cdfc7eSAndroid Build Coastguard Worker 	while ((grp = getgrent()) != 0) {
107*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; grp->gr_mem[i]; i++) {
108*49cdfc7eSAndroid Build Coastguard Worker 			if (strcmp(grp->gr_mem[i], TESTUSER) == 0) {
109*49cdfc7eSAndroid Build Coastguard Worker 				groups[ngrps++] = grp->gr_gid;
110*49cdfc7eSAndroid Build Coastguard Worker 			}
111*49cdfc7eSAndroid Build Coastguard Worker 		}
112*49cdfc7eSAndroid Build Coastguard Worker 	}
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 	/* The getgroups specification says:
115*49cdfc7eSAndroid Build Coastguard Worker 	   It is unspecified whether the effective group ID of the
116*49cdfc7eSAndroid Build Coastguard Worker 	   calling process is included in the returned list.  (Thus,
117*49cdfc7eSAndroid Build Coastguard Worker 	   an application should also call getegid(2) and add or
118*49cdfc7eSAndroid Build Coastguard Worker 	   remove the resulting value.).  So, add the value here if
119*49cdfc7eSAndroid Build Coastguard Worker 	   it's not in.  */
120*49cdfc7eSAndroid Build Coastguard Worker 
121*49cdfc7eSAndroid Build Coastguard Worker 	found = 0;
122*49cdfc7eSAndroid Build Coastguard Worker 	g = getegid();
123*49cdfc7eSAndroid Build Coastguard Worker 
124*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < ngrps; i++) {
125*49cdfc7eSAndroid Build Coastguard Worker 		if (groups[i] == g)
126*49cdfc7eSAndroid Build Coastguard Worker 			found = 1;
127*49cdfc7eSAndroid Build Coastguard Worker 	}
128*49cdfc7eSAndroid Build Coastguard Worker 	if (found == 0)
129*49cdfc7eSAndroid Build Coastguard Worker 		groups[ngrps++] = g;
130*49cdfc7eSAndroid Build Coastguard Worker 
131*49cdfc7eSAndroid Build Coastguard Worker 	endgrent();
132*49cdfc7eSAndroid Build Coastguard Worker 	return ngrps;
133*49cdfc7eSAndroid Build Coastguard Worker }
134*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)135*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
136*49cdfc7eSAndroid Build Coastguard Worker {
137*49cdfc7eSAndroid Build Coastguard Worker 	tst_require_root();
138*49cdfc7eSAndroid Build Coastguard Worker 
139*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
140*49cdfc7eSAndroid Build Coastguard Worker 
141*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
142*49cdfc7eSAndroid Build Coastguard Worker 
143*49cdfc7eSAndroid Build Coastguard Worker 	/*
144*49cdfc7eSAndroid Build Coastguard Worker 	 * Get the IDs of all the groups of "root"
145*49cdfc7eSAndroid Build Coastguard Worker 	 * from /etc/group file
146*49cdfc7eSAndroid Build Coastguard Worker 	 */
147*49cdfc7eSAndroid Build Coastguard Worker 	ngroups = readgroups(groups);
148*49cdfc7eSAndroid Build Coastguard Worker 
149*49cdfc7eSAndroid Build Coastguard Worker 	/* Setgroups is called by the login(1) process
150*49cdfc7eSAndroid Build Coastguard Worker 	 * if the testcase is executed via an ssh session this
151*49cdfc7eSAndroid Build Coastguard Worker 	 * testcase will fail. So execute setgroups() before executing
152*49cdfc7eSAndroid Build Coastguard Worker 	 * getgroups()
153*49cdfc7eSAndroid Build Coastguard Worker 	 */
154*49cdfc7eSAndroid Build Coastguard Worker 	if (SETGROUPS(cleanup, ngroups, groups) == -1)
155*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "setgroups failed");
156*49cdfc7eSAndroid Build Coastguard Worker }
157*49cdfc7eSAndroid Build Coastguard Worker 
158*49cdfc7eSAndroid Build Coastguard Worker /*
159*49cdfc7eSAndroid Build Coastguard Worker  * verify_groups(int)  - Verify supplimentary group id values.
160*49cdfc7eSAndroid Build Coastguard Worker  *   This function verifies the gid values returned by getgroups() with
161*49cdfc7eSAndroid Build Coastguard Worker  *   the read values from /etc/group file.
162*49cdfc7eSAndroid Build Coastguard Worker  *  This function returns flag value which indicates success or failure
163*49cdfc7eSAndroid Build Coastguard Worker  *  of verification.
164*49cdfc7eSAndroid Build Coastguard Worker  */
verify_groups(int ret_ngroups)165*49cdfc7eSAndroid Build Coastguard Worker static void verify_groups(int ret_ngroups)
166*49cdfc7eSAndroid Build Coastguard Worker {
167*49cdfc7eSAndroid Build Coastguard Worker 	int i, j;
168*49cdfc7eSAndroid Build Coastguard Worker 	GID_T egid;
169*49cdfc7eSAndroid Build Coastguard Worker 	int egid_flag = 1;
170*49cdfc7eSAndroid Build Coastguard Worker 	int fflag = 1;
171*49cdfc7eSAndroid Build Coastguard Worker 
172*49cdfc7eSAndroid Build Coastguard Worker 	/*
173*49cdfc7eSAndroid Build Coastguard Worker 	 * Loop through the array to verify the gids
174*49cdfc7eSAndroid Build Coastguard Worker 	 * returned by getgroups().
175*49cdfc7eSAndroid Build Coastguard Worker 	 * First, compare each element of the array
176*49cdfc7eSAndroid Build Coastguard Worker 	 * returned by getgroups() with that read from
177*49cdfc7eSAndroid Build Coastguard Worker 	 * group file.
178*49cdfc7eSAndroid Build Coastguard Worker 	 */
179*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < ret_ngroups; i++) {
180*49cdfc7eSAndroid Build Coastguard Worker 		for (j = 0; j < ngroups; j++) {
181*49cdfc7eSAndroid Build Coastguard Worker 			if (groups_list[i] != groups[j]) {
182*49cdfc7eSAndroid Build Coastguard Worker 				/* If loop ends and gids are not matching */
183*49cdfc7eSAndroid Build Coastguard Worker 				if (j == ngroups - 1) {
184*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TFAIL, "getgroups returned "
185*49cdfc7eSAndroid Build Coastguard Worker 						 "incorrect gid %d",
186*49cdfc7eSAndroid Build Coastguard Worker 						 groups_list[i]);
187*49cdfc7eSAndroid Build Coastguard Worker 					fflag = 0;
188*49cdfc7eSAndroid Build Coastguard Worker 				} else {
189*49cdfc7eSAndroid Build Coastguard Worker 					continue;
190*49cdfc7eSAndroid Build Coastguard Worker 				}
191*49cdfc7eSAndroid Build Coastguard Worker 			} else {
192*49cdfc7eSAndroid Build Coastguard Worker 				break;
193*49cdfc7eSAndroid Build Coastguard Worker 			}
194*49cdfc7eSAndroid Build Coastguard Worker 		}
195*49cdfc7eSAndroid Build Coastguard Worker 	}
196*49cdfc7eSAndroid Build Coastguard Worker 
197*49cdfc7eSAndroid Build Coastguard Worker 	/* Now do the reverse comparison */
198*49cdfc7eSAndroid Build Coastguard Worker 	egid = getegid();
199*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < ngroups; i++) {
200*49cdfc7eSAndroid Build Coastguard Worker 		for (j = 0; j < ret_ngroups; j++) {
201*49cdfc7eSAndroid Build Coastguard Worker 			if (groups[i] != groups_list[j]) {
202*49cdfc7eSAndroid Build Coastguard Worker 				/*
203*49cdfc7eSAndroid Build Coastguard Worker 				 * If the loop ends & gids are not matching
204*49cdfc7eSAndroid Build Coastguard Worker 				 * if gid is not egid, exit with error
205*49cdfc7eSAndroid Build Coastguard Worker 				 * else egid is returned by getgroups()
206*49cdfc7eSAndroid Build Coastguard Worker 				 */
207*49cdfc7eSAndroid Build Coastguard Worker 				if (j == (ret_ngroups - 1)) {
208*49cdfc7eSAndroid Build Coastguard Worker 					if (groups[i] != egid) {
209*49cdfc7eSAndroid Build Coastguard Worker 						tst_resm(TFAIL, "getgroups "
210*49cdfc7eSAndroid Build Coastguard Worker 							 "didn't return %d one "
211*49cdfc7eSAndroid Build Coastguard Worker 							 "of the gids of %s",
212*49cdfc7eSAndroid Build Coastguard Worker 							 groups[i], TESTUSER);
213*49cdfc7eSAndroid Build Coastguard Worker 						fflag = 0;
214*49cdfc7eSAndroid Build Coastguard Worker 					} else {
215*49cdfc7eSAndroid Build Coastguard Worker 						/*
216*49cdfc7eSAndroid Build Coastguard Worker 						 * egid is not present in
217*49cdfc7eSAndroid Build Coastguard Worker 						 * group_list.
218*49cdfc7eSAndroid Build Coastguard Worker 						 * Reset the egid flag
219*49cdfc7eSAndroid Build Coastguard Worker 						 */
220*49cdfc7eSAndroid Build Coastguard Worker 						egid_flag = 0;
221*49cdfc7eSAndroid Build Coastguard Worker 					}
222*49cdfc7eSAndroid Build Coastguard Worker 				}
223*49cdfc7eSAndroid Build Coastguard Worker 			} else {
224*49cdfc7eSAndroid Build Coastguard Worker 				break;
225*49cdfc7eSAndroid Build Coastguard Worker 			}
226*49cdfc7eSAndroid Build Coastguard Worker 		}
227*49cdfc7eSAndroid Build Coastguard Worker 	}
228*49cdfc7eSAndroid Build Coastguard Worker 
229*49cdfc7eSAndroid Build Coastguard Worker 	/*
230*49cdfc7eSAndroid Build Coastguard Worker 	 * getgroups() should return the no. of gids for TESTUSER with
231*49cdfc7eSAndroid Build Coastguard Worker 	 * or without egid taken into account.
232*49cdfc7eSAndroid Build Coastguard Worker 	 * Decrement ngroups, if egid is not returned by getgroups()
233*49cdfc7eSAndroid Build Coastguard Worker 	 * Now, if ngroups matches ret_val, as above comparisons of the array
234*49cdfc7eSAndroid Build Coastguard Worker 	 * are successful, this implies that the array contents match.
235*49cdfc7eSAndroid Build Coastguard Worker 	 */
236*49cdfc7eSAndroid Build Coastguard Worker 	if (egid_flag == 0)
237*49cdfc7eSAndroid Build Coastguard Worker 		ngroups--;
238*49cdfc7eSAndroid Build Coastguard Worker 	if (ngroups != ret_ngroups) {
239*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL,
240*49cdfc7eSAndroid Build Coastguard Worker 			 "getgroups(2) returned incorrect no. of gids %d "
241*49cdfc7eSAndroid Build Coastguard Worker 			 "(expected %d)", ret_ngroups, ngroups);
242*49cdfc7eSAndroid Build Coastguard Worker 		fflag = 0;
243*49cdfc7eSAndroid Build Coastguard Worker 	}
244*49cdfc7eSAndroid Build Coastguard Worker 
245*49cdfc7eSAndroid Build Coastguard Worker 	if (fflag)
246*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TPASS, "getgroups functionality correct");
247*49cdfc7eSAndroid Build Coastguard Worker }
248*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)249*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
250*49cdfc7eSAndroid Build Coastguard Worker {
251*49cdfc7eSAndroid Build Coastguard Worker }
252