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" |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 54 | #include <pwd.h> |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 55 | #include "compat_16.h" |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 56 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 57 | TCID_DEFINE(setresuid04); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 58 | int TST_TOTAL = 1; |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 59 | char nobody_uid[] = "nobody"; |
| 60 | char testfile[256] = ""; |
| 61 | struct passwd *ltpuser; |
| 62 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 63 | int fd = -1; |
| 64 | |
| 65 | void setup(void); |
| 66 | void cleanup(void); |
| 67 | void do_master_child(); |
| 68 | |
| 69 | int main(int ac, char **av) |
| 70 | { |
| 71 | pid_t pid; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 72 | const char *msg; |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 73 | |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 74 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 75 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 76 | setup(); |
| 77 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 78 | pid = FORK_OR_VFORK(); |
| 79 | if (pid < 0) |
| 80 | tst_brkm(TBROK, cleanup, "Fork failed"); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 81 | |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 82 | if (pid == 0) |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 83 | do_master_child(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 84 | |
Stanislav Kholmanskikh | 0a45153 | 2014-11-28 17:36:21 +0300 | [diff] [blame] | 85 | tst_record_childstatus(cleanup, pid); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 86 | |
| 87 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 88 | tst_exit(); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
| 92 | * do_master_child() |
| 93 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 94 | void do_master_child(void) |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 95 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 96 | int lc; |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 97 | int pid; |
| 98 | int status; |
| 99 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 100 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 101 | int tst_fd; |
| 102 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 103 | /* Reset tst_count in case we are looping */ |
| 104 | tst_count = 0; |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 105 | |
Stanislav Kholmanskikh | 0a45153 | 2014-11-28 17:36:21 +0300 | [diff] [blame] | 106 | if (SETRESUID(NULL, 0, ltpuser->pw_uid, 0) == -1) { |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 107 | perror("setresuid failed"); |
Stanislav Kholmanskikh | 0a45153 | 2014-11-28 17:36:21 +0300 | [diff] [blame] | 108 | exit(TFAIL); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* Test 1: Check the process with new uid cannot open the file |
| 112 | * with RDWR permissions. |
| 113 | */ |
| 114 | TEST(tst_fd = open(testfile, O_RDWR)); |
| 115 | |
| 116 | if (TEST_RETURN != -1) { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 117 | printf("open succeeded unexpectedly\n"); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 118 | close(tst_fd); |
Stanislav Kholmanskikh | 0a45153 | 2014-11-28 17:36:21 +0300 | [diff] [blame] | 119 | exit(TFAIL); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | if (TEST_ERRNO == EACCES) { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 123 | printf("open failed with EACCES as expected\n"); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 124 | } else { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 125 | perror("open failed unexpectedly"); |
Stanislav Kholmanskikh | 0a45153 | 2014-11-28 17:36:21 +0300 | [diff] [blame] | 126 | exit(TFAIL); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /* Test 2: Check a son process cannot open the file |
| 130 | * with RDWR permissions. |
| 131 | */ |
| 132 | pid = FORK_OR_VFORK(); |
| 133 | if (pid < 0) |
Stanislav Kholmanskikh | 0a45153 | 2014-11-28 17:36:21 +0300 | [diff] [blame] | 134 | tst_brkm(TBROK, NULL, "Fork failed"); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 135 | |
| 136 | if (pid == 0) { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 137 | int tst_fd2; |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 138 | |
| 139 | /* Test to open the file in son process */ |
| 140 | TEST(tst_fd2 = open(testfile, O_RDWR)); |
| 141 | |
| 142 | if (TEST_RETURN != -1) { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 143 | printf("call succeeded unexpectedly\n"); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 144 | close(tst_fd2); |
Stanislav Kholmanskikh | 0a45153 | 2014-11-28 17:36:21 +0300 | [diff] [blame] | 145 | exit(TFAIL); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 146 | } |
| 147 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 148 | if (TEST_ERRNO == EACCES) { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 149 | printf("open failed with EACCES as expected\n"); |
Stanislav Kholmanskikh | 0a45153 | 2014-11-28 17:36:21 +0300 | [diff] [blame] | 150 | exit(TPASS); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 151 | } else { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 152 | printf("open failed unexpectedly\n"); |
Stanislav Kholmanskikh | 0a45153 | 2014-11-28 17:36:21 +0300 | [diff] [blame] | 153 | exit(TFAIL); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 154 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 155 | } else { |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 156 | /* Wait for son completion */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 157 | if (waitpid(pid, &status, 0) == -1) { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 158 | perror("waitpid failed"); |
Stanislav Kholmanskikh | 0a45153 | 2014-11-28 17:36:21 +0300 | [diff] [blame] | 159 | exit(TFAIL); |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 160 | } |
Stanislav Kholmanskikh | 0a45153 | 2014-11-28 17:36:21 +0300 | [diff] [blame] | 161 | |
| 162 | if (!WIFEXITED(status)) |
| 163 | exit(TFAIL); |
| 164 | |
| 165 | if (WEXITSTATUS(status) != TPASS) |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 166 | exit(WEXITSTATUS(status)); |
| 167 | } |
| 168 | |
| 169 | /* Test 3: Fallback to initial uid and check we can again open |
| 170 | * the file with RDWR permissions. |
| 171 | */ |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 172 | tst_count++; |
Stanislav Kholmanskikh | 0a45153 | 2014-11-28 17:36:21 +0300 | [diff] [blame] | 173 | if (SETRESUID(NULL, 0, 0, 0) == -1) { |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 174 | perror("setresuid failed"); |
Stanislav Kholmanskikh | 0a45153 | 2014-11-28 17:36:21 +0300 | [diff] [blame] | 175 | exit(TFAIL); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | TEST(tst_fd = open(testfile, O_RDWR)); |
| 179 | |
| 180 | if (TEST_RETURN == -1) { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 181 | perror("open failed unexpectedly"); |
Stanislav Kholmanskikh | 0a45153 | 2014-11-28 17:36:21 +0300 | [diff] [blame] | 182 | exit(TFAIL); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 183 | } else { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 184 | printf("open call succeeded\n"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 185 | close(tst_fd); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
Stanislav Kholmanskikh | 0a45153 | 2014-11-28 17:36:21 +0300 | [diff] [blame] | 188 | exit(TPASS); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* |
| 192 | * setup() - performs all ONE TIME setup for this test |
| 193 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 194 | void setup(void) |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 195 | { |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 196 | tst_require_root(NULL); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 197 | |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 198 | ltpuser = getpwnam(nobody_uid); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 199 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 200 | UID16_CHECK(ltpuser->pw_uid, "setresuid", cleanup) |
| 201 | |
subrata_modak | 9e89d9a | 2008-09-08 08:52:52 +0000 | [diff] [blame] | 202 | tst_tmpdir(); |
| 203 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 204 | sprintf(testfile, "setresuid04file%d.tst", getpid()); |
| 205 | |
| 206 | /* Create test file */ |
| 207 | fd = open(testfile, O_CREAT | O_RDWR, 0644); |
| 208 | if (fd < 0) |
| 209 | tst_brkm(TBROK, cleanup, "cannot creat test file"); |
| 210 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 211 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 212 | |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 213 | TEST_PAUSE; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 218 | * or premature exit |
| 219 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 220 | void cleanup(void) |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 221 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 222 | close(fd); |
subrata_modak | cbfac50 | 2008-08-19 07:23:42 +0000 | [diff] [blame] | 223 | |
subrata_modak | 9e89d9a | 2008-09-08 08:52:52 +0000 | [diff] [blame] | 224 | tst_rmdir(); |
| 225 | |
Garrett Cooper | 1da938b | 2011-02-23 21:21:15 -0800 | [diff] [blame] | 226 | } |