blob: fbda38e866f2463438027032619cd19f5742461f [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
plars865695b2001-08-27 22:15:12 +00002 * Copyright (c) International Business Machines Corp., 2001
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000017 */
18
19/*
plars865695b2001-08-27 22:15:12 +000020 * Testcase to test whether chroot(2) sets errno correctly.
21 *
plars865695b2001-08-27 22:15:12 +000022 * 1. Test for ENAMETOOLONG:
23 * Create a bad directory name with length more than
24 * VFS_MAXNAMELEN (Linux kernel variable), and pass it as the
25 * path to chroot(2).
26 *
27 * 2. Test for ENOENT:
28 * Attempt to chroot(2) on a non-existent directory
29 *
30 * 3. Test for ENOTDIR:
31 * Attempt to chdir(2) on a file.
32 *
33 * 4. Test for EFAULT:
34 * The pathname parameter to chroot() points to an invalid address,
35 * chroot(2) fails with EPERM.
36 *
Xiaoguang Wang2edca2f2013-10-31 10:15:05 +080037 * 5. Test for ELOOP:
38 * Too many symbolic links were encountered When resolving the
39 * pathname parameter.
40 *
plars865695b2001-08-27 22:15:12 +000041 * 07/2001 Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +000042 */
43
44#include <stdio.h>
45#include <errno.h>
46#include <sys/stat.h>
plars1ad84512002-07-23 13:11:18 +000047#include <sys/mman.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080048#include "test.h"
robbiew2c945242002-11-11 19:01:25 +000049#include <fcntl.h>
Xiaoguang Wang2edca2f2013-10-31 10:15:05 +080050#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000051
52char *TCID = "chroot03";
plars865695b2001-08-27 22:15:12 +000053
Wanlong Gao278743a2013-10-31 16:37:47 +080054static int fd;
55static char fname[255];
56static char good_dir[100] = "/tmp/testdir";
57static char bad_dir[] = "abcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz";
58static char symbolic_dir[] = "sym_dir1";
plars865695b2001-08-27 22:15:12 +000059
plars865695b2001-08-27 22:15:12 +000060struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +000061 char *dir;
62 int error;
plars865695b2001-08-27 22:15:12 +000063} TC[] = {
64 /*
65 * to test whether chroot() is setting ENAMETOOLONG if the
66 * pathname is more than VFS_MAXNAMELEN
67 */
subrata_modak56207ce2009-03-23 13:35:39 +000068 {
69 bad_dir, ENAMETOOLONG},
70 /*
71 * to test whether chroot() is setting ENOTDIR if the argument
72 * is not a directory.
73 */
74 {
75 fname, ENOTDIR},
76 /*
77 * to test whether chroot() is setting ENOENT if the directory
78 * does not exist.
79 */
80 {
81 good_dir, ENOENT},
vapierb5ed1f62006-08-24 04:16:32 +000082#if !defined(UCLINUX)
subrata_modak56207ce2009-03-23 13:35:39 +000083 /*
84 * attempt to chroot to a path pointing to an invalid address
85 * and expect EFAULT as errno
86 */
87 {
Xiaoguang Wang2edca2f2013-10-31 10:15:05 +080088 (char *)-1, EFAULT},
vapierb5ed1f62006-08-24 04:16:32 +000089#endif
Xiaoguang Wang2edca2f2013-10-31 10:15:05 +080090 {symbolic_dir, ELOOP}
plars865695b2001-08-27 22:15:12 +000091};
92
Cyril Hrubisb863a0b2014-09-24 13:15:29 +020093int TST_TOTAL = ARRAY_SIZE(TC);
vapierb5ed1f62006-08-24 04:16:32 +000094
Wanlong Gao278743a2013-10-31 16:37:47 +080095static char *bad_addr;
plars1ad84512002-07-23 13:11:18 +000096
Wanlong Gao278743a2013-10-31 16:37:47 +080097static void setup(void);
98static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000099
subrata_modak56207ce2009-03-23 13:35:39 +0000100int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000101{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200102 int lc;
plars865695b2001-08-27 22:15:12 +0000103 int i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200104 const char *msg;
plars865695b2001-08-27 22:15:12 +0000105
Wanlong Gao278743a2013-10-31 16:37:47 +0800106 msg = parse_opts(ac, av, NULL, NULL);
107 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800108 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000109
110 setup();
111
plars865695b2001-08-27 22:15:12 +0000112 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800113 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000114
plars865695b2001-08-27 22:15:12 +0000115 for (i = 0; i < TST_TOTAL; i++) {
plars865695b2001-08-27 22:15:12 +0000116 TEST(chroot(TC[i].dir));
117
subrata_modak56207ce2009-03-23 13:35:39 +0000118 if (TEST_RETURN != -1) {
119 tst_resm(TFAIL, "call succeeded unexpectedly");
120 continue;
121 }
plars865695b2001-08-27 22:15:12 +0000122
subrata_modak56207ce2009-03-23 13:35:39 +0000123 if (TEST_ERRNO == TC[i].error) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800124 tst_resm(TPASS | TTERRNO, "failed as expected");
subrata_modak56207ce2009-03-23 13:35:39 +0000125 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800126 tst_resm(TFAIL | TTERRNO,
127 "didn't fail as expected (expected errno "
128 "= %d : %s)",
129 TC[i].error, strerror(TC[i].error));
plars865695b2001-08-27 22:15:12 +0000130 }
131 }
132 }
plars865695b2001-08-27 22:15:12 +0000133
Wanlong Gao278743a2013-10-31 16:37:47 +0800134 cleanup();
Garrett Cooperf23dd412010-12-17 01:57:35 -0800135 tst_exit();
136}
plars865695b2001-08-27 22:15:12 +0000137
Wanlong Gao278743a2013-10-31 16:37:47 +0800138static void setup(void)
plars865695b2001-08-27 22:15:12 +0000139{
plars865695b2001-08-27 22:15:12 +0000140 tst_sig(NOFORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000141 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000142 tst_tmpdir();
143
144 /*
145 * create a file and use it to test whether chroot() is setting
146 * ENOTDIR if the argument is not a directory.
147 */
subrata_modak56207ce2009-03-23 13:35:39 +0000148 (void)sprintf(fname, "tfile_%d", getpid());
Wanlong Gao278743a2013-10-31 16:37:47 +0800149 fd = creat(fname, 0777);
150 if (fd == -1)
plars865695b2001-08-27 22:15:12 +0000151 tst_brkm(TBROK, cleanup, "Failed to creat a temp file");
plars865695b2001-08-27 22:15:12 +0000152
153 /*
154 * set up good_dir to test whether chroot() is setting ENOENT if the
155 * directory does not exist.
156 */
157 (void)sprintf(good_dir, "%s.%d", good_dir, getpid());
plars1ad84512002-07-23 13:11:18 +0000158
vapier62b16cf2007-02-09 20:48:23 +0000159#if !defined(UCLINUX)
robbiewd34d5812005-07-11 22:28:09 +0000160 bad_addr = mmap(0, 1, PROT_NONE,
subrata_modak56207ce2009-03-23 13:35:39 +0000161 MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
Wanlong Gao278743a2013-10-31 16:37:47 +0800162 if (bad_addr == MAP_FAILED)
plars1ad84512002-07-23 13:11:18 +0000163 tst_brkm(TBROK, cleanup, "mmap failed");
Wanlong Gao278743a2013-10-31 16:37:47 +0800164
plars1ad84512002-07-23 13:11:18 +0000165 TC[3].dir = bad_addr;
vapierb5ed1f62006-08-24 04:16:32 +0000166#endif
Xiaoguang Wang2edca2f2013-10-31 10:15:05 +0800167 /*
168 * create two symbolic directory who point to each other to
169 * test ELOOP.
170 */
171 SAFE_SYMLINK(cleanup, "sym_dir1/", "sym_dir2");
172 SAFE_SYMLINK(cleanup, "sym_dir2/", "sym_dir1");
plars865695b2001-08-27 22:15:12 +0000173}
174
Wanlong Gao278743a2013-10-31 16:37:47 +0800175static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000176{
subrata_modak56207ce2009-03-23 13:35:39 +0000177 close(fd);
plars865695b2001-08-27 22:15:12 +0000178 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700179}