1 /*
2 * Copyright (C) Bull S.A. 2001
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
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * NAME
22 * chroot04.c
23 *
24 * DESCRIPTION
25 * Testcase to check that chroot sets errno to EACCES.
26 *
27 * ALGORITHM
28 * As a non-root user attempt to perform chroot() to a directory. The
29 * chroot() call should fail with EACCES
30 *
31 * USAGE: <for command-line>
32 * chroot04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
33 * where, -c n : Run n copies concurrently.
34 * -e : Turn on errno logging.
35 * -i n : Execute test n times.
36 * -I x : Execute test for x seconds.
37 * -P x : Pause for x seconds between iterations.
38 * -t : Turn on syscall timing.
39 *
40 * HISTORY
41 * 04/2002 Ported by Jacky Malcles
42 *
43 * RESTRICTIONS
44 * Must be run as non-root user.
45 */
46
47 #include <stdio.h>
48 #include <errno.h>
49 #include <sys/stat.h>
50 #include "test.h"
51 #include <pwd.h>
52
53 char *TCID = "chroot04";
54 int TST_TOTAL = 1;
55
56 #define TEST_TMPDIR "chroot04_tmpdir"
57
58 char nobody_uid[] = "nobody";
59 struct passwd *ltpuser;
60
61 void setup(void);
62 void cleanup(void);
63
main(int ac,char ** av)64 int main(int ac, char **av)
65 {
66 int lc;
67
68 tst_parse_opts(ac, av, NULL, NULL);
69
70 setup();
71
72 /* Check for looping state if -i option is given */
73 for (lc = 0; TEST_LOOPING(lc); lc++) {
74
75 /* reset tst_count in case we are looping */
76 tst_count = 0;
77
78 TEST(chroot(TEST_TMPDIR));
79
80 if (TEST_RETURN != -1)
81 tst_resm(TFAIL, "call succeeded unexpectedly");
82 else if (TEST_ERRNO == EACCES)
83 tst_resm(TPASS, "got EACCESS as expected");
84 else
85 tst_resm(TFAIL | TTERRNO,
86 "did not get EACCES as expected");
87
88 }
89 cleanup();
90
91 tst_exit();
92
93 }
94
95 /*
96 * setup() - performs all ONE TIME setup for this test.
97 */
setup(void)98 void setup(void)
99 {
100 tst_sig(NOFORK, DEF_HANDLER, cleanup);
101
102 tst_require_root();
103
104 TEST_PAUSE;
105
106 /* make a temporary directory and cd to it */
107 tst_tmpdir();
108
109 /*
110 * create a temporary directory
111 */
112 if (mkdir(TEST_TMPDIR, 0222) != 0) {
113 tst_resm(TBROK, "mkdir(%s) failed", TEST_TMPDIR);
114 }
115
116 ltpuser = getpwnam(nobody_uid);
117 if (seteuid(ltpuser->pw_uid) == -1) {
118 tst_brkm(TBROK, cleanup, "seteuid to nobody failed");
119 }
120
121 }
122
123 /*
124 * cleanup() - performs all ONE TIME cleanup for this test at
125 * completion or premature exit.
126 */
cleanup(void)127 void cleanup(void)
128 {
129 /* reset the process ID to the saved ID (root) */
130 if (setuid(0) == -1) {
131 tst_brkm(TBROK | TERRNO, NULL, "setuid(0) failed");
132 }
133 if (rmdir(TEST_TMPDIR) != 0) {
134 tst_brkm(TFAIL | TERRNO, NULL, "rmdir(%s) failed", TEST_TMPDIR);
135 }
136
137 /* delete the test directory created in setup() */
138 tst_rmdir();
139
140 }
141