• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (c) International Business Machines  Corp., 2001
4  *
5  *	 07/2001 Ported by Wayne Boyer
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Testcase to test whether chroot(2) sets errno correctly.
12  *
13  * - to test whether chroot() is setting ENAMETOOLONG if the
14  *   pathname is more than VFS_MAXNAMELEN.
15  * - to test whether chroot() is setting ENOTDIR if the argument
16  *   is not a directory.
17  * - to test whether chroot() is setting ENOENT if the directory
18  *   does not exist.
19  * - attempt to chroot to a path pointing to an invalid address
20  *   and expect EFAULT as errno.
21  * - to test whether chroot() is setting ELOOP if the two
22  *   symbolic directory who point to each other.
23  */
24 
25 #include <stdio.h>
26 #include "tst_test.h"
27 
28 static char fname[255];
29 static char nonexistent_dir[100] = "testdir";
30 static char bad_dir[] = "abcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz";
31 static char symbolic_dir[] = "sym_dir1";
32 
33 static struct tcase {
34 	char *dir;
35 	int error;
36 	char *desc;
37 } tcases[] = {
38 	{bad_dir, ENAMETOOLONG, "chroot(longer than VFS_MAXNAMELEN)"},
39 	{fname, ENOTDIR, "chroot(not a directory)"},
40 	{nonexistent_dir, ENOENT, "chroot(does not exists)"},
41 	{(char *)-1, EFAULT, "chroot(an invalid address)"},
42 	{symbolic_dir, ELOOP, "chroot(symlink loop)"}
43 };
44 
verify_chroot(unsigned int n)45 static void verify_chroot(unsigned int n)
46 {
47 	struct tcase *tc = &tcases[n];
48 
49 	TST_EXP_FAIL(chroot(tc->dir), tc->error, "%s", tc->desc);
50 }
51 
setup(void)52 static void setup(void)
53 {
54 	unsigned int i;
55 
56 	(void)sprintf(fname, "tfile_%d", getpid());
57 	SAFE_TOUCH(fname, 0666, NULL);
58 
59 	for (i = 0; i < ARRAY_SIZE(tcases); i++) {
60 		if (tcases[i].error == EFAULT)
61 			tcases[3].dir = tst_get_bad_addr(NULL);
62 	}
63 
64 	SAFE_SYMLINK("sym_dir1/", "sym_dir2");
65 	SAFE_SYMLINK("sym_dir2/", "sym_dir1");
66 }
67 
68 static struct tst_test test = {
69 	.setup = setup,
70 	.tcnt = ARRAY_SIZE(tcases),
71 	.test = verify_chroot,
72 	.needs_tmpdir = 1,
73 };
74