blob: 2c56b4de394cd623baea3e241c1b81400e05c665 [file] [log] [blame]
robbiew3650fef2002-04-25 15:38:20 +00001 /*
subrata_modak56207ce2009-03-23 13:35:39 +00002 * 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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
subrata_modak56207ce2009-03-23 13:35:39 +000018 */
robbiew3650fef2002-04-25 15:38:20 +000019
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>
robbiew2c945242002-11-11 19:01:25 +000049#include <sys/stat.h>
robbiew3650fef2002-04-25 15:38:20 +000050#include "test.h"
robbiew3650fef2002-04-25 15:38:20 +000051#include <pwd.h>
52
53char *TCID = "chroot04";
54int TST_TOTAL = 1;
robbiew3650fef2002-04-25 15:38:20 +000055
Garrett Cooperf23dd412010-12-17 01:57:35 -080056#define TEST_TMPDIR "chroot04_tmpdir"
57
robbiew3650fef2002-04-25 15:38:20 +000058char nobody_uid[] = "nobody";
59struct passwd *ltpuser;
60
61void setup(void);
62void cleanup(void);
63
subrata_modak56207ce2009-03-23 13:35:39 +000064int main(int ac, char **av)
robbiew3650fef2002-04-25 15:38:20 +000065{
subrata_modak56207ce2009-03-23 13:35:39 +000066 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020067 const char *msg;
robbiew3650fef2002-04-25 15:38:20 +000068
Garrett Cooperf23dd412010-12-17 01:57:35 -080069 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080070 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew3650fef2002-04-25 15:38:20 +000071
subrata_modak56207ce2009-03-23 13:35:39 +000072 setup();
robbiew3650fef2002-04-25 15:38:20 +000073
subrata_modak56207ce2009-03-23 13:35:39 +000074 /* Check for looping state if -i option is given */
75 for (lc = 0; TEST_LOOPING(lc); lc++) {
robbiew3650fef2002-04-25 15:38:20 +000076
Caspar Zhangd59a6592013-03-07 14:59:12 +080077 /* reset tst_count in case we are looping */
78 tst_count = 0;
robbiew3650fef2002-04-25 15:38:20 +000079
Garrett Cooperf23dd412010-12-17 01:57:35 -080080 TEST(chroot(TEST_TMPDIR));
robbiew3650fef2002-04-25 15:38:20 +000081
Garrett Cooperf23dd412010-12-17 01:57:35 -080082 if (TEST_RETURN != -1)
subrata_modak56207ce2009-03-23 13:35:39 +000083 tst_resm(TFAIL, "call succeeded unexpectedly");
Garrett Cooperf23dd412010-12-17 01:57:35 -080084 else if (TEST_ERRNO == EACCES)
85 tst_resm(TPASS, "got EACCESS as expected");
86 else
Wanlong Gao354ebb42012-12-07 10:10:04 +080087 tst_resm(TFAIL | TTERRNO,
88 "did not get EACCES as expected");
robbiew3650fef2002-04-25 15:38:20 +000089
subrata_modak56207ce2009-03-23 13:35:39 +000090 }
91 cleanup();
robbiew3650fef2002-04-25 15:38:20 +000092
Garrett Cooperf23dd412010-12-17 01:57:35 -080093 tst_exit();
94
95}
robbiew3650fef2002-04-25 15:38:20 +000096
97/*
98 * setup() - performs all ONE TIME setup for this test.
99 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400100void setup(void)
robbiew3650fef2002-04-25 15:38:20 +0000101{
subrata_modak56207ce2009-03-23 13:35:39 +0000102 tst_sig(NOFORK, DEF_HANDLER, cleanup);
robbiew3650fef2002-04-25 15:38:20 +0000103
subrata_modak56207ce2009-03-23 13:35:39 +0000104 TEST_PAUSE;
robbiew3650fef2002-04-25 15:38:20 +0000105
subrata_modak56207ce2009-03-23 13:35:39 +0000106 /* make a temporary directory and cd to it */
107 tst_tmpdir();
robbiew3650fef2002-04-25 15:38:20 +0000108
subrata_modak56207ce2009-03-23 13:35:39 +0000109 /*
110 * create a temporary directory
111 */
Garrett Cooperf23dd412010-12-17 01:57:35 -0800112 if (mkdir(TEST_TMPDIR, 0222) != 0) {
113 tst_resm(TBROK, "mkdir(%s) failed", TEST_TMPDIR);
subrata_modak56207ce2009-03-23 13:35:39 +0000114 }
robbiew3650fef2002-04-25 15:38:20 +0000115
subrata_modak56207ce2009-03-23 13:35:39 +0000116 ltpuser = getpwnam(nobody_uid);
117 if (seteuid(ltpuser->pw_uid) == -1) {
Garrett Cooperf23dd412010-12-17 01:57:35 -0800118 tst_brkm(TBROK, cleanup, "seteuid to nobody failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000119 }
robbiew3650fef2002-04-25 15:38:20 +0000120
121}
122
robbiew3650fef2002-04-25 15:38:20 +0000123/*
124 * cleanup() - performs all ONE TIME cleanup for this test at
125 * completion or premature exit.
126 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400127void cleanup(void)
robbiew3650fef2002-04-25 15:38:20 +0000128{
subrata_modak56207ce2009-03-23 13:35:39 +0000129 /* reset the process ID to the saved ID (root) */
130 if (setuid(0) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800131 tst_brkm(TBROK | TERRNO, NULL, "setuid(0) failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000132 }
Garrett Cooperf23dd412010-12-17 01:57:35 -0800133 if (rmdir(TEST_TMPDIR) != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800134 tst_brkm(TFAIL | TERRNO, NULL, "rmdir(%s) failed", TEST_TMPDIR);
subrata_modak56207ce2009-03-23 13:35:39 +0000135 }
robbiew3650fef2002-04-25 15:38:20 +0000136
subrata_modak56207ce2009-03-23 13:35:39 +0000137 /* delete the test directory created in setup() */
138 tst_rmdir();
robbiew3650fef2002-04-25 15:38:20 +0000139
Chris Dearmanec6edca2012-10-17 19:54:01 -0700140}