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 | * setresuid02.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Test that a non-root user can change the real, effective and saved |
| 26 | * uid values through the setresuid system call. |
| 27 | * |
| 28 | * ALGORITHM |
| 29 | * |
| 30 | * Setup: |
| 31 | * Setup signal handling |
| 32 | * Get user information. |
| 33 | * Pause for SIGUSER1 if option specified. |
| 34 | * |
| 35 | * Setup test values. |
| 36 | * Loop if the proper options are given. |
| 37 | * For each test set execute the system call |
| 38 | * Check that we received the expected result. |
| 39 | * Verify that the uid, euid and suid values are still correct. |
| 40 | * Cleanup: |
| 41 | * Print errno log and/or timing stats if option given. |
| 42 | * |
| 43 | * USAGE: <for command-line> |
| 44 | * setresuid02 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 45 | * where, -c n : Run n copies concurrently. |
| 46 | * -f : Turn off functionality Testing. |
| 47 | * -i n : Execute test n times. |
| 48 | * -I x : Execute test for x seconds. |
| 49 | * -P x : Pause for x seconds between iterations. |
| 50 | * -t : Turn on syscall timing. |
| 51 | * |
| 52 | * History |
| 53 | * 07/2001 John George |
| 54 | * -Ported |
| 55 | * |
| 56 | * Restrictions |
| 57 | * This test must be run by root. |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 58 | * nobody and bin must be a valid users. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | */ |
| 60 | |
vapier | bed036a | 2006-05-26 06:48:55 +0000 | [diff] [blame] | 61 | #define _GNU_SOURCE 1 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | #include <pwd.h> |
Garrett Cooper | bacc849 | 2011-01-14 00:36:17 -0800 | [diff] [blame] | 63 | #include <stdlib.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 64 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | #include <errno.h> |
| 66 | #include <sys/wait.h> |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 67 | #include "compat_16.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 69 | TCID_DEFINE(setresuid02); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | |
vapier | c4e1c24 | 2006-05-26 06:39:42 +0000 | [diff] [blame] | 71 | uid_t neg_one = -1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
| 73 | /* flag to tell parent if child passed or failed. */ |
| 74 | int flag = 0; |
| 75 | |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 76 | uid_t nobody_pw_uid, bin_pw_uid; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | char user1name[] = "nobody"; |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 78 | char user2name[] = "bin"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 80 | struct passwd nobody, bin; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * The following structure contains all test data. Each structure in the array |
| 84 | * is used for a separate test. The tests are executed in the for loop below. |
| 85 | */ |
| 86 | |
| 87 | struct test_data_t { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | uid_t *real_uid; |
| 89 | uid_t *eff_uid; |
| 90 | uid_t *sav_uid; |
| 91 | struct passwd *exp_real_usr; |
| 92 | struct passwd *exp_eff_usr; |
| 93 | struct passwd *exp_sav_usr; |
| 94 | char *test_msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | } test_data[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 96 | { |
| 97 | &neg_one, &neg_one, &bin_pw_uid, &nobody, &bin, &bin, |
| 98 | "After setresuid(-1, -1, bin),"}, { |
| 99 | &neg_one, &nobody_pw_uid, &neg_one, &nobody, &nobody, &bin, |
| 100 | "After setresuid(-1, nobody -1),"}, { |
| 101 | &bin_pw_uid, &neg_one, &neg_one, &bin, &nobody, &bin, |
| 102 | "After setresuid(bin, -1 -1),"},}; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 104 | int TST_TOTAL = sizeof(test_data) / sizeof(test_data[0]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | |
| 106 | void setup(void); |
| 107 | void cleanup(void); |
| 108 | |
| 109 | void |
| 110 | uid_verify(struct passwd *ru, struct passwd *eu, struct passwd *su, char *); |
| 111 | |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 112 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | { |
| 114 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 115 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 117 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 119 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 120 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | setup(); |
| 123 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 125 | int i, pid; |
| 126 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 127 | /* reset tst_count in case we are looping */ |
| 128 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | |
| 130 | /* set the appropriate ownership values */ |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 131 | if (setresuid(nobody_pw_uid, bin_pw_uid, nobody_pw_uid) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | tst_brkm(TFAIL, cleanup, "Initial setresuid failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 133 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 135 | if ((pid = FORK_OR_VFORK()) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | tst_brkm(TBROK, cleanup, "fork failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 137 | } else if (pid == 0) { /* child */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | |
| 139 | for (i = 0; i < TST_TOTAL; i++) { |
| 140 | |
| 141 | /* Set the real, effective or saved user id */ |
Stanislav Kholmanskikh | 0a45153 | 2014-11-28 17:36:21 +0300 | [diff] [blame] | 142 | TEST(SETRESUID(NULL, *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) { |
| 147 | tst_resm(TPASS, "setresuid(%d, %d, %d) " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 148 | "succeeded as expected.", |
| 149 | *test_data[i].real_uid, |
| 150 | *test_data[i].eff_uid, |
| 151 | *test_data[i].sav_uid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | } else { |
| 153 | tst_resm(TFAIL, "setresuid(%d, %d, %d) " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | "did not return as expected.", |
| 155 | *test_data[i].real_uid, |
| 156 | *test_data[i].eff_uid, |
| 157 | *test_data[i].sav_uid); |
| 158 | flag = -1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 161 | uid_verify(test_data[i].exp_real_usr, |
| 162 | test_data[i].exp_eff_usr, |
| 163 | test_data[i].exp_sav_usr, |
| 164 | test_data[i].test_msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | } |
| 166 | exit(flag); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 167 | } else { /* parent */ |
Stanislav Kholmanskikh | 0a45153 | 2014-11-28 17:36:21 +0300 | [diff] [blame] | 168 | tst_record_childstatus(cleanup, pid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 172 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* |
| 176 | * setup() |
| 177 | * performs all ONE TIME setup for this test |
| 178 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 179 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | { |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 181 | tst_require_root(NULL); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 182 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 184 | |
| 185 | if (getpwnam("nobody") == NULL) { |
| 186 | tst_brkm(TBROK, NULL, "nobody must be a valid user."); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 187 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 189 | if (getpwnam("bin") == NULL) { |
| 190 | tst_brkm(TBROK, NULL, "bin must be a valid user."); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 191 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 193 | nobody = *(getpwnam("nobody")); |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 194 | UID16_CHECK((nobody_pw_uid = nobody.pw_uid), "setresuid", cleanup) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 195 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 196 | bin = *(getpwnam("bin")); |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 197 | UID16_CHECK((bin_pw_uid = bin.pw_uid), "setresuid", cleanup) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | |
| 199 | /* Pause if that option was specified |
| 200 | * TEST_PAUSE contains the code to fork the test with the -i option. |
| 201 | * You want to make sure you do this before you create your temporary |
| 202 | * directory. |
| 203 | */ |
| 204 | TEST_PAUSE; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * cleanup() |
| 209 | * performs all ONE TIME cleanup for this test at |
| 210 | * completion or premature exit |
| 211 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 212 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 213 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 214 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 215 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 216 | |
| 217 | void |
| 218 | uid_verify(struct passwd *ru, struct passwd *eu, struct passwd *su, char *when) |
| 219 | { |
| 220 | uid_t cur_ru, cur_eu, cur_su; |
| 221 | if (getresuid(&cur_ru, &cur_eu, &cur_su) != 0) { |
| 222 | flag = -1; |
| 223 | tst_brkm(TBROK, cleanup, "Set getresuid() failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 224 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 225 | 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] | 226 | su->pw_uid)) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 227 | tst_resm(TFAIL, "ERROR: %s real uid = %d; effective uid = %d; " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 228 | "saved uid = %d", when, cur_ru, cur_eu, cur_su); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 229 | tst_resm(TINFO, "Expected: real uid = %d, effective uid = %d " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 230 | "saved uid = %d", ru->pw_uid, eu->pw_uid, su->pw_uid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 231 | flag = -1; |
| 232 | } else { |
| 233 | tst_resm(TINFO, "real uid = %d, effective uid = %d, and " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 234 | "saved uid = %d as expected", cur_ru, cur_eu, cur_su); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 235 | } |
Garrett Cooper | bacc849 | 2011-01-14 00:36:17 -0800 | [diff] [blame] | 236 | } |