robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 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 |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame^] | 13 | * with this program; if not, write the Free Software Foundation, Inc., |
| 14 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 15 | * |
| 16 | */ |
| 17 | /********************************************************** |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 18 | * |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 19 | * TEST IDENTIFIER : setresgid03 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 20 | * |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 21 | * EXECUTED BY : root / superuser |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 22 | * |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 23 | * TEST TITLE : Checking error conditions for setresgid(2) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 24 | * |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 25 | * TEST CASE TOTAL : 4 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 26 | * |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 27 | * AUTHOR : Madhu T L <madhu.tarikere@wipro.com> |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 28 | * |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 29 | * 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 43 | * |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 44 | * 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 51 | * |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 52 | * 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 59 | * |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 60 | * Cleanup: |
| 61 | * Print errno log and/or timing stats if options given |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 62 | * |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 63 | * 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 74 | * |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 75 | ****************************************************************/ |
| 76 | |
vapier | bed036a | 2006-05-26 06:48:55 +0000 | [diff] [blame] | 77 | #define _GNU_SOURCE 1 |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 78 | #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 | |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 89 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | struct test_case_t { /* test case structure */ |
| 91 | uid_t *rgid; /* real GID */ |
| 92 | uid_t *egid; /* effective GID */ |
| 93 | uid_t *sgid; /* saved GID */ |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 94 | 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 | |
| 99 | char *TCID = "setresgid03"; |
| 100 | static int testno; |
| 101 | static struct passwd nobody, bin, root; |
| 102 | static uid_t nobody_gid, bin_gid, neg = -1; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | static int exp_enos[] = { EPERM, 0 }; |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 104 | |
| 105 | static int test_functionality(uid_t, uid_t, uid_t); |
| 106 | static void setup(void); |
| 107 | static void cleanup(void); |
| 108 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | static 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}, |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); |
| 117 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 118 | int main(int argc, char **argv) |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 119 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 120 | int lc; /* loop counter */ |
| 121 | char *msg; /* message returned from parse_opts */ |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 122 | |
| 123 | /* parse standard options */ |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 124 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != |
| 125 | NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 126 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | setup(); |
| 130 | |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 131 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 138 | *tdat[testno].sgid)); |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 139 | |
| 140 | TEST_ERROR_LOG(TEST_ERRNO); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 141 | if ((TEST_RETURN == EXP_RET_VAL) && |
| 142 | (TEST_ERRNO == EXP_ERRNO)) { |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 143 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 144 | if (!test_functionality |
| 145 | (tdat[testno].exp_rgid->pw_gid, |
| 146 | tdat[testno].exp_egid->pw_gid, |
| 147 | tdat[testno].exp_sgid->pw_gid)) { |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 148 | |
| 149 | tst_resm(TPASS, "setresgid() failed as " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 150 | "expected for %s : errno %d", |
| 151 | TEST_DESC, TEST_ERRNO); |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 152 | } else { |
| 153 | tst_resm(TFAIL, "Functionality test " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | "for setresgid() for %s failed", |
| 155 | TEST_DESC); |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | } else { |
| 159 | tst_resm(TFAIL, "setresgid() returned " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 160 | "unexpected results for %s ; returned" |
vapier | b0b6f45 | 2009-08-28 14:37:06 +0000 | [diff] [blame] | 161 | " %ld (expected %d), errno %d (expected" |
| 162 | " %d)", TEST_DESC, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 163 | TEST_RETURN, EXP_RET_VAL, TEST_ERRNO, |
| 164 | EXP_ERRNO); |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | cleanup(); |
| 169 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 170 | tst_exit(); |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 171 | } |
| 172 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 173 | static int test_functionality(uid_t exp_rgid, uid_t exp_egid, uid_t exp_sgid) |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 174 | { |
| 175 | uid_t cur_rgid, cur_egid, cur_sgid; |
| 176 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 177 | /* |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 178 | * Perform functional verification, if STD_FUNCTIONAL_TEST is |
| 179 | * set (-f options is not used) |
| 180 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 181 | if (STD_FUNCTIONAL_TEST == 0) { |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | /* Get current real, effective and saved group id */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 185 | if (getresgid(&cur_rgid, &cur_egid, &cur_sgid) == -1) { |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 186 | tst_brkm(TBROK, cleanup, "getresgid() failed"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 187 | |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 188 | } |
| 189 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 190 | if ((cur_rgid == exp_rgid) && (cur_egid == exp_egid) |
| 191 | && (cur_sgid == exp_sgid)) { |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 192 | return 0; |
| 193 | } |
| 194 | return 1; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * setup() |
| 199 | * performs all ONE TIME setup for this test |
| 200 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 201 | void setup(void) |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 202 | { |
| 203 | struct passwd *passwd_p; |
| 204 | |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 205 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 206 | |
| 207 | /* Check whether we are root */ |
| 208 | if (geteuid() != 0) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 209 | tst_brkm(TBROK, NULL, "Must be root for this test!"); |
| 210 | } |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 211 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 212 | if ((passwd_p = getpwnam("root")) == NULL) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 213 | tst_brkm(TBROK, NULL, "getpwnam() failed for root"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 214 | |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 215 | } |
| 216 | root = *passwd_p; |
| 217 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 218 | if ((passwd_p = getpwnam("bin")) == NULL) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 219 | tst_brkm(TBROK, NULL, "bin user id doesn't exist"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 220 | |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 221 | } |
| 222 | bin = *passwd_p; |
| 223 | bin_gid = bin.pw_gid; |
| 224 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 225 | if ((passwd_p = getpwnam("nobody")) == NULL) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 226 | tst_brkm(TBROK, NULL, "nobody user id doesn't exist"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 227 | |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 228 | } |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 236 | if (setresgid(nobody_gid, nobody_gid, nobody_gid) == -1) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 237 | tst_brkm(TBROK, NULL, "setup() failed for setting while" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 238 | " setting real/effective/saved gid"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 239 | |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 240 | } |
| 241 | /* Set euid to nobody */ |
| 242 | if (setuid(nobody.pw_uid) == -1) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 243 | tst_brkm(TBROK, NULL, "setuid failed to " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 244 | "to set the effective uid to nodody"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 245 | |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 246 | } |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 258 | void cleanup(void) |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 259 | { |
| 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 Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 267 | } |