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 named_fork_path named_fork_path.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 static char template[MAXPATHLEN];
41 static char *testdir = NULL;
42 static char rsrc[PATH_MAX];
43 static char file[PATH_MAX], file_rfork[PATH_MAX];
44 static char file2[PATH_MAX], file2_rfork[PATH_MAX];
45 static sandbox_params_t params = NULL;
46 static sandbox_profile_t profile = NULL;
47
48 T_GLOBAL_META(
49 T_META_NAMESPACE("xnu.vfs"),
50 T_META_RADAR_COMPONENT_NAME("xnu"),
51 T_META_RADAR_COMPONENT_VERSION("vfs"),
52 T_META_ASROOT(false),
53 T_META_ENABLED(RUN_TEST),
54 T_META_CHECK_LEAKS(false));
55
56 static void
cleanup(void)57 cleanup(void)
58 {
59 if (profile) {
60 sandbox_free_profile(profile);
61 }
62 if (params) {
63 sandbox_free_params(params);
64 }
65 if (file[0] != '\0') {
66 unlink(file);
67 }
68 if (file2[0] != '\0') {
69 unlink(file2);
70 }
71 if (rsrc[0] != '\0') {
72 unlink(rsrc);
73 }
74 if (testdir) {
75 rmdir(testdir);
76 }
77 }
78
79 static void
create_profile_string(char * buff,size_t size)80 create_profile_string(char *buff, size_t size)
81 {
82 snprintf(buff, size, "(version 1) \n\
83 (allow default) \n\
84 (import \"system.sb\") \n\
85 (deny file-read-xattr file-write-xattr (path \"%s\")) \n\
86 (deny file-read-xattr file-write-xattr (path \"%s\")) \n",
87 file, file2);
88 }
89
90 T_DECL(named_fork_path,
91 "Named fork paths to check file-read-xattr and file-write-xattr Sandbox permissions")
92 {
93 #if (!RUN_TEST)
94 T_SKIP("Not macOS");
95 #endif
96
97 int fd, fd2, fd3, fd_rfork;
98 char xattr_buff[100];
99 char *sberror = NULL;
100 const char *xattr = "test1234";
101 char profile_string[1000];
102 size_t xattr_len = strlen(xattr);
103 char testdir_path[MAXPATHLEN];
104
105 file[0] = file2[0] = rsrc[0] = '\0';
106
107 T_ATEND(cleanup);
108 T_SETUPBEGIN;
109
110 /* Create test root dir */
111 snprintf(template, sizeof(template), "%s/named_fork_path-XXXXXX", dt_tmpdir());
112 T_ASSERT_POSIX_NOTNULL((testdir = mkdtemp(template)), "Creating test root dir");
113 T_ASSERT_POSIX_SUCCESS((fd = open(testdir, O_SEARCH, 0777)), "Opening test root directory %s", testdir);
114 T_ASSERT_POSIX_SUCCESS(fcntl(fd, F_GETPATH, testdir_path), "Calling fcntl() to get the path");
115 T_ASSERT_POSIX_SUCCESS(close(fd), "Closing %s", testdir_path);
116
117 /* Setup file names */
118 snprintf(file, sizeof(file), "%s/%s", testdir_path, "file");
119 snprintf(file_rfork, sizeof(file_rfork), "%s/..namedfork/rsrc", file);
120
121 snprintf(file2, sizeof(file2), "%s/%s", testdir_path, "file2");
122 snprintf(file2_rfork, sizeof(file2_rfork), "%s/..namedfork/rsrc", file2);
123
124 snprintf(rsrc, sizeof(rsrc), "%s/%s", testdir_path, "rsrc");
125
126 /* Create the test files */
127 T_ASSERT_POSIX_SUCCESS((fd = open(file, O_CREAT | O_RDWR, 0777)), "Creating %s", file);
128 T_ASSERT_POSIX_SUCCESS((fd2 = open(file2, O_CREAT | O_RDWR, 0777)), "Creating %s", file2);
129 T_ASSERT_POSIX_SUCCESS((fd3 = open(rsrc, O_CREAT | O_RDWR, 0777)), "Creating %s", rsrc);
130
131 /* Set ResourceFork extended attribute */
132 T_ASSERT_POSIX_SUCCESS(fsetxattr(fd, XATTR_RESOURCEFORK_NAME, xattr, xattr_len, 0, 0), "Setting ResourceFork of %s to '%s'", file, xattr);
133
134 /* Create sandbox variables */
135 T_ASSERT_POSIX_NOTNULL(params = sandbox_create_params(), "Creating Sandbox params object");
136 create_profile_string(profile_string, sizeof(profile_string));
137 T_ASSERT_POSIX_NOTNULL(profile = sandbox_compile_string(profile_string, params, &sberror), "Creating Sandbox profile object");
138
139 T_SETUPEND;
140
141 /* Test rename to/from an ..namedfork/rsrc path */
142 T_ASSERT_POSIX_FAILURE(rename(file_rfork, rsrc), EPERM, "Verifying rename from an ..namedfork/rsrc path isn't a supported (EPERM)");
143 T_ASSERT_POSIX_FAILURE(rename(rsrc, file_rfork), EPERM, "Verifying trename to an ..namedfork/rsrc path isn't a supported (EPERM)");
144
145 /* Read ResourceFork extended attribute using getxattr() */
146 T_ASSERT_EQ((ssize_t)xattr_len, fgetxattr(fd, XATTR_RESOURCEFORK_NAME, xattr_buff, sizeof(xattr_buff), 0, 0),
147 "Trying to get ResourceFork extended attribute");
148 T_ASSERT_EQ(0, strncmp(xattr, xattr_buff, xattr_len), "Verifying ResourceFork extended content");
149
150 /* Read ResourceFork extended attribute using the ..namedfork/rsrc path */
151 T_ASSERT_POSIX_SUCCESS((fd_rfork = open(file_rfork, O_RDONLY, 0777)), "Opening %s", file_rfork);
152 T_ASSERT_EQ((ssize_t)xattr_len, read(fd_rfork, xattr_buff, sizeof(xattr_buff)), "Trying to read ResourceFork extended attribute");
153 T_ASSERT_EQ(0, strncmp(xattr, xattr_buff, xattr_len), "Verifying ResourceFork extended content");
154 T_ASSERT_POSIX_SUCCESS(close(fd_rfork), "Closing %s", file_rfork);
155
156 /* Apply sandbox profile */
157 T_ASSERT_POSIX_SUCCESS(sandbox_apply(profile), "Applying Sandbox profile");
158
159 /* Test ResourceFork extended attribute using fgetxattr(), fsetxattr() and fremovexattr() */
160 T_ASSERT_POSIX_FAILURE(fgetxattr(fd, XATTR_RESOURCEFORK_NAME, xattr_buff, sizeof(xattr_buff), 0, 0), EPERM, "Verifying that fgetxattr() fails to get ResourceFork with EPERM");
161 T_ASSERT_POSIX_FAILURE(fremovexattr(fd, XATTR_RESOURCEFORK_NAME, 0), EPERM, "Verifying that fremovexattr() fails to remove ResourceFork with EPERM");
162 T_ASSERT_POSIX_FAILURE(fsetxattr(fd2, XATTR_RESOURCEFORK_NAME, xattr, xattr_len, 0, 0), EPERM, "Verifying that fsetxattr() fails to set ResourceFork with EPERM");
163
164 /* Test ResourceFork extended attribute using the ..namedfork/rsrc path */
165 T_ASSERT_POSIX_FAILURE((fd_rfork = open(file_rfork, O_RDONLY, 0777)), EPERM, "Verifying that open() fails with EPERM");
166 T_ASSERT_POSIX_FAILURE((fd_rfork = open(file2_rfork, O_CREAT | O_RDONLY, 0777)), EPERM, "Verifying that open(O_CREAT) fails with EPERM");
167 T_ASSERT_POSIX_FAILURE(unlink(file_rfork), EPERM, "Verifying that unlink() fails with EPERM");
168
169 /* Close the open files */
170 T_ASSERT_POSIX_SUCCESS(close(fd), "Closing %s", file);
171 T_ASSERT_POSIX_SUCCESS(close(fd2), "Closing %s", file2);
172 T_ASSERT_POSIX_SUCCESS(close(fd3), "Closing %s", rsrc);
173 }
174