blob: 37c529277ab5320f248d19729d7daf9be6412134 [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 * chroot01.c
23 *
24 * DESCRIPTION
25 * Testcase to check the whether chroot sets errno to EPERM.
26 *
27 * ALGORITHM
28 * As a non-root user attempt to perform chroot() to a directory. The
29 * chroot() call should fail with EPERM
30 *
31 * USAGE: <for command-line>
32 * chroot01 [-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 * 07/2001 Ported by Wayne Boyer
42 *
43 * RESTRICTIONS
44 * Must be run as non-root user.
45 */
46
47#include <stdio.h>
48#include <errno.h>
49#include "test.h"
50#include "usctest.h"
robbiew096b0812001-08-31 15:32:25 +000051#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000052
53char *TCID = "chroot01";
54int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000055int fail;
56
57char path[] = "/tmp";
subrata_modak56207ce2009-03-23 13:35:39 +000058int exp_enos[] = { EPERM, 0 };
robbiew096b0812001-08-31 15:32:25 +000059char nobody_uid[] = "nobody";
60struct passwd *ltpuser;
plars865695b2001-08-27 22:15:12 +000061
62void setup(void);
63void cleanup(void);
64
Garrett Cooperdc2793f2010-12-16 18:43:08 -080065int
66main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000067{
68 int lc;
69 char *msg;
70
Garrett Cooperdc2793f2010-12-16 18:43:08 -080071 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080072 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000073
74 setup();
75
plars865695b2001-08-27 22:15:12 +000076 TEST_EXP_ENOS(exp_enos);
77
plars865695b2001-08-27 22:15:12 +000078 for (lc = 0; TEST_LOOPING(lc); lc++) {
79
plars865695b2001-08-27 22:15:12 +000080 Tst_count = 0;
81
82 TEST(chroot(path));
83
Garrett Cooperdc2793f2010-12-16 18:43:08 -080084 if (TEST_RETURN != -1)
85 tst_resm(TFAIL, "call succeeded unexpectedly");
86 else if (errno != EPERM)
87 tst_resm(TFAIL|TTERRNO, "chroot failed unexpectedly");
88 else
plars865695b2001-08-27 22:15:12 +000089 tst_resm(TPASS, "chroot set errno to EPERM.");
plars865695b2001-08-27 22:15:12 +000090 }
91 cleanup();
92
Garrett Cooperdc2793f2010-12-16 18:43:08 -080093 tst_exit();
plars865695b2001-08-27 22:15:12 +000094
Garrett Cooperdc2793f2010-12-16 18:43:08 -080095}
96
97void
98setup(void)
plars865695b2001-08-27 22:15:12 +000099{
Garrett Cooperdc2793f2010-12-16 18:43:08 -0800100 tst_require_root(NULL);
robbiew096b0812001-08-31 15:32:25 +0000101
Garrett Cooperdc2793f2010-12-16 18:43:08 -0800102 tst_tmpdir();
103
104 if ((ltpuser = getpwnam(nobody_uid)) == NULL)
105 tst_brkm(TBROK|TERRNO, cleanup, "getpwnam(\"nobody\") failed");
106
107 if (seteuid(ltpuser->pw_uid) == -1)
108 tst_brkm(TBROK|TERRNO, cleanup, "seteuid to nobody failed");
plars865695b2001-08-27 22:15:12 +0000109
plars865695b2001-08-27 22:15:12 +0000110 tst_sig(NOFORK, DEF_HANDLER, cleanup);
111
plars865695b2001-08-27 22:15:12 +0000112 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000113}
114
Garrett Cooperdc2793f2010-12-16 18:43:08 -0800115void
116cleanup(void)
plars865695b2001-08-27 22:15:12 +0000117{
plars865695b2001-08-27 22:15:12 +0000118 TEST_CLEANUP;
119
Garrett Cooperdc2793f2010-12-16 18:43:08 -0800120 if (seteuid(0) == -1)
121 tst_brkm(TBROK|TERRNO, NULL, "setuid(0) failed");
plars865695b2001-08-27 22:15:12 +0000122
Garrett Cooperdc2793f2010-12-16 18:43:08 -0800123 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700124}