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