subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 1 | /******************************************************************************/ |
| 2 | /* Copyright (c) Kerlabs 2008. */ |
| 3 | /* Copyright (c) International Business Machines Corp., 2008 */ |
| 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 */ |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 18 | /* */ |
| 19 | /******************************************************************************/ |
| 20 | /* |
| 21 | * NAME |
| 22 | * setresuid04.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Check if setresuid behaves correctly with file permissions. |
| 26 | * The test creates a file as ROOT with permissions 0644, does a setresuid |
| 27 | * and then tries to open the file with RDWR permissions. |
| 28 | * The same test is done in a fork to check if new UIDs are correctly |
| 29 | * passed to the son. |
| 30 | * |
| 31 | * USAGE: <for command-line> |
| 32 | * setresuid04 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 33 | * where, -c n : Run n copies concurrently. |
| 34 | * -e : Turn on errno logging. |
| 35 | * -i n : Execute test n times. |
| 36 | * -I x : Execute test for x seconds. |
| 37 | * -P x : Pause for x seconds between iterations. |
| 38 | * -t : Turn on syscall timing. |
| 39 | * |
| 40 | * HISTORY |
| 41 | * 07/2001 Created by Renaud Lottiaux |
| 42 | * |
| 43 | * RESTRICTIONS |
| 44 | * Must be run as root. |
| 45 | */ |
| 46 | #define _GNU_SOURCE 1 |
| 47 | #include <errno.h> |
| 48 | #include <sys/types.h> |
| 49 | #include <sys/stat.h> |
| 50 | #include <sys/wait.h> |
| 51 | #include <fcntl.h> |
| 52 | #include <unistd.h> |
| 53 | #include "test.h" |
| 54 | #include "usctest.h" |
| 55 | #include <pwd.h> |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame^] | 56 | #include "compat_16.h" |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 57 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame^] | 58 | TCID_DEFINE(setresuid04); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 59 | int TST_TOTAL = 1; |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 60 | char nobody_uid[] = "nobody"; |
| 61 | char testfile[256] = ""; |
| 62 | struct passwd *ltpuser; |
| 63 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 64 | int exp_enos[] = { EACCES, 0 }; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 65 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 66 | int fd = -1; |
| 67 | |
| 68 | void setup(void); |
| 69 | void cleanup(void); |
| 70 | void do_master_child(); |
| 71 | |
| 72 | int main(int ac, char **av) |
| 73 | { |
| 74 | pid_t pid; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 75 | const char *msg; |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 76 | int status; |
| 77 | |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 78 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 79 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 80 | setup(); |
| 81 | |
| 82 | TEST_EXP_ENOS(exp_enos); |
| 83 | |
| 84 | pid = FORK_OR_VFORK(); |
| 85 | if (pid < 0) |
| 86 | tst_brkm(TBROK, cleanup, "Fork failed"); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 87 | |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 88 | if (pid == 0) |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 89 | do_master_child(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 90 | |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 91 | if (waitpid(pid, &status, 0) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 92 | tst_resm(TBROK | TERRNO, "waitpid failed"); |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 93 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) |
| 94 | tst_resm(TFAIL, "child process terminated abnormally"); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 95 | |
| 96 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 97 | tst_exit(); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* |
| 101 | * do_master_child() |
| 102 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 103 | void do_master_child(void) |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 104 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 105 | int lc; |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 106 | int pid; |
| 107 | int status; |
| 108 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 109 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 110 | int tst_fd; |
| 111 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 112 | /* Reset tst_count in case we are looping */ |
| 113 | tst_count = 0; |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 114 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame^] | 115 | if (SETRESUID(cleanup, 0, ltpuser->pw_uid, 0) == -1) { |
| 116 | perror("setresuid failed"); |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 117 | exit(1); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* Test 1: Check the process with new uid cannot open the file |
| 121 | * with RDWR permissions. |
| 122 | */ |
| 123 | TEST(tst_fd = open(testfile, O_RDWR)); |
| 124 | |
| 125 | if (TEST_RETURN != -1) { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 126 | printf("open succeeded unexpectedly\n"); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 127 | close(tst_fd); |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 128 | exit(1); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | if (TEST_ERRNO == EACCES) { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 132 | printf("open failed with EACCES as expected\n"); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 133 | } else { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 134 | perror("open failed unexpectedly"); |
| 135 | exit(1); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* Test 2: Check a son process cannot open the file |
| 139 | * with RDWR permissions. |
| 140 | */ |
| 141 | pid = FORK_OR_VFORK(); |
| 142 | if (pid < 0) |
| 143 | tst_brkm(TBROK, cleanup, "Fork failed"); |
| 144 | |
| 145 | if (pid == 0) { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 146 | int tst_fd2; |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 147 | |
| 148 | /* Test to open the file in son process */ |
| 149 | TEST(tst_fd2 = open(testfile, O_RDWR)); |
| 150 | |
| 151 | if (TEST_RETURN != -1) { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 152 | printf("call succeeded unexpectedly\n"); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 153 | close(tst_fd2); |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 154 | exit(1); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | TEST_ERROR_LOG(TEST_ERRNO); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 158 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 159 | if (TEST_ERRNO == EACCES) { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 160 | printf("open failed with EACCES as expected\n"); |
| 161 | exit(0); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 162 | } else { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 163 | printf("open failed unexpectedly\n"); |
| 164 | exit(1); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 165 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 166 | } else { |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 167 | /* Wait for son completion */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 168 | if (waitpid(pid, &status, 0) == -1) { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 169 | perror("waitpid failed"); |
| 170 | exit(1); |
| 171 | } |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 172 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) |
| 173 | exit(WEXITSTATUS(status)); |
| 174 | } |
| 175 | |
| 176 | /* Test 3: Fallback to initial uid and check we can again open |
| 177 | * the file with RDWR permissions. |
| 178 | */ |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 179 | tst_count++; |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame^] | 180 | if (SETRESUID(cleanup, 0, 0, 0) == -1) { |
| 181 | perror("setresuid failed"); |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 182 | exit(1); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | TEST(tst_fd = open(testfile, O_RDWR)); |
| 186 | |
| 187 | if (TEST_RETURN == -1) { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 188 | perror("open failed unexpectedly"); |
| 189 | exit(1); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 190 | } else { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 191 | printf("open call succeeded\n"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 192 | close(tst_fd); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 195 | exit(0); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* |
| 199 | * setup() - 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) |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 202 | { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 203 | tst_require_root(NULL); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 204 | |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 205 | ltpuser = getpwnam(nobody_uid); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 206 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame^] | 207 | UID16_CHECK(ltpuser->pw_uid, "setresuid", cleanup) |
| 208 | |
subrata_modak | 9e89d9a | 2008-09-08 08:52:52 +0000 | [diff] [blame] | 209 | tst_tmpdir(); |
| 210 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 211 | sprintf(testfile, "setresuid04file%d.tst", getpid()); |
| 212 | |
| 213 | /* Create test file */ |
| 214 | fd = open(testfile, O_CREAT | O_RDWR, 0644); |
| 215 | if (fd < 0) |
| 216 | tst_brkm(TBROK, cleanup, "cannot creat test file"); |
| 217 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 218 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 219 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 220 | TEST_PAUSE; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 225 | * or premature exit |
| 226 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 227 | void cleanup(void) |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 228 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 229 | close(fd); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 230 | |
| 231 | /* |
| 232 | * print timing status if that option was specified |
| 233 | * print errno log if that option was specified |
| 234 | */ |
| 235 | TEST_CLEANUP; |
| 236 | |
subrata_modak | 9e89d9a | 2008-09-08 08:52:52 +0000 | [diff] [blame] | 237 | tst_rmdir(); |
| 238 | |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 239 | } |