xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/chroot/chroot03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 #define FILE_NAME "test_file"
29 #define LOOP_DIR "sym_dir1"
30 #define NONEXISTENT_DIR "does_not_exist"
31 
32 static char *longname_dir;
33 static char *file_name;
34 static char *nonexistent_dir;
35 static char *bad_ptr;
36 static char *loop_dir;
37 
38 static struct tcase {
39 	char **dir;
40 	int error;
41 	char *desc;
42 } tcases[] = {
43 	{&longname_dir, ENAMETOOLONG, "chroot(longer than VFS_MAXNAMELEN)"},
44 	{&file_name, ENOTDIR, "chroot(not a directory)"},
45 	{&nonexistent_dir, ENOENT, "chroot(does not exists)"},
46 	{&bad_ptr, EFAULT, "chroot(an invalid address)"},
47 	{&loop_dir, ELOOP, "chroot(symlink loop)"}
48 };
49 
verify_chroot(unsigned int n)50 static void verify_chroot(unsigned int n)
51 {
52 	struct tcase *tc = &tcases[n];
53 
54 	TST_EXP_FAIL(chroot(*tc->dir), tc->error, "%s", tc->desc);
55 }
56 
setup(void)57 static void setup(void)
58 {
59 	SAFE_TOUCH(FILE_NAME, 0666, NULL);
60 	bad_ptr = tst_get_bad_addr(NULL);
61 
62 	memset(longname_dir, 'a', PATH_MAX + 1);
63 	longname_dir[PATH_MAX+1] = 0;
64 
65 	SAFE_SYMLINK("sym_dir1/", "sym_dir2");
66 	SAFE_SYMLINK("sym_dir2/", "sym_dir1");
67 }
68 
69 static struct tst_test test = {
70 	.setup = setup,
71 	.tcnt = ARRAY_SIZE(tcases),
72 	.test = verify_chroot,
73 	.needs_tmpdir = 1,
74 	.bufs = (struct tst_buffers []) {
75 		{&file_name, .str = FILE_NAME},
76 		{&nonexistent_dir, .str = NONEXISTENT_DIR},
77 		{&loop_dir, .str = LOOP_DIR},
78 		{&longname_dir, .size = PATH_MAX+2},
79 		{}
80 	}
81 };
82