xref: /xnu-8020.140.41/libsyscall/wrappers/_libkernel_init.c (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1 /*
2  * Copyright (c) 2010 Apple 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 #include <TargetConditionals.h>
30 #include <stdbool.h>
31 #include <strings.h>
32 #include <unistd.h>
33 #include <mach/vm_page_size.h>
34 #include "_libkernel_init.h"
35 #include "system-version-compat-support.h"
36 
37 extern int mach_init(void);
38 
39 #if SYSTEM_VERSION_COMPAT_ENABLED
40 
41 #include <sys/sysctl.h>
42 
43 extern bool _system_version_compat_check_path_suffix(const char *orig_path);
44 extern int _system_version_compat_open_shim(int opened_fd, int openat_fd, const char *orig_path, int oflag, mode_t mode,
45     int (*close_syscall)(int), int (*open_syscall)(const char *, int, mode_t),
46     int (*openat_syscall)(int, const char *, int, mode_t),
47     int (*fcntl_syscall)(int, int, long));
48 
49 extern bool (*system_version_compat_check_path_suffix)(const char *orig_path);
50 extern int (*system_version_compat_open_shim)(int opened_fd, int openat_fd, const char *orig_path, int oflag, mode_t mode,
51     int (*close_syscall)(int), int (*open_syscall)(const char *, int, mode_t),
52     int (*openat_syscall)(int, const char *, int, mode_t),
53     int (*fcntl_syscall)(int, int, long));
54 
55 extern system_version_compat_mode_t system_version_compat_mode;
56 
57 struct _posix_spawn_args_desc;
58 extern bool (*posix_spawn_with_filter)(pid_t *pid, const char *fname, char * const *argp,
59     char * const *envp, struct _posix_spawn_args_desc *adp, int *ret);
60 extern bool _posix_spawn_with_filter(pid_t *pid, const char *fname, char * const *argp,
61     char * const *envp, struct _posix_spawn_args_desc *adp, int *ret);
62 extern int (*execve_with_filter)(char *fname, char **argp, char **envp);
63 extern int _execve_with_filter(char *fname, char **argp, char **envp);
64 
65 int  __sysctlbyname(const char *name, size_t namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
66 #endif /* SYSTEM_VERSION_COMPAT_ENABLED */
67 
68 #if TARGET_OS_OSX
69 __attribute__((visibility("default")))
70 extern bool _os_xbs_chrooted;
71 bool _os_xbs_chrooted;
72 #endif /* TARGET_OS_OSX */
73 
74 /* dlsym() funcptr is for legacy support in exc_catcher */
75 void* (*_dlsym)(void*, const char*) __attribute__((visibility("hidden")));
76 
77 __attribute__((visibility("hidden")))
78 _libkernel_functions_t _libkernel_functions;
79 
80 
81 void
__libkernel_init(_libkernel_functions_t fns,const char * envp[],const char * apple[],const struct ProgramVars * vars)82 __libkernel_init(_libkernel_functions_t fns,
83     const char *envp[] __attribute__((unused)),
84     const char *apple[] __attribute__((unused)),
85     const struct ProgramVars *vars __attribute__((unused)))
86 {
87 	_libkernel_functions = fns;
88 	if (fns->dlsym) {
89 		_dlsym = fns->dlsym;
90 	}
91 	mach_init();
92 #if TARGET_OS_OSX
93 	for (size_t i = 0; envp[i]; i++) {
94 
95 #if defined(__i386__) || defined(__x86_64__)
96 		const char *VM_KERNEL_PAGE_SHIFT_ENV = "VM_KERNEL_PAGE_SIZE_4K=1";
97 		if (vm_kernel_page_shift != 12 && strcmp(VM_KERNEL_PAGE_SHIFT_ENV, envp[i]) == 0) {
98 			vm_kernel_page_shift = 12;
99 			vm_kernel_page_size = 1 << vm_kernel_page_shift;
100 			vm_kernel_page_mask = vm_kernel_page_size - 1;
101 		}
102 #endif /* defined(__i386__) || defined(__x86_64__) */
103 	}
104 #endif /* TARGET_OS_OSX */
105 }
106 
107 void
__libkernel_init_late(_libkernel_late_init_config_t config)108 __libkernel_init_late(_libkernel_late_init_config_t config)
109 {
110 	if (config->version >= 1) {
111 #if TARGET_OS_OSX && !defined(__i386__)
112 		if (config->enable_system_version_compat) {
113 			/* enable the version compatibility shim for this process (macOS only) */
114 
115 			/* first hook up the shims we reference from open{at}() */
116 			system_version_compat_check_path_suffix = _system_version_compat_check_path_suffix;
117 			system_version_compat_open_shim = _system_version_compat_open_shim;
118 
119 			system_version_compat_mode = SYSTEM_VERSION_COMPAT_MODE_MACOSX;
120 
121 			/*
122 			 * tell the kernel the shim is enabled for this process so it can shim any
123 			 * necessary sysctls
124 			 */
125 			int enable = 1;
126 			__sysctlbyname("kern.system_version_compat", strlen("kern.system_version_compat"),
127 			    NULL, NULL, &enable, sizeof(enable));
128 		} else if ((config->version >= 2) && config->enable_ios_version_compat) {
129 			/* enable the iOS ProductVersion compatibility shim for this process */
130 
131 			/* first hook up the shims we reference from open{at}() */
132 			system_version_compat_check_path_suffix = _system_version_compat_check_path_suffix;
133 			system_version_compat_open_shim = _system_version_compat_open_shim;
134 
135 			system_version_compat_mode = SYSTEM_VERSION_COMPAT_MODE_IOS;
136 
137 			/*
138 			 * We don't currently shim any sysctls for iOS apps running on macOS so we
139 			 * don't need to inform the kernel that this app has the SystemVersion shim enabled.
140 			 */
141 		}
142 
143 		if ((config->version >= 3) && config->enable_posix_spawn_filtering) {
144 			posix_spawn_with_filter = _posix_spawn_with_filter;
145 			execve_with_filter = _execve_with_filter;
146 		}
147 #endif /* TARGET_OS_OSX && !defined(__i386__) */
148 	}
149 }
150 
151 void
__libkernel_init_after_boot_tasks(_libkernel_init_after_boot_tasks_config_t config)152 __libkernel_init_after_boot_tasks(
153 	_libkernel_init_after_boot_tasks_config_t config)
154 {
155 	if (config->version >= 1) {
156 #if TARGET_OS_OSX && !defined(__i386__)
157 		if (config->enable_posix_spawn_filtering) {
158 			posix_spawn_with_filter = _posix_spawn_with_filter;
159 			execve_with_filter = _execve_with_filter;
160 		}
161 #endif
162 	}
163 }
164