1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * written by Wayne Boyer
5 * Copyright (c) 2013 Markos Chandras
6 * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
7 */
8
9 /*\
10 * [Description]
11 *
12 * Verify that:
13 *
14 * - getdents() fails with EBADF if file descriptor fd is invalid
15 * - getdents() fails with EINVAL if result buffer is too small
16 * - getdents() fails with ENOTDIR if file descriptor does not refer to a directory
17 * - getdents() fails with ENOENT if directory was unlinked()
18 * - getdents() fails with EFAULT if argument points outside the calling process's address space
19 */
20
21 #define _GNU_SOURCE
22 #include <errno.h>
23
24 #include "tst_test.h"
25 #include "getdents.h"
26
27 #define DIR_MODE 0755
28 #define MNTPOINT "mntpoint"
29 #define TEST_DIR MNTPOINT "/test_dir"
30 #define TEST_FILE MNTPOINT "/test"
31
32 static char *dirp;
33 static size_t size;
34
35 static char dirp1_arr[1];
36 static char *dirp1 = dirp1_arr;
37 static char *dirp_bad;
38 static size_t size1 = 1;
39
40 static int fd_inv = -5;
41 static int fd;
42 static int fd_file;
43 static int fd_unlinked;
44
45 static struct tcase {
46 int *fd;
47 char **dirp;
48 size_t *size;
49 int exp_errno;
50 } tcases[] = {
51 { &fd_inv, &dirp, &size, EBADF },
52 { &fd, &dirp1, &size1, EINVAL },
53 { &fd_file, &dirp, &size, ENOTDIR },
54 { &fd_unlinked, &dirp, &size, ENOENT },
55 { &fd, &dirp_bad, &size, EFAULT },
56 };
57
setup(void)58 static void setup(void)
59 {
60 getdents_info();
61
62 size = tst_dirp_size();
63 dirp = tst_alloc(size);
64
65 fd = SAFE_OPEN(MNTPOINT, O_RDONLY);
66 fd_file = SAFE_OPEN(TEST_FILE, O_CREAT | O_RDWR, 0644);
67
68 dirp_bad = tst_get_bad_addr(NULL);
69
70 SAFE_MKDIR(TEST_DIR, DIR_MODE);
71 fd_unlinked = SAFE_OPEN(TEST_DIR, O_DIRECTORY);
72 SAFE_RMDIR(TEST_DIR);
73 }
74
run(unsigned int i)75 static void run(unsigned int i)
76 {
77 struct tcase *tc = tcases + i;
78
79 TST_EXP_FAIL2(tst_getdents(*tc->fd, *tc->dirp, *tc->size),
80 tc->exp_errno, "fd=%i dirp=%p size=%zu",
81 *tc->fd, *tc->dirp, *tc->size);
82 }
83
84 static struct tst_test test = {
85 .test = run,
86 .setup = setup,
87 .tcnt = ARRAY_SIZE(tcases),
88 .test_variants = TEST_VARIANTS,
89 .needs_root = 1,
90 .all_filesystems = 1,
91 .mount_device = 1,
92 .mntpoint = MNTPOINT
93 };
94