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) 2020 Viresh Kumar <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * openat2() tests with various resolve flags.
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
8*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/openat2.h"
9*49cdfc7eSAndroid Build Coastguard Worker
10*49cdfc7eSAndroid Build Coastguard Worker #define FOO_SYMLINK "foo_symlink"
11*49cdfc7eSAndroid Build Coastguard Worker
12*49cdfc7eSAndroid Build Coastguard Worker static struct open_how *how;
13*49cdfc7eSAndroid Build Coastguard Worker
14*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
15*49cdfc7eSAndroid Build Coastguard Worker const char *name;
16*49cdfc7eSAndroid Build Coastguard Worker const char *pathname;
17*49cdfc7eSAndroid Build Coastguard Worker uint64_t resolve;
18*49cdfc7eSAndroid Build Coastguard Worker int exp_errno;
19*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
20*49cdfc7eSAndroid Build Coastguard Worker /* Success cases */
21*49cdfc7eSAndroid Build Coastguard Worker {"open /proc/version", "/proc/version", 0, 0},
22*49cdfc7eSAndroid Build Coastguard Worker {"open magiclinks", "/proc/self/exe", 0, 0},
23*49cdfc7eSAndroid Build Coastguard Worker {"open symlinks", FOO_SYMLINK, 0, 0},
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker /* Failure cases */
26*49cdfc7eSAndroid Build Coastguard Worker {"resolve-no-xdev", "/proc/version", RESOLVE_NO_XDEV, EXDEV},
27*49cdfc7eSAndroid Build Coastguard Worker {"resolve-no-magiclinks", "/proc/self/exe", RESOLVE_NO_MAGICLINKS, ELOOP},
28*49cdfc7eSAndroid Build Coastguard Worker {"resolve-no-symlinks", FOO_SYMLINK, RESOLVE_NO_SYMLINKS, ELOOP},
29*49cdfc7eSAndroid Build Coastguard Worker {"resolve-beneath", "/proc/version", RESOLVE_BENEATH, EXDEV},
30*49cdfc7eSAndroid Build Coastguard Worker {"resolve-beneath", "../foo", RESOLVE_BENEATH, EXDEV},
31*49cdfc7eSAndroid Build Coastguard Worker {"resolve-no-in-root", "/proc/version", RESOLVE_IN_ROOT, ENOENT},
32*49cdfc7eSAndroid Build Coastguard Worker };
33*49cdfc7eSAndroid Build Coastguard Worker
setup(void)34*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
35*49cdfc7eSAndroid Build Coastguard Worker {
36*49cdfc7eSAndroid Build Coastguard Worker openat2_supported_by_kernel();
37*49cdfc7eSAndroid Build Coastguard Worker SAFE_SYMLINK("/proc/version", FOO_SYMLINK);
38*49cdfc7eSAndroid Build Coastguard Worker }
39*49cdfc7eSAndroid Build Coastguard Worker
run(unsigned int n)40*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int n)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker how->flags = O_RDONLY | O_CREAT;
45*49cdfc7eSAndroid Build Coastguard Worker how->mode = S_IRUSR;
46*49cdfc7eSAndroid Build Coastguard Worker how->resolve = tc->resolve;
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker TEST(openat2(AT_FDCWD, tc->pathname, how, sizeof(*how)));
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker if (!tc->exp_errno) {
51*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET < 0) {
52*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "%s: openat2() failed",
53*49cdfc7eSAndroid Build Coastguard Worker tc->name);
54*49cdfc7eSAndroid Build Coastguard Worker return;
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(TST_RET);
58*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "%s: openat2() passed", tc->name);
59*49cdfc7eSAndroid Build Coastguard Worker } else {
60*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET >= 0) {
61*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(TST_RET);
62*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "%s: openat2() passed unexpectedly",
63*49cdfc7eSAndroid Build Coastguard Worker tc->name);
64*49cdfc7eSAndroid Build Coastguard Worker return;
65*49cdfc7eSAndroid Build Coastguard Worker }
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker if (tc->exp_errno != TST_ERR) {
68*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "%s: openat2() should fail with %s",
69*49cdfc7eSAndroid Build Coastguard Worker tc->name, tst_strerrno(tc->exp_errno));
70*49cdfc7eSAndroid Build Coastguard Worker return;
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "%s: openat2() failed as expected",
74*49cdfc7eSAndroid Build Coastguard Worker tc->name);
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker
78*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
79*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
80*49cdfc7eSAndroid Build Coastguard Worker .test = run,
81*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
82*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
83*49cdfc7eSAndroid Build Coastguard Worker .bufs = (struct tst_buffers []) {
84*49cdfc7eSAndroid Build Coastguard Worker {&how, .size = sizeof(*how)},
85*49cdfc7eSAndroid Build Coastguard Worker {},
86*49cdfc7eSAndroid Build Coastguard Worker }
87*49cdfc7eSAndroid Build Coastguard Worker };
88