1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 */
5
6 /*
7 * Test Description:
8 * Verify that access() succeeds to check the existence or read/write/execute
9 * permissions on a file if the mode argument passed was F_OK/R_OK/W_OK/X_OK.
10 *
11 * Also verify that, access() succeeds to test the accessibility of the file
12 * referred to by symbolic link if the pathname points to a symbolic link.
13 *
14 * As well as verify that, these test files can be
15 * stat/read/written/executed indeed as root and nobody respectively.
16 *
17 * Ported to LTP: Wayne Boyer
18 * 06/2016 Modified by Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
19 */
20
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <pwd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <paths.h>
28 #include "tst_test.h"
29
30 #define FNAME_F "file_f"
31 #define FNAME_R "file_r"
32 #define FNAME_W "file_w"
33 #define FNAME_X "file_x"
34 #define SNAME_F "symlink_f"
35 #define SNAME_R "symlink_r"
36 #define SNAME_W "symlink_w"
37 #define SNAME_X "symlink_x"
38
39 static uid_t uid;
40
41 static struct tcase {
42 const char *pathname;
43 int mode;
44 char *name;
45 const char *targetname;
46 } tcases[] = {
47 {FNAME_F, F_OK, "F_OK", FNAME_F},
48 {FNAME_R, R_OK, "R_OK", FNAME_R},
49 {FNAME_W, W_OK, "W_OK", FNAME_W},
50 {FNAME_X, X_OK, "X_OK", FNAME_X},
51 {SNAME_F, F_OK, "F_OK", FNAME_F},
52 {SNAME_R, R_OK, "R_OK", FNAME_R},
53 {SNAME_W, W_OK, "W_OK", FNAME_W},
54 {SNAME_X, X_OK, "X_OK", FNAME_X}
55 };
56
access_test(struct tcase * tc,const char * user)57 static void access_test(struct tcase *tc, const char *user)
58 {
59 struct stat stat_buf;
60 char command[64];
61
62 TST_EXP_PASS_SILENT(access(tc->pathname, tc->mode),
63 "access(%s, %s) as %s", tc->pathname, tc->name, user);
64
65 if (!TST_PASS)
66 return;
67
68 switch (tc->mode) {
69 case F_OK:
70 /*
71 * The specified file(or pointed to by symbolic link)
72 * exists, attempt to get its status, if successful,
73 * access() behaviour is correct.
74 */
75 TEST(stat(tc->targetname, &stat_buf));
76
77 if (TST_RET == -1) {
78 tst_res(TFAIL | TTERRNO, "stat(%s) as %s failed",
79 tc->targetname, user);
80 return;
81 }
82
83 break;
84 case R_OK:
85 /*
86 * The specified file(or pointed to by symbolic link)
87 * has read access, attempt to open the file with O_RDONLY,
88 * if we get a valid fd, access() behaviour is correct.
89 */
90 TEST(open(tc->targetname, O_RDONLY));
91
92 if (TST_RET == -1) {
93 tst_res(TFAIL | TTERRNO,
94 "open %s with O_RDONLY as %s failed",
95 tc->targetname, user);
96 return;
97 }
98
99 SAFE_CLOSE(TST_RET);
100
101 break;
102 case W_OK:
103 /*
104 * The specified file(or pointed to by symbolic link)
105 * has write access, attempt to open the file with O_WRONLY,
106 * if we get a valid fd, access() behaviour is correct.
107 */
108 TEST(open(tc->targetname, O_WRONLY));
109
110 if (TST_RET == -1) {
111 tst_res(TFAIL | TTERRNO,
112 "open %s with O_WRONLY as %s failed",
113 tc->targetname, user);
114 return;
115 }
116
117 SAFE_CLOSE(TST_RET);
118
119 break;
120 case X_OK:
121 /*
122 * The specified file(or pointed to by symbolic link)
123 * has execute access, attempt to execute the executable
124 * file, if successful, access() behaviour is correct.
125 */
126 sprintf(command, "./%s", tc->targetname);
127
128 TEST(system(command));
129
130 if (TST_RET != 0) {
131 tst_res(TFAIL | TTERRNO, "execute %s as %s failed",
132 tc->targetname, user);
133 return;
134 }
135
136 break;
137 default:
138 break;
139 }
140
141 tst_res(TPASS, "access(%s, %s) as %s behaviour is correct.",
142 tc->pathname, tc->name, user);
143 }
144
verify_access(unsigned int n)145 static void verify_access(unsigned int n)
146 {
147 struct tcase *tc = &tcases[n];
148 pid_t pid;
149
150 /* test as root */
151 access_test(tc, "root");
152
153 /* test as nobody */
154 pid = SAFE_FORK();
155 if (pid) {
156 SAFE_WAITPID(pid, NULL, 0);
157 } else {
158 SAFE_SETUID(uid);
159 access_test(tc, "nobody");
160 }
161 }
162
setup(void)163 static void setup(void)
164 {
165 struct passwd *pw;
166
167 pw = SAFE_GETPWNAM("nobody");
168
169 uid = pw->pw_uid;
170
171 SAFE_TOUCH(FNAME_F, 0000, NULL);
172 SAFE_TOUCH(FNAME_R, 0444, NULL);
173 SAFE_TOUCH(FNAME_W, 0222, NULL);
174 SAFE_TOUCH(FNAME_X, 0555, NULL);
175 SAFE_FILE_PRINTF(FNAME_X, "#!%s\n", _PATH_BSHELL);
176
177 SAFE_SYMLINK(FNAME_F, SNAME_F);
178 SAFE_SYMLINK(FNAME_R, SNAME_R);
179 SAFE_SYMLINK(FNAME_W, SNAME_W);
180 SAFE_SYMLINK(FNAME_X, SNAME_X);
181 }
182
183 static struct tst_test test = {
184 .tcnt = ARRAY_SIZE(tcases),
185 .needs_tmpdir = 1,
186 .needs_root = 1,
187 .forks_child = 1,
188 .setup = setup,
189 .test = verify_access,
190 };
191