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