aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/syscalls/chroot/chroot03.c
blob: 87faec3162f679f337f4b628452624e259c706b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *   Copyright (c) International Business Machines  Corp., 2001
 *
 *	 07/2001 Ported by Wayne Boyer
 */

/*\
 * [Description]
 *
 * Testcase to test whether chroot(2) sets errno correctly.
 *
 * - to test whether chroot() is setting ENAMETOOLONG if the
 *   pathname is more than VFS_MAXNAMELEN.
 * - to test whether chroot() is setting ENOTDIR if the argument
 *   is not a directory.
 * - to test whether chroot() is setting ENOENT if the directory
 *   does not exist.
 * - attempt to chroot to a path pointing to an invalid address
 *   and expect EFAULT as errno.
 * - to test whether chroot() is setting ELOOP if the two
 *   symbolic directory who point to each other.
 */

#include <stdio.h>
#include "tst_test.h"

#define FILE_NAME "test_file"
#define LOOP_DIR "sym_dir1"
#define NONEXISTENT_DIR "does_not_exist"

static char *longname_dir;
static char *file_name;
static char *nonexistent_dir;
static char *bad_ptr;
static char *loop_dir;

static struct tcase {
	char **dir;
	int error;
	char *desc;
} tcases[] = {
	{&longname_dir, ENAMETOOLONG, "chroot(longer than VFS_MAXNAMELEN)"},
	{&file_name, ENOTDIR, "chroot(not a directory)"},
	{&nonexistent_dir, ENOENT, "chroot(does not exists)"},
	{&bad_ptr, EFAULT, "chroot(an invalid address)"},
	{&loop_dir, ELOOP, "chroot(symlink loop)"}
};

static void verify_chroot(unsigned int n)
{
	struct tcase *tc = &tcases[n];

	TST_EXP_FAIL(chroot(*tc->dir), tc->error, "%s", tc->desc);
}

static void setup(void)
{
	SAFE_TOUCH(FILE_NAME, 0666, NULL);
	bad_ptr = tst_get_bad_addr(NULL);

	memset(longname_dir, 'a', PATH_MAX + 1);
	longname_dir[PATH_MAX+1] = 0;

	SAFE_SYMLINK("sym_dir1/", "sym_dir2");
	SAFE_SYMLINK("sym_dir2/", "sym_dir1");
}

static struct tst_test test = {
	.setup = setup,
	.tcnt = ARRAY_SIZE(tcases),
	.test = verify_chroot,
	.needs_tmpdir = 1,
	.bufs = (struct tst_buffers []) {
		{&file_name, .str = FILE_NAME},
		{&nonexistent_dir, .str = NONEXISTENT_DIR},
		{&loop_dir, .str = LOOP_DIR},
		{&longname_dir, .size = PATH_MAX+2},
		{}
	}
};