blob: cc662f3e078a6217fa74ae68019d71ea8e0ae977 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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
plars47eadf02001-09-13 16:20:59 +000036 * set to be nobody
plars865695b2001-08-27 22:15:12 +000037 * create old dir with mode 0700
38 * creat a file under it
39 * fork the second child
plars47eadf02001-09-13 16:20:59 +000040 * set to bin
plars865695b2001-08-27 22:15:12 +000041 * 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>
plars74948ad2002-11-14 16:16:14 +000075#include <sys/stat.h>
plars865695b2001-08-27 22:15:12 +000076#include <fcntl.h>
77#include <pwd.h>
78#include <sys/wait.h>
79#include <unistd.h>
80
subrata_modak4bb656a2009-02-26 12:02:09 +000081#include "test.h"
plars865695b2001-08-27 22:15:12 +000082
83void setup();
84void cleanup();
85extern void do_file_setup(char *);
86extern struct passwd *my_getpwnam(char *);
87
88#define PERMS 0700
89
plars47eadf02001-09-13 16:20:59 +000090char user1name[] = "nobody";
91char user2name[] = "bin";
plars865695b2001-08-27 22:15:12 +000092
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020093char *TCID = "rename09";
94int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000095
96char fdir[255], mdir[255];
97char fname[255], mname[255];
plars47eadf02001-09-13 16:20:59 +000098struct passwd *nobody, *bin;
plars865695b2001-08-27 22:15:12 +000099
subrata_modak56207ce2009-03-23 13:35:39 +0000100int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000101{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200102 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200103 const char *msg;
plars865695b2001-08-27 22:15:12 +0000104 int rval;
105 pid_t pid, pid1;
106 int status;
107
108 /*
109 * parse standard options
110 */
Garrett Cooper53740502010-12-16 00:04:01 -0800111 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800112 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000113
114 /*
115 * perform global setup for test
116 */
117 setup();
subrata_modakbdbaec52009-02-26 12:14:51 +0000118
plars865695b2001-08-27 22:15:12 +0000119 /*
120 * check looping state if -i option given
121 */
subrata_modak56207ce2009-03-23 13:35:39 +0000122 for (lc = 0; TEST_LOOPING(lc); lc++) {
123
Caspar Zhangd59a6592013-03-07 14:59:12 +0800124 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000125
robbiewd34d5812005-07-11 22:28:09 +0000126 if ((pid = FORK_OR_VFORK()) == -1) {
plars865695b2001-08-27 22:15:12 +0000127 tst_brkm(TBROK, cleanup, "fork() #1 failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800128 }
plars865695b2001-08-27 22:15:12 +0000129
subrata_modak56207ce2009-03-23 13:35:39 +0000130 if (pid == 0) { /* first child */
plars47eadf02001-09-13 16:20:59 +0000131 /* set to nobody */
132 rval = setreuid(nobody->pw_uid, nobody->pw_uid);
plars865695b2001-08-27 22:15:12 +0000133 if (rval < 0) {
134 tst_resm(TWARN, "setreuid failed to "
135 "to set the real uid to %d and "
136 "effective uid to %d",
plars47eadf02001-09-13 16:20:59 +0000137 nobody->pw_uid, nobody->pw_uid);
plars865695b2001-08-27 22:15:12 +0000138 perror("setreuid");
139 exit(1);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800140 }
plars865695b2001-08-27 22:15:12 +0000141
142 /* create the a directory with 0700 permits */
143 if (mkdir(fdir, PERMS) == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000144 tst_resm(TWARN, "mkdir(%s, %#o) Failed",
145 fdir, PERMS);
146 exit(1);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800147 }
plars865695b2001-08-27 22:15:12 +0000148
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_modakbdbaec52009-02-26 12:14:51 +0000158 tst_brkm(TBROK, cleanup, "First child failed to set "
plars865695b2001-08-27 22:15:12 +0000159 "up conditions for the test");
160 }
161
robbiewd34d5812005-07-11 22:28:09 +0000162 if ((pid1 = FORK_OR_VFORK()) == -1) {
plars865695b2001-08-27 22:15:12 +0000163 tst_brkm(TBROK, cleanup, "fork() #2 failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800164 }
plars865695b2001-08-27 22:15:12 +0000165
subrata_modak56207ce2009-03-23 13:35:39 +0000166 if (pid1 == 0) { /* second child */
plars47eadf02001-09-13 16:20:59 +0000167 /* set to bin */
168 if ((rval = seteuid(bin->pw_uid)) == -1) {
plars865695b2001-08-27 22:15:12 +0000169 tst_resm(TWARN, "seteuid() failed");
170 perror("setreuid");
171 exit(1);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800172 }
plars865695b2001-08-27 22:15:12 +0000173
174 /* create "new" directory */
175 if (mkdir(mdir, PERMS) == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000176 tst_resm(TWARN, "mkdir(%s, %#o) failed",
177 mdir, PERMS);
178 exit(1);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800179 }
plars865695b2001-08-27 22:15:12 +0000180
subrata_modak56207ce2009-03-23 13:35:39 +0000181 /* create the new file */
plars865695b2001-08-27 22:15:12 +0000182 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
plars865695b2001-08-27 22:15:12 +0000191 if (TEST_ERRNO != EACCES) {
192 tst_resm(TFAIL, "Expected EACCES got %d",
193 TEST_ERRNO);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800194 } else {
plars865695b2001-08-27 22:15:12 +0000195 tst_resm(TPASS, "rename() returned EACCES");
196 }
subrata_modak56207ce2009-03-23 13:35:39 +0000197
plars865695b2001-08-27 22:15:12 +0000198 /* 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_modak56207ce2009-03-23 13:35:39 +0000219 waitpid(pid1, &status, 0);
robbiewd47da632001-09-17 20:22:39 +0000220 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
221 exit(WEXITSTATUS(status));
222 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000223 exit(0);
robbiewd47da632001-09-17 20:22:39 +0000224 }
plars865695b2001-08-27 22:15:12 +0000225 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800226 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000227
plars865695b2001-08-27 22:15:12 +0000228 /*
229 * cleanup and exit
230 */
231 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800232 tst_exit();
subrata_modakbdbaec52009-02-26 12:14:51 +0000233
plars865695b2001-08-27 22:15:12 +0000234}
235
236/*
237 * setup() - performs all ONE TIME setup for this test.
238 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400239void setup(void)
plars865695b2001-08-27 22:15:12 +0000240{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200241 tst_require_root(NULL);
subrata_modakbdbaec52009-02-26 12:14:51 +0000242
plars865695b2001-08-27 22:15:12 +0000243 tst_sig(FORK, DEF_HANDLER, cleanup);
244
plars865695b2001-08-27 22:15:12 +0000245 TEST_PAUSE;
246
247 /* Create a temporary directory and make it current. */
248 tst_tmpdir();
249
plars865695b2001-08-27 22:15:12 +0000250 umask(0);
subrata_modakbdbaec52009-02-26 12:14:51 +0000251
subrata_modak56207ce2009-03-23 13:35:39 +0000252 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());
plars865695b2001-08-27 22:15:12 +0000256
plars47eadf02001-09-13 16:20:59 +0000257 nobody = my_getpwnam(user1name);
258 bin = my_getpwnam(user2name);
plars865695b2001-08-27 22:15:12 +0000259}
260
261/*
262 * cleanup() - performs all ONE TIME cleanup for this test at
263 * completion or premature exit.
264 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400265void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000266{
plars865695b2001-08-27 22:15:12 +0000267
268 /*
269 * Remove the temporary directory.
270 */
271 tst_rmdir();
subrata_modakbdbaec52009-02-26 12:14:51 +0000272
plars865695b2001-08-27 22:15:12 +0000273 /*
274 * Exit with return code appropriate for results.
275 */
Garrett Cooper2c282152010-12-16 00:55:50 -0800276
Chris Dearmanec6edca2012-10-17 19:54:01 -0700277}