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 | * rename12 |
| 23 | * |
| 24 | * DESCRIPTION |
robbiew | f6addef | 2004-04-14 21:42:09 +0000 | [diff] [blame] | 25 | * check rename() fails with EPERM or EACCES |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 | * create a directory fdir and set the sticky bit |
| 36 | * create file fname under fdir |
| 37 | * fork a child |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 38 | * set to nobody |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | * try to rename fname to mname |
| 40 | * check the return value, if succeeded (return=0) |
| 41 | * Log the errno and Issue a FAIL message. |
| 42 | * Otherwise, |
| 43 | * Verify the errno |
robbiew | f6addef | 2004-04-14 21:42:09 +0000 | [diff] [blame] | 44 | * if equals to EPERMS or EACCES, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | * Issue Pass message. |
| 46 | * Otherwise, |
| 47 | * Issue Fail message. |
| 48 | * Cleanup: |
| 49 | * Print errno log and/or timing stats if options given |
| 50 | * Delete the temporary directory created. |
| 51 | * USAGE |
| 52 | * rename12 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 53 | * where, -c n : Run n copies concurrently. |
| 54 | * -e : Turn on errno logging. |
| 55 | * -i n : Execute test n times. |
| 56 | * -I x : Execute test for x seconds. |
| 57 | * -P x : Pause for x seconds between iterations. |
| 58 | * -t : Turn on syscall timing. |
| 59 | * |
| 60 | * HISTORY |
| 61 | * 07/2001 Ported by Wayne Boyer |
| 62 | * |
| 63 | * RESTRICTIONS |
| 64 | * Must run test as root. |
| 65 | * |
| 66 | */ |
| 67 | #include <errno.h> |
| 68 | #include <sys/stat.h> |
| 69 | #include <sys/types.h> |
robbiew | d47da63 | 2001-09-17 20:22:39 +0000 | [diff] [blame] | 70 | #include <sys/wait.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | #include <fcntl.h> |
| 72 | #include <pwd.h> |
| 73 | #include <unistd.h> |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 74 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
| 76 | void setup(); |
| 77 | void cleanup(); |
| 78 | extern void do_file_setup(char *); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | extern struct passwd *my_getpwnam(char *); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
| 81 | #define PERMS 0777 |
| 82 | |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 83 | char user1name[] = "nobody"; |
| 84 | char user2name[] = "bin"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 86 | char *TCID = "rename12"; |
| 87 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | |
| 89 | int fd; |
| 90 | char fdir[255]; |
| 91 | char fname[255], mname[255]; |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 92 | struct passwd *nobody; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | struct stat buf1; |
| 94 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 97 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 98 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | pid_t pid; |
robbiew | d47da63 | 2001-09-17 20:22:39 +0000 | [diff] [blame] | 100 | int status; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
| 102 | /* |
| 103 | * parse standard options |
| 104 | */ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 105 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 106 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * perform global setup for test |
| 110 | */ |
| 111 | setup(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 112 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | /* |
| 114 | * check looping state if -i option given |
| 115 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 116 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 117 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 118 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 120 | /* |
| 121 | * rename a file whose parent directory has |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | * the sticky bit set without root permission |
| 123 | * or effective uid |
| 124 | */ |
| 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() 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) { /* child */ |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 131 | /* set to nobody */ |
| 132 | if (seteuid(nobody->pw_uid) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | tst_resm(TWARN, "setreuid failed"); |
| 134 | perror("setreuid"); |
| 135 | exit(1); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 136 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | |
| 138 | /* rename "old" to "new" */ |
| 139 | TEST(rename(fname, mname)); |
| 140 | |
| 141 | if (TEST_RETURN != -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 142 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 143 | exit(1); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 144 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 146 | if ((TEST_ERRNO != EPERM) && (TEST_ERRNO != EACCES)) { |
| 147 | tst_resm(TFAIL, |
| 148 | "Expected EPERM or EACCES, got %d", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | TEST_ERRNO); |
| 150 | exit(1); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 151 | } else { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | tst_resm(TPASS, |
| 153 | "rename returned EPERM or EACCES"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* set the id back to root */ |
| 157 | if (seteuid(0) == -1) { |
| 158 | tst_resm(TWARN, "seteuid(0) failed"); |
| 159 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 160 | } else { /* parent */ |
robbiew | d47da63 | 2001-09-17 20:22:39 +0000 | [diff] [blame] | 161 | wait(&status); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 162 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) { |
| 163 | exit(WEXITSTATUS(status)); |
| 164 | } else { |
| 165 | exit(0); |
| 166 | } |
robbiew | d47da63 | 2001-09-17 20:22:39 +0000 | [diff] [blame] | 167 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 169 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 170 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 172 | tst_exit(); |
robbiew | c2cbe20 | 2003-03-27 18:32:02 +0000 | [diff] [blame] | 173 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* |
| 177 | * setup() - performs all ONE TIME setup for this test. |
| 178 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [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); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 182 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 184 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | TEST_PAUSE; |
| 186 | |
| 187 | /* Create a temporary directory and make it current. */ |
| 188 | tst_tmpdir(); |
| 189 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 190 | umask(0); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 191 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 192 | sprintf(fdir, "./tdir_%d", getpid()); |
| 193 | sprintf(fname, "%s/tfile_%d", fdir, getpid()); |
| 194 | sprintf(mname, "%s/rnfile_%d", fdir, getpid()); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 195 | |
| 196 | /* create a directory */ |
| 197 | if (mkdir(fdir, PERMS) == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 198 | tst_brkm(TBROK, cleanup, "Could not create directory %s", fdir); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 199 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 201 | if (stat(fdir, &buf1) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 202 | tst_brkm(TBROK, cleanup, "failed to stat directory %s", fdir); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 203 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* set the sticky bit */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 207 | if (chmod(fdir, buf1.st_mode | S_ISVTX) != 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 208 | tst_brkm(TBROK, cleanup, "failed to set the S_ISVTX bit"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 209 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* create a file under fdir */ |
| 213 | do_file_setup(fname); |
| 214 | |
plars | 47eadf0 | 2001-09-13 16:20:59 +0000 | [diff] [blame] | 215 | /* get nobody password file info */ |
| 216 | nobody = my_getpwnam(user1name); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /* |
| 220 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 221 | * completion or premature exit. |
| 222 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 223 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 224 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 225 | |
| 226 | /* |
| 227 | * Remove the temporary directory. |
| 228 | */ |
| 229 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 230 | } |