1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions * Copyright (c) 2024 Apple Computer, Inc. All rights reserved.
3*043036a2SApple OSS Distributions *
4*043036a2SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions *
6*043036a2SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*043036a2SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*043036a2SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*043036a2SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*043036a2SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*043036a2SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*043036a2SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*043036a2SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*043036a2SApple OSS Distributions *
15*043036a2SApple OSS Distributions * Please obtain a copy of the License at
16*043036a2SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*043036a2SApple OSS Distributions *
18*043036a2SApple OSS Distributions * The Original Code and all software distributed under the License are
19*043036a2SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*043036a2SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*043036a2SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*043036a2SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*043036a2SApple OSS Distributions * Please see the License for the specific language governing rights and
24*043036a2SApple OSS Distributions * limitations under the License.
25*043036a2SApple OSS Distributions *
26*043036a2SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*043036a2SApple OSS Distributions */
28*043036a2SApple OSS Distributions
29*043036a2SApple OSS Distributions /* compile: xcrun -sdk macosx.internal clang -ldarwintest -o devfs_fdesc devfs_fdesc.c -g -Weverything */
30*043036a2SApple OSS Distributions /* sign: codesign --force --sign - --timestamp=none --entitlements devfs_fdesc.entitlements devfs_fdesc */
31*043036a2SApple OSS Distributions
32*043036a2SApple OSS Distributions #include <darwintest.h>
33*043036a2SApple OSS Distributions #include <darwintest/utils.h>
34*043036a2SApple OSS Distributions #include <stdlib.h>
35*043036a2SApple OSS Distributions #include <fcntl.h>
36*043036a2SApple OSS Distributions #include <sys/param.h>
37*043036a2SApple OSS Distributions #include <sys/stat.h>
38*043036a2SApple OSS Distributions #include <sys/mount.h>
39*043036a2SApple OSS Distributions #include <unistd.h>
40*043036a2SApple OSS Distributions
41*043036a2SApple OSS Distributions T_GLOBAL_META(
42*043036a2SApple OSS Distributions T_META_NAMESPACE("xnu.vfs"),
43*043036a2SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
44*043036a2SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("vfs"),
45*043036a2SApple OSS Distributions T_META_ASROOT(false),
46*043036a2SApple OSS Distributions T_META_CHECK_LEAKS(false));
47*043036a2SApple OSS Distributions
48*043036a2SApple OSS Distributions static int
docheck(int fd,int perm)49*043036a2SApple OSS Distributions docheck(int fd, int perm)
50*043036a2SApple OSS Distributions {
51*043036a2SApple OSS Distributions char path[MAXPATHLEN];
52*043036a2SApple OSS Distributions
53*043036a2SApple OSS Distributions path[0] = '\0';
54*043036a2SApple OSS Distributions snprintf(path, sizeof(path), "/dev/fd/%d", fd);
55*043036a2SApple OSS Distributions errno = 0;
56*043036a2SApple OSS Distributions
57*043036a2SApple OSS Distributions return access(path, perm);
58*043036a2SApple OSS Distributions }
59*043036a2SApple OSS Distributions
60*043036a2SApple OSS Distributions /* The devfs_access test should not run as root */
61*043036a2SApple OSS Distributions T_DECL(devfs_fdesc_access, "Calculate the allowed access based on the open-flags for fdesc vnodes")
62*043036a2SApple OSS Distributions {
63*043036a2SApple OSS Distributions const char *path = "/dev/null";
64*043036a2SApple OSS Distributions int fd_rdonly, fd_wronly, fd_evtonly, fd_evtonly_drw;
65*043036a2SApple OSS Distributions
66*043036a2SApple OSS Distributions if (geteuid() == 0) {
67*043036a2SApple OSS Distributions T_SKIP("Test should NOT run as root");
68*043036a2SApple OSS Distributions }
69*043036a2SApple OSS Distributions
70*043036a2SApple OSS Distributions T_SETUPBEGIN;
71*043036a2SApple OSS Distributions
72*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fd_rdonly = open(path, O_RDONLY),
73*043036a2SApple OSS Distributions "Setup: Opening file with O_RDONLY permissions, fd_rdonly = %d",
74*043036a2SApple OSS Distributions fd_rdonly);
75*043036a2SApple OSS Distributions
76*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fd_wronly = open(path, O_WRONLY),
77*043036a2SApple OSS Distributions "Setup: Opening file with O_WRONLY permissions, fd_wronly = %d",
78*043036a2SApple OSS Distributions fd_wronly);
79*043036a2SApple OSS Distributions
80*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fd_evtonly = open(path, O_EVTONLY),
81*043036a2SApple OSS Distributions "Setup: Opening file with O_EVTONLY permissions, fd_evtonly = %d",
82*043036a2SApple OSS Distributions fd_evtonly);
83*043036a2SApple OSS Distributions
84*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setiopolicy_np(IOPOL_TYPE_VFS_DISALLOW_RW_FOR_O_EVTONLY,
85*043036a2SApple OSS Distributions IOPOL_SCOPE_PROCESS,
86*043036a2SApple OSS Distributions IOPOL_VFS_DISALLOW_RW_FOR_O_EVTONLY_ON),
87*043036a2SApple OSS Distributions "Setup: Disallowing RW for O_EVTONLY");
88*043036a2SApple OSS Distributions
89*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fd_evtonly_drw = open(path, O_EVTONLY),
90*043036a2SApple OSS Distributions "Setup: Opening file with O_EVTONLY permissions while RW is disabled, fd_evtonly_drw = %d",
91*043036a2SApple OSS Distributions fd_evtonly_drw);
92*043036a2SApple OSS Distributions
93*043036a2SApple OSS Distributions T_SETUPEND;
94*043036a2SApple OSS Distributions
95*043036a2SApple OSS Distributions T_LOG("Test rdonly-fd's access");
96*043036a2SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(docheck(fd_rdonly, R_OK), "Testing R_OK permissions");
97*043036a2SApple OSS Distributions T_EXPECT_POSIX_FAILURE(docheck(fd_rdonly, W_OK), EACCES, "Testing W_OK permissions");
98*043036a2SApple OSS Distributions T_EXPECT_POSIX_FAILURE(docheck(fd_rdonly, R_OK | W_OK), EACCES, "Testing R_OK | W_OK permissions");
99*043036a2SApple OSS Distributions T_EXPECT_POSIX_FAILURE(docheck(fd_rdonly, X_OK), EACCES, "Testing X_OK permissions");
100*043036a2SApple OSS Distributions
101*043036a2SApple OSS Distributions T_LOG("Test wronly-fd's access");
102*043036a2SApple OSS Distributions T_EXPECT_POSIX_FAILURE(docheck(fd_wronly, R_OK), EACCES, "Testing R_OK permissions");
103*043036a2SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(docheck(fd_wronly, W_OK), "Testing W_OK permissions");
104*043036a2SApple OSS Distributions T_EXPECT_POSIX_FAILURE(docheck(fd_wronly, R_OK | W_OK), EACCES, "Testing R_OK | W_OK permissions");
105*043036a2SApple OSS Distributions T_EXPECT_POSIX_FAILURE(docheck(fd_wronly, X_OK), EACCES, "Testing X_OK permissions");
106*043036a2SApple OSS Distributions
107*043036a2SApple OSS Distributions T_LOG("Test evtonly-fd's access");
108*043036a2SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(docheck(fd_evtonly, R_OK), "Testing R_OK permissions");
109*043036a2SApple OSS Distributions T_EXPECT_POSIX_FAILURE(docheck(fd_evtonly, W_OK), EACCES, "Testing W_OK permissions");
110*043036a2SApple OSS Distributions T_EXPECT_POSIX_FAILURE(docheck(fd_evtonly, R_OK | W_OK), EACCES, "Testing R_OK | W_OK permissions");
111*043036a2SApple OSS Distributions T_EXPECT_POSIX_FAILURE(docheck(fd_evtonly, X_OK), EACCES, "Testing X_OK permissions");
112*043036a2SApple OSS Distributions
113*043036a2SApple OSS Distributions T_LOG("Test evtonly-drw-fd's access");
114*043036a2SApple OSS Distributions T_EXPECT_POSIX_FAILURE(docheck(fd_evtonly_drw, R_OK), EACCES, "Testing R_OK permissions");
115*043036a2SApple OSS Distributions T_EXPECT_POSIX_FAILURE(docheck(fd_evtonly_drw, W_OK), EACCES, "Testing W_OK permissions");
116*043036a2SApple OSS Distributions T_EXPECT_POSIX_FAILURE(docheck(fd_evtonly_drw, R_OK | W_OK), EACCES, "Testing R_OK | W_OK permissions");
117*043036a2SApple OSS Distributions T_EXPECT_POSIX_FAILURE(docheck(fd_evtonly_drw, X_OK), EACCES, "Testing X_OK permissions");
118*043036a2SApple OSS Distributions
119*043036a2SApple OSS Distributions /* Close open file descriptors */
120*043036a2SApple OSS Distributions close(fd_rdonly);
121*043036a2SApple OSS Distributions close(fd_wronly);
122*043036a2SApple OSS Distributions close(fd_evtonly);
123*043036a2SApple OSS Distributions close(fd_evtonly_drw);
124*043036a2SApple OSS Distributions }
125*043036a2SApple OSS Distributions
126*043036a2SApple OSS Distributions T_DECL(devfs_fdesc_mount_block, "Test that mounting over /dev/fd/<fd> is blocked")
127*043036a2SApple OSS Distributions {
128*043036a2SApple OSS Distributions int dir_fd;
129*043036a2SApple OSS Distributions char fdesc_path[MAXPATHLEN];
130*043036a2SApple OSS Distributions char temp_dir[MAXPATHLEN];
131*043036a2SApple OSS Distributions int ret;
132*043036a2SApple OSS Distributions
133*043036a2SApple OSS Distributions T_SETUPBEGIN;
134*043036a2SApple OSS Distributions
135*043036a2SApple OSS Distributions /* Create a temporary directory */
136*043036a2SApple OSS Distributions snprintf(temp_dir, sizeof(temp_dir), "%s/devfs_fdesc_mount_test.XXXXXX", dt_tmpdir());
137*043036a2SApple OSS Distributions T_ASSERT_NOTNULL(mkdtemp(temp_dir), "Create temporary directory");
138*043036a2SApple OSS Distributions
139*043036a2SApple OSS Distributions /* Open the temporary directory */
140*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(dir_fd = open(temp_dir, O_DIRECTORY),
141*043036a2SApple OSS Distributions "Setup: Opening temporary directory with O_DIRECTORY, dir_fd = %d",
142*043036a2SApple OSS Distributions dir_fd);
143*043036a2SApple OSS Distributions
144*043036a2SApple OSS Distributions /* Construct /dev/fd/<fd> path */
145*043036a2SApple OSS Distributions snprintf(fdesc_path, sizeof(fdesc_path), "/dev/fd/%d", dir_fd);
146*043036a2SApple OSS Distributions
147*043036a2SApple OSS Distributions T_SETUPEND;
148*043036a2SApple OSS Distributions
149*043036a2SApple OSS Distributions T_LOG("Testing mount blocking on /dev/fd/%d path: %s", dir_fd, fdesc_path);
150*043036a2SApple OSS Distributions
151*043036a2SApple OSS Distributions /* Test: Attempt to mount tmpfs over /dev/fd/<fd> - should fail with ENOTSUP */
152*043036a2SApple OSS Distributions ret = mount("tmpfs", fdesc_path, MNT_RDONLY, NULL);
153*043036a2SApple OSS Distributions T_EXPECT_POSIX_FAILURE(ret, ENOTSUP,
154*043036a2SApple OSS Distributions "Mounting tmpfs over %s should fail with ENOTSUP", fdesc_path);
155*043036a2SApple OSS Distributions
156*043036a2SApple OSS Distributions /* Test: Attempt to mount devfs over /dev/fd/<fd> - should also fail with ENOTSUP */
157*043036a2SApple OSS Distributions ret = mount("devfs", fdesc_path, MNT_RDONLY, NULL);
158*043036a2SApple OSS Distributions T_EXPECT_POSIX_FAILURE(ret, ENOTSUP,
159*043036a2SApple OSS Distributions "Mounting devfs over %s should fail with ENOTSUP", fdesc_path);
160*043036a2SApple OSS Distributions
161*043036a2SApple OSS Distributions /* Cleanup */
162*043036a2SApple OSS Distributions close(dir_fd);
163*043036a2SApple OSS Distributions rmdir(temp_dir);
164*043036a2SApple OSS Distributions }
165*043036a2SApple OSS Distributions
166*043036a2SApple OSS Distributions T_DECL(devfs_fdesc_unmount_block, "Test that unmounting /dev/fd/<fd> is blocked")
167*043036a2SApple OSS Distributions {
168*043036a2SApple OSS Distributions int dir_fd;
169*043036a2SApple OSS Distributions char fdesc_path[MAXPATHLEN];
170*043036a2SApple OSS Distributions char temp_dir[MAXPATHLEN];
171*043036a2SApple OSS Distributions int ret;
172*043036a2SApple OSS Distributions
173*043036a2SApple OSS Distributions T_SETUPBEGIN;
174*043036a2SApple OSS Distributions
175*043036a2SApple OSS Distributions /* Create a temporary directory */
176*043036a2SApple OSS Distributions snprintf(temp_dir, sizeof(temp_dir), "%s/devfs_fdesc_unmount_test.XXXXXX", dt_tmpdir());
177*043036a2SApple OSS Distributions T_ASSERT_NOTNULL(mkdtemp(temp_dir), "Create temporary directory");
178*043036a2SApple OSS Distributions
179*043036a2SApple OSS Distributions /* Open the temporary directory */
180*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(dir_fd = open(temp_dir, O_DIRECTORY),
181*043036a2SApple OSS Distributions "Setup: Opening temporary directory with O_DIRECTORY, dir_fd = %d",
182*043036a2SApple OSS Distributions dir_fd);
183*043036a2SApple OSS Distributions
184*043036a2SApple OSS Distributions /* Construct /dev/fd/<fd> path */
185*043036a2SApple OSS Distributions snprintf(fdesc_path, sizeof(fdesc_path), "/dev/fd/%d", dir_fd);
186*043036a2SApple OSS Distributions
187*043036a2SApple OSS Distributions T_SETUPEND;
188*043036a2SApple OSS Distributions
189*043036a2SApple OSS Distributions T_LOG("Testing unmount blocking on /dev/fd/%d path: %s", dir_fd, fdesc_path);
190*043036a2SApple OSS Distributions
191*043036a2SApple OSS Distributions /* Test: Attempt to unmount /dev/fd/<fd> - should fail with ENOTSUP */
192*043036a2SApple OSS Distributions ret = unmount(fdesc_path, 0);
193*043036a2SApple OSS Distributions T_EXPECT_POSIX_FAILURE(ret, ENOTSUP,
194*043036a2SApple OSS Distributions "Unmounting %s should fail with ENOTSUP", fdesc_path);
195*043036a2SApple OSS Distributions
196*043036a2SApple OSS Distributions /* Test: Attempt to force unmount /dev/fd/<fd> - should also fail with ENOTSUP */
197*043036a2SApple OSS Distributions ret = unmount(fdesc_path, MNT_FORCE);
198*043036a2SApple OSS Distributions T_EXPECT_POSIX_FAILURE(ret, ENOTSUP,
199*043036a2SApple OSS Distributions "Force unmounting %s should fail with ENOTSUP", fdesc_path);
200*043036a2SApple OSS Distributions
201*043036a2SApple OSS Distributions /* Cleanup */
202*043036a2SApple OSS Distributions close(dir_fd);
203*043036a2SApple OSS Distributions rmdir(temp_dir);
204*043036a2SApple OSS Distributions }
205