1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 Ported by Wayne Boyer
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * Testcase to test whether chroot(2) sets errno correctly.
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * - to test whether chroot() is setting ENAMETOOLONG if the
14*49cdfc7eSAndroid Build Coastguard Worker * pathname is more than VFS_MAXNAMELEN.
15*49cdfc7eSAndroid Build Coastguard Worker * - to test whether chroot() is setting ENOTDIR if the argument
16*49cdfc7eSAndroid Build Coastguard Worker * is not a directory.
17*49cdfc7eSAndroid Build Coastguard Worker * - to test whether chroot() is setting ENOENT if the directory
18*49cdfc7eSAndroid Build Coastguard Worker * does not exist.
19*49cdfc7eSAndroid Build Coastguard Worker * - attempt to chroot to a path pointing to an invalid address
20*49cdfc7eSAndroid Build Coastguard Worker * and expect EFAULT as errno.
21*49cdfc7eSAndroid Build Coastguard Worker * - to test whether chroot() is setting ELOOP if the two
22*49cdfc7eSAndroid Build Coastguard Worker * symbolic directory who point to each other.
23*49cdfc7eSAndroid Build Coastguard Worker */
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker #define FILE_NAME "test_file"
29*49cdfc7eSAndroid Build Coastguard Worker #define LOOP_DIR "sym_dir1"
30*49cdfc7eSAndroid Build Coastguard Worker #define NONEXISTENT_DIR "does_not_exist"
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker static char *longname_dir;
33*49cdfc7eSAndroid Build Coastguard Worker static char *file_name;
34*49cdfc7eSAndroid Build Coastguard Worker static char *nonexistent_dir;
35*49cdfc7eSAndroid Build Coastguard Worker static char *bad_ptr;
36*49cdfc7eSAndroid Build Coastguard Worker static char *loop_dir;
37*49cdfc7eSAndroid Build Coastguard Worker
38*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
39*49cdfc7eSAndroid Build Coastguard Worker char **dir;
40*49cdfc7eSAndroid Build Coastguard Worker int error;
41*49cdfc7eSAndroid Build Coastguard Worker char *desc;
42*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
43*49cdfc7eSAndroid Build Coastguard Worker {&longname_dir, ENAMETOOLONG, "chroot(longer than VFS_MAXNAMELEN)"},
44*49cdfc7eSAndroid Build Coastguard Worker {&file_name, ENOTDIR, "chroot(not a directory)"},
45*49cdfc7eSAndroid Build Coastguard Worker {&nonexistent_dir, ENOENT, "chroot(does not exists)"},
46*49cdfc7eSAndroid Build Coastguard Worker {&bad_ptr, EFAULT, "chroot(an invalid address)"},
47*49cdfc7eSAndroid Build Coastguard Worker {&loop_dir, ELOOP, "chroot(symlink loop)"}
48*49cdfc7eSAndroid Build Coastguard Worker };
49*49cdfc7eSAndroid Build Coastguard Worker
verify_chroot(unsigned int n)50*49cdfc7eSAndroid Build Coastguard Worker static void verify_chroot(unsigned int n)
51*49cdfc7eSAndroid Build Coastguard Worker {
52*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL(chroot(*tc->dir), tc->error, "%s", tc->desc);
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker
setup(void)57*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker SAFE_TOUCH(FILE_NAME, 0666, NULL);
60*49cdfc7eSAndroid Build Coastguard Worker bad_ptr = tst_get_bad_addr(NULL);
61*49cdfc7eSAndroid Build Coastguard Worker
62*49cdfc7eSAndroid Build Coastguard Worker memset(longname_dir, 'a', PATH_MAX + 1);
63*49cdfc7eSAndroid Build Coastguard Worker longname_dir[PATH_MAX+1] = 0;
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker SAFE_SYMLINK("sym_dir1/", "sym_dir2");
66*49cdfc7eSAndroid Build Coastguard Worker SAFE_SYMLINK("sym_dir2/", "sym_dir1");
67*49cdfc7eSAndroid Build Coastguard Worker }
68*49cdfc7eSAndroid Build Coastguard Worker
69*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
70*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
71*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
72*49cdfc7eSAndroid Build Coastguard Worker .test = verify_chroot,
73*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
74*49cdfc7eSAndroid Build Coastguard Worker .bufs = (struct tst_buffers []) {
75*49cdfc7eSAndroid Build Coastguard Worker {&file_name, .str = FILE_NAME},
76*49cdfc7eSAndroid Build Coastguard Worker {&nonexistent_dir, .str = NONEXISTENT_DIR},
77*49cdfc7eSAndroid Build Coastguard Worker {&loop_dir, .str = LOOP_DIR},
78*49cdfc7eSAndroid Build Coastguard Worker {&longname_dir, .size = PATH_MAX+2},
79*49cdfc7eSAndroid Build Coastguard Worker {}
80*49cdfc7eSAndroid Build Coastguard Worker }
81*49cdfc7eSAndroid Build Coastguard Worker };
82