blob: 2aa8428d0b930818b9e09d088a77e7a4efb3b47d [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"
robbiew096b0812001-08-31 15:32:25 +000050#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000051
52char *TCID = "chroot01";
53int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000054int fail;
55
56char path[] = "/tmp";
Wanlong Gao354ebb42012-12-07 10:10:04 +080057
robbiew096b0812001-08-31 15:32:25 +000058char nobody_uid[] = "nobody";
59struct passwd *ltpuser;
plars865695b2001-08-27 22:15:12 +000060
61void setup(void);
62void cleanup(void);
63
Wanlong Gao354ebb42012-12-07 10:10:04 +080064int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000065{
66 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020067 const char *msg;
plars865695b2001-08-27 22:15:12 +000068
Garrett Cooperdc2793f2010-12-16 18:43:08 -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);
plars865695b2001-08-27 22:15:12 +000071
72 setup();
73
plars865695b2001-08-27 22:15:12 +000074 for (lc = 0; TEST_LOOPING(lc); lc++) {
75
Caspar Zhangd59a6592013-03-07 14:59:12 +080076 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000077
78 TEST(chroot(path));
79
Garrett Cooperdc2793f2010-12-16 18:43:08 -080080 if (TEST_RETURN != -1)
81 tst_resm(TFAIL, "call succeeded unexpectedly");
82 else if (errno != EPERM)
Wanlong Gao354ebb42012-12-07 10:10:04 +080083 tst_resm(TFAIL | TTERRNO, "chroot failed unexpectedly");
Garrett Cooperdc2793f2010-12-16 18:43:08 -080084 else
plars865695b2001-08-27 22:15:12 +000085 tst_resm(TPASS, "chroot set errno to EPERM.");
plars865695b2001-08-27 22:15:12 +000086 }
87 cleanup();
88
Garrett Cooperdc2793f2010-12-16 18:43:08 -080089 tst_exit();
plars865695b2001-08-27 22:15:12 +000090
Garrett Cooperdc2793f2010-12-16 18:43:08 -080091}
92
Wanlong Gao354ebb42012-12-07 10:10:04 +080093void setup(void)
plars865695b2001-08-27 22:15:12 +000094{
Garrett Cooperdc2793f2010-12-16 18:43:08 -080095 tst_require_root(NULL);
robbiew096b0812001-08-31 15:32:25 +000096
Garrett Cooperdc2793f2010-12-16 18:43:08 -080097 tst_tmpdir();
98
99 if ((ltpuser = getpwnam(nobody_uid)) == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800100 tst_brkm(TBROK | TERRNO, cleanup,
101 "getpwnam(\"nobody\") failed");
Garrett Cooperdc2793f2010-12-16 18:43:08 -0800102
103 if (seteuid(ltpuser->pw_uid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800104 tst_brkm(TBROK | TERRNO, cleanup, "seteuid to nobody failed");
plars865695b2001-08-27 22:15:12 +0000105
plars865695b2001-08-27 22:15:12 +0000106 tst_sig(NOFORK, DEF_HANDLER, cleanup);
107
plars865695b2001-08-27 22:15:12 +0000108 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000109}
110
Wanlong Gao354ebb42012-12-07 10:10:04 +0800111void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000112{
Garrett Cooperdc2793f2010-12-16 18:43:08 -0800113 if (seteuid(0) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800114 tst_brkm(TBROK | TERRNO, NULL, "setuid(0) failed");
plars865695b2001-08-27 22:15:12 +0000115
Garrett Cooperdc2793f2010-12-16 18:43:08 -0800116 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700117}