xref: /xnu-12377.61.12/tests/vfs/resolve_namespace.c (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8)
1 /*
2  * Copyright (c) 2024 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 /* compile: xcrun -sdk macosx.internal clang -ldarwintest -o resolve_namespace resolve_namespace.c -g -Weverything */
30 
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <sys/stat.h>
34 #include <sys/xattr.h>
35 #include <sys/paths.h>
36 #include <sys/syslimits.h>
37 
38 #include <darwintest.h>
39 #include <darwintest/utils.h>
40 
41 static char template[MAXPATHLEN];
42 static char testdir_path[MAXPATHLEN + 1];
43 static char *testdir = NULL;
44 static int testdir_fd = -1, test_fd = -1;
45 
46 #ifndef ENOTCAPABLE
47 #define ENOTCAPABLE            107
48 #endif
49 
50 #ifndef RESOLVE_NOFOLLOW_ANY
51 #define RESOLVE_NOFOLLOW_ANY   0x00000001
52 #endif
53 
54 #ifndef RESOLVE_NODOTDOT
55 #define RESOLVE_NODOTDOT       0x00000002
56 #endif
57 
58 #ifndef RESOLVE_NODEVFS
59 #define RESOLVE_NODEVFS        0x00000008
60 #endif
61 
62 #ifndef RESOLVE_UNIQUE
63 #define RESOLVE_UNIQUE         0x00000020
64 #endif
65 
66 #ifndef RESOLVE_NOXATTRS
67 #define RESOLVE_NOXATTRS       0x00000040
68 #endif
69 
70 #define TEST_DIR       "test_dir"
71 #define FILE           "test_dir/file.txt"
72 #define FILE2          "test_dir/file2.txt"
73 #define FILE3          "test_dir/file3.txt"
74 #define DIR_SYMLINK    "test_dir/dir_symlink"
75 #define FILE_SYMLINK   "test_dir/dir_symlink/file_symlink.txt"
76 #define FILE_SYMLINK_2 "test_dir/dir_symlink/file_symlink_2.txt"
77 
78 T_GLOBAL_META(
79 	T_META_NAMESPACE("xnu.vfs"),
80 	T_META_RADAR_COMPONENT_NAME("xnu"),
81 	T_META_RADAR_COMPONENT_VERSION("vfs"),
82 	T_META_ASROOT(false),
83 	T_META_CHECK_LEAKS(false));
84 
85 static void
cleanup(void)86 cleanup(void)
87 {
88 	if (test_fd != -1) {
89 		close(test_fd);
90 	}
91 	if (testdir_fd != -1) {
92 		unlinkat(testdir_fd, FILE_SYMLINK, 0);
93 		unlinkat(testdir_fd, FILE_SYMLINK_2, 0);
94 		unlinkat(testdir_fd, FILE, 0);
95 		unlinkat(testdir_fd, DIR_SYMLINK, 0);
96 		unlinkat(testdir_fd, TEST_DIR, AT_REMOVEDIR);
97 
98 		close(testdir_fd);
99 		if (rmdir(testdir)) {
100 			T_FAIL("Unable to remove the test directory (%s)", testdir);
101 		}
102 	}
103 }
104 
105 static void
setup(const char * dirname)106 setup(const char *dirname)
107 {
108 	int fd;
109 	char symlink_path[PATH_MAX];
110 
111 	testdir_fd = test_fd = -1;
112 
113 	/* Create test root directory */
114 	snprintf(template, sizeof(template), "%s/%s-XXXXXX", dt_tmpdir(), dirname);
115 	T_ASSERT_POSIX_NOTNULL((testdir = mkdtemp(template)), "Creating test root directory");
116 	T_ASSERT_POSIX_SUCCESS((testdir_fd = open(testdir, O_SEARCH, 0777)), "Opening test root directory %s", testdir);
117 	T_ASSERT_POSIX_SUCCESS(fcntl(testdir_fd, F_GETPATH, testdir_path), "Calling fcntl() to get the path");
118 
119 	/* Create test directories */
120 	T_ASSERT_POSIX_SUCCESS(mkdirat(testdir_fd, TEST_DIR, 0777), "Creating %s/%s", testdir_path, TEST_DIR);
121 	T_ASSERT_POSIX_SUCCESS((test_fd = openat(testdir_fd, TEST_DIR, O_SEARCH, 0777)), "Opening test directory %s/%s", testdir_path, TEST_DIR);
122 
123 	/* Create the test files */
124 	snprintf(symlink_path, sizeof(symlink_path), "%s/%s/../", testdir_path, TEST_DIR);
125 	T_ASSERT_POSIX_SUCCESS(symlinkat(symlink_path, testdir_fd, DIR_SYMLINK), "Creating symbolic link %s ---> %s", DIR_SYMLINK, symlink_path);
126 	T_ASSERT_POSIX_SUCCESS((fd = openat(testdir_fd, FILE, O_CREAT | O_RDWR, 0777)), "Creating %s", FILE);
127 	close(fd);
128 	T_ASSERT_POSIX_SUCCESS((fd = openat(testdir_fd, FILE_SYMLINK, O_CREAT | O_RDWR, 0777)), "Creating %s", FILE_SYMLINK);
129 	close(fd);
130 }
131 
132 T_DECL(resolve_namespace_nofollow,
133     "Test the RESOLVE_NOFOLLOW_ANY prefix-path")
134 {
135 	int fd;
136 
137 	char file_nofollow[PATH_MAX];
138 	char symlink_nofollow[PATH_MAX];
139 	char symlink_resolve[PATH_MAX];
140 
141 	T_SETUPBEGIN;
142 
143 	T_ATEND(cleanup);
144 	setup("resolve_namespace_nofollow");
145 
146 	/* Setup file names */
147 	snprintf(file_nofollow, sizeof(file_nofollow), "/.nofollow/%s/%s", testdir_path, FILE);
148 	snprintf(symlink_nofollow, sizeof(symlink_nofollow), "/.nofollow/%s/%s", testdir_path, FILE_SYMLINK);
149 	snprintf(symlink_resolve, sizeof(symlink_resolve), "/.resolve/%d/%s/%s", RESOLVE_NOFOLLOW_ANY, testdir_path, FILE_SYMLINK_2);
150 
151 	T_SETUPEND;
152 
153 	T_EXPECT_POSIX_SUCCESS((fd = openat(testdir_fd, FILE, O_NOFOLLOW_ANY)), "Testing openat(O_NOFOLLOW_ANY) using path with no symlinks");
154 	close(fd);
155 
156 	T_EXPECT_POSIX_SUCCESS((fd = open(file_nofollow, O_NOFOLLOW_ANY)), "Testing open() using path with no symlinks and '.nofollow' prefix");
157 	close(fd);
158 
159 	T_EXPECT_POSIX_FAILURE((fd = openat(testdir_fd, FILE_SYMLINK, O_NOFOLLOW_ANY)), ELOOP, "Testing openat(O_NOFOLLOW_ANY) using path with a symlink");
160 	T_EXPECT_POSIX_FAILURE((fd = open(symlink_nofollow, 0)), ELOOP, "Testing open() using path with a symlink and '.nofollow' prefix");
161 
162 	T_EXPECT_POSIX_FAILURE((fd = openat(testdir_fd, FILE_SYMLINK_2, O_CREAT | O_NOFOLLOW_ANY)), ELOOP, "Testing openat(O_CREAT | O_NOFOLLOW_ANY) using path with a symlink");
163 	T_EXPECT_POSIX_FAILURE((fd = open(symlink_resolve, O_CREAT)), ELOOP, "Testing open(O_CREAT) using path with a symlink and '.resolve' prefix");
164 }
165 
166 T_DECL(resolve_namespace_nodotdot,
167     "Test the RESOLVE_NODOTDOT prefix-path")
168 {
169 	int fd;
170 
171 	char file_dotdot[PATH_MAX];
172 	char file_nodotdot[PATH_MAX];
173 	char symlink_dotdot[PATH_MAX];
174 
175 	T_SETUPBEGIN;
176 
177 	T_ATEND(cleanup);
178 	setup("resolve_namespace_nodotdot");
179 
180 	/* Setup file names */
181 	snprintf(file_dotdot, sizeof(file_dotdot), "/.resolve/%d/%s/%s/../%s", RESOLVE_NODOTDOT, testdir_path, TEST_DIR, FILE);
182 	snprintf(file_nodotdot, sizeof(file_nodotdot), "/.resolve/%d/%s/%s", RESOLVE_NODOTDOT, testdir_path, FILE);
183 	snprintf(symlink_dotdot, sizeof(symlink_dotdot), "/.resolve/%d/%s/%s", RESOLVE_NODOTDOT, testdir_path, FILE_SYMLINK);
184 
185 	T_SETUPEND;
186 
187 	T_EXPECT_POSIX_SUCCESS((fd = open(file_nodotdot, O_RDONLY)), "Testing open(O_RDONLY) without '..'");
188 	close(fd);
189 
190 	T_EXPECT_POSIX_FAILURE((fd = open(file_dotdot, O_RDONLY)), ENOTCAPABLE, "Testing open(O_RDONLY) using path including '..'");
191 	T_EXPECT_POSIX_FAILURE((fd = open(file_dotdot, O_RDONLY | O_CREAT)), ENOTCAPABLE, "Testing open(O_RDONLY | O_CREAT) using path including '..'");
192 	T_EXPECT_POSIX_FAILURE((fd = open(symlink_dotdot, O_RDONLY)), ENOTCAPABLE, "Testing open(O_RDONLY) using path with a symlink including '..'");
193 	T_EXPECT_POSIX_FAILURE((fd = open(symlink_dotdot, O_RDONLY | O_CREAT)), ENOTCAPABLE, "Testing open(O_RDONLY | O_CREAT) using path with a symlink including '..'");
194 }
195 
196 T_DECL(resolve_namespace_nodevfs,
197     "Test the RESOLVE_NODEVFS prefix-path")
198 {
199 	int fd, dirfd;
200 	struct stat statbuf;
201 	char path[PATH_MAX];
202 	const char *dir = "/private/var/tmp/";
203 
204 	T_SETUPBEGIN;
205 
206 	T_ASSERT_POSIX_SUCCESS((dirfd = open(dir, O_RDONLY | O_DIRECTORY)), "Opening %s", dir);
207 
208 	T_SETUPEND;
209 
210 	snprintf(path, sizeof(path), "/dev/null");
211 	T_EXPECT_POSIX_SUCCESS((fd = open(path, O_RDONLY)), "Opening %s -> should PASS", path);
212 	close(fd);
213 
214 	snprintf(path, sizeof(path), "/.resolve/%d/dev/null", RESOLVE_NODEVFS);
215 	T_EXPECT_POSIX_FAILURE((fd = open(path, O_RDONLY)), ENOTCAPABLE, "Opening %s -> Should fail with ENOTCAPABLE", path);
216 
217 	snprintf(path, sizeof(path), "/dev/nosuchdir/nosuchfile.txt");
218 	T_EXPECT_POSIX_FAILURE((fd = open(path, O_RDONLY)), ENOENT, "Opening a non-existent file %s -> Should fail with ENOENT", path);
219 
220 	snprintf(path, sizeof(path), "/.resolve/%d/dev/nosuchdir/nosuchfile.txt", RESOLVE_NODEVFS);
221 	T_EXPECT_POSIX_FAILURE((fd = open(path, O_RDONLY)), ENOTCAPABLE, "Opening a non-existent file %s -> Should fail with ENOTCAPABLE", path);
222 
223 	snprintf(path, sizeof(path), "/dev/nosuchfile.txt");
224 	T_EXPECT_EQ((fd = open(path, O_RDWR | O_CREAT)), -1, "Creating a file %s -> Should fail with an error", path);
225 
226 	snprintf(path, sizeof(path), "/.resolve/%d/dev/nosuchfile.txt", RESOLVE_NODEVFS);
227 	T_EXPECT_POSIX_FAILURE((fd = open(path, O_RDWR | O_CREAT)), ENOTCAPABLE, "Creating a file %s -> Should fail with ENOTCAPABLE", path);
228 
229 	snprintf(path, sizeof(path), "/dev/../");
230 	T_EXPECT_POSIX_SUCCESS((stat(path, &statbuf)), "Calling stat() for %s -> Should PASS", path);
231 	T_EXPECT_POSIX_SUCCESS((fd = open(path, O_RDONLY)), "Opening %s -> Should PASS", path);
232 	close(fd);
233 
234 	snprintf(path, sizeof(path), "/.resolve/%d/dev/../", RESOLVE_NODEVFS);
235 	T_EXPECT_POSIX_FAILURE((stat(path, &statbuf)), ENOTCAPABLE, "Calling stat() for %s -> Should fail with ENOTCAPABLE", path);
236 	T_EXPECT_POSIX_FAILURE((fd = open(path, O_RDONLY)), ENOTCAPABLE, "Opening %s -> Should fail with ENOTCAPABLE", path);
237 
238 	snprintf(path, sizeof(path), "/dev/fd/%d", dirfd);
239 	T_EXPECT_POSIX_SUCCESS((stat(path, &statbuf)), "Calling stat() for %s -> Should PASS", path);
240 	T_EXPECT_POSIX_SUCCESS((fd = open(path, O_RDONLY)), "Opening %s paths -> Should PASS", path);
241 	close(fd);
242 
243 	snprintf(path, sizeof(path), "/.resolve/%d/dev/fd/%d", RESOLVE_NODEVFS, dirfd);
244 	T_EXPECT_POSIX_FAILURE((stat(path, &statbuf)), ENOTCAPABLE, "Calling stat() for %s -> Should fail with ENOTCAPABLE", path);
245 	T_EXPECT_POSIX_FAILURE((fd = open(path, O_RDONLY)), ENOTCAPABLE, "Opening %s -> Should fail with ENOTCAPABLE", path);
246 
247 	close(dirfd);
248 }
249 
250 T_DECL(resolve_namespace_unique,
251     "Test the RESOLVE_UNIQUE prefix-path")
252 {
253 	int fd;
254 	struct stat statbuf;
255 	char file_unique[PATH_MAX], file_unique_symlink[PATH_MAX], file3[PATH_MAX];
256 	T_SETUPBEGIN;
257 
258 	T_ATEND(cleanup);
259 	setup("resolve_namespace_unique");
260 	snprintf(file3, sizeof(file3), "%s/%s", testdir_path, FILE3);
261 	snprintf(file_unique, sizeof(file_unique), "/.resolve/%d/%s/%s", RESOLVE_UNIQUE, testdir_path, FILE);
262 	snprintf(file_unique_symlink, sizeof(file_unique_symlink), "/.resolve/%d/%s/%s/%s", RESOLVE_UNIQUE, testdir_path, DIR_SYMLINK, FILE);
263 
264 	T_SETUPEND;
265 
266 	/* Validate nlink count equals 1 */
267 	T_EXPECT_POSIX_SUCCESS((stat(file_unique, &statbuf)), "Calling stat() for %s -> Should PASS", file_unique);
268 	T_EXPECT_EQ(statbuf.st_nlink, 1, "Validate nlink equals 1");
269 	T_EXPECT_POSIX_SUCCESS((fd = open(file_unique, O_RDONLY)), "Opening %s -> Should PASS", file_unique);
270 	close(fd);
271 
272 	/* Increase nlink count */
273 	T_EXPECT_POSIX_SUCCESS(linkat(testdir_fd, FILE, testdir_fd, FILE2, 0), "Calling linkat() for %s, %s -> Should PASS", FILE, FILE2);
274 
275 	/* Validate nlink count equals 2 */
276 	T_EXPECT_POSIX_SUCCESS((fstatat(testdir_fd, FILE, &statbuf, 0)), "Calling fstatat() for %s -> Should PASS", FILE);
277 	T_EXPECT_EQ(statbuf.st_nlink, 2, "Validate nlink equals 2");
278 
279 	/* Validate ENOTCAPABLE */
280 	T_EXPECT_POSIX_FAILURE((stat(file_unique, &statbuf)), ENOTCAPABLE, "Calling stat() for %s -> Should fail with ENOTCAPABLE", file_unique);
281 	T_EXPECT_POSIX_FAILURE((fd = open(file_unique, O_RDONLY)), ENOTCAPABLE, "Opening %s -> Should fail with ENOTCAPABLE", file_unique);
282 	T_EXPECT_POSIX_FAILURE(link(file_unique, file3), ENOTCAPABLE, "Calling link() for %s, %s -> Should fail with ENOTCAPABLE", file_unique, file3);
283 	T_EXPECT_POSIX_FAILURE(rename(file_unique_symlink, file3), ENOTCAPABLE, "Calling rename() for %s, %s -> Should fail with ENOTCAPABLE", file_unique_symlink, file3);
284 
285 	/* Reduce nlink count */
286 	T_EXPECT_POSIX_SUCCESS(unlinkat(testdir_fd, FILE2, 0), "Calling unlinkat() for %s -> Should PASS", FILE2);
287 
288 	/* Validate nlink count equals 1 */
289 	T_EXPECT_POSIX_SUCCESS((stat(file_unique, &statbuf)), "Calling stat() for %s -> Should PASS", file_unique);
290 	T_EXPECT_EQ(statbuf.st_nlink, 1, "Validate nlink equals 1");
291 	T_EXPECT_POSIX_SUCCESS((fd = open(file_unique, O_RDONLY)), "Opening %s -> Should PASS", file_unique);
292 	close(fd);
293 }
294 
295 T_DECL(resolve_namespace_noxattrs,
296     "Test the RESOLVE_NOXATTRS prefix-path")
297 {
298 	int fd;
299 	struct stat statbuf;
300 	const char *xattr = "test1234";
301 	size_t xattr_len = strlen(xattr);
302 	char file_path[PATH_MAX];
303 	char file_rfork[PATH_MAX], file_noxattrs_rfork[PATH_MAX];
304 
305 	T_SETUPBEGIN;
306 
307 	T_ATEND(cleanup);
308 	setup("resolve_namespace_noxattrs");
309 	snprintf(file_path, sizeof(file_path), "%s/%s", testdir_path, FILE);
310 
311 	snprintf(file_rfork, sizeof(file_rfork), "%s/%s/%s", testdir_path, FILE, _PATH_RSRCFORKSPEC);
312 	snprintf(file_noxattrs_rfork, sizeof(file_noxattrs_rfork), "/.resolve/%d/%s/%s/%s", RESOLVE_NOXATTRS, testdir_path, FILE, _PATH_RSRCFORKSPEC);
313 
314 	/* Set ResourceFork extended attribute */
315 	T_ASSERT_POSIX_SUCCESS(setxattr(file_path, XATTR_RESOURCEFORK_NAME, xattr, xattr_len, 0, 0), "Setting ResourceFork of %s to '%s'", file_path, xattr);
316 
317 	T_SETUPEND;
318 
319 	/* Call stat() for the resource fork file */
320 	T_EXPECT_POSIX_SUCCESS((stat(file_rfork, &statbuf)), "Calling stat() for %s -> Should PASS", file_rfork);
321 	T_EXPECT_POSIX_FAILURE((stat(file_noxattrs_rfork, &statbuf)), ENOTCAPABLE, "Calling stat() for %s -> Should fail with ENOTCAPABLE", file_noxattrs_rfork);
322 
323 	/* Open the resource fork file */
324 	T_EXPECT_POSIX_SUCCESS((fd = open(file_rfork, O_RDONLY)), "Opening %s -> Should PASS", file_rfork);
325 	close(fd);
326 	T_EXPECT_POSIX_FAILURE((fd = open(file_noxattrs_rfork, O_RDONLY)), ENOTCAPABLE, "Opening %s -> Should fail with ENOTCAPABLE", file_noxattrs_rfork);
327 }
328