1 /*
2 * Copyright (c) 2025 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 devfs_nested_dir devfs_nested_dirs.c -g -Weverything */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <sys/mount.h>
37 #include <sys/param.h>
38 #include <sys/stat.h>
39 #include <darwintest.h>
40 #include <darwintest/utils.h>
41
42 T_GLOBAL_META(
43 T_META_NAMESPACE("xnu.vfs"),
44 T_META_RADAR_COMPONENT_NAME("xnu"),
45 T_META_RADAR_COMPONENT_VERSION("vfs"),
46 T_META_ASROOT(false),
47 T_META_ENABLED(TARGET_OS_OSX),
48 T_META_CHECK_LEAKS(false));
49
50
51 #define MAX_NESTED_SUBDIRS 16
52
53 static char template[MAXPATHLEN];
54 static char *mount_path;
55
56 static void
cleanup(void)57 cleanup(void)
58 {
59 if (mount_path) {
60 unmount(mount_path, MNT_FORCE);
61 rmdir(mount_path);
62 }
63 }
64
65 /* Create n-level nested subdirs and Return the last parent dir's fd. */
66 static int
create_nested_subdirs(char * dir,int levels)67 create_nested_subdirs(char *dir, int levels)
68 {
69 int parentdirfd = -1;
70
71 T_QUIET; T_ASSERT_POSIX_SUCCESS((parentdirfd = open(mount_path, O_SEARCH, 0777)),
72 "Opening mount root diretory");
73
74 for (int i = 0; i <= levels; i++) {
75 int dirfd;
76
77 T_QUIET; T_ASSERT_POSIX_SUCCESS(mkdirat(parentdirfd, dir, 0777),
78 "Verifying that creating subdir at level %d should pass", i);
79 T_QUIET; T_ASSERT_POSIX_SUCCESS((dirfd = openat(parentdirfd, dir, O_SEARCH)),
80 "Verifying that opening subdir at level %d should pass", i);
81
82 close(parentdirfd);
83 parentdirfd = dirfd;
84 }
85
86 return parentdirfd;
87 }
88
89 T_DECL(nested_subdirs_limit,
90 "Test creating and renaming nested subdirs in devfs mount",
91 T_META_ENABLED(true))
92 {
93 char todirpath[PATH_MAX];
94 int lastdirfd, rootdirfd, todirfd;
95
96 #if (!TARGET_OS_OSX)
97 T_SKIP("Not macOS");
98 #endif
99
100 T_ATEND(cleanup);
101
102 T_SETUPBEGIN;
103
104 snprintf(template, sizeof(template), "%s/devfs", dt_tmpdir());
105 T_ASSERT_POSIX_NOTNULL((mount_path = mkdtemp(template)),
106 "Creating mount path %s", template);
107
108 T_ASSERT_POSIX_SUCCESS(mount("devfs", mount_path, MNT_IGNORE_OWNERSHIP, NULL),
109 "Mounting devfs mount using path %s", mount_path);
110
111 T_ASSERT_POSIX_SUCCESS(chmod(mount_path, 0777),
112 "Changing permission on root diretory");
113
114 T_SETUPEND;
115
116 lastdirfd = create_nested_subdirs("aaa", MAX_NESTED_SUBDIRS);
117 T_ASSERT_NE_INT(lastdirfd, -1, "create_nested_subdirs(aaa, 16)");
118
119 T_ASSERT_POSIX_FAILURE(mkdirat(lastdirfd, "aaa", 0777), EMLINK,
120 "Verifying that creating subdir at max level (>16) should fail");
121 close(lastdirfd);
122
123 /* Create a nested subdirs of 8 levels deep.*/
124 lastdirfd = create_nested_subdirs("bbb", 8);
125 T_ASSERT_NE_INT(lastdirfd, -1, "create_nested_subdirs(bbb, 8)");
126 close(lastdirfd);
127
128 /* Create a nested subdirs of 10 levels deep.*/
129 lastdirfd = create_nested_subdirs("ccc", 10);
130 T_ASSERT_NE_INT(lastdirfd, -1, "create_nested_subdirs(ccc, 10)");
131 close(lastdirfd);
132
133 T_QUIET; T_ASSERT_POSIX_SUCCESS((rootdirfd = open(mount_path, O_SEARCH, 0777)),
134 "Opening mount root diretory");
135
136 snprintf(todirpath, sizeof(todirpath),
137 "%s/bbb/bbb/bbb/bbb/bbb/bbb/bbb/bbb/bbb", mount_path);
138 T_QUIET; T_ASSERT_POSIX_SUCCESS((todirfd = open(todirpath, O_SEARCH, 0777)),
139 "Opening target diretory");
140
141 /*
142 * Verify that rename should fail if the deepest subdir (after rename)
143 * would exceed the max nested subdirs limit.
144 */
145 T_ASSERT_POSIX_FAILURE(renameat(rootdirfd, "ccc", todirfd, "ccc"), EMLINK,
146 "Verifying that rename a dir with deep nested subdirs that exceed limit should fail");
147
148 /* Create a nested subdirs of 4 levels deep.*/
149 lastdirfd = create_nested_subdirs("ddd", 4);
150 T_ASSERT_NE_INT(lastdirfd, -1, "create_nested_subdirs(ddd, 4)");
151 close(lastdirfd);
152
153 /*
154 * Verify that rename should succeed if the deepest subdir (after rename)
155 * doesn't exceed the max nested subdirs limit.
156 */
157 T_ASSERT_POSIX_SUCCESS(renameat(rootdirfd, "ddd", todirfd, "ddd"),
158 "Verifying that rename a dir with nested subdirs that doesn't exceed limit should succeed");
159
160 close(todirfd);
161 close(rootdirfd);
162 }
163