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 * Copyright (c) 2013 Fujitsu Ltd.
5*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Linux Test Project, 2003-2023
6*49cdfc7eSAndroid Build Coastguard Worker * Ported to LTP: Wayne Boyer
7*49cdfc7eSAndroid Build Coastguard Worker * 11/2013 Ported by Xiaoguang Wang <[email protected]>
8*49cdfc7eSAndroid Build Coastguard Worker * 11/2016 Modified by Guangwen Feng <[email protected]>
9*49cdfc7eSAndroid Build Coastguard Worker */
10*49cdfc7eSAndroid Build Coastguard Worker
11*49cdfc7eSAndroid Build Coastguard Worker /*\
12*49cdfc7eSAndroid Build Coastguard Worker * [Description]
13*49cdfc7eSAndroid Build Coastguard Worker *
14*49cdfc7eSAndroid Build Coastguard Worker * - access() fails with -1 return value and sets errno to EINVAL
15*49cdfc7eSAndroid Build Coastguard Worker * if the specified access mode argument is invalid.
16*49cdfc7eSAndroid Build Coastguard Worker * - access() fails with -1 return value and sets errno to ENOENT
17*49cdfc7eSAndroid Build Coastguard Worker * if the specified file doesn't exist (or pathname is NULL).
18*49cdfc7eSAndroid Build Coastguard Worker * - access() fails with -1 return value and sets errno to ENAMETOOLONG
19*49cdfc7eSAndroid Build Coastguard Worker * if the pathname size is > PATH_MAX characters.
20*49cdfc7eSAndroid Build Coastguard Worker * - access() fails with -1 return value and sets errno to ENOTDIR
21*49cdfc7eSAndroid Build Coastguard Worker * if a component used as a directory in pathname is not a directory.
22*49cdfc7eSAndroid Build Coastguard Worker * - access() fails with -1 return value and sets errno to ELOOP
23*49cdfc7eSAndroid Build Coastguard Worker * if too many symbolic links were encountered in resolving pathname.
24*49cdfc7eSAndroid Build Coastguard Worker * - access() fails with -1 return value and sets errno to EROFS
25*49cdfc7eSAndroid Build Coastguard Worker * if write permission was requested for files on a read-only file system.
26*49cdfc7eSAndroid Build Coastguard Worker */
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mount.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
34*49cdfc7eSAndroid Build Coastguard Worker
35*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker #define FNAME1 "accessfile1"
38*49cdfc7eSAndroid Build Coastguard Worker #define FNAME2 "accessfile2/accessfile2"
39*49cdfc7eSAndroid Build Coastguard Worker #define DNAME "accessfile2"
40*49cdfc7eSAndroid Build Coastguard Worker #define SNAME1 "symlink1"
41*49cdfc7eSAndroid Build Coastguard Worker #define SNAME2 "symlink2"
42*49cdfc7eSAndroid Build Coastguard Worker #define MNT_POINT "mntpoint"
43*49cdfc7eSAndroid Build Coastguard Worker #define LONGPATHSIZE (PATH_MAX + 2)
44*49cdfc7eSAndroid Build Coastguard Worker
45*49cdfc7eSAndroid Build Coastguard Worker static uid_t uid;
46*49cdfc7eSAndroid Build Coastguard Worker static char *longpathname;
47*49cdfc7eSAndroid Build Coastguard Worker static char *fname1;
48*49cdfc7eSAndroid Build Coastguard Worker static char *fname2;
49*49cdfc7eSAndroid Build Coastguard Worker static char *sname1;
50*49cdfc7eSAndroid Build Coastguard Worker static char *empty_fname;
51*49cdfc7eSAndroid Build Coastguard Worker static char *mnt_point;
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
54*49cdfc7eSAndroid Build Coastguard Worker char **pathname;
55*49cdfc7eSAndroid Build Coastguard Worker int mode;
56*49cdfc7eSAndroid Build Coastguard Worker int exp_errno;
57*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
58*49cdfc7eSAndroid Build Coastguard Worker {&fname1, -1, EINVAL},
59*49cdfc7eSAndroid Build Coastguard Worker {&empty_fname, W_OK, ENOENT},
60*49cdfc7eSAndroid Build Coastguard Worker {&longpathname, R_OK, ENAMETOOLONG},
61*49cdfc7eSAndroid Build Coastguard Worker {&fname2, R_OK, ENOTDIR},
62*49cdfc7eSAndroid Build Coastguard Worker {&sname1, R_OK, ELOOP},
63*49cdfc7eSAndroid Build Coastguard Worker {&mnt_point, W_OK, EROFS}
64*49cdfc7eSAndroid Build Coastguard Worker };
65*49cdfc7eSAndroid Build Coastguard Worker
access_test(struct tcase * tc,const char * user)66*49cdfc7eSAndroid Build Coastguard Worker static void access_test(struct tcase *tc, const char *user)
67*49cdfc7eSAndroid Build Coastguard Worker {
68*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL(access(*tc->pathname, tc->mode), tc->exp_errno,
69*49cdfc7eSAndroid Build Coastguard Worker "access as %s", user);
70*49cdfc7eSAndroid Build Coastguard Worker }
71*49cdfc7eSAndroid Build Coastguard Worker
verify_access(unsigned int n)72*49cdfc7eSAndroid Build Coastguard Worker static void verify_access(unsigned int n)
73*49cdfc7eSAndroid Build Coastguard Worker {
74*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = tcases + n;
75*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker access_test(tc, "root");
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
80*49cdfc7eSAndroid Build Coastguard Worker if (pid) {
81*49cdfc7eSAndroid Build Coastguard Worker SAFE_WAITPID(pid, NULL, 0);
82*49cdfc7eSAndroid Build Coastguard Worker } else {
83*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETUID(uid);
84*49cdfc7eSAndroid Build Coastguard Worker access_test(tc, "nobody");
85*49cdfc7eSAndroid Build Coastguard Worker }
86*49cdfc7eSAndroid Build Coastguard Worker }
87*49cdfc7eSAndroid Build Coastguard Worker
setup(void)88*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
89*49cdfc7eSAndroid Build Coastguard Worker {
90*49cdfc7eSAndroid Build Coastguard Worker struct passwd *pw;
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard Worker pw = SAFE_GETPWNAM("nobody");
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker uid = pw->pw_uid;
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker memset(longpathname, 'a', LONGPATHSIZE - 1);
97*49cdfc7eSAndroid Build Coastguard Worker longpathname[LONGPATHSIZE-1] = 0;
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Worker SAFE_TOUCH(FNAME1, 0333, NULL);
100*49cdfc7eSAndroid Build Coastguard Worker SAFE_TOUCH(DNAME, 0644, NULL);
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker SAFE_SYMLINK(SNAME1, SNAME2);
103*49cdfc7eSAndroid Build Coastguard Worker SAFE_SYMLINK(SNAME2, SNAME1);
104*49cdfc7eSAndroid Build Coastguard Worker }
105*49cdfc7eSAndroid Build Coastguard Worker
106*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
107*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
108*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
109*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
110*49cdfc7eSAndroid Build Coastguard Worker .needs_rofs = 1,
111*49cdfc7eSAndroid Build Coastguard Worker .mntpoint = MNT_POINT,
112*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
113*49cdfc7eSAndroid Build Coastguard Worker .test = verify_access,
114*49cdfc7eSAndroid Build Coastguard Worker .bufs = (struct tst_buffers []) {
115*49cdfc7eSAndroid Build Coastguard Worker {&fname1, .str = FNAME1},
116*49cdfc7eSAndroid Build Coastguard Worker {&fname2, .str = FNAME2},
117*49cdfc7eSAndroid Build Coastguard Worker {&sname1, .str = SNAME1},
118*49cdfc7eSAndroid Build Coastguard Worker {&empty_fname, .str = ""},
119*49cdfc7eSAndroid Build Coastguard Worker {&longpathname, .size = LONGPATHSIZE},
120*49cdfc7eSAndroid Build Coastguard Worker {&mnt_point, .str = MNT_POINT},
121*49cdfc7eSAndroid Build Coastguard Worker {}
122*49cdfc7eSAndroid Build Coastguard Worker }
123*49cdfc7eSAndroid Build Coastguard Worker };
124