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 unique unique.c -g -Weverything */
30
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <sys/stat.h>
34
35 #include <darwintest.h>
36 #include <darwintest/utils.h>
37
38 static char template[MAXPATHLEN];
39 static char testdir_path[MAXPATHLEN + 1];
40 static char file_path[MAXPATHLEN];
41 static char *testdir = NULL;
42 static int testdir_fd = -1;
43 struct stat statbuf = {};
44
45 #ifndef O_UNIQUE
46 #define O_UNIQUE 0x00002000
47 #endif
48
49 #ifndef AT_UNIQUE
50 #define AT_UNIQUE 0x8000
51 #endif
52
53 #define FILE "file.txt"
54 #define FILE2 "file2.txt"
55 #define FILE3 "file3.txt"
56
57 T_GLOBAL_META(
58 T_META_NAMESPACE("xnu.vfs"),
59 T_META_RADAR_COMPONENT_NAME("xnu"),
60 T_META_RADAR_COMPONENT_VERSION("vfs"),
61 T_META_ASROOT(false),
62 T_META_CHECK_LEAKS(false));
63
64 static void
cleanup(void)65 cleanup(void)
66 {
67 if (testdir_fd != -1) {
68 unlinkat(testdir_fd, FILE, 0);
69 unlinkat(testdir_fd, FILE2, 0);
70 unlinkat(testdir_fd, FILE3, 0);
71
72 close(testdir_fd);
73 if (rmdir(testdir)) {
74 T_FAIL("Unable to remove the test directory (%s)", testdir);
75 }
76 }
77 }
78
79 static void
setup(const char * dirname)80 setup(const char *dirname)
81 {
82 int fd;
83
84 /* Create test root directory */
85 snprintf(template, sizeof(template), "%s/%s-XXXXXX", dt_tmpdir(), dirname);
86 T_ASSERT_POSIX_NOTNULL((testdir = mkdtemp(template)), "Creating test root directory");
87 T_ASSERT_POSIX_SUCCESS((testdir_fd = open(testdir, O_SEARCH, 0777)), "Opening test root directory %s", testdir);
88 T_ASSERT_POSIX_SUCCESS(fcntl(testdir_fd, F_GETPATH, testdir_path), "Calling fcntl() to get the path");
89
90 /* Create the test files */
91 snprintf(file_path, sizeof(file_path), "%s/%s", testdir_path, FILE);
92 T_ASSERT_POSIX_SUCCESS((fd = openat(testdir_fd, FILE, O_CREAT | O_RDWR | O_UNIQUE, 0777)), "Creating %s using openat() with O_UNIQUE -> Should PASS", FILE);
93 close(fd);
94 }
95
96 T_DECL(unique_open,
97 "Validate the functionality of the O_UNIQUE flag in the open/openat syscalls")
98 {
99 int fd;
100
101 T_SETUPBEGIN;
102
103 T_ATEND(cleanup);
104 setup("unique_open");
105
106 T_SETUPEND;
107
108 /* Validate nlink count equals 1 */
109 T_EXPECT_POSIX_SUCCESS((fstatat(testdir_fd, FILE, &statbuf, 0)), "Calling stat() for %s -> Should PASS", FILE);
110 T_EXPECT_EQ(statbuf.st_nlink, 1, "Validate nlink equals 1");
111 T_EXPECT_POSIX_SUCCESS((fd = openat(testdir_fd, FILE, O_RDONLY | O_UNIQUE, 0)), "Opening %s using O_UNIQUE -> Should PASS", FILE);
112 close(fd);
113
114 /* Increase nlink count */
115 T_EXPECT_POSIX_SUCCESS(linkat(testdir_fd, FILE, testdir_fd, FILE2, 0), "Calling linkat() for %s, %s -> Should PASS", FILE, FILE2);
116
117 /* Validate nlink count equals 2 */
118 T_EXPECT_POSIX_SUCCESS((fstatat(testdir_fd, FILE, &statbuf, 0)), "Calling fstatat() for %s -> Should PASS", FILE);
119 T_EXPECT_EQ(statbuf.st_nlink, 2, "Validate nlink equals 2");
120 T_EXPECT_POSIX_SUCCESS((fd = openat(testdir_fd, FILE, O_RDONLY, 0)), "Opening %s -> Should PASS", FILE);
121 close(fd);
122
123 /* Validate ENOTCAPABLE */
124 T_EXPECT_POSIX_FAILURE((fd = open(file_path, O_RDONLY | O_UNIQUE, 0)), ENOTCAPABLE, "Opening using open() with O_UNIQUE -> Should FAIL with ENOTCAPABLE");
125
126 T_EXPECT_POSIX_FAILURE((fd = openat(testdir_fd, FILE, O_WRONLY | O_UNIQUE, 0)), ENOTCAPABLE, "Opening %s using openat() with O_UNIQUE -> Should FAIL with ENOTCAPABLE", FILE);
127
128 T_EXPECT_POSIX_FAILURE((fd = openat(testdir_fd, FILE2, O_CREAT | O_RDWR | O_UNIQUE, 0)), ENOTCAPABLE, "Opening %s using openat() with O_UNIQUE -> Should FAIL with ENOTCAPABLE", FILE2);
129
130 /* Reduce nlink count */
131 T_EXPECT_POSIX_SUCCESS(unlinkat(testdir_fd, FILE2, 0), "Calling unlinkat() for %s -> Should PASS", FILE2);
132
133 /* Validate nlink count equals 1 */
134 T_EXPECT_POSIX_SUCCESS((fstatat(testdir_fd, FILE, &statbuf, 0)), "Calling fstatat() for %s -> Should PASS", FILE);
135 T_EXPECT_EQ(statbuf.st_nlink, 1, "Validate nlink equals 1");
136 T_EXPECT_POSIX_SUCCESS((fd = openat(testdir_fd, FILE, O_RDONLY | O_UNIQUE, 0)), "Opening %s -> Should PASS", FILE);
137 close(fd);
138 }
139
140 T_DECL(unique_faccessat,
141 "test faccessat() using the O_UNIQUE flag")
142 {
143 T_SETUPBEGIN;
144
145 T_ATEND(cleanup);
146 setup("unique_faccessat");
147
148 T_SETUPEND;
149
150 T_LOG("Testing the faccessat() syscall using O_UNIQUE");
151
152 /* Validate nlink count equals 1 */
153 T_EXPECT_POSIX_SUCCESS((fstatat(testdir_fd, FILE, &statbuf, 0)), "Calling stat() for %s -> Should PASS", FILE);
154 T_EXPECT_EQ(statbuf.st_nlink, 1, "Validate nlink equals 1");
155
156 /* Validate PASS when link count is 1 */
157 T_EXPECT_POSIX_SUCCESS(faccessat(testdir_fd, FILE, R_OK, AT_UNIQUE), "Calling faccessat() %s using AT_UNIQUE -> Should PASS", FILE);
158
159 /* Increase nlink count */
160 T_EXPECT_POSIX_SUCCESS(linkat(testdir_fd, FILE, testdir_fd, FILE2, 0), "Calling linkat() for %s, %s -> Should PASS", FILE, FILE2);
161
162 /* Validate ENOTCAPABLE */
163 T_EXPECT_POSIX_FAILURE(faccessat(testdir_fd, FILE, R_OK, AT_UNIQUE), ENOTCAPABLE, "Calling faccessat() %s with AT_UNIQUE -> Should FAIL with ENOTCAPABLE", FILE);
164 }
165
166 T_DECL(unique_fstatat,
167 "test fstatat() using the O_UNIQUE flag")
168 {
169 T_SETUPBEGIN;
170
171 T_ATEND(cleanup);
172 setup("unique_fstatat");
173
174 T_SETUPEND;
175
176 T_LOG("Testing the fstatat() syscall using O_UNIQUE");
177
178 /* Validate nlink count equals 1 */
179 T_EXPECT_POSIX_SUCCESS((fstatat(testdir_fd, FILE, &statbuf, 0)), "Calling stat() for %s -> Should PASS", FILE);
180 T_EXPECT_EQ(statbuf.st_nlink, 1, "Validate nlink equals 1");
181
182 /* Validate PASS when link count is 1 */
183 T_EXPECT_POSIX_SUCCESS(fstatat(testdir_fd, FILE, &statbuf, AT_UNIQUE), "Calling fstatat() %s using AT_UNIQUE -> Should PASS", FILE);
184
185 /* Increase nlink count */
186 T_EXPECT_POSIX_SUCCESS(linkat(testdir_fd, FILE, testdir_fd, FILE2, 0), "Calling linkat() for %s, %s -> Should PASS", FILE, FILE2);
187
188 /* Validate ENOTCAPABLE */
189 T_EXPECT_POSIX_FAILURE(fstatat(testdir_fd, FILE, &statbuf, AT_UNIQUE), ENOTCAPABLE, "Calling fstatat() %s with AT_UNIQUE -> Should FAIL with ENOTCAPABLE", FILE);
190 }
191
192 T_DECL(unique_fchmodat,
193 "test fchmodat() using the O_UNIQUE flag")
194 {
195 T_SETUPBEGIN;
196
197 T_ATEND(cleanup);
198 setup("unique_fchmodat");
199
200 T_SETUPEND;
201
202 T_LOG("Testing the fchmodat() syscall using O_UNIQUE");
203
204 /* Validate nlink count equals 1 */
205 T_EXPECT_POSIX_SUCCESS((fstatat(testdir_fd, FILE, &statbuf, 0)), "Calling stat() for %s -> Should PASS", FILE);
206 T_EXPECT_EQ(statbuf.st_nlink, 1, "Validate nlink equals 1");
207
208 /* Validate PASS when link count is 1 */
209 T_EXPECT_POSIX_SUCCESS(fchmodat(testdir_fd, FILE, ACCESSPERMS, AT_UNIQUE), "Calling fchmodat() %s using AT_UNIQUE -> Should PASS", FILE);
210
211 /* Increase nlink count */
212 T_EXPECT_POSIX_SUCCESS(linkat(testdir_fd, FILE, testdir_fd, FILE2, 0), "Calling linkat() for %s, %s -> Should PASS", FILE, FILE2);
213
214 /* Validate ENOTCAPABLE */
215 T_EXPECT_POSIX_FAILURE(fchmodat(testdir_fd, FILE, ACCESSPERMS, AT_UNIQUE), ENOTCAPABLE, "Calling fchmodat() %s with AT_UNIQUE -> Should FAIL with ENOTCAPABLE", FILE);
216 }
217
218 T_DECL(unique_fchownat,
219 "test fchownat() using the O_UNIQUE flag")
220 {
221 T_SETUPBEGIN;
222
223 T_ATEND(cleanup);
224 setup("unique_fchownat");
225
226 T_SETUPEND;
227
228 T_LOG("Testing the fchownat() syscall using O_UNIQUE");
229
230 /* Validate nlink count equals 1 */
231 T_EXPECT_POSIX_SUCCESS((fstatat(testdir_fd, FILE, &statbuf, 0)), "Calling stat() for %s -> Should PASS", FILE);
232 T_EXPECT_EQ(statbuf.st_nlink, 1, "Validate nlink equals 1");
233
234 /* Validate PASS when link count is 1 */
235 T_EXPECT_POSIX_SUCCESS(fchownat(testdir_fd, FILE, statbuf.st_uid, statbuf.st_gid, AT_UNIQUE), "Calling fchownat() %s using AT_UNIQUE -> Should PASS", FILE);
236
237 /* Increase nlink count */
238 T_EXPECT_POSIX_SUCCESS(linkat(testdir_fd, FILE, testdir_fd, FILE2, 0), "Calling linkat() for %s, %s -> Should PASS", FILE, FILE2);
239
240 /* Validate ENOTCAPABLE */
241 T_EXPECT_POSIX_FAILURE(fchownat(testdir_fd, FILE, statbuf.st_uid, statbuf.st_gid, AT_UNIQUE), ENOTCAPABLE, "Calling fchownat() %s with AT_UNIQUE -> Should FAIL with ENOTCAPABLE", FILE);
242 }
243
244 T_DECL(unique_linkat,
245 "test linkat() using the O_UNIQUE flag")
246 {
247 T_SETUPBEGIN;
248
249 T_ATEND(cleanup);
250 setup("unique_linkat");
251
252 T_SETUPEND;
253
254 T_LOG("Testing the linkat() syscall using O_UNIQUE");
255
256 /* Validate nlink count equals 1 */
257 T_EXPECT_POSIX_SUCCESS((fstatat(testdir_fd, FILE, &statbuf, 0)), "Calling stat() for %s -> Should PASS", FILE);
258 T_EXPECT_EQ(statbuf.st_nlink, 1, "Validate nlink equals 1");
259
260 /* Validate PASS when link count is 1 */
261 T_EXPECT_POSIX_SUCCESS(linkat(testdir_fd, FILE, testdir_fd, FILE2, AT_UNIQUE), "Calling linkat() for %s, %s using AT_UNIQUE -> Should PASS", FILE, FILE2);
262
263 /* Validate EEXIST */
264 T_EXPECT_POSIX_FAILURE(linkat(testdir_fd, FILE, testdir_fd, FILE2, 0), EEXIST, "Calling linkat() for %s, %s -> Should FAIL with EEXIST", FILE, FILE2);
265
266 /* Validate ENOTCAPABLE */
267 T_EXPECT_POSIX_FAILURE(linkat(testdir_fd, FILE, testdir_fd, FILE3, AT_UNIQUE), ENOTCAPABLE, "Calling linkat() for %s, %s with AT_UNIQUE -> Should FAIL with ENOTCAPABLE", FILE, FILE3);
268 }
269
270 T_DECL(unique_unlinkat,
271 "test unlinkat() using the O_UNIQUE flag")
272 {
273 int fd;
274
275 T_SETUPBEGIN;
276
277 T_ATEND(cleanup);
278 setup("unique_unlinkat");
279
280 T_SETUPEND;
281
282 T_LOG("Testing the unlinkat() syscall using O_UNIQUE");
283
284 /* Validate nlink count equals 1 */
285 T_EXPECT_POSIX_SUCCESS((fstatat(testdir_fd, FILE, &statbuf, 0)), "Calling stat() for %s -> Should PASS", FILE);
286 T_EXPECT_EQ(statbuf.st_nlink, 1, "Validate nlink equals 1");
287
288 /* Validate PASS when link count is 1 */
289 T_EXPECT_POSIX_SUCCESS(unlinkat(testdir_fd, FILE, AT_UNIQUE), "Calling unlinkat() %s using AT_UNIQUE -> Should PASS", FILE);
290
291 /* Recreate the test file */
292 T_ASSERT_POSIX_SUCCESS((fd = openat(testdir_fd, FILE, O_CREAT | O_RDWR, 0777)), "Creating %s", FILE);
293 close(fd);
294
295 /* Increase nlink count */
296 T_EXPECT_POSIX_SUCCESS(linkat(testdir_fd, FILE, testdir_fd, FILE2, 0), "Calling linkat() for %s, %s -> Should PASS", FILE, FILE2);
297
298 /* Validate ENOTCAPABLE */
299 T_EXPECT_POSIX_FAILURE(unlinkat(testdir_fd, FILE, AT_UNIQUE), ENOTCAPABLE, "Calling unlinkat() %s with AT_UNIQUE -> Should FAIL with ENOTCAPABLE", FILE);
300 }
301