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 -lsandbox -ldarwintest -o statfs_ext statfs_ext.c -g -Weverything */
30
31 #include <stdlib.h>
32 #include <fcntl.h>
33 #include <System/sys/mount.h>
34 #include <sandbox/libsandbox.h>
35 #include <sys/stat.h>
36
37 #include <darwintest.h>
38 #include <darwintest/utils.h>
39
40 #define RUN_TEST TARGET_OS_OSX
41
42 #define FSTYPE_DEVFS "devfs"
43 static char template[MAXPATHLEN];
44 static char *testdir = NULL;
45 static sandbox_params_t params = NULL;
46 static sandbox_profile_t profile = NULL;
47
48 #define TEST_MODE_STATFS 0
49 #define TEST_MODE_FSTATFS 1
50
51 static const char *flag_name[] =
52 { "0", "STATFS_EXT_NOBLOCK" };
53
54 static const char *mode_name[] =
55 { "TEST_MODE_STATFS", "TEST_MODE_FSTATFS" };
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_CHECK_LEAKS(false));
62
63 static void
cleanup(void)64 cleanup(void)
65 {
66 if (profile) {
67 sandbox_free_profile(profile);
68 }
69 if (params) {
70 sandbox_free_params(params);
71 }
72 if (testdir) {
73 unmount(testdir, MNT_FORCE);
74 rmdir(testdir);
75 }
76 }
77
78 static void
statfs_compare(const char * path,struct statfs * sfs_ext,int mode,int flag,int expected_err)79 statfs_compare(const char *path, struct statfs *sfs_ext, int mode, int flag, int expected_err)
80 {
81 int fd;
82 struct statfs sfs = {};
83
84 T_LOG("Testing: path %s, sfs_ext %p, mode %s, flag 0x%x, expected_err %d", path, (void *) sfs_ext, mode_name[mode], (unsigned int)flag, expected_err);
85
86 if (sfs_ext) {
87 bzero(sfs_ext, sizeof(struct statfs));
88 }
89
90 switch (mode) {
91 case TEST_MODE_STATFS:
92 if (expected_err) {
93 T_ASSERT_POSIX_FAILURE(statfs_ext(path, sfs_ext, flag), expected_err, "Verifying that statfs_ext() fails with %d (%s)", expected_err, strerror(expected_err));
94 } else {
95 T_ASSERT_POSIX_SUCCESS(statfs_ext(path, sfs_ext, flag), "Calling statfs_ext() using the %s flag", flag_name[flag]);
96 T_ASSERT_POSIX_SUCCESS(statfs(path, &sfs), "Calling stafs()");
97 }
98 break;
99 case TEST_MODE_FSTATFS:
100 T_ASSERT_POSIX_SUCCESS(fd = open(path, O_DIRECTORY | O_RDONLY), "Opening %s", path);
101 if (expected_err) {
102 T_ASSERT_POSIX_FAILURE(fstatfs_ext(fd, sfs_ext, flag), expected_err, "Verifying that fstatfs_ext() fails with %d (%s)", expected_err, strerror(expected_err));
103 } else {
104 T_ASSERT_POSIX_SUCCESS(fstatfs(fd, &sfs), "Calling fstafs()");
105 T_ASSERT_POSIX_SUCCESS(fstatfs_ext(fd, sfs_ext, flag), "Calling fstatfs_ext() using the %s flag", flag_name[flag]);
106 }
107 T_ASSERT_POSIX_SUCCESS(close(fd), "Closing fd");
108 break;
109 default:
110 T_FAIL("Unknown test mode");
111 }
112
113 if (expected_err) {
114 return;
115 }
116
117 switch (flag) {
118 case 0:
119 T_ASSERT_EQ(memcmp(&sfs, sfs_ext, sizeof(struct statfs)), 0, "Validating statfs structure");
120 break;
121 case STATFS_EXT_NOBLOCK:
122 T_ASSERT_EQ(sfs.f_fsid.val[0], sfs_ext->f_fsid.val[0], "Validating f_fsid.val[0]");
123 T_ASSERT_EQ(sfs.f_fsid.val[1], sfs_ext->f_fsid.val[1], "Validating f_fsid.val[1]");
124 T_ASSERT_EQ(sfs.f_owner, sfs_ext->f_owner, "Validating f_owner");
125 T_ASSERT_EQ(sfs.f_type, sfs_ext->f_type, "Validating f_type");
126 T_ASSERT_EQ(sfs.f_flags, sfs_ext->f_flags, "Validating f_flags");
127 T_ASSERT_EQ(sfs.f_fssubtype, sfs_ext->f_fssubtype, "Validating f_fssubtype");
128 T_ASSERT_EQ_STR(sfs.f_fstypename, sfs_ext->f_fstypename, "Validating f_fstypename");
129 T_ASSERT_EQ_STR(sfs.f_mntonname, sfs_ext->f_mntonname, "Validating f_mntonname");
130 T_ASSERT_EQ_STR(sfs.f_mntfromname, sfs_ext->f_mntfromname, "Validating f_mntfromname");
131 T_ASSERT_EQ(sfs.f_flags_ext, sfs_ext->f_flags_ext, "Validating f_flags_ext");
132 break;
133 default:
134 T_FAIL("Unknown flag");
135 }
136 }
137
138 T_DECL(statfs_ext,
139 "test statfs_ext and fstatfs_ext",
140 T_META_ENABLED(RUN_TEST), T_META_ASROOT(false))
141 {
142 #if (!RUN_TEST)
143 T_SKIP("Not macOS");
144 #endif
145
146 struct statfs sfs_ext;
147
148 T_ATEND(cleanup);
149
150 T_SETUPBEGIN;
151
152 snprintf(template, sizeof(template), "%s/statfs_ext-XXXXXX", dt_tmpdir());
153 T_ASSERT_POSIX_NOTNULL((testdir = mkdtemp(template)), "Creating test root dir");
154 T_ASSERT_POSIX_SUCCESS(mount(FSTYPE_DEVFS, testdir, MNT_RDONLY, NULL), "Mounting temporary %s mount using path %s", FSTYPE_DEVFS, testdir);
155
156 T_SETUPEND;
157
158 /* Test fstatfs_ext() with invalid flags */
159 statfs_compare("/dev", &sfs_ext, TEST_MODE_STATFS, 0x10, EINVAL);
160 statfs_compare(testdir, &sfs_ext, TEST_MODE_FSTATFS, STATFS_EXT_NOBLOCK | 0x8, EINVAL);
161
162 /* Test invalid inputs */
163 statfs_compare(NULL, &sfs_ext, TEST_MODE_STATFS, STATFS_EXT_NOBLOCK, EFAULT);
164 statfs_compare("/", NULL, TEST_MODE_STATFS, STATFS_EXT_NOBLOCK, EFAULT);
165 statfs_compare("/dev", NULL, TEST_MODE_FSTATFS, STATFS_EXT_NOBLOCK, EFAULT);
166
167 /* Test fstatfs_ext() with zero flags */
168 statfs_compare("/", &sfs_ext, TEST_MODE_STATFS, 0, 0);
169 statfs_compare("/private/var/tmp", &sfs_ext, TEST_MODE_FSTATFS, 0, 0);
170
171 /* Test fstatfs_ext() with the STATFS_EXT_NOBLOCK flag */
172 statfs_compare("/", &sfs_ext, TEST_MODE_STATFS, STATFS_EXT_NOBLOCK, 0);
173 statfs_compare("/dev", &sfs_ext, TEST_MODE_STATFS, STATFS_EXT_NOBLOCK, 0);
174 statfs_compare("/private/var/tmp", &sfs_ext, TEST_MODE_FSTATFS, STATFS_EXT_NOBLOCK, 0);
175 statfs_compare(testdir, &sfs_ext, TEST_MODE_FSTATFS, STATFS_EXT_NOBLOCK, 0);
176 }
177
178 static void
create_profile_string(char * buff,size_t size)179 create_profile_string(char *buff, size_t size)
180 {
181 snprintf(buff, size, "(version 1) \n\
182 (allow default) \n\
183 (import \"system.sb\") \n\
184 (deny syscall-unix (syscall-number SYS_getattrlist) (syscall-number SYS_fgetattrlist)) \n");
185 }
186
187 T_DECL(statfs_ext_sandboxed,
188 "test statfs_ext and fstatfs_ext when the sandbox profile denies getattrlist/fgetattrlist",
189 T_META_ENABLED(false), T_META_ASROOT(true))
190 {
191 #if (!RUN_TEST)
192 T_SKIP("Not macOS");
193 #endif
194
195 struct statfs sfs_ext;
196 char *sberror = NULL;
197 char profile_string[1000];
198
199 #if (!RUN_TEST)
200 T_SKIP("Not macOS");
201 #endif
202
203 if (geteuid() != 0) {
204 T_SKIP("Test should run as root");
205 }
206
207 T_ATEND(cleanup);
208 T_SETUPBEGIN;
209
210 /* Create sandbox variables */
211 T_ASSERT_POSIX_NOTNULL(params = sandbox_create_params(), "Creating Sandbox params object");
212 create_profile_string(profile_string, sizeof(profile_string));
213 T_ASSERT_POSIX_NOTNULL(profile = sandbox_compile_string(profile_string, params, &sberror), "Creating Sandbox profile object");
214
215 T_SETUPEND;
216
217 /* Apply sandbox profile */
218 T_ASSERT_POSIX_SUCCESS(sandbox_apply(profile), "Applying Sandbox profile");
219
220 /* Test fstatfs_ext() with zero flags */
221 statfs_compare("/", &sfs_ext, TEST_MODE_STATFS, STATFS_EXT_NOBLOCK, 0);
222
223 /* Test fstatfs_ext() with the STATFS_EXT_NOBLOCK flag */
224 statfs_compare("/private/var/tmp", &sfs_ext, TEST_MODE_FSTATFS, STATFS_EXT_NOBLOCK, 0);
225 }
226