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 -lsandbox -o sandbox_appledouble_write sandbox_appledouble_write.c -g -Weverything */
30
31 #include <sys/xattr.h>
32 #include <sandbox/libsandbox.h>
33 #include <TargetConditionals.h>
34
35 #include <darwintest.h>
36 #include <darwintest/utils.h>
37
38 #define RUN_TEST TARGET_OS_OSX
39
40 #define FILE "file"
41 #define FILE_AD "._file"
42 #define TMP_FILE_AD "._tmpfile"
43
44 #define FILE2 "f"
45 #define FILE2_AD "._f"
46 #define TMP_FILE2_AD "._g"
47
48 static char template[MAXPATHLEN];
49 static char *testdir = NULL;
50 static char file[PATH_MAX], file2[PATH_MAX];
51 static sandbox_params_t params = NULL;
52 static sandbox_profile_t profile = NULL;
53
54 T_GLOBAL_META(
55 T_META_NAMESPACE("xnu.vfs"),
56 T_META_RADAR_COMPONENT_NAME("xnu"),
57 T_META_RADAR_COMPONENT_VERSION("vfs"),
58 T_META_ASROOT(true),
59 T_META_ENABLED(RUN_TEST),
60 T_META_CHECK_LEAKS(false));
61
62 static void
cleanup(void)63 cleanup(void)
64 {
65 if (profile) {
66 sandbox_free_profile(profile);
67 }
68 if (params) {
69 sandbox_free_params(params);
70 }
71 if (file[0] != '\0') {
72 unlink(file);
73 }
74 if (file2[0] != '\0') {
75 unlink(file2);
76 }
77 if (testdir) {
78 unmount(testdir, MNT_FORCE);
79 rmdir(testdir);
80 }
81 }
82
83 static void
create_profile_string(char * buff,size_t size)84 create_profile_string(char *buff, size_t size)
85 {
86 snprintf(buff, size, "(version 1) \n\
87 (allow default) \n\
88 (import \"system.sb\") \n\
89 (deny file-write-xattr (path \"%s\")) \n\
90 (deny file-write-xattr (path \"%s\")) \n",
91 file, file2);
92 }
93
94 T_DECL(sandbox_appledouble_write,
95 "Verify that the 'file-write-xattr' permission is enforced for apple-double files")
96 {
97 int testdirfd, fd;
98 char *sberror = NULL;
99 char profile_string[1000];
100 char testdir_path[MAXPATHLEN];
101 char mount_tmpfs_cmd[1000];
102 file[0] = file2[0] = '\0';
103
104 #if (!RUN_TEST)
105 T_SKIP("Not macOS");
106 #endif
107
108 if (geteuid() != 0) {
109 T_SKIP("Test should run as root");
110 }
111
112 T_ATEND(cleanup);
113 T_SETUPBEGIN;
114
115 /* Create test root dir */
116 snprintf(template, sizeof(template), "%s/sandbox_appledouble_write-XXXXXX", dt_tmpdir());
117 T_ASSERT_POSIX_NOTNULL((testdir = mkdtemp(template)), "Creating test root dir");
118 T_ASSERT_POSIX_SUCCESS((fd = open(testdir, O_SEARCH, 0777)), "Opening test root directory %s", testdir);
119 T_ASSERT_POSIX_SUCCESS(fcntl(fd, F_GETPATH, testdir_path), "Calling fcntl() to get the path");
120 close(fd);
121
122 /* mount tmpfs */
123 snprintf(mount_tmpfs_cmd, sizeof(mount_tmpfs_cmd), "/sbin/mount_tmpfs -s 50m %s", testdir_path);
124 T_ASSERT_POSIX_SUCCESS(system(mount_tmpfs_cmd), "Mounting tmpfs mount -> Should PASS");
125
126 T_EXPECT_POSIX_SUCCESS((testdirfd = open(testdir_path, O_SEARCH, 0777)), "Opening test root directory");
127
128 /* Setup file names */
129 snprintf(file, sizeof(file), "%s/%s", testdir_path, FILE);
130 snprintf(file2, sizeof(file2), "%s/%s", testdir_path, FILE2);
131
132 /* Create the test files */
133 T_ASSERT_POSIX_SUCCESS((fd = openat(testdirfd, FILE, O_CREAT | O_RDWR, 0777)), "Creating %s", FILE);
134 close(fd);
135
136 T_ASSERT_POSIX_SUCCESS((fd = openat(testdirfd, FILE_AD, O_CREAT | O_RDWR, 0777)), "Creating %s", FILE_AD);
137 close(fd);
138
139 T_ASSERT_POSIX_SUCCESS((fd = openat(testdirfd, FILE2, O_CREAT | O_RDWR, 0777)), "Creating %s", FILE2);
140 close(fd);
141
142 T_ASSERT_POSIX_SUCCESS((fd = openat(testdirfd, FILE2_AD, O_CREAT | O_RDWR, 0777)), "Creating %s", FILE2_AD);
143 close(fd);
144
145 /* Create sandbox variables */
146 T_ASSERT_POSIX_NOTNULL(params = sandbox_create_params(), "Creating Sandbox params object");
147 create_profile_string(profile_string, sizeof(profile_string));
148 T_ASSERT_POSIX_NOTNULL(profile = sandbox_compile_string(profile_string, params, &sberror), "Creating Sandbox profile object");
149
150 T_SETUPEND;
151
152 /* Validate SUCCESS for rename */
153 T_EXPECT_POSIX_SUCCESS(renameat(testdirfd, FILE_AD, testdirfd, TMP_FILE_AD), "Verifying that rename() of '%s' -> '%s' succeeded", FILE_AD, TMP_FILE_AD);
154 T_EXPECT_POSIX_SUCCESS(renameat(testdirfd, TMP_FILE_AD, testdirfd, FILE_AD), "Verifying that rename() of '%s' -> '%s' succeeded", TMP_FILE_AD, FILE_AD);
155
156 T_EXPECT_POSIX_SUCCESS(renameat(testdirfd, FILE2_AD, testdirfd, TMP_FILE2_AD), "Verifying that rename() of '%s' -> '%s' succeeded", FILE2_AD, TMP_FILE2_AD);
157 T_EXPECT_POSIX_SUCCESS(renameat(testdirfd, TMP_FILE2_AD, testdirfd, FILE2_AD), "Verifying that rename() of '%s' -> '%s' succeeded", TMP_FILE2_AD, FILE2_AD);
158
159 /* Validate SUCCESS for unlink */
160 T_EXPECT_POSIX_SUCCESS(unlinkat(testdirfd, FILE_AD, 0), "Verifying that unlink() of '%s' succeeded", FILE_AD);
161 T_EXPECT_POSIX_SUCCESS(unlinkat(testdirfd, FILE2_AD, 0), "Verifying that unlink() of '%s' succeeded", FILE2_AD);
162
163 /* Validate SUCCESS for open/create */
164 T_EXPECT_POSIX_SUCCESS((fd = openat(testdirfd, FILE_AD, O_CREAT | O_WRONLY, 0777)), "Verifying that open() with O_WRONLY of '%s' succeeded ", FILE_AD);
165 if (fd >= 0) {
166 close(fd);
167 }
168 T_EXPECT_POSIX_SUCCESS((fd = openat(testdirfd, FILE2_AD, O_CREAT | O_TRUNC, 0777)), "Verifying that open() with O_TRUNC of '%s' succeeded", FILE2_AD);
169 if (fd >= 0) {
170 close(fd);
171 }
172
173 /* Apply sandbox profile */
174 T_ASSERT_POSIX_SUCCESS(sandbox_apply(profile), "Applying Sandbox profile");
175
176 /* Validate EPERM for rename */
177 T_EXPECT_POSIX_FAILURE(renameat(testdirfd, FILE_AD, testdirfd, TMP_FILE_AD), EPERM, "Verifying that rename() of '%s' -> '%s' fails with EPERM", FILE_AD, TMP_FILE_AD);
178 T_EXPECT_POSIX_FAILURE(renameat(testdirfd, FILE2_AD, testdirfd, TMP_FILE2_AD), EPERM, "Verifying that rename() of '%s' -> '%s' fails with EPERM", FILE2_AD, TMP_FILE2_AD);
179
180 /* Validate EPERM for unlink */
181 T_EXPECT_POSIX_FAILURE(unlinkat(testdirfd, FILE_AD, 0), EPERM, "Verifying that unlink() of '%s' fails with EPERM", FILE_AD);
182 T_EXPECT_POSIX_FAILURE(unlinkat(testdirfd, FILE2_AD, 0), EPERM, "Verifying that unlink() of '%s' fails with EPERM", FILE2_AD);
183
184 /* Validate EPERM for open */
185 T_EXPECT_POSIX_FAILURE((fd = openat(testdirfd, FILE_AD, O_WRONLY, 0777)), EPERM, "Verifying that open() with O_WRONLY of '%s' fails with EPERM", FILE_AD);
186 if (fd >= 0) {
187 close(fd);
188 }
189 T_EXPECT_POSIX_FAILURE((fd = openat(testdirfd, FILE2_AD, O_TRUNC, 0777)), EPERM, "Verifying that open() with O_TRUNC of '%s' fails with EPERM", FILE2_AD);
190 if (fd >= 0) {
191 close(fd);
192 }
193
194 T_ASSERT_POSIX_SUCCESS(close(testdirfd), "Closing %s", testdir_path);
195 }
196