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 "compat_16.h"
84
85 #define EXP_RET_VAL 0
86
87 struct test_case_t { /* test case structure */
88 uid_t *rgid; /* real GID */
89 uid_t *egid; /* effective GID */
90 uid_t *sgid; /* saved GID */
91 struct passwd *exp_rgid; /* Expected real GID */
92 struct passwd *exp_egid; /* Expected effective GID */
93 struct passwd *exp_sgid; /* Expected saved GID */
94 char *desc; /* Test description */
95 };
96
97 TCID_DEFINE(setresgid02);
98 static int testno;
99 static struct passwd nobody, bin, root;
100 static uid_t nobody_gid, root_gid, bin_gid, neg = -1;
101
102 static int test_functionality(uid_t, uid_t, uid_t);
103 static void setup(void);
104 static void cleanup(void);
105
106 /* Don't change order of these test cases */
107 static struct test_case_t tdat[] = {
108 {&neg, &neg, &neg, &root, &nobody, &nobody,
109 "setresgid(-1, -1, -1)"},
110 {&neg, &neg, &bin.pw_gid, &root, &nobody, &bin,
111 "setresgid(-1, -1, bin)"},
112 {&neg, &bin.pw_gid, &neg, &root, &bin, &bin,
113 "setresgid(-1, bin, -1)"},
114 {&bin.pw_gid, &neg, &neg, &bin, &bin, &bin,
115 "setresgid(bin, -1, -1)"},
116 {&root.pw_gid, &root.pw_gid, &root.pw_gid, &root, &root, &root,
117 "setresgid(root, root, root)"},
118 {&root.pw_gid, &nobody.pw_gid, &nobody.pw_gid, &root, &nobody, &nobody,
119 "setresgid(root, nobody, nobody)"},
120 };
121
122 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
123
main(int argc,char ** argv)124 int main(int argc, char **argv)
125 {
126 int lc;
127
128 tst_parse_opts(argc, argv, NULL, NULL);
129
130 setup();
131
132 for (lc = 0; TEST_LOOPING(lc); lc++) {
133 /* reset tst_count in case we are looping */
134 tst_count = 0;
135
136 for (testno = 0; testno < TST_TOTAL; ++testno) {
137
138 TEST(SETRESGID(cleanup, *tdat[testno].rgid, *tdat[testno].egid,
139 *tdat[testno].sgid));
140
141 if (TEST_RETURN == EXP_RET_VAL) {
142 if (!test_functionality
143 (tdat[testno].exp_rgid->pw_gid,
144 tdat[testno].exp_egid->pw_gid,
145 tdat[testno].exp_sgid->pw_gid)) {
146
147 tst_resm(TPASS, "Test for %s "
148 "successful",
149 tdat[testno].desc);
150 } else {
151 tst_resm(TFAIL, "Functionality test "
152 "for %s failed",
153 tdat[testno].desc);
154 }
155 } else {
156 tst_resm(TFAIL, "Test for %s failed; returned"
157 " %ld (expected %d), errno %d (expected"
158 " 0)", tdat[testno].desc,
159 TEST_RETURN, EXP_RET_VAL, TEST_ERRNO);
160 }
161 }
162 }
163 cleanup();
164
165 tst_exit();
166 }
167
test_functionality(uid_t exp_rgid,uid_t exp_egid,uid_t exp_sgid)168 static int test_functionality(uid_t exp_rgid, uid_t exp_egid, uid_t exp_sgid)
169 {
170 uid_t cur_rgid, cur_egid, cur_sgid;
171
172 /* Get current real, effective and saved group id */
173 if (getresgid(&cur_rgid, &cur_egid, &cur_sgid) == -1) {
174 tst_brkm(TBROK, cleanup, "getresgid() failed");
175
176 }
177
178 if ((cur_rgid == exp_rgid) && (cur_egid == exp_egid)
179 && (cur_sgid == exp_sgid)) {
180 return 0;
181 }
182 return 1;
183 }
184
185 /*
186 * setup()
187 * performs all ONE TIME setup for this test
188 */
setup(void)189 void setup(void)
190 {
191 struct passwd *passwd_p;
192
193 tst_require_root();
194
195 tst_sig(NOFORK, DEF_HANDLER, cleanup);
196
197 if ((passwd_p = getpwnam("root")) == NULL) {
198 tst_brkm(TBROK, NULL, "getpwnam() failed for root");
199
200 }
201 root = *passwd_p;
202 GID16_CHECK((root_gid = root.pw_gid), "setresgid", cleanup)
203
204 if ((passwd_p = getpwnam("bin")) == NULL) {
205 tst_brkm(TBROK, NULL, "bin user id doesn't exist");
206
207 }
208 bin = *passwd_p;
209 GID16_CHECK((bin_gid = bin.pw_gid), "setresgid", cleanup)
210
211 if ((passwd_p = getpwnam("nobody")) == NULL) {
212 tst_brkm(TBROK, NULL, "nobody user id doesn't exist");
213
214 }
215 nobody = *passwd_p;
216 GID16_CHECK((nobody_gid = nobody.pw_gid), "setresgid", cleanup)
217
218 /* Set effective/saved gid to nobody */
219 if (setresgid(-1, nobody_gid, nobody_gid) == -1) {
220 tst_brkm(TBROK, NULL, "setup() failed for setting while"
221 " setting real/effective/saved gid");
222
223 }
224
225 /* Pause if that option was specified
226 * TEST_PAUSE contains the code to fork the test with the -c option.
227 */
228 TEST_PAUSE;
229 }
230
231 /*
232 * cleanup()
233 * performs all ONE TIME cleanup for this test at
234 * completion or premature exit
235 */
cleanup(void)236 void cleanup(void)
237 {
238
239 }
240