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