• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	TEST(access(tc->pathname, tc->mode));
63 
64 	if (TST_RET == -1) {
65 		tst_res(TFAIL | TTERRNO, "access(%s, %s) as %s failed",
66 			tc->pathname, tc->name, user);
67 		return;
68 	}
69 
70 	switch (tc->mode) {
71 	case F_OK:
72 		/*
73 		 * The specified file(or pointed to by symbolic link)
74 		 * exists, attempt to get its status, if successful,
75 		 * access() behaviour is correct.
76 		 */
77 		TEST(stat(tc->targetname, &stat_buf));
78 
79 		if (TST_RET == -1) {
80 			tst_res(TFAIL | TTERRNO, "stat(%s) as %s failed",
81 				tc->targetname, user);
82 			return;
83 		}
84 
85 		break;
86 	case R_OK:
87 		/*
88 		 * The specified file(or pointed to by symbolic link)
89 		 * has read access, attempt to open the file with O_RDONLY,
90 		 * if we get a valid fd, access() behaviour is correct.
91 		 */
92 		TEST(open(tc->targetname, O_RDONLY));
93 
94 		if (TST_RET == -1) {
95 			tst_res(TFAIL | TTERRNO,
96 				"open %s with O_RDONLY as %s failed",
97 				tc->targetname, user);
98 			return;
99 		}
100 
101 		SAFE_CLOSE(TST_RET);
102 
103 		break;
104 	case W_OK:
105 		/*
106 		 * The specified file(or pointed to by symbolic link)
107 		 * has write access, attempt to open the file with O_WRONLY,
108 		 * if we get a valid fd, access() behaviour is correct.
109 		 */
110 		TEST(open(tc->targetname, O_WRONLY));
111 
112 		if (TST_RET == -1) {
113 			tst_res(TFAIL | TTERRNO,
114 				"open %s with O_WRONLY as %s failed",
115 				tc->targetname, user);
116 			return;
117 		}
118 
119 		SAFE_CLOSE(TST_RET);
120 
121 		break;
122 	case X_OK:
123 		/*
124 		 * The specified file(or pointed to by symbolic link)
125 		 * has execute access, attempt to execute the executable
126 		 * file, if successful, access() behaviour is correct.
127 		 */
128 		sprintf(command, "./%s", tc->targetname);
129 
130 		TEST(system(command));
131 
132 		if (TST_RET != 0) {
133 			tst_res(TFAIL | TTERRNO, "execute %s as %s failed",
134 				tc->targetname, user);
135 			return;
136 		}
137 
138 		break;
139 	default:
140 		break;
141 	}
142 
143 	tst_res(TPASS, "access(%s, %s) as %s behaviour is correct.",
144 		tc->pathname, tc->name, user);
145 }
146 
verify_access(unsigned int n)147 static void verify_access(unsigned int n)
148 {
149 	struct tcase *tc = &tcases[n];
150 	pid_t pid;
151 
152 	/* test as root */
153 	access_test(tc, "root");
154 
155 	/* test as nobody */
156 	pid = SAFE_FORK();
157 	if (pid) {
158 		SAFE_WAITPID(pid, NULL, 0);
159 	} else {
160 		SAFE_SETUID(uid);
161 		access_test(tc, "nobody");
162 	}
163 }
164 
setup(void)165 static void setup(void)
166 {
167 	struct passwd *pw;
168 
169 	pw = SAFE_GETPWNAM("nobody");
170 
171 	uid = pw->pw_uid;
172 
173 	SAFE_TOUCH(FNAME_F, 0000, NULL);
174 	SAFE_TOUCH(FNAME_R, 0444, NULL);
175 	SAFE_TOUCH(FNAME_W, 0222, NULL);
176 	SAFE_TOUCH(FNAME_X, 0555, NULL);
177 	SAFE_FILE_PRINTF(FNAME_X, "#!%s\n", _PATH_BSHELL);
178 
179 	SAFE_SYMLINK(FNAME_F, SNAME_F);
180 	SAFE_SYMLINK(FNAME_R, SNAME_R);
181 	SAFE_SYMLINK(FNAME_W, SNAME_W);
182 	SAFE_SYMLINK(FNAME_X, SNAME_X);
183 }
184 
185 static struct tst_test test = {
186 	.tcnt = ARRAY_SIZE(tcases),
187 	.needs_tmpdir = 1,
188 	.needs_root = 1,
189 	.forks_child = 1,
190 	.setup = setup,
191 	.test = verify_access,
192 };
193