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 : setresgid02 |
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 | * |
| 23 | * TEST TITLE : Checking functionality of setresgid(2) for |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 24 | * non-root group id. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 25 | * |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 26 | * TEST CASE TOTAL : 6 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 27 | * |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 28 | * AUTHOR : Madhu T L <madhu.tarikere@wipro.com> |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 29 | * |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 30 | * 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 42 | * |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 43 | * 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 48 | * |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 49 | * 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 56 | * |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 57 | * Cleanup: |
| 58 | * Print errno log and/or timing stats if options given |
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 | * 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. |
robbiew | 116125c | 2003-04-09 15:59:13 +0000 | [diff] [blame] | 71 | * |
| 72 | * CHANGE: Madhu T L <madhu.tarikere@wipro.com> |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 73 | * Date: April 9 2003 |
| 74 | * Replaced setegid() by setresgid() in setup() |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 75 | ****************************************************************/ |
robbiew | 116125c | 2003-04-09 15:59:13 +0000 | [diff] [blame] | 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" |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 83 | #include "compat_16.h" |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 84 | |
| 85 | #define EXP_RET_VAL 0 |
| 86 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 87 | struct test_case_t { /* test case structure */ |
| 88 | uid_t *rgid; /* real GID */ |
| 89 | uid_t *egid; /* effective GID */ |
| 90 | uid_t *sgid; /* saved GID */ |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 91 | struct passwd *exp_rgid; /* Expected real GID */ |
| 92 | struct passwd *exp_egid; /* Expected effective GID */ |
| 93 | struct passwd *exp_sgid; /* Expected saved GID */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 94 | char *desc; /* Test description */ |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 97 | TCID_DEFINE(setresgid02); |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 98 | static int testno; |
| 99 | static struct passwd nobody, bin, root; |
| 100 | static uid_t nobody_gid, root_gid, bin_gid, neg = -1; |
| 101 | |
| 102 | static int test_functionality(uid_t, uid_t, uid_t); |
| 103 | static void setup(void); |
| 104 | static void cleanup(void); |
| 105 | |
| 106 | /* Don't change order of these test cases */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | static 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)"}, |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); |
| 123 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 124 | int main(int argc, char **argv) |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 125 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 126 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 127 | const char *msg; |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 128 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 129 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 130 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | setup(); |
| 134 | |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 135 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 136 | /* reset tst_count in case we are looping */ |
| 137 | tst_count = 0; |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 138 | |
| 139 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
| 140 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 141 | TEST(SETRESGID(cleanup, *tdat[testno].rgid, *tdat[testno].egid, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 142 | *tdat[testno].sgid)); |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 143 | |
| 144 | if (TEST_RETURN == EXP_RET_VAL) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 145 | if (!test_functionality |
| 146 | (tdat[testno].exp_rgid->pw_gid, |
| 147 | tdat[testno].exp_egid->pw_gid, |
| 148 | tdat[testno].exp_sgid->pw_gid)) { |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 149 | |
| 150 | tst_resm(TPASS, "Test for %s " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 151 | "successful", |
| 152 | tdat[testno].desc); |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 153 | } else { |
| 154 | tst_resm(TFAIL, "Functionality test " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 155 | "for %s failed", |
| 156 | tdat[testno].desc); |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 157 | } |
| 158 | } else { |
| 159 | tst_resm(TFAIL, "Test for %s failed; returned" |
vapier | b0b6f45 | 2009-08-28 14:37:06 +0000 | [diff] [blame] | 160 | " %ld (expected %d), errno %d (expected" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 161 | " 0)", tdat[testno].desc, |
| 162 | TEST_RETURN, EXP_RET_VAL, TEST_ERRNO); |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | cleanup(); |
| 167 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 168 | tst_exit(); |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 169 | } |
| 170 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 171 | 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] | 172 | { |
| 173 | uid_t cur_rgid, cur_egid, cur_sgid; |
| 174 | |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 175 | /* Get current real, effective and saved group id */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 176 | if (getresgid(&cur_rgid, &cur_egid, &cur_sgid) == -1) { |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 177 | tst_brkm(TBROK, cleanup, "getresgid() failed"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 178 | |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 179 | } |
| 180 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 181 | if ((cur_rgid == exp_rgid) && (cur_egid == exp_egid) |
| 182 | && (cur_sgid == exp_sgid)) { |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | return 1; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * setup() |
| 190 | * performs all ONE TIME setup for this test |
| 191 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 192 | void setup(void) |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 193 | { |
| 194 | struct passwd *passwd_p; |
| 195 | |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 196 | tst_require_root(NULL); |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 197 | |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 198 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 199 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 200 | if ((passwd_p = getpwnam("root")) == NULL) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 201 | tst_brkm(TBROK, NULL, "getpwnam() failed for root"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 202 | |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 203 | } |
| 204 | root = *passwd_p; |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 205 | GID16_CHECK((root_gid = root.pw_gid), "setresgid", cleanup) |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 206 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 207 | if ((passwd_p = getpwnam("bin")) == NULL) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 208 | tst_brkm(TBROK, NULL, "bin user id doesn't exist"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 209 | |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 210 | } |
| 211 | bin = *passwd_p; |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 212 | GID16_CHECK((bin_gid = bin.pw_gid), "setresgid", cleanup) |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 213 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 214 | if ((passwd_p = getpwnam("nobody")) == NULL) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 215 | tst_brkm(TBROK, NULL, "nobody user id doesn't exist"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 216 | |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 217 | } |
| 218 | nobody = *passwd_p; |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 219 | GID16_CHECK((nobody_gid = nobody.pw_gid), "setresgid", cleanup) |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 220 | |
robbiew | 116125c | 2003-04-09 15:59:13 +0000 | [diff] [blame] | 221 | /* Set effective/saved gid to nobody */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 222 | if (setresgid(-1, nobody_gid, nobody_gid) == -1) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 223 | tst_brkm(TBROK, NULL, "setup() failed for setting while" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 224 | " setting real/effective/saved gid"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 225 | |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 226 | } |
robbiew | 116125c | 2003-04-09 15:59:13 +0000 | [diff] [blame] | 227 | |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 239 | void cleanup(void) |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 240 | { |
robbiew | 6f906ea | 2002-12-27 18:46:55 +0000 | [diff] [blame] | 241 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 242 | } |