plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * setresuid01.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Test setresuid() when executed by root. |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * |
| 29 | * Setup: |
| 30 | * Setup signal handling |
| 31 | * Get user information. |
| 32 | * Pause for SIGUSER1 if option specified. |
| 33 | * Setup test values. |
| 34 | * Loop if the proper options are given. |
| 35 | * For each test execute the system call |
| 36 | * Check return code, if system call failed (return=-1) |
| 37 | * Log the errno and Issue a FAIL message. |
| 38 | * Otherwise, |
| 39 | * Verify the Functionality of system call |
| 40 | * if successful, |
| 41 | * Issue Functionality-Pass message. |
| 42 | * Otherwise, |
| 43 | * Issue Functionality-Fail message. |
| 44 | * Cleanup: |
| 45 | * Print errno log and/or timing stats if options given. |
| 46 | * |
| 47 | * USAGE: <for command-line> |
| 48 | * setresuid01 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t] |
| 49 | * where, -c n : Run n copies concurrently. |
| 50 | * -e : Turn on errno logging. |
| 51 | * -f : Turn off functionality Testing. |
| 52 | * -i n : Execute test n times. |
| 53 | * -I x : Execute test for x seconds. |
| 54 | * -P x : Pause for x seconds between iterations. |
| 55 | * -t : Turn on syscall timing. |
| 56 | * History |
| 57 | * 07/2001 John George |
| 58 | * -Ported |
| 59 | * |
| 60 | * Restrictions |
| 61 | * This test must be ran as root. |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 62 | * nobody and bin must be valid users. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | */ |
| 64 | |
vapier | bed036a | 2006-05-26 06:48:55 +0000 | [diff] [blame] | 65 | #define _GNU_SOURCE 1 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | #include <pwd.h> |
Garrett Cooper | bacc849 | 2011-01-14 00:36:17 -0800 | [diff] [blame] | 67 | #include <stdlib.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | #include <string.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 69 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | #include <errno.h> |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 71 | #include "compat_16.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 73 | TCID_DEFINE(setresuid01); |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 74 | |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 75 | uid_t nobody_pw_uid, root_pw_uid, bin_pw_uid; |
vapier | c4e1c24 | 2006-05-26 06:39:42 +0000 | [diff] [blame] | 76 | uid_t neg_one = -1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 78 | struct passwd nobody, bin, root; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
| 80 | /* |
| 81 | * The following structure contains all test data. Each structure in the array |
| 82 | * is used for a separate test. The tests are executed in the for loop below. |
| 83 | */ |
| 84 | |
| 85 | struct test_data_t { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | uid_t *real_uid; |
| 87 | uid_t *eff_uid; |
| 88 | uid_t *sav_uid; |
| 89 | struct passwd *exp_real_usr; |
| 90 | struct passwd *exp_eff_usr; |
| 91 | struct passwd *exp_sav_usr; |
| 92 | char *test_msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | } test_data[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 94 | { |
| 95 | &neg_one, &neg_one, &neg_one, &root, &root, &root, |
| 96 | "After setresuid(-1, -1, -1),"}, { |
| 97 | &neg_one, &neg_one, &nobody_pw_uid, &root, &root, &nobody, |
| 98 | "After setresuid(-1, -1, nobody),"}, { |
| 99 | &neg_one, &bin_pw_uid, &neg_one, &root, &bin, &nobody, |
| 100 | "After setresuid(-1, bin, -1),"}, { |
| 101 | &neg_one, &neg_one, &root_pw_uid, &root, &bin, &root, |
| 102 | "After setresuid(-1, -1, root),"}, { |
| 103 | &neg_one, &neg_one, &bin_pw_uid, &root, &bin, &bin, |
| 104 | "After setresuid(-1, -1, bin),"}, { |
| 105 | &neg_one, &root_pw_uid, &neg_one, &root, &root, &bin, |
| 106 | "After setresuid(-1, root, -1),"}, { |
| 107 | &nobody_pw_uid, &neg_one, &neg_one, &nobody, &root, &bin, |
| 108 | "After setresuid(nobody, -1, -1)"}, { |
| 109 | &neg_one, &root_pw_uid, &neg_one, &nobody, &root, &bin, |
| 110 | "After setresuid(-1, root, -1),"}, { |
| 111 | &root_pw_uid, &neg_one, &root_pw_uid, &root, &root, &root, |
| 112 | "After setresuid(root, -1, -1),"},}; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | int TST_TOTAL = sizeof(test_data) / sizeof(test_data[0]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 116 | void setup(void); |
| 117 | void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | |
| 119 | void |
| 120 | uid_verify(struct passwd *ru, struct passwd *eu, struct passwd *su, char *when); |
| 121 | |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 122 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 124 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 125 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 127 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 129 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 130 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | setup(); |
| 133 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 135 | int i; |
| 136 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 137 | /* reset tst_count in case we are looping */ |
| 138 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | |
| 140 | for (i = 0; i < TST_TOTAL; i++) { |
| 141 | /* Set the real, effective or user id */ |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 142 | TEST(SETRESUID(cleanup, *test_data[i].real_uid, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | *test_data[i].eff_uid, |
| 144 | *test_data[i].sav_uid)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | |
| 146 | if (TEST_RETURN == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | tst_resm(TFAIL, "setresuid(%d, %d, %d) failed", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 148 | *test_data[i].real_uid, |
| 149 | *test_data[i].eff_uid, |
| 150 | *test_data[i].sav_uid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 152 | uid_verify(test_data[i].exp_real_usr, |
| 153 | test_data[i].exp_eff_usr, |
| 154 | test_data[i].exp_sav_usr, |
| 155 | test_data[i].test_msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 159 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 161 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* |
| 165 | * setup() |
| 166 | * performs all ONE TIME setup for this test |
| 167 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 168 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | { |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 170 | tst_require_root(NULL); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 171 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 173 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | if (getpwnam("nobody") == NULL) { |
| 175 | tst_brkm(TBROK, NULL, "nobody must be a valid user."); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 176 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 178 | if (getpwnam("bin") == NULL) { |
| 179 | tst_brkm(TBROK, NULL, "bin must be a valid user."); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 180 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | root = *(getpwnam("root")); |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 183 | UID16_CHECK((root_pw_uid = root.pw_uid), "setresuid", cleanup) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | |
| 185 | nobody = *(getpwnam("nobody")); |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 186 | UID16_CHECK((nobody_pw_uid = nobody.pw_uid), "setresuid", cleanup) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 188 | bin = *(getpwnam("bin")); |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 189 | UID16_CHECK((bin_pw_uid = bin.pw_uid), "setresuid", cleanup) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 190 | |
| 191 | /* Pause if that option was specified |
| 192 | * TEST_PAUSE contains the code to fork the test with the -c option. |
| 193 | */ |
| 194 | TEST_PAUSE; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * cleanup() |
| 199 | * performs all ONE TIME cleanup for this test at |
| 200 | * completion or premature exit |
| 201 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 202 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 203 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 205 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | |
| 207 | void |
| 208 | uid_verify(struct passwd *ru, struct passwd *eu, struct passwd *su, char *when) |
| 209 | { |
| 210 | uid_t cur_ru, cur_eu, cur_su; |
| 211 | if (getresuid(&cur_ru, &cur_eu, &cur_su) != 0) { |
| 212 | tst_brkm(TBROK, cleanup, "Set getresuid() failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 213 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 214 | if ((cur_ru != ru->pw_uid) || (cur_eu != eu->pw_uid) || (cur_su != |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 215 | su->pw_uid)) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 216 | tst_resm(TFAIL, "ERROR: %s real uid = %d; effective uid = %d; " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 217 | "saved uid = %d", when, cur_ru, cur_eu, cur_su); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 218 | tst_resm(TINFO, "Expected: real uid = %d, effective uid = %d " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 219 | "saved uid = %d", ru->pw_uid, eu->pw_uid, su->pw_uid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 220 | } else { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 221 | tst_resm(TPASS, |
| 222 | "real uid = %d, effective uid = %d, and saved uid = " |
| 223 | "%d as expected", cur_ru, cur_eu, cur_su); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 224 | } |
Garrett Cooper | bacc849 | 2011-01-14 00:36:17 -0800 | [diff] [blame] | 225 | } |