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 | * rename09 |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * check rename() fails with EACCES |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * Setup: |
| 29 | * Setup signal handling. |
| 30 | * Create temporary directory. |
| 31 | * Pause for SIGUSR1 if option specified. |
| 32 | * |
| 33 | * Test: |
| 34 | * Loop if the proper options are given. |
| 35 | * fork the first child |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 36 | * set to be nobody |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 37 | * create old dir with mode 0700 |
| 38 | * creat a file under it |
| 39 | * fork the second child |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 40 | * set to bin |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 41 | * create new dir with mode 0700 |
| 42 | * create a "new" file under it |
| 43 | * try to rename file under old dir to file under new dir |
| 44 | * check the return value, if succeeded (return=0) |
| 45 | * Issue a FAIL message. |
| 46 | * Otherwise, |
| 47 | * Log the errno |
| 48 | * Verify the errno |
| 49 | * if equals to EACCESS, |
| 50 | * Issue Pass message. |
| 51 | * Otherwise, |
| 52 | * Issue Fail message. |
| 53 | * Cleanup: |
| 54 | * Print errno log and/or timing stats if options given |
| 55 | * Delete the temporary directory created. |
| 56 | * |
| 57 | * USAGE |
| 58 | * rename09 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 59 | * where, -c n : Run n copies concurrently. |
| 60 | * -e : Turn on errno logging. |
| 61 | * -i n : Execute test n times. |
| 62 | * -I x : Execute test for x seconds. |
| 63 | * -P x : Pause for x seconds between iterations. |
| 64 | * -t : Turn on syscall timing. |
| 65 | * |
| 66 | * HISTORY |
| 67 | * 07/2001 Ported by Wayne Boyer |
| 68 | * |
| 69 | * RESTRICTIONS |
| 70 | * Must run test as root. |
| 71 | * |
| 72 | */ |
| 73 | #include <errno.h> |
| 74 | #include <sys/types.h> |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 75 | #include <sys/stat.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | #include <fcntl.h> |
| 77 | #include <pwd.h> |
| 78 | #include <sys/wait.h> |
| 79 | #include <unistd.h> |
| 80 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 81 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | |
| 83 | void setup(); |
| 84 | void cleanup(); |
| 85 | extern void do_file_setup(char *); |
| 86 | extern struct passwd *my_getpwnam(char *); |
| 87 | |
| 88 | #define PERMS 0700 |
| 89 | |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 90 | char user1name[] = "nobody"; |
| 91 | char user2name[] = "bin"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 93 | char *TCID = "rename09"; |
| 94 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
| 96 | char fdir[255], mdir[255]; |
| 97 | char fname[255], mname[255]; |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 98 | struct passwd *nobody, *bin; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 100 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 102 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 103 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | int rval; |
| 105 | pid_t pid, pid1; |
| 106 | int status; |
| 107 | |
| 108 | /* |
| 109 | * parse standard options |
| 110 | */ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 111 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 112 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | * perform global setup for test |
| 116 | */ |
| 117 | setup(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 118 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | /* |
| 120 | * check looping state if -i option given |
| 121 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 122 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 123 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 124 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 126 | if ((pid = FORK_OR_VFORK()) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | tst_brkm(TBROK, cleanup, "fork() #1 failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 128 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 130 | if (pid == 0) { /* first child */ |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 131 | /* set to nobody */ |
| 132 | rval = setreuid(nobody->pw_uid, nobody->pw_uid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | if (rval < 0) { |
| 134 | tst_resm(TWARN, "setreuid failed to " |
| 135 | "to set the real uid to %d and " |
| 136 | "effective uid to %d", |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 137 | nobody->pw_uid, nobody->pw_uid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | perror("setreuid"); |
| 139 | exit(1); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 140 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | |
| 142 | /* create the a directory with 0700 permits */ |
| 143 | if (mkdir(fdir, PERMS) == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 144 | tst_resm(TWARN, "mkdir(%s, %#o) Failed", |
| 145 | fdir, PERMS); |
| 146 | exit(1); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 147 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | |
| 149 | /* create "old" file under it */ |
| 150 | do_file_setup(fname); |
| 151 | |
| 152 | exit(0); |
| 153 | } |
| 154 | |
| 155 | /* wait for child to exit */ |
| 156 | wait(&status); |
| 157 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) { |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 158 | tst_brkm(TBROK, cleanup, "First child failed to set " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | "up conditions for the test"); |
| 160 | } |
| 161 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 162 | if ((pid1 = FORK_OR_VFORK()) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | tst_brkm(TBROK, cleanup, "fork() #2 failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 164 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 166 | if (pid1 == 0) { /* second child */ |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 167 | /* set to bin */ |
| 168 | if ((rval = seteuid(bin->pw_uid)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | tst_resm(TWARN, "seteuid() failed"); |
| 170 | perror("setreuid"); |
| 171 | exit(1); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 172 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | |
| 174 | /* create "new" directory */ |
| 175 | if (mkdir(mdir, PERMS) == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 176 | tst_resm(TWARN, "mkdir(%s, %#o) failed", |
| 177 | mdir, PERMS); |
| 178 | exit(1); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 179 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 181 | /* create the new file */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | do_file_setup(mname); |
| 183 | |
| 184 | /* rename "old" to "new" */ |
| 185 | TEST(rename(fname, mname)); |
| 186 | if (TEST_RETURN != -1) { |
| 187 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 188 | continue; |
| 189 | } |
| 190 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | if (TEST_ERRNO != EACCES) { |
| 192 | tst_resm(TFAIL, "Expected EACCES got %d", |
| 193 | TEST_ERRNO); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 194 | } else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 195 | tst_resm(TPASS, "rename() returned EACCES"); |
| 196 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 197 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | /* set the process id back to root */ |
| 199 | if (seteuid(0) == -1) { |
| 200 | tst_resm(TWARN, "seteuid(0) failed"); |
| 201 | exit(1); |
| 202 | } |
| 203 | |
| 204 | /* clean up things in case we are looping */ |
| 205 | if (unlink(fname) == -1) { |
| 206 | tst_brkm(TBROK, cleanup, "unlink() #1 failed"); |
| 207 | } |
| 208 | if (unlink(mname) == -1) { |
| 209 | tst_brkm(TBROK, cleanup, "unlink() #2 failed"); |
| 210 | } |
| 211 | if (rmdir(fdir) == -1) { |
| 212 | tst_brkm(TBROK, cleanup, "rmdir() #1 failed"); |
| 213 | } |
| 214 | if (rmdir(mdir) == -1) { |
| 215 | tst_brkm(TBROK, cleanup, "rmdir() #2 failed"); |
| 216 | } |
| 217 | } else { |
| 218 | /* parent - let the second child carry on */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 219 | waitpid(pid1, &status, 0); |
robbiew | d47da63 | 2001-09-17 20:22:39 +0000 | [diff] [blame] | 220 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) { |
| 221 | exit(WEXITSTATUS(status)); |
| 222 | } else { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 223 | exit(0); |
robbiew | d47da63 | 2001-09-17 20:22:39 +0000 | [diff] [blame] | 224 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 225 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 226 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 227 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 228 | /* |
| 229 | * cleanup and exit |
| 230 | */ |
| 231 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 232 | tst_exit(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 233 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* |
| 237 | * setup() - performs all ONE TIME setup for this test. |
| 238 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 239 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 240 | { |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 241 | tst_require_root(NULL); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 242 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 243 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 244 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 245 | TEST_PAUSE; |
| 246 | |
| 247 | /* Create a temporary directory and make it current. */ |
| 248 | tst_tmpdir(); |
| 249 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 250 | umask(0); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 251 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 252 | sprintf(fdir, "tdir_%d", getpid()); |
| 253 | sprintf(mdir, "rndir_%d", getpid()); |
| 254 | sprintf(fname, "%s/tfile_%d", fdir, getpid()); |
| 255 | sprintf(mname, "%s/rnfile_%d", mdir, getpid()); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 256 | |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 257 | nobody = my_getpwnam(user1name); |
| 258 | bin = my_getpwnam(user2name); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* |
| 262 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 263 | * completion or premature exit. |
| 264 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 265 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 266 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 267 | |
| 268 | /* |
| 269 | * Remove the temporary directory. |
| 270 | */ |
| 271 | tst_rmdir(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 272 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 273 | /* |
| 274 | * Exit with return code appropriate for results. |
| 275 | */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 276 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 277 | } |