blob: 643457aefa021ef60d168f25dcc1ea58c5bab5a2 [file] [log] [blame]
robbiew6f906ea2002-12-27 18:46:55 +00001/*
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
Wanlong Gaofed96412012-10-24 10:10:29 +080013 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
robbiew6f906ea2002-12-27 18:46:55 +000015 *
16 */
17/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
robbiew6f906ea2002-12-27 18:46:55 +000019 * TEST IDENTIFIER : setresgid03
subrata_modak4bb656a2009-02-26 12:02:09 +000020 *
robbiew6f906ea2002-12-27 18:46:55 +000021 * EXECUTED BY : root / superuser
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
robbiew6f906ea2002-12-27 18:46:55 +000023 * TEST TITLE : Checking error conditions for setresgid(2)
subrata_modak4bb656a2009-02-26 12:02:09 +000024 *
robbiew6f906ea2002-12-27 18:46:55 +000025 * TEST CASE TOTAL : 4
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
robbiew6f906ea2002-12-27 18:46:55 +000027 * AUTHOR : Madhu T L <madhu.tarikere@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000028 *
robbiew6f906ea2002-12-27 18:46:55 +000029 * SIGNALS
30 * Uses SIGUSR1 to pause before test if option set.
31 * (See the parse_opts(3) man page).
32 *
33 * DESCRIPTION
34 * Verify that,
35 * 1. setresgid(2) fails with EPERM for unprivileged user in setting
36 * saved group id.
37 * 2. setresgid(2) fails with EPERM for unprivileged user in setting
38 * effective group id.
39 * 3. setresgid(2) fails with EPERM for unprivileged user in setting
40 * real group id.
41 * 4. setresgid(2) fails with EPERM for unprivileged user in setting
42 * real/effective/saved group id.
subrata_modak4bb656a2009-02-26 12:02:09 +000043 *
robbiew6f906ea2002-12-27 18:46:55 +000044 * Setup:
45 * Setup signal handling.
46 * Test caller is superuser
47 * Check existence of user id's root/bin/nobody
48 * Set real/effective/saved gid to nobody
49 * Set effective uid to nobody
50 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000051 *
robbiew6f906ea2002-12-27 18:46:55 +000052 * Test:
53 * Loop if the proper options are given.
54 * Execute system call
55 * Check return value, errno and functionality, if success,
56 * Issue PASS message
57 * Otherwise,
58 * Issue FAIL message
subrata_modak4bb656a2009-02-26 12:02:09 +000059 *
robbiew6f906ea2002-12-27 18:46:55 +000060 * Cleanup:
61 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000062 *
robbiew6f906ea2002-12-27 18:46:55 +000063 * USAGE: <for command-line>
64 * setresgid03 [-c n] [-e] [-f] [-h] [-i n] [-I x] [-p] [-P x] [-t]
65 * where, -c n : Run n copies concurrently.
66 * -e : Turn on errno logging.
67 * -f : Turn off functional testing
68 * -h : Show help screen
69 * -i n : Execute test n times.
70 * -I x : Execute test for x seconds.
71 * -p : Pause for SIGUSR1 before starting
72 * -P x : Pause for x seconds between iterations.
73 * -t : Turn on syscall timing.
subrata_modak4bb656a2009-02-26 12:02:09 +000074 *
robbiew6f906ea2002-12-27 18:46:55 +000075 ****************************************************************/
76
vapierbed036a2006-05-26 06:48:55 +000077#define _GNU_SOURCE 1
robbiew6f906ea2002-12-27 18:46:55 +000078#include <errno.h>
79#include <pwd.h>
80#include <sys/types.h>
81#include <unistd.h>
82#include "test.h"
83#include "usctest.h"
84
85#define EXP_RET_VAL -1
86#define EXP_ERRNO EPERM
87#define TEST_DESC "unprivileged user"
88
robbiew6f906ea2002-12-27 18:46:55 +000089
subrata_modak56207ce2009-03-23 13:35:39 +000090struct test_case_t { /* test case structure */
91 uid_t *rgid; /* real GID */
92 uid_t *egid; /* effective GID */
93 uid_t *sgid; /* saved GID */
robbiew6f906ea2002-12-27 18:46:55 +000094 struct passwd *exp_rgid; /* Expected real GID */
95 struct passwd *exp_egid; /* Expected effective GID */
96 struct passwd *exp_sgid; /* Expected saved GID */
97};
98
99char *TCID = "setresgid03";
100static int testno;
101static struct passwd nobody, bin, root;
102static uid_t nobody_gid, bin_gid, neg = -1;
subrata_modak56207ce2009-03-23 13:35:39 +0000103static int exp_enos[] = { EPERM, 0 };
robbiew6f906ea2002-12-27 18:46:55 +0000104
105static int test_functionality(uid_t, uid_t, uid_t);
106static void setup(void);
107static void cleanup(void);
108
subrata_modak56207ce2009-03-23 13:35:39 +0000109static struct test_case_t tdat[] = {
110 {&neg, &neg, &bin.pw_gid, &nobody, &nobody, &nobody},
111 {&neg, &bin.pw_gid, &neg, &nobody, &nobody, &nobody},
112 {&bin.pw_gid, &neg, &neg, &nobody, &nobody, &nobody},
113 {&bin.pw_gid, &bin.pw_gid, &bin.pw_gid, &nobody, &nobody, &nobody},
robbiew6f906ea2002-12-27 18:46:55 +0000114};
115
116int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
117
subrata_modak56207ce2009-03-23 13:35:39 +0000118int main(int argc, char **argv)
robbiew6f906ea2002-12-27 18:46:55 +0000119{
subrata_modak56207ce2009-03-23 13:35:39 +0000120 int lc; /* loop counter */
121 char *msg; /* message returned from parse_opts */
robbiew6f906ea2002-12-27 18:46:55 +0000122
123 /* parse standard options */
Garrett Cooper45e285d2010-11-22 12:19:25 -0800124 if ((msg = parse_opts(argc, argv, NULL, NULL)) !=
125 NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800126 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew6f906ea2002-12-27 18:46:55 +0000127 }
128
129 setup();
130
robbiew6f906ea2002-12-27 18:46:55 +0000131 for (lc = 0; TEST_LOOPING(lc); lc++) {
132 /* reset Tst_count in case we are looping */
133 Tst_count = 0;
134
135 for (testno = 0; testno < TST_TOTAL; ++testno) {
136
137 TEST(setresgid(*tdat[testno].rgid, *tdat[testno].egid,
subrata_modak56207ce2009-03-23 13:35:39 +0000138 *tdat[testno].sgid));
robbiew6f906ea2002-12-27 18:46:55 +0000139
140 TEST_ERROR_LOG(TEST_ERRNO);
subrata_modak56207ce2009-03-23 13:35:39 +0000141 if ((TEST_RETURN == EXP_RET_VAL) &&
142 (TEST_ERRNO == EXP_ERRNO)) {
robbiew6f906ea2002-12-27 18:46:55 +0000143
subrata_modak56207ce2009-03-23 13:35:39 +0000144 if (!test_functionality
145 (tdat[testno].exp_rgid->pw_gid,
146 tdat[testno].exp_egid->pw_gid,
147 tdat[testno].exp_sgid->pw_gid)) {
robbiew6f906ea2002-12-27 18:46:55 +0000148
149 tst_resm(TPASS, "setresgid() failed as "
subrata_modak56207ce2009-03-23 13:35:39 +0000150 "expected for %s : errno %d",
151 TEST_DESC, TEST_ERRNO);
robbiew6f906ea2002-12-27 18:46:55 +0000152 } else {
153 tst_resm(TFAIL, "Functionality test "
subrata_modak56207ce2009-03-23 13:35:39 +0000154 "for setresgid() for %s failed",
155 TEST_DESC);
robbiew6f906ea2002-12-27 18:46:55 +0000156 }
157
158 } else {
159 tst_resm(TFAIL, "setresgid() returned "
subrata_modak56207ce2009-03-23 13:35:39 +0000160 "unexpected results for %s ; returned"
vapierb0b6f452009-08-28 14:37:06 +0000161 " %ld (expected %d), errno %d (expected"
162 " %d)", TEST_DESC,
subrata_modak56207ce2009-03-23 13:35:39 +0000163 TEST_RETURN, EXP_RET_VAL, TEST_ERRNO,
164 EXP_ERRNO);
robbiew6f906ea2002-12-27 18:46:55 +0000165 }
166 }
167 }
168 cleanup();
169
Garrett Cooper53740502010-12-16 00:04:01 -0800170 tst_exit();
robbiew6f906ea2002-12-27 18:46:55 +0000171}
172
subrata_modak56207ce2009-03-23 13:35:39 +0000173static int test_functionality(uid_t exp_rgid, uid_t exp_egid, uid_t exp_sgid)
robbiew6f906ea2002-12-27 18:46:55 +0000174{
175 uid_t cur_rgid, cur_egid, cur_sgid;
176
subrata_modak4bb656a2009-02-26 12:02:09 +0000177 /*
robbiew6f906ea2002-12-27 18:46:55 +0000178 * Perform functional verification, if STD_FUNCTIONAL_TEST is
179 * set (-f options is not used)
180 */
subrata_modak56207ce2009-03-23 13:35:39 +0000181 if (STD_FUNCTIONAL_TEST == 0) {
robbiew6f906ea2002-12-27 18:46:55 +0000182 return 0;
183 }
184 /* Get current real, effective and saved group id */
subrata_modak56207ce2009-03-23 13:35:39 +0000185 if (getresgid(&cur_rgid, &cur_egid, &cur_sgid) == -1) {
robbiew6f906ea2002-12-27 18:46:55 +0000186 tst_brkm(TBROK, cleanup, "getresgid() failed");
Garrett Cooper2c282152010-12-16 00:55:50 -0800187
robbiew6f906ea2002-12-27 18:46:55 +0000188 }
189
subrata_modak56207ce2009-03-23 13:35:39 +0000190 if ((cur_rgid == exp_rgid) && (cur_egid == exp_egid)
191 && (cur_sgid == exp_sgid)) {
robbiew6f906ea2002-12-27 18:46:55 +0000192 return 0;
193 }
194 return 1;
195}
196
197/*
198 * setup()
199 * performs all ONE TIME setup for this test
200 */
subrata_modak56207ce2009-03-23 13:35:39 +0000201void setup(void)
robbiew6f906ea2002-12-27 18:46:55 +0000202{
203 struct passwd *passwd_p;
204
robbiew6f906ea2002-12-27 18:46:55 +0000205 tst_sig(NOFORK, DEF_HANDLER, cleanup);
206
207 /* Check whether we are root */
208 if (geteuid() != 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800209 tst_brkm(TBROK, NULL, "Must be root for this test!");
210 }
robbiew6f906ea2002-12-27 18:46:55 +0000211
subrata_modak56207ce2009-03-23 13:35:39 +0000212 if ((passwd_p = getpwnam("root")) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800213 tst_brkm(TBROK, NULL, "getpwnam() failed for root");
Garrett Cooper2c282152010-12-16 00:55:50 -0800214
robbiew6f906ea2002-12-27 18:46:55 +0000215 }
216 root = *passwd_p;
217
subrata_modak56207ce2009-03-23 13:35:39 +0000218 if ((passwd_p = getpwnam("bin")) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800219 tst_brkm(TBROK, NULL, "bin user id doesn't exist");
Garrett Cooper2c282152010-12-16 00:55:50 -0800220
robbiew6f906ea2002-12-27 18:46:55 +0000221 }
222 bin = *passwd_p;
223 bin_gid = bin.pw_gid;
224
subrata_modak56207ce2009-03-23 13:35:39 +0000225 if ((passwd_p = getpwnam("nobody")) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800226 tst_brkm(TBROK, NULL, "nobody user id doesn't exist");
Garrett Cooper2c282152010-12-16 00:55:50 -0800227
robbiew6f906ea2002-12-27 18:46:55 +0000228 }
229 nobody = *passwd_p;
230 nobody_gid = nobody.pw_gid;
231
232 /* set the expected errnos... */
233 TEST_EXP_ENOS(exp_enos);
234
235 /* Set real/effective/saved gid to nobody */
subrata_modak56207ce2009-03-23 13:35:39 +0000236 if (setresgid(nobody_gid, nobody_gid, nobody_gid) == -1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800237 tst_brkm(TBROK, NULL, "setup() failed for setting while"
subrata_modak56207ce2009-03-23 13:35:39 +0000238 " setting real/effective/saved gid");
Garrett Cooper2c282152010-12-16 00:55:50 -0800239
robbiew6f906ea2002-12-27 18:46:55 +0000240 }
241 /* Set euid to nobody */
242 if (setuid(nobody.pw_uid) == -1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800243 tst_brkm(TBROK, NULL, "setuid failed to "
subrata_modak56207ce2009-03-23 13:35:39 +0000244 "to set the effective uid to nodody");
Garrett Cooper2c282152010-12-16 00:55:50 -0800245
robbiew6f906ea2002-12-27 18:46:55 +0000246 }
247 /* Pause if that option was specified
248 * TEST_PAUSE contains the code to fork the test with the -c option.
249 */
250 TEST_PAUSE;
251}
252
253/*
254 * cleanup()
255 * performs all ONE TIME cleanup for this test at
256 * completion or premature exit
257 */
subrata_modak56207ce2009-03-23 13:35:39 +0000258void cleanup(void)
robbiew6f906ea2002-12-27 18:46:55 +0000259{
260 /*
261 * print timing stats if that option was specified.
262 * print errno log if that option was specified.
263 */
264
265 TEST_CLEANUP;
266
Chris Dearmanec6edca2012-10-17 19:54:01 -0700267 }