blob: c93fbbc05ced567c501a449bc6086d75b56d0618 [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 * rename12
23 *
24 * DESCRIPTION
robbiewf6addef2004-04-14 21:42:09 +000025 * check rename() fails with EPERM or EACCES
plars865695b2001-08-27 22:15:12 +000026 *
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
plars47eadf02001-09-13 16:20:59 +000038 * set to nobody
plars865695b2001-08-27 22:15:12 +000039 * 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
robbiewf6addef2004-04-14 21:42:09 +000044 * if equals to EPERMS or EACCES,
plars865695b2001-08-27 22:15:12 +000045 * 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>
robbiewd47da632001-09-17 20:22:39 +000070#include <sys/wait.h>
plars865695b2001-08-27 22:15:12 +000071#include <fcntl.h>
72#include <pwd.h>
73#include <unistd.h>
subrata_modak4bb656a2009-02-26 12:02:09 +000074#include "test.h"
plars865695b2001-08-27 22:15:12 +000075
76void setup();
77void cleanup();
78extern void do_file_setup(char *);
subrata_modak56207ce2009-03-23 13:35:39 +000079extern struct passwd *my_getpwnam(char *);
plars865695b2001-08-27 22:15:12 +000080
81#define PERMS 0777
82
plars47eadf02001-09-13 16:20:59 +000083char user1name[] = "nobody";
84char user2name[] = "bin";
plars865695b2001-08-27 22:15:12 +000085
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020086char *TCID = "rename12";
87int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000088
89int fd;
90char fdir[255];
91char fname[255], mname[255];
plars47eadf02001-09-13 16:20:59 +000092struct passwd *nobody;
plars865695b2001-08-27 22:15:12 +000093struct stat buf1;
94
subrata_modak56207ce2009-03-23 13:35:39 +000095int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000096{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020097 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020098 const char *msg;
plars865695b2001-08-27 22:15:12 +000099 pid_t pid;
robbiewd47da632001-09-17 20:22:39 +0000100 int status;
plars865695b2001-08-27 22:15:12 +0000101
102 /*
103 * parse standard options
104 */
Garrett Cooper53740502010-12-16 00:04:01 -0800105 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800106 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000107
108 /*
109 * perform global setup for test
110 */
111 setup();
subrata_modakbdbaec52009-02-26 12:14:51 +0000112
plars865695b2001-08-27 22:15:12 +0000113 /*
114 * check looping state if -i option given
115 */
subrata_modak56207ce2009-03-23 13:35:39 +0000116 for (lc = 0; TEST_LOOPING(lc); lc++) {
117
Caspar Zhangd59a6592013-03-07 14:59:12 +0800118 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000119
subrata_modak4bb656a2009-02-26 12:02:09 +0000120 /*
121 * rename a file whose parent directory has
plars865695b2001-08-27 22:15:12 +0000122 * the sticky bit set without root permission
123 * or effective uid
124 */
125
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() 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) { /* child */
plars47eadf02001-09-13 16:20:59 +0000131 /* set to nobody */
132 if (seteuid(nobody->pw_uid) == -1) {
plars865695b2001-08-27 22:15:12 +0000133 tst_resm(TWARN, "setreuid failed");
134 perror("setreuid");
135 exit(1);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800136 }
plars865695b2001-08-27 22:15:12 +0000137
138 /* rename "old" to "new" */
139 TEST(rename(fname, mname));
140
141 if (TEST_RETURN != -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000142 tst_resm(TFAIL, "call succeeded unexpectedly");
143 exit(1);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800144 }
plars865695b2001-08-27 22:15:12 +0000145
subrata_modak56207ce2009-03-23 13:35:39 +0000146 if ((TEST_ERRNO != EPERM) && (TEST_ERRNO != EACCES)) {
147 tst_resm(TFAIL,
148 "Expected EPERM or EACCES, got %d",
plars865695b2001-08-27 22:15:12 +0000149 TEST_ERRNO);
150 exit(1);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800151 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000152 tst_resm(TPASS,
153 "rename returned EPERM or EACCES");
plars865695b2001-08-27 22:15:12 +0000154 }
155
156 /* set the id back to root */
157 if (seteuid(0) == -1) {
158 tst_resm(TWARN, "seteuid(0) failed");
159 }
subrata_modak56207ce2009-03-23 13:35:39 +0000160 } else { /* parent */
robbiewd47da632001-09-17 20:22:39 +0000161 wait(&status);
subrata_modak56207ce2009-03-23 13:35:39 +0000162 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
163 exit(WEXITSTATUS(status));
164 } else {
165 exit(0);
166 }
robbiewd47da632001-09-17 20:22:39 +0000167
plars865695b2001-08-27 22:15:12 +0000168 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800169 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000170
plars865695b2001-08-27 22:15:12 +0000171 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800172 tst_exit();
robbiewc2cbe202003-03-27 18:32:02 +0000173
plars865695b2001-08-27 22:15:12 +0000174}
175
176/*
177 * setup() - performs all ONE TIME setup for this test.
178 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400179void setup(void)
plars865695b2001-08-27 22:15:12 +0000180{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200181 tst_require_root(NULL);
subrata_modakbdbaec52009-02-26 12:14:51 +0000182
plars865695b2001-08-27 22:15:12 +0000183 tst_sig(FORK, DEF_HANDLER, cleanup);
184
plars865695b2001-08-27 22:15:12 +0000185 TEST_PAUSE;
186
187 /* Create a temporary directory and make it current. */
188 tst_tmpdir();
189
plars865695b2001-08-27 22:15:12 +0000190 umask(0);
subrata_modakbdbaec52009-02-26 12:14:51 +0000191
subrata_modak56207ce2009-03-23 13:35:39 +0000192 sprintf(fdir, "./tdir_%d", getpid());
193 sprintf(fname, "%s/tfile_%d", fdir, getpid());
194 sprintf(mname, "%s/rnfile_%d", fdir, getpid());
plars865695b2001-08-27 22:15:12 +0000195
196 /* create a directory */
197 if (mkdir(fdir, PERMS) == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000198 tst_brkm(TBROK, cleanup, "Could not create directory %s", fdir);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800199 }
plars865695b2001-08-27 22:15:12 +0000200
subrata_modak56207ce2009-03-23 13:35:39 +0000201 if (stat(fdir, &buf1) == -1) {
plars865695b2001-08-27 22:15:12 +0000202 tst_brkm(TBROK, cleanup, "failed to stat directory %s", fdir);
Garrett Cooper2c282152010-12-16 00:55:50 -0800203
plars865695b2001-08-27 22:15:12 +0000204 }
205
206 /* set the sticky bit */
subrata_modak56207ce2009-03-23 13:35:39 +0000207 if (chmod(fdir, buf1.st_mode | S_ISVTX) != 0) {
plars865695b2001-08-27 22:15:12 +0000208 tst_brkm(TBROK, cleanup, "failed to set the S_ISVTX bit");
Garrett Cooper2c282152010-12-16 00:55:50 -0800209
plars865695b2001-08-27 22:15:12 +0000210 }
211
212 /* create a file under fdir */
213 do_file_setup(fname);
214
plars47eadf02001-09-13 16:20:59 +0000215 /* get nobody password file info */
216 nobody = my_getpwnam(user1name);
plars865695b2001-08-27 22:15:12 +0000217}
218
219/*
220 * cleanup() - performs all ONE TIME cleanup for this test at
221 * completion or premature exit.
222 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400223void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000224{
plars865695b2001-08-27 22:15:12 +0000225
226 /*
227 * Remove the temporary directory.
228 */
229 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700230}