blob: b08818ed15a1b2bbbc59a7e632246e5862ec2abb [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"
Han Pingtian75fb0572014-11-28 16:33:41 +080083#include "compat_16.h"
robbiew6f906ea2002-12-27 18:46:55 +000084
85#define EXP_RET_VAL -1
86#define EXP_ERRNO EPERM
87#define TEST_DESC "unprivileged user"
88
subrata_modak56207ce2009-03-23 13:35:39 +000089struct test_case_t { /* test case structure */
90 uid_t *rgid; /* real GID */
91 uid_t *egid; /* effective GID */
92 uid_t *sgid; /* saved GID */
robbiew6f906ea2002-12-27 18:46:55 +000093 struct passwd *exp_rgid; /* Expected real GID */
94 struct passwd *exp_egid; /* Expected effective GID */
95 struct passwd *exp_sgid; /* Expected saved GID */
96};
97
Han Pingtian75fb0572014-11-28 16:33:41 +080098TCID_DEFINE(setresgid03);
robbiew6f906ea2002-12-27 18:46:55 +000099static int testno;
100static struct passwd nobody, bin, root;
101static uid_t nobody_gid, bin_gid, neg = -1;
robbiew6f906ea2002-12-27 18:46:55 +0000102
103static int test_functionality(uid_t, uid_t, uid_t);
104static void setup(void);
105static void cleanup(void);
106
subrata_modak56207ce2009-03-23 13:35:39 +0000107static struct test_case_t tdat[] = {
108 {&neg, &neg, &bin.pw_gid, &nobody, &nobody, &nobody},
109 {&neg, &bin.pw_gid, &neg, &nobody, &nobody, &nobody},
110 {&bin.pw_gid, &neg, &neg, &nobody, &nobody, &nobody},
111 {&bin.pw_gid, &bin.pw_gid, &bin.pw_gid, &nobody, &nobody, &nobody},
robbiew6f906ea2002-12-27 18:46:55 +0000112};
113
114int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
115
subrata_modak56207ce2009-03-23 13:35:39 +0000116int main(int argc, char **argv)
robbiew6f906ea2002-12-27 18:46:55 +0000117{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200118 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200119 const char *msg;
robbiew6f906ea2002-12-27 18:46:55 +0000120
Wanlong Gao354ebb42012-12-07 10:10:04 +0800121 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800122 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew6f906ea2002-12-27 18:46:55 +0000123 }
124
125 setup();
126
robbiew6f906ea2002-12-27 18:46:55 +0000127 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800128 /* reset tst_count in case we are looping */
129 tst_count = 0;
robbiew6f906ea2002-12-27 18:46:55 +0000130
131 for (testno = 0; testno < TST_TOTAL; ++testno) {
132
Han Pingtian75fb0572014-11-28 16:33:41 +0800133 TEST(SETRESGID(cleanup, *tdat[testno].rgid, *tdat[testno].egid,
subrata_modak56207ce2009-03-23 13:35:39 +0000134 *tdat[testno].sgid));
robbiew6f906ea2002-12-27 18:46:55 +0000135
subrata_modak56207ce2009-03-23 13:35:39 +0000136 if ((TEST_RETURN == EXP_RET_VAL) &&
137 (TEST_ERRNO == EXP_ERRNO)) {
robbiew6f906ea2002-12-27 18:46:55 +0000138
subrata_modak56207ce2009-03-23 13:35:39 +0000139 if (!test_functionality
140 (tdat[testno].exp_rgid->pw_gid,
141 tdat[testno].exp_egid->pw_gid,
142 tdat[testno].exp_sgid->pw_gid)) {
robbiew6f906ea2002-12-27 18:46:55 +0000143
144 tst_resm(TPASS, "setresgid() failed as "
subrata_modak56207ce2009-03-23 13:35:39 +0000145 "expected for %s : errno %d",
146 TEST_DESC, TEST_ERRNO);
robbiew6f906ea2002-12-27 18:46:55 +0000147 } else {
148 tst_resm(TFAIL, "Functionality test "
subrata_modak56207ce2009-03-23 13:35:39 +0000149 "for setresgid() for %s failed",
150 TEST_DESC);
robbiew6f906ea2002-12-27 18:46:55 +0000151 }
152
153 } else {
154 tst_resm(TFAIL, "setresgid() returned "
subrata_modak56207ce2009-03-23 13:35:39 +0000155 "unexpected results for %s ; returned"
vapierb0b6f452009-08-28 14:37:06 +0000156 " %ld (expected %d), errno %d (expected"
157 " %d)", TEST_DESC,
subrata_modak56207ce2009-03-23 13:35:39 +0000158 TEST_RETURN, EXP_RET_VAL, TEST_ERRNO,
159 EXP_ERRNO);
robbiew6f906ea2002-12-27 18:46:55 +0000160 }
161 }
162 }
163 cleanup();
164
Garrett Cooper53740502010-12-16 00:04:01 -0800165 tst_exit();
robbiew6f906ea2002-12-27 18:46:55 +0000166}
167
subrata_modak56207ce2009-03-23 13:35:39 +0000168static int test_functionality(uid_t exp_rgid, uid_t exp_egid, uid_t exp_sgid)
robbiew6f906ea2002-12-27 18:46:55 +0000169{
170 uid_t cur_rgid, cur_egid, cur_sgid;
171
robbiew6f906ea2002-12-27 18:46:55 +0000172 /* Get current real, effective and saved group id */
subrata_modak56207ce2009-03-23 13:35:39 +0000173 if (getresgid(&cur_rgid, &cur_egid, &cur_sgid) == -1) {
robbiew6f906ea2002-12-27 18:46:55 +0000174 tst_brkm(TBROK, cleanup, "getresgid() failed");
Garrett Cooper2c282152010-12-16 00:55:50 -0800175
robbiew6f906ea2002-12-27 18:46:55 +0000176 }
177
subrata_modak56207ce2009-03-23 13:35:39 +0000178 if ((cur_rgid == exp_rgid) && (cur_egid == exp_egid)
179 && (cur_sgid == exp_sgid)) {
robbiew6f906ea2002-12-27 18:46:55 +0000180 return 0;
181 }
182 return 1;
183}
184
185/*
186 * setup()
187 * performs all ONE TIME setup for this test
188 */
subrata_modak56207ce2009-03-23 13:35:39 +0000189void setup(void)
robbiew6f906ea2002-12-27 18:46:55 +0000190{
191 struct passwd *passwd_p;
192
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200193 tst_require_root(NULL);
robbiew6f906ea2002-12-27 18:46:55 +0000194
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200195 tst_sig(NOFORK, DEF_HANDLER, cleanup);
robbiew6f906ea2002-12-27 18:46:55 +0000196
subrata_modak56207ce2009-03-23 13:35:39 +0000197 if ((passwd_p = getpwnam("root")) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800198 tst_brkm(TBROK, NULL, "getpwnam() failed for root");
Garrett Cooper2c282152010-12-16 00:55:50 -0800199
robbiew6f906ea2002-12-27 18:46:55 +0000200 }
201 root = *passwd_p;
202
subrata_modak56207ce2009-03-23 13:35:39 +0000203 if ((passwd_p = getpwnam("bin")) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800204 tst_brkm(TBROK, NULL, "bin user id doesn't exist");
Garrett Cooper2c282152010-12-16 00:55:50 -0800205
robbiew6f906ea2002-12-27 18:46:55 +0000206 }
207 bin = *passwd_p;
Han Pingtian75fb0572014-11-28 16:33:41 +0800208 GID16_CHECK((bin_gid = bin.pw_gid), "setresgid", cleanup)
robbiew6f906ea2002-12-27 18:46:55 +0000209
subrata_modak56207ce2009-03-23 13:35:39 +0000210 if ((passwd_p = getpwnam("nobody")) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800211 tst_brkm(TBROK, NULL, "nobody user id doesn't exist");
Garrett Cooper2c282152010-12-16 00:55:50 -0800212
robbiew6f906ea2002-12-27 18:46:55 +0000213 }
214 nobody = *passwd_p;
Han Pingtian75fb0572014-11-28 16:33:41 +0800215 GID16_CHECK((nobody_gid = nobody.pw_gid), "setresgid", cleanup)
robbiew6f906ea2002-12-27 18:46:55 +0000216
robbiew6f906ea2002-12-27 18:46:55 +0000217 /* Set real/effective/saved gid to nobody */
subrata_modak56207ce2009-03-23 13:35:39 +0000218 if (setresgid(nobody_gid, nobody_gid, nobody_gid) == -1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800219 tst_brkm(TBROK, NULL, "setup() failed for setting while"
subrata_modak56207ce2009-03-23 13:35:39 +0000220 " setting real/effective/saved gid");
robbiew6f906ea2002-12-27 18:46:55 +0000221 }
222 /* Set euid to nobody */
223 if (setuid(nobody.pw_uid) == -1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800224 tst_brkm(TBROK, NULL, "setuid failed to "
subrata_modak56207ce2009-03-23 13:35:39 +0000225 "to set the effective uid to nodody");
Garrett Cooper2c282152010-12-16 00:55:50 -0800226
robbiew6f906ea2002-12-27 18:46:55 +0000227 }
228 /* 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}