blob: 94fd0c1554f0fde48d031ac64fd44a42d52673dc [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 : setresgid02
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 *
23 * TEST TITLE : Checking functionality of setresgid(2) for
robbiew6f906ea2002-12-27 18:46:55 +000024 * non-root group id.
subrata_modak4bb656a2009-02-26 12:02:09 +000025 *
robbiew6f906ea2002-12-27 18:46:55 +000026 * TEST CASE TOTAL : 6
subrata_modak4bb656a2009-02-26 12:02:09 +000027 *
robbiew6f906ea2002-12-27 18:46:55 +000028 * AUTHOR : Madhu T L <madhu.tarikere@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000029 *
robbiew6f906ea2002-12-27 18:46:55 +000030 * 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)
subrata_modak4bb656a2009-02-26 12:02:09 +000042 *
robbiew6f906ea2002-12-27 18:46:55 +000043 * 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.
subrata_modak4bb656a2009-02-26 12:02:09 +000048 *
robbiew6f906ea2002-12-27 18:46:55 +000049 * 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
subrata_modak4bb656a2009-02-26 12:02:09 +000056 *
robbiew6f906ea2002-12-27 18:46:55 +000057 * Cleanup:
58 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000059 *
robbiew6f906ea2002-12-27 18:46:55 +000060 * 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.
robbiew116125c2003-04-09 15:59:13 +000071 *
72 * CHANGE: Madhu T L <madhu.tarikere@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000073 * Date: April 9 2003
74 * Replaced setegid() by setresgid() in setup()
robbiew6f906ea2002-12-27 18:46:55 +000075 ****************************************************************/
robbiew116125c2003-04-09 15:59:13 +000076
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"
Han Pingtian75fb0572014-11-28 16:33:41 +080083#include "compat_16.h"
robbiew6f906ea2002-12-27 18:46:55 +000084
85#define EXP_RET_VAL 0
86
subrata_modak56207ce2009-03-23 13:35:39 +000087struct test_case_t { /* test case structure */
88 uid_t *rgid; /* real GID */
89 uid_t *egid; /* effective GID */
90 uid_t *sgid; /* saved GID */
robbiew6f906ea2002-12-27 18:46:55 +000091 struct passwd *exp_rgid; /* Expected real GID */
92 struct passwd *exp_egid; /* Expected effective GID */
93 struct passwd *exp_sgid; /* Expected saved GID */
subrata_modak56207ce2009-03-23 13:35:39 +000094 char *desc; /* Test description */
robbiew6f906ea2002-12-27 18:46:55 +000095};
96
Han Pingtian75fb0572014-11-28 16:33:41 +080097TCID_DEFINE(setresgid02);
robbiew6f906ea2002-12-27 18:46:55 +000098static int testno;
99static struct passwd nobody, bin, root;
100static uid_t nobody_gid, root_gid, bin_gid, neg = -1;
101
102static int test_functionality(uid_t, uid_t, uid_t);
103static void setup(void);
104static void cleanup(void);
105
106/* Don't change order of these test cases */
subrata_modak56207ce2009-03-23 13:35:39 +0000107static 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)"},
robbiew6f906ea2002-12-27 18:46:55 +0000120};
121
122int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
123
subrata_modak56207ce2009-03-23 13:35:39 +0000124int main(int argc, char **argv)
robbiew6f906ea2002-12-27 18:46:55 +0000125{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200126 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200127 const char *msg;
robbiew6f906ea2002-12-27 18:46:55 +0000128
Wanlong Gao354ebb42012-12-07 10:10:04 +0800129 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800130 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew6f906ea2002-12-27 18:46:55 +0000131 }
132
133 setup();
134
robbiew6f906ea2002-12-27 18:46:55 +0000135 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800136 /* reset tst_count in case we are looping */
137 tst_count = 0;
robbiew6f906ea2002-12-27 18:46:55 +0000138
139 for (testno = 0; testno < TST_TOTAL; ++testno) {
140
Han Pingtian75fb0572014-11-28 16:33:41 +0800141 TEST(SETRESGID(cleanup, *tdat[testno].rgid, *tdat[testno].egid,
subrata_modak56207ce2009-03-23 13:35:39 +0000142 *tdat[testno].sgid));
robbiew6f906ea2002-12-27 18:46:55 +0000143
144 if (TEST_RETURN == EXP_RET_VAL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000145 if (!test_functionality
146 (tdat[testno].exp_rgid->pw_gid,
147 tdat[testno].exp_egid->pw_gid,
148 tdat[testno].exp_sgid->pw_gid)) {
robbiew6f906ea2002-12-27 18:46:55 +0000149
150 tst_resm(TPASS, "Test for %s "
subrata_modak56207ce2009-03-23 13:35:39 +0000151 "successful",
152 tdat[testno].desc);
robbiew6f906ea2002-12-27 18:46:55 +0000153 } else {
154 tst_resm(TFAIL, "Functionality test "
subrata_modak56207ce2009-03-23 13:35:39 +0000155 "for %s failed",
156 tdat[testno].desc);
robbiew6f906ea2002-12-27 18:46:55 +0000157 }
158 } else {
159 tst_resm(TFAIL, "Test for %s failed; returned"
vapierb0b6f452009-08-28 14:37:06 +0000160 " %ld (expected %d), errno %d (expected"
subrata_modak56207ce2009-03-23 13:35:39 +0000161 " 0)", tdat[testno].desc,
162 TEST_RETURN, EXP_RET_VAL, TEST_ERRNO);
robbiew6f906ea2002-12-27 18:46:55 +0000163 }
164 }
165 }
166 cleanup();
167
Garrett Cooper53740502010-12-16 00:04:01 -0800168 tst_exit();
robbiew6f906ea2002-12-27 18:46:55 +0000169}
170
subrata_modak56207ce2009-03-23 13:35:39 +0000171static int test_functionality(uid_t exp_rgid, uid_t exp_egid, uid_t exp_sgid)
robbiew6f906ea2002-12-27 18:46:55 +0000172{
173 uid_t cur_rgid, cur_egid, cur_sgid;
174
robbiew6f906ea2002-12-27 18:46:55 +0000175 /* Get current real, effective and saved group id */
subrata_modak56207ce2009-03-23 13:35:39 +0000176 if (getresgid(&cur_rgid, &cur_egid, &cur_sgid) == -1) {
robbiew6f906ea2002-12-27 18:46:55 +0000177 tst_brkm(TBROK, cleanup, "getresgid() failed");
Garrett Cooper2c282152010-12-16 00:55:50 -0800178
robbiew6f906ea2002-12-27 18:46:55 +0000179 }
180
subrata_modak56207ce2009-03-23 13:35:39 +0000181 if ((cur_rgid == exp_rgid) && (cur_egid == exp_egid)
182 && (cur_sgid == exp_sgid)) {
robbiew6f906ea2002-12-27 18:46:55 +0000183 return 0;
184 }
185 return 1;
186}
187
188/*
189 * setup()
190 * performs all ONE TIME setup for this test
191 */
subrata_modak56207ce2009-03-23 13:35:39 +0000192void setup(void)
robbiew6f906ea2002-12-27 18:46:55 +0000193{
194 struct passwd *passwd_p;
195
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200196 tst_require_root(NULL);
robbiew6f906ea2002-12-27 18:46:55 +0000197
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200198 tst_sig(NOFORK, DEF_HANDLER, cleanup);
robbiew6f906ea2002-12-27 18:46:55 +0000199
subrata_modak56207ce2009-03-23 13:35:39 +0000200 if ((passwd_p = getpwnam("root")) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800201 tst_brkm(TBROK, NULL, "getpwnam() failed for root");
Garrett Cooper2c282152010-12-16 00:55:50 -0800202
robbiew6f906ea2002-12-27 18:46:55 +0000203 }
204 root = *passwd_p;
Han Pingtian75fb0572014-11-28 16:33:41 +0800205 GID16_CHECK((root_gid = root.pw_gid), "setresgid", cleanup)
robbiew6f906ea2002-12-27 18:46:55 +0000206
subrata_modak56207ce2009-03-23 13:35:39 +0000207 if ((passwd_p = getpwnam("bin")) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800208 tst_brkm(TBROK, NULL, "bin user id doesn't exist");
Garrett Cooper2c282152010-12-16 00:55:50 -0800209
robbiew6f906ea2002-12-27 18:46:55 +0000210 }
211 bin = *passwd_p;
Han Pingtian75fb0572014-11-28 16:33:41 +0800212 GID16_CHECK((bin_gid = bin.pw_gid), "setresgid", cleanup)
robbiew6f906ea2002-12-27 18:46:55 +0000213
subrata_modak56207ce2009-03-23 13:35:39 +0000214 if ((passwd_p = getpwnam("nobody")) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800215 tst_brkm(TBROK, NULL, "nobody user id doesn't exist");
Garrett Cooper2c282152010-12-16 00:55:50 -0800216
robbiew6f906ea2002-12-27 18:46:55 +0000217 }
218 nobody = *passwd_p;
Han Pingtian75fb0572014-11-28 16:33:41 +0800219 GID16_CHECK((nobody_gid = nobody.pw_gid), "setresgid", cleanup)
robbiew6f906ea2002-12-27 18:46:55 +0000220
robbiew116125c2003-04-09 15:59:13 +0000221 /* Set effective/saved gid to nobody */
subrata_modak56207ce2009-03-23 13:35:39 +0000222 if (setresgid(-1, nobody_gid, nobody_gid) == -1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800223 tst_brkm(TBROK, NULL, "setup() failed for setting while"
subrata_modak56207ce2009-03-23 13:35:39 +0000224 " setting real/effective/saved gid");
Garrett Cooper2c282152010-12-16 00:55:50 -0800225
robbiew6f906ea2002-12-27 18:46:55 +0000226 }
robbiew116125c2003-04-09 15:59:13 +0000227
robbiew6f906ea2002-12-27 18:46:55 +0000228 /* Pause if that option was specified
229 * TEST_PAUSE contains the code to fork the test with the -c option.
230 */
231 TEST_PAUSE;
232}
233
234/*
235 * cleanup()
236 * performs all ONE TIME cleanup for this test at
237 * completion or premature exit
238 */
subrata_modak56207ce2009-03-23 13:35:39 +0000239void cleanup(void)
robbiew6f906ea2002-12-27 18:46:55 +0000240{
robbiew6f906ea2002-12-27 18:46:55 +0000241
Wanlong Gao354ebb42012-12-07 10:10:04 +0800242}