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 -o volfs_chroot volfs_chroot.c -g -Weverything */ 30 31 #include <darwintest.h> 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <fcntl.h> 35 #include <unistd.h> 36 #include <errno.h> 37 #include <sys/param.h> 38 #include <sys/stat.h> 39 #include <TargetConditionals.h> 40 41 T_GLOBAL_META( 42 T_META_NAMESPACE("xnu.vfs"), 43 T_META_RADAR_COMPONENT_NAME("xnu"), 44 T_META_RADAR_COMPONENT_VERSION("vfs"), 45 T_META_ENABLED(TARGET_OS_OSX), 46 T_META_ASROOT(true), 47 T_META_CHECK_LEAKS(false)); 48 49 T_DECL(volfs_chroot, 50 "Check for and fail if the volfs path is not under the chroot") 51 { 52 #if TARGET_OS_OSX 53 int fd; 54 char root_volfs[MAXPATHLEN]; 55 const char *root_path = "/", *private_path = "/private"; 56 struct stat root_stat, root_stat2, private_stat, fd_stat; 57 58 T_SETUPBEGIN; 59 60 T_ASSERT_POSIX_SUCCESS(stat(root_path, &root_stat), 61 "Setup: Calling stat() on %s", 62 root_path); 63 64 T_ASSERT_POSIX_SUCCESS(snprintf(root_volfs, sizeof(root_volfs), "/.vol/%d/2", root_stat.st_dev), 65 "Setup: Creating root_volfs path"); 66 67 T_ASSERT_POSIX_SUCCESS(stat(root_volfs, &root_stat2), 68 "Setup: Calling stat() on %s", 69 root_volfs); 70 71 T_ASSERT_POSIX_SUCCESS(stat(private_path, &private_stat), 72 "Setup: Calling stat() on %s", 73 private_path); 74 75 T_ASSERT_POSIX_SUCCESS(chroot(private_path), 76 "Setup: Calling chroot() on %s", 77 private_path); 78 79 T_SETUPEND; 80 81 T_ASSERT_EQ(root_stat.st_ino, root_stat2.st_ino, "Verifing %s and %s are the same file", root_path, root_volfs); 82 T_ASSERT_POSIX_SUCCESS((fd = open(root_path, 0)), "Opening the updated root path"); 83 T_ASSERT_POSIX_SUCCESS((fstat(fd, &fd_stat)), "Calling stat on the updated root path"); 84 T_ASSERT_EQ(fd_stat.st_ino, private_stat.st_ino, "Verifing %s was opened", private_path); 85 T_ASSERT_POSIX_FAILURE(open(root_volfs, 0), ENOENT, "Verifing %s can not be opened because path is not under the chroot", root_volfs); 86 #else 87 T_SKIP("Not macOS"); 88 #endif 89 } 90