1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * 07/2001 Ported by Wayne Boyer
5 * Copyright (c) 2024 SUSE LLC <[email protected]>
6 */
7
8 /*\
9 * [Description]
10 *
11 * Test functionality and error conditions of open(O_NOFOLLOW) system call.
12 */
13
14 #include "tst_test.h"
15 #include "tst_safe_macros.h"
16
17 #define TESTFILE "testfile"
18 #define TESTDIR "testdir"
19 #define SYMFILE1 "symfile1"
20 #define SYMFILE2 "symfile2"
21 #define SYMDIR1 "symdir1"
22 #define SYMDIR2 "symdir2"
23 #define PASSFILE "symdir1/testfile"
24
25 static struct testcase {
26 const char *path;
27 int err;
28 const char *desc;
29 } testcase_list[] = {
30 {SYMFILE1, ELOOP, "open(O_NOFOLLOW) a symlink to file"},
31 {SYMFILE2, ELOOP, "open(O_NOFOLLOW) a double symlink to file"},
32 {SYMDIR1, ELOOP, "open(O_NOFOLLOW) a symlink to directory"},
33 {SYMDIR2, ELOOP, "open(O_NOFOLLOW) a double symlink to directory"},
34 {PASSFILE, 0, "open(O_NOFOLLOW) a file in symlinked directory"},
35 };
36
setup(void)37 static void setup(void)
38 {
39 int fd;
40
41 umask(0);
42 fd = SAFE_CREAT(TESTFILE, 0644);
43 SAFE_CLOSE(fd);
44 SAFE_MKDIR(TESTDIR, 0755);
45
46 SAFE_SYMLINK(TESTFILE, SYMFILE1);
47 SAFE_SYMLINK(SYMFILE1, SYMFILE2);
48 SAFE_SYMLINK(TESTDIR, SYMDIR1);
49 SAFE_SYMLINK(SYMDIR1, SYMDIR2);
50
51 fd = SAFE_CREAT(PASSFILE, 0644);
52 SAFE_CLOSE(fd);
53 }
54
run(unsigned int n)55 static void run(unsigned int n)
56 {
57 const struct testcase *tc = testcase_list + n;
58
59 if (tc->err) {
60 TST_EXP_FAIL2(open(tc->path, O_NOFOLLOW | O_RDONLY), tc->err,
61 "%s", tc->desc);
62 } else {
63 TST_EXP_FD(open(tc->path, O_NOFOLLOW | O_RDONLY),
64 "%s", tc->desc);
65 }
66
67 if (TST_RET >= 0)
68 SAFE_CLOSE(TST_RET);
69 }
70
71 static struct tst_test test = {
72 .setup = setup,
73 .test = run,
74 .tcnt = ARRAY_SIZE(testcase_list),
75 .needs_tmpdir = 1
76 };
77