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) 2023 FUJITSU LIMITED. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Linux Test Project, 2003-2023
5*49cdfc7eSAndroid Build Coastguard Worker * Author: Yang Xu <[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 basic error handling of faccessat2 syscall:
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * - faccessat2() fails with EFAULT if pathname is a bad pathname point.
14*49cdfc7eSAndroid Build Coastguard Worker * - faccessat2() fails with EINVAL if flags is -1.
15*49cdfc7eSAndroid Build Coastguard Worker * - faccessat2() fails with EINVAL if mode is -1.
16*49cdfc7eSAndroid Build Coastguard Worker * - faccessat2() fails with EBADF if dirfd is -1.
17*49cdfc7eSAndroid Build Coastguard Worker * - faccessat2() fails with ENOTDIR if pathname is relative path to a
18*49cdfc7eSAndroid Build Coastguard Worker * file and dir_fd is file descriptor for this file.
19*49cdfc7eSAndroid Build Coastguard Worker * - faccessat2() fails with EACCES if flags is AT_EACCESS and not using
20*49cdfc7eSAndroid Build Coastguard Worker * the effective user and group IDs.
21*49cdfc7eSAndroid Build Coastguard Worker *
22*49cdfc7eSAndroid Build Coastguard Worker * Minimum Linux version required is v5.8.
23*49cdfc7eSAndroid Build Coastguard Worker */
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
28*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
29*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/faccessat.h"
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker #define TESTUSER "nobody"
32*49cdfc7eSAndroid Build Coastguard Worker #define TESTDIR "faccessat2dir"
33*49cdfc7eSAndroid Build Coastguard Worker #define RELPATH "faccessat2dir/faccessat2file"
34*49cdfc7eSAndroid Build Coastguard Worker
35*49cdfc7eSAndroid Build Coastguard Worker static int fd;
36*49cdfc7eSAndroid Build Coastguard Worker static int bad_fd = -1;
37*49cdfc7eSAndroid Build Coastguard Worker static int atcwd_fd = AT_FDCWD;
38*49cdfc7eSAndroid Build Coastguard Worker static char *bad_path;
39*49cdfc7eSAndroid Build Coastguard Worker static char *rel_path;
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard Worker static struct passwd *ltpuser;
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
44*49cdfc7eSAndroid Build Coastguard Worker int *fd;
45*49cdfc7eSAndroid Build Coastguard Worker char **filename;
46*49cdfc7eSAndroid Build Coastguard Worker int mode;
47*49cdfc7eSAndroid Build Coastguard Worker int flags;
48*49cdfc7eSAndroid Build Coastguard Worker int exp_errno;
49*49cdfc7eSAndroid Build Coastguard Worker const char *desc;
50*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
51*49cdfc7eSAndroid Build Coastguard Worker {&atcwd_fd, &bad_path, R_OK, 0, EFAULT, "invalid address"},
52*49cdfc7eSAndroid Build Coastguard Worker {&atcwd_fd, &rel_path, R_OK, -1, EINVAL, "invalid flags"},
53*49cdfc7eSAndroid Build Coastguard Worker {&atcwd_fd, &rel_path, -1, 0, EINVAL, "invalid mode"},
54*49cdfc7eSAndroid Build Coastguard Worker {&bad_fd, &rel_path, R_OK, 0, EBADF, "invalid fd"},
55*49cdfc7eSAndroid Build Coastguard Worker {&fd, &rel_path, R_OK, 0, ENOTDIR, "fd pointing to file"},
56*49cdfc7eSAndroid Build Coastguard Worker {&atcwd_fd, &rel_path, R_OK, AT_EACCESS, EACCES,
57*49cdfc7eSAndroid Build Coastguard Worker "AT_EACCESS and unprivileged EUID"},
58*49cdfc7eSAndroid Build Coastguard Worker };
59*49cdfc7eSAndroid Build Coastguard Worker
verify_faccessat2(unsigned int i)60*49cdfc7eSAndroid Build Coastguard Worker static void verify_faccessat2(unsigned int i)
61*49cdfc7eSAndroid Build Coastguard Worker {
62*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[i];
63*49cdfc7eSAndroid Build Coastguard Worker
64*49cdfc7eSAndroid Build Coastguard Worker if (tc->exp_errno == EACCES)
65*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEUID(ltpuser->pw_uid);
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL(faccessat2(*tc->fd, *tc->filename, tc->mode, tc->flags),
68*49cdfc7eSAndroid Build Coastguard Worker tc->exp_errno, "faccessat2() with %s", tc->desc);
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker if (tc->exp_errno == EACCES)
71*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEUID(0);
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker
setup(void)74*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
75*49cdfc7eSAndroid Build Coastguard Worker {
76*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(TESTDIR, 0666);
77*49cdfc7eSAndroid Build Coastguard Worker SAFE_TOUCH(RELPATH, 0444, NULL);
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(RELPATH, O_RDONLY);
80*49cdfc7eSAndroid Build Coastguard Worker bad_path = tst_get_bad_addr(NULL);
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker ltpuser = SAFE_GETPWNAM(TESTUSER);
83*49cdfc7eSAndroid Build Coastguard Worker }
84*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)85*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
86*49cdfc7eSAndroid Build Coastguard Worker {
87*49cdfc7eSAndroid Build Coastguard Worker if (fd > -1)
88*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
89*49cdfc7eSAndroid Build Coastguard Worker }
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
92*49cdfc7eSAndroid Build Coastguard Worker .test = verify_faccessat2,
93*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
94*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
95*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
96*49cdfc7eSAndroid Build Coastguard Worker .bufs = (struct tst_buffers []) {
97*49cdfc7eSAndroid Build Coastguard Worker {&rel_path, .str = RELPATH},
98*49cdfc7eSAndroid Build Coastguard Worker {},
99*49cdfc7eSAndroid Build Coastguard Worker },
100*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
101*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
102*49cdfc7eSAndroid Build Coastguard Worker };
103