Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | /* * Copyright (c) 2024 Apple Computer, Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* compile: xcrun -sdk macosx.internal clang -arch arm64e -arch x86_64 -ldarwintest -o chroot_path chroot_path.c */ #include <darwintest.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> #include <sys/param.h> #include <sys/stat.h> #include <sys/fsgetpath.h> #include <TargetConditionals.h> T_GLOBAL_META( T_META_NAMESPACE("xnu.vfs"), T_META_RADAR_COMPONENT_NAME("xnu"), T_META_RADAR_COMPONENT_VERSION("vfs"), T_META_ENABLED(TARGET_OS_OSX), T_META_ASROOT(true), T_META_CHECK_LEAKS(false)); T_DECL(chroot_path_volfs, "Check for and fail if the volfs path is not under the chroot") { #if TARGET_OS_OSX int fd; char root_volfs[MAXPATHLEN]; const char *root_path = "/", *private_path = "/private"; struct stat root_stat, root_stat2, private_stat, fd_stat; T_SETUPBEGIN; T_ASSERT_POSIX_SUCCESS(stat(root_path, &root_stat), "Setup: Calling stat() on %s", root_path); T_ASSERT_POSIX_SUCCESS(snprintf(root_volfs, sizeof(root_volfs), "/.vol/%d/2", root_stat.st_dev), "Setup: Creating root_volfs path"); T_ASSERT_POSIX_SUCCESS(stat(root_volfs, &root_stat2), "Setup: Calling stat() on %s", root_volfs); T_ASSERT_POSIX_SUCCESS(stat(private_path, &private_stat), "Setup: Calling stat() on %s", private_path); T_ASSERT_POSIX_SUCCESS(chroot(private_path), "Setup: Calling chroot() on %s", private_path); T_SETUPEND; T_ASSERT_EQ(root_stat.st_ino, root_stat2.st_ino, "Verifing %s and %s are the same file", root_path, root_volfs); T_ASSERT_POSIX_SUCCESS((fd = open(root_path, 0)), "Opening the updated root path"); T_ASSERT_POSIX_SUCCESS((fstat(fd, &fd_stat)), "Calling stat on the updated root path"); T_ASSERT_EQ(fd_stat.st_ino, private_stat.st_ino, "Verifing %s was opened", private_path); T_ASSERT_POSIX_FAILURE(open(root_volfs, 0), ENOENT, "Verifing %s can not be opened because path is not under the chroot", root_volfs); #else T_SKIP("Not macOS"); #endif } T_DECL(chroot_path_fsgetpath, "Check that fsgetpath fails if the path is outside chroot") { #if TARGET_OS_OSX char fspath[MAXPATHLEN]; const char *root_path = "/", *private_path = "/private"; struct stat root_stat, private_stat; fsid_t fsid; T_SETUPBEGIN; T_ASSERT_POSIX_SUCCESS(stat(root_path, &root_stat), "Setup: Calling stat() on %s", root_path); T_ASSERT_POSIX_SUCCESS(stat(private_path, &private_stat), "Setup: Calling stat() on %s", private_path); T_SETUPEND; /* Test fsgetpath before chroot - should succeed */ fsid.val[0] = root_stat.st_dev; fsid.val[1] = 0; T_ASSERT_POSIX_SUCCESS(fsgetpath(fspath, sizeof(fspath), &fsid, root_stat.st_ino), "fsgetpath should succeed before chroot"); T_LOG("fsgetpath returned: %s", fspath); /* Enter chroot */ T_ASSERT_POSIX_SUCCESS(chroot(private_path), "Calling chroot() on %s", private_path); /* Test fsgetpath after chroot for file outside chroot - should fail */ T_ASSERT_POSIX_FAILURE(fsgetpath(fspath, sizeof(fspath), &fsid, root_stat.st_ino), ENOENT, "fsgetpath should fail with ENOENT for path outside chroot"); /* Test fsgetpath for file inside chroot - should succeed */ fsid.val[0] = private_stat.st_dev; T_ASSERT_POSIX_SUCCESS(fsgetpath(fspath, sizeof(fspath), &fsid, private_stat.st_ino), "fsgetpath should succeed for path inside chroot"); T_LOG("fsgetpath for chroot dir returned: %s", fspath); #else T_SKIP("Not macOS"); #endif } T_DECL(chroot_path_fgetpath, "Check that F_GETPATH fails if the path is outside chroot") { #if TARGET_OS_OSX char fspath[MAXPATHLEN]; const char *root_path = "/", *private_path = "/private"; struct stat private_stat; int root_fd, private_fd; T_SETUPBEGIN; T_ASSERT_POSIX_SUCCESS(stat(private_path, &private_stat), "Setup: Calling stat() on %s", private_path); /* Open root directory before chroot */ T_ASSERT_POSIX_SUCCESS((root_fd = open(root_path, O_RDONLY)), "Setup: Opening %s", root_path); /* Open private directory before chroot */ T_ASSERT_POSIX_SUCCESS((private_fd = open(private_path, O_RDONLY)), "Setup: Opening %s", private_path); T_SETUPEND; /* Test F_GETPATH before chroot - should succeed */ T_ASSERT_POSIX_SUCCESS(fcntl(root_fd, F_GETPATH, fspath), "F_GETPATH should succeed before chroot"); T_LOG("F_GETPATH returned: %s", fspath); /* Enter chroot */ T_ASSERT_POSIX_SUCCESS(chroot(private_path), "Calling chroot() on %s", private_path); /* Test F_GETPATH after chroot for fd outside chroot - should fail */ T_ASSERT_POSIX_FAILURE(fcntl(root_fd, F_GETPATH, fspath), ENOENT, "F_GETPATH should fail with ENOENT for fd outside chroot"); /* Test F_GETPATH for fd inside chroot - should succeed */ T_ASSERT_POSIX_SUCCESS(fcntl(private_fd, F_GETPATH, fspath), "F_GETPATH should succeed for fd inside chroot"); T_LOG("F_GETPATH for chroot dir returned: %s", fspath); /* Clean up */ close(root_fd); close(private_fd); #else T_SKIP("Not macOS"); #endif } T_DECL(chroot_path_openat_fgetpath, "Check that F_GETPATH works with openat() within chroot") { #if TARGET_OS_OSX char fspath[MAXPATHLEN]; const char *chroot_path = "/"; const char *at_path = "."; const char *relative_path = "private"; int at_fd, target_fd; T_SETUPBEGIN; /* Verify the target directory exists before chroot */ char full_target_path[MAXPATHLEN]; T_ASSERT_POSIX_SUCCESS(snprintf(full_target_path, sizeof(full_target_path), "%s%s", chroot_path, relative_path), "Setup: Creating full target path"); struct stat target_stat; T_ASSERT_POSIX_SUCCESS(stat(full_target_path, &target_stat), "Setup: Verifying %s exists", full_target_path); T_SETUPEND; /* Enter chroot */ T_ASSERT_POSIX_SUCCESS(chroot(chroot_path), "Calling chroot() on %s", chroot_path); /* Open the base directory within chroot */ T_ASSERT_POSIX_SUCCESS((at_fd = open(chroot_path, O_RDONLY | O_DIRECTORY)), "Opening base directory %s within chroot", at_path); /* Open target directory using openat() */ T_ASSERT_POSIX_SUCCESS((target_fd = openat(at_fd, relative_path, O_RDONLY | O_DIRECTORY)), "Opening %s relative to %s using openat()", relative_path, at_path); /* Test F_GETPATH on the openat() file descriptor - should succeed */ T_ASSERT_POSIX_SUCCESS(fcntl(target_fd, F_GETPATH, fspath), "F_GETPATH should succeed for openat() fd within chroot"); T_LOG("F_GETPATH for openat(%s, %s) returned: %s", at_path, relative_path, fspath); /* Verify the returned path is within the chroot */ T_ASSERT_TRUE(fspath[0] == '/', "Returned path should be absolute"); T_ASSERT_TRUE(strstr(fspath, relative_path) != NULL, "Returned path should contain the relative path component"); /* Clean up */ close(target_fd); close(at_fd); #else T_SKIP("Not macOS"); #endif } |