• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * You should have received a copy of the GNU General Public License along
13  * with this program; if not, write the Free Software Foundation, Inc.,
14  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  */
17 /**********************************************************
18  *
19  *    TEST IDENTIFIER   : setresgid02
20  *
21  *    EXECUTED BY       : root / superuser
22  *
23  *    TEST TITLE        : Checking functionality of setresgid(2) for
24  *			  non-root group id.
25  *
26  *    TEST CASE TOTAL   : 6
27  *
28  *    AUTHOR            : Madhu T L <madhu.tarikere@wipro.com>
29  *
30  *    SIGNALS
31  *      Uses SIGUSR1 to pause before test if option set.
32  *      (See the parse_opts(3) man page).
33  *
34  *    DESCRIPTION
35  *      Verify that for non-root effective group id,
36  *	1. setresgid(2) is successful for setresgid(-1, -1, -1)
37  *	2. setresgid(2) is successful for setresgid(-1, -1, bin)
38  *	3. setresgid(2) is successful for setresgid(-1, bin, -1)
39  *	4. setresgid(2) is successful for setresgid(bin, -1, -1)
40  *	5. setresgid(2) is successful for setresgid(root, root, root)
41  *	6. setresgid(2) is successful for setresgid(root, nobody, nobody)
42  *
43  *      Setup:
44  *	  Setup signal handling.
45  *	  Test caller is superuser
46  *	  Check existence of root, bin and nobody user id's
47  *	  Pause for SIGUSR1 if option specified.
48  *
49  *	Test:
50  *	 Loop if the proper options are given.
51  *	  Execute system call
52  *	  Check return value and functionality, if success,
53  *		 Issue PASS message
54  *	Otherwise,
55  *		Issue FAIL message
56  *
57  *	Cleanup:
58  *	  Print errno log and/or timing stats if options given
59  *
60  * USAGE:  <for command-line>
61  *  setresgid02 [-c n] [-e] [-f] [-h] [-i n] [-I x] [-p] [-P x] [-t]
62  *		where,  -c n : Run n copies concurrently.
63  *			-e   : Turn on errno logging.
64  *			-f   : Turn off functional testing
65  *			-h   : Show help screen
66  *			-i n : Execute test n times.
67  *			-I x : Execute test for x seconds.
68  *			-p   : Pause for SIGUSR1 before starting
69  *			-P x : Pause for x seconds between iterations.
70  *			-t   : Turn on syscall timing.
71  *
72  * CHANGE:  Madhu T L <madhu.tarikere@wipro.com>
73  * Date: April 9 2003
74  * Replaced setegid() by setresgid() in setup()
75  ****************************************************************/
76 
77 #define _GNU_SOURCE 1
78 #include <errno.h>
79 #include <pwd.h>
80 #include <sys/types.h>
81 #include <unistd.h>
82 #include "test.h"
83 #include "safe_macros.h"
84 #include "compat_16.h"
85 
86 #define EXP_RET_VAL	0
87 
88 struct test_case_t {		/* test case structure */
89 	uid_t *rgid;		/* real GID */
90 	uid_t *egid;		/* effective GID */
91 	uid_t *sgid;		/* saved GID */
92 	struct passwd *exp_rgid;	/* Expected real GID */
93 	struct passwd *exp_egid;	/* Expected effective GID */
94 	struct passwd *exp_sgid;	/* Expected saved GID */
95 	char *desc;		/* Test description */
96 };
97 
98 TCID_DEFINE(setresgid02);
99 static int testno;
100 static struct passwd nobody, bin, root;
101 static uid_t nobody_gid, root_gid, bin_gid, neg = -1;
102 
103 static int test_functionality(uid_t, uid_t, uid_t);
104 static void setup(void);
105 static void cleanup(void);
106 
107 /* Don't change order of these test cases */
108 static struct test_case_t tdat[] = {
109 	{&neg, &neg, &neg, &root, &nobody, &nobody,
110 	 "setresgid(-1, -1, -1)"},
111 	{&neg, &neg, &bin.pw_gid, &root, &nobody, &bin,
112 	 "setresgid(-1, -1, bin)"},
113 	{&neg, &bin.pw_gid, &neg, &root, &bin, &bin,
114 	 "setresgid(-1, bin, -1)"},
115 	{&bin.pw_gid, &neg, &neg, &bin, &bin, &bin,
116 	 "setresgid(bin, -1, -1)"},
117 	{&root.pw_gid, &root.pw_gid, &root.pw_gid, &root, &root, &root,
118 	 "setresgid(root, root, root)"},
119 	{&root.pw_gid, &nobody.pw_gid, &nobody.pw_gid, &root, &nobody, &nobody,
120 	 "setresgid(root, nobody, nobody)"},
121 };
122 
123 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
124 
main(int argc,char ** argv)125 int main(int argc, char **argv)
126 {
127 	int lc;
128 
129 	tst_parse_opts(argc, argv, NULL, NULL);
130 
131 	setup();
132 
133 	for (lc = 0; TEST_LOOPING(lc); lc++) {
134 		/* reset tst_count in case we are looping */
135 		tst_count = 0;
136 
137 		for (testno = 0; testno < TST_TOTAL; ++testno) {
138 
139 			TEST(SETRESGID(cleanup, *tdat[testno].rgid, *tdat[testno].egid,
140 				       *tdat[testno].sgid));
141 
142 			if (TEST_RETURN == EXP_RET_VAL) {
143 				if (!test_functionality
144 				    (tdat[testno].exp_rgid->pw_gid,
145 				     tdat[testno].exp_egid->pw_gid,
146 				     tdat[testno].exp_sgid->pw_gid)) {
147 
148 					tst_resm(TPASS, "Test for %s "
149 						 "successful",
150 						 tdat[testno].desc);
151 				} else {
152 					tst_resm(TFAIL, "Functionality test "
153 						 "for %s failed",
154 						 tdat[testno].desc);
155 				}
156 			} else {
157 				tst_resm(TFAIL, "Test for %s failed; returned"
158 					 " %ld (expected %d), errno %d (expected"
159 					 " 0)", tdat[testno].desc,
160 					 TEST_RETURN, EXP_RET_VAL, TEST_ERRNO);
161 			}
162 		}
163 	}
164 	cleanup();
165 
166 	tst_exit();
167 }
168 
test_functionality(uid_t exp_rgid,uid_t exp_egid,uid_t exp_sgid)169 static int test_functionality(uid_t exp_rgid, uid_t exp_egid, uid_t exp_sgid)
170 {
171 	uid_t cur_rgid, cur_egid, cur_sgid;
172 
173 	/* Get current real, effective and saved group id */
174 	SAFE_GETRESGID(cleanup, &cur_rgid, &cur_egid, &cur_sgid);
175 
176 	if ((cur_rgid == exp_rgid) && (cur_egid == exp_egid)
177 	    && (cur_sgid == exp_sgid)) {
178 		return 0;
179 	}
180 	return 1;
181 }
182 
183 /*
184  * setup()
185  *	performs all ONE TIME setup for this test
186  */
setup(void)187 void setup(void)
188 {
189 	struct passwd *passwd_p;
190 
191 	tst_require_root();
192 
193 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
194 
195 	if ((passwd_p = getpwnam("root")) == NULL) {
196 		tst_brkm(TBROK, NULL, "getpwnam() failed for root");
197 
198 	}
199 	root = *passwd_p;
200 	GID16_CHECK((root_gid = root.pw_gid), "setresgid", cleanup)
201 
202 	if ((passwd_p = getpwnam("bin")) == NULL) {
203 		tst_brkm(TBROK, NULL, "bin user id doesn't exist");
204 
205 	}
206 	bin = *passwd_p;
207 	GID16_CHECK((bin_gid = bin.pw_gid), "setresgid", cleanup)
208 
209 	if ((passwd_p = getpwnam("nobody")) == NULL) {
210 		tst_brkm(TBROK, NULL, "nobody user id doesn't exist");
211 
212 	}
213 	nobody = *passwd_p;
214 	GID16_CHECK((nobody_gid = nobody.pw_gid), "setresgid", cleanup)
215 
216 	/* Set effective/saved gid to nobody */
217 	if (setresgid(-1, nobody_gid, nobody_gid) == -1) {
218 		tst_brkm(TBROK, NULL, "setup() failed for setting while"
219 			 " setting real/effective/saved gid");
220 
221 	}
222 
223 	/* Pause if that option was specified
224 	 * TEST_PAUSE contains the code to fork the test with the -c option.
225 	 */
226 	TEST_PAUSE;
227 }
228 
229 /*
230  * cleanup()
231  *	performs all ONE TIME cleanup for this test at
232  *	completion or premature exit
233  */
cleanup(void)234 void cleanup(void)
235 {
236 
237 }
238