blob: 9f758bf69e0e477d1e92eee7bc12250dfd519ced [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 * chdir03
23 *
24 * DESCRIPTION
25 * Testcase for testing that chdir(2) sets EACCES errno
26 *
27 * ALGORITHM
28 * 1. create a child process, sets its uid to ltpuser1
29 * 2. this child creates a directory with perm 700, and exits
30 * 3. create another child process, sets its uid to ltpuser2
31 * 4. this child attempts to chdir(2) to the directory created in 2.
32 * and expects to get an EACCES.
33 *
34 * USAGE: <for command-line>
35 * chdir03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
36 * where, -c n : Run n copies concurrently.
37 * -e : Turn on errno logging.
38 * -i n : Execute test n times.
39 * -I x : Execute test for x seconds.
40 * -P x : Pause for x seconds between iterations.
41 * -t : Turn on syscall timing.
42 *
43 * HISTORY
44 * 07/2001 Ported by Wayne Boyer
45 *
46 * RESTRICTIONS
47 * This test must be run as root.
48 */
49
50#include <stdio.h>
51#include <sys/types.h>
52#include <unistd.h>
53#include <pwd.h>
54#include <errno.h>
55#include <sys/wait.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080056#include "test.h"
robbiew15226cd2002-04-10 16:10:40 +000057#include <stdlib.h>
58#include <string.h>
59#include <sys/stat.h>
60#include <sys/types.h>
plars865695b2001-08-27 22:15:12 +000061
62char *TCID = "chdir03";
63int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000064
65void setup(void);
66void cleanup(void);
67
68char user1name[] = "nobody";
plarseea85922001-08-31 15:00:38 +000069char user2name[] = "bin";
plars865695b2001-08-27 22:15:12 +000070
plars865695b2001-08-27 22:15:12 +000071char good_dir[100];
72
73struct passwd *ltpuser1, *ltpuser2;
74
75extern struct passwd *my_getpwnam(char *);
76
robbiew15226cd2002-04-10 16:10:40 +000077int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000078{
Garrett Cooperb7dcfca2010-12-17 11:25:52 -080079 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020080 const char *msg;
plars865695b2001-08-27 22:15:12 +000081
82 pid_t pid, pid1;
robbiew15226cd2002-04-10 16:10:40 +000083 int status;
plars865695b2001-08-27 22:15:12 +000084
Garrett Cooperb7dcfca2010-12-17 11:25:52 -080085 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080086 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000087
88 setup();
89
plars865695b2001-08-27 22:15:12 +000090 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080091 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000092
robbiewd34d5812005-07-11 22:28:09 +000093 if ((pid = FORK_OR_VFORK()) < 0) {
plars865695b2001-08-27 22:15:12 +000094 tst_brkm(TBROK, cleanup, "first fork failed");
95 }
96
Garrett Cooperb7dcfca2010-12-17 11:25:52 -080097 if (pid == 0) {
plars865695b2001-08-27 22:15:12 +000098 if (setreuid(ltpuser1->pw_uid, ltpuser1->pw_uid) != 0) {
Garrett Cooperb7dcfca2010-12-17 11:25:52 -080099 perror("setreuid failed in child #1");
plars865695b2001-08-27 22:15:12 +0000100 exit(1);
101 }
102 if (mkdir(good_dir, 00700) != 0) {
Garrett Cooperb7dcfca2010-12-17 11:25:52 -0800103 perror("mkdir failed in child #1");
plars865695b2001-08-27 22:15:12 +0000104 exit(1);
105 }
106 exit(0);
107 }
108 wait(&status);
109
Garrett Cooperb7dcfca2010-12-17 11:25:52 -0800110 if ((pid1 = FORK_OR_VFORK()) < 0)
plars865695b2001-08-27 22:15:12 +0000111 tst_brkm(TBROK, cleanup, "second fork failed");
plars865695b2001-08-27 22:15:12 +0000112
subrata_modak56207ce2009-03-23 13:35:39 +0000113 if (pid1 == 0) { /* second child */
Garrett Cooperb7dcfca2010-12-17 11:25:52 -0800114
115 int rval;
116
plars865695b2001-08-27 22:15:12 +0000117 /*
118 * set the child's ID to ltpuser2 using seteuid()
119 * so that the ID can be changed back after the
120 * TEST call is made.
121 */
122 if (seteuid(ltpuser2->pw_uid) != 0) {
Garrett Cooperb7dcfca2010-12-17 11:25:52 -0800123 perror("setreuid failed in child #2");
plars865695b2001-08-27 22:15:12 +0000124 exit(1);
125 }
126
127 TEST(chdir(good_dir));
128
129 if (TEST_RETURN != -1) {
Garrett Cooperb7dcfca2010-12-17 11:25:52 -0800130 printf("call succeeded unexpectedly\n");
131 rval = 1;
plars865695b2001-08-27 22:15:12 +0000132 } else if (TEST_ERRNO != EACCES) {
Garrett Cooperb7dcfca2010-12-17 11:25:52 -0800133 printf("didn't get EACCES as expected; got ");
134 rval = 1;
plars865695b2001-08-27 22:15:12 +0000135 } else {
Garrett Cooperb7dcfca2010-12-17 11:25:52 -0800136 printf("got EACCES as expected\n");
137 rval = 0;
138 }
139 /* Only really required with vfork. */
140 if (seteuid(0) != 0) {
141 perror("seteuid(0) failed");
142 rval = 1;
plars865695b2001-08-27 22:15:12 +0000143 }
144
Garrett Cooperb7dcfca2010-12-17 11:25:52 -0800145 exit(rval);
plars865695b2001-08-27 22:15:12 +0000146
Garrett Cooperb7dcfca2010-12-17 11:25:52 -0800147 } else {
148 if (wait(&status) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800149 tst_brkm(TBROK | TERRNO, cleanup,
150 "wait failed");
Garrett Cooperb7dcfca2010-12-17 11:25:52 -0800151 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
152 tst_brkm(TBROK, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800153 "child exited abnormally");
plars865695b2001-08-27 22:15:12 +0000154 }
plars865695b2001-08-27 22:15:12 +0000155 if (rmdir(good_dir) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800156 tst_brkm(TBROK | TERRNO, cleanup,
157 "rmdir(%s) failed", good_dir);
plars865695b2001-08-27 22:15:12 +0000158 }
159 }
Garrett Cooperb7dcfca2010-12-17 11:25:52 -0800160
plars865695b2001-08-27 22:15:12 +0000161 cleanup();
Garrett Cooperb7dcfca2010-12-17 11:25:52 -0800162 tst_exit();
plars865695b2001-08-27 22:15:12 +0000163
Garrett Cooperb7dcfca2010-12-17 11:25:52 -0800164}
plars865695b2001-08-27 22:15:12 +0000165
Mike Frysingerc57fba52014-04-09 18:56:30 -0400166void setup(void)
plars865695b2001-08-27 22:15:12 +0000167{
168 char *cur_dir = NULL;
169
Garrett Cooperb7dcfca2010-12-17 11:25:52 -0800170 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000171
plars865695b2001-08-27 22:15:12 +0000172 tst_sig(FORK, DEF_HANDLER, cleanup);
173
plars865695b2001-08-27 22:15:12 +0000174 TEST_PAUSE;
175
plars865695b2001-08-27 22:15:12 +0000176 tst_tmpdir();
177
Garrett Cooperb7dcfca2010-12-17 11:25:52 -0800178 if ((cur_dir = getcwd(cur_dir, 0)) == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800179 tst_brkm(TBROK | TERRNO, cleanup, "getcwd failed");
plars865695b2001-08-27 22:15:12 +0000180
robbiewb41614c2004-08-24 17:08:24 +0000181 sprintf(good_dir, "%s/%d", cur_dir, getpid());
plars865695b2001-08-27 22:15:12 +0000182
183 ltpuser1 = my_getpwnam(user1name);
184 ltpuser2 = my_getpwnam(user2name);
185}
186
Mike Frysingerc57fba52014-04-09 18:56:30 -0400187void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000188{
plars865695b2001-08-27 22:15:12 +0000189 tst_rmdir();
190
Chris Dearmanec6edca2012-10-17 19:54:01 -0700191}