xref: /xnu-10002.81.5/libsyscall/wrappers/_libkernel_init.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 2010 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  *
4*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions  *
6*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions  *
15*5e3eaea3SApple OSS Distributions  * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions  *
18*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions  * limitations under the License.
25*5e3eaea3SApple OSS Distributions  *
26*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions  */
28*5e3eaea3SApple OSS Distributions 
29*5e3eaea3SApple OSS Distributions #include <TargetConditionals.h>
30*5e3eaea3SApple OSS Distributions #include <stdbool.h>
31*5e3eaea3SApple OSS Distributions #include <strings.h>
32*5e3eaea3SApple OSS Distributions #include <unistd.h>
33*5e3eaea3SApple OSS Distributions #include <mach/vm_page_size.h>
34*5e3eaea3SApple OSS Distributions #include <spawn_filtering_private.h>
35*5e3eaea3SApple OSS Distributions #include "_libkernel_init.h"
36*5e3eaea3SApple OSS Distributions #include "system-version-compat-support.h"
37*5e3eaea3SApple OSS Distributions 
38*5e3eaea3SApple OSS Distributions extern int mach_init(void);
39*5e3eaea3SApple OSS Distributions 
40*5e3eaea3SApple OSS Distributions #if SYSTEM_VERSION_COMPAT_ENABLED
41*5e3eaea3SApple OSS Distributions 
42*5e3eaea3SApple OSS Distributions #include <sys/sysctl.h>
43*5e3eaea3SApple OSS Distributions 
44*5e3eaea3SApple OSS Distributions extern bool _system_version_compat_check_path_suffix(const char *orig_path);
45*5e3eaea3SApple OSS Distributions extern int _system_version_compat_open_shim(int opened_fd, int openat_fd, const char *orig_path, int oflag, mode_t mode,
46*5e3eaea3SApple OSS Distributions     int (*close_syscall)(int), int (*open_syscall)(const char *, int, mode_t),
47*5e3eaea3SApple OSS Distributions     int (*openat_syscall)(int, const char *, int, mode_t),
48*5e3eaea3SApple OSS Distributions     int (*fcntl_syscall)(int, int, long));
49*5e3eaea3SApple OSS Distributions 
50*5e3eaea3SApple OSS Distributions extern bool (*system_version_compat_check_path_suffix)(const char *orig_path);
51*5e3eaea3SApple OSS Distributions extern int (*system_version_compat_open_shim)(int opened_fd, int openat_fd, const char *orig_path, int oflag, mode_t mode,
52*5e3eaea3SApple OSS Distributions     int (*close_syscall)(int), int (*open_syscall)(const char *, int, mode_t),
53*5e3eaea3SApple OSS Distributions     int (*openat_syscall)(int, const char *, int, mode_t),
54*5e3eaea3SApple OSS Distributions     int (*fcntl_syscall)(int, int, long));
55*5e3eaea3SApple OSS Distributions 
56*5e3eaea3SApple OSS Distributions extern system_version_compat_mode_t system_version_compat_mode;
57*5e3eaea3SApple OSS Distributions 
58*5e3eaea3SApple OSS Distributions int  __sysctlbyname(const char *name, size_t namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
59*5e3eaea3SApple OSS Distributions #endif /* SYSTEM_VERSION_COMPAT_ENABLED */
60*5e3eaea3SApple OSS Distributions 
61*5e3eaea3SApple OSS Distributions #if POSIX_SPAWN_FILTERING_ENABLED
62*5e3eaea3SApple OSS Distributions struct _posix_spawn_args_desc;
63*5e3eaea3SApple OSS Distributions extern bool (*posix_spawn_with_filter)(pid_t *pid, const char *fname, char * const *argp,
64*5e3eaea3SApple OSS Distributions     char * const *envp, struct _posix_spawn_args_desc *adp, int *ret);
65*5e3eaea3SApple OSS Distributions extern bool _posix_spawn_with_filter(pid_t *pid, const char *fname, char * const *argp,
66*5e3eaea3SApple OSS Distributions     char * const *envp, struct _posix_spawn_args_desc *adp, int *ret);
67*5e3eaea3SApple OSS Distributions extern int (*execve_with_filter)(char *fname, char **argp, char **envp);
68*5e3eaea3SApple OSS Distributions extern int _execve_with_filter(char *fname, char **argp, char **envp);
69*5e3eaea3SApple OSS Distributions #endif /* POSIX_SPAWN_FILTERING_ENABLED */
70*5e3eaea3SApple OSS Distributions 
71*5e3eaea3SApple OSS Distributions #if TARGET_OS_OSX
72*5e3eaea3SApple OSS Distributions __attribute__((visibility("default")))
73*5e3eaea3SApple OSS Distributions extern bool _os_xbs_chrooted;
74*5e3eaea3SApple OSS Distributions bool _os_xbs_chrooted;
75*5e3eaea3SApple OSS Distributions #endif /* TARGET_OS_OSX */
76*5e3eaea3SApple OSS Distributions 
77*5e3eaea3SApple OSS Distributions /* dlsym() funcptr is for legacy support in exc_catcher */
78*5e3eaea3SApple OSS Distributions void* (*LIBKERNEL_FUNCTION_PTRAUTH(_dlsym))(void*, const char*) __attribute__((visibility("hidden")));
79*5e3eaea3SApple OSS Distributions 
80*5e3eaea3SApple OSS Distributions __attribute__((visibility("hidden")))
81*5e3eaea3SApple OSS Distributions _libkernel_functions_t _libkernel_functions;
82*5e3eaea3SApple OSS Distributions 
83*5e3eaea3SApple OSS Distributions 
84*5e3eaea3SApple OSS Distributions void
__libkernel_init(_libkernel_functions_t fns,const char * envp[],const char * apple[],const struct ProgramVars * vars)85*5e3eaea3SApple OSS Distributions __libkernel_init(_libkernel_functions_t fns,
86*5e3eaea3SApple OSS Distributions     const char *envp[] __attribute__((unused)),
87*5e3eaea3SApple OSS Distributions     const char *apple[] __attribute__((unused)),
88*5e3eaea3SApple OSS Distributions     const struct ProgramVars *vars __attribute__((unused)))
89*5e3eaea3SApple OSS Distributions {
90*5e3eaea3SApple OSS Distributions 	_libkernel_functions = fns;
91*5e3eaea3SApple OSS Distributions 	if (fns->dlsym) {
92*5e3eaea3SApple OSS Distributions 		_dlsym = fns->dlsym;
93*5e3eaea3SApple OSS Distributions 	}
94*5e3eaea3SApple OSS Distributions 	mach_init();
95*5e3eaea3SApple OSS Distributions #if TARGET_OS_OSX
96*5e3eaea3SApple OSS Distributions 	for (size_t i = 0; envp[i]; i++) {
97*5e3eaea3SApple OSS Distributions 
98*5e3eaea3SApple OSS Distributions #if defined(__i386__) || defined(__x86_64__)
99*5e3eaea3SApple OSS Distributions 		const char *VM_KERNEL_PAGE_SHIFT_ENV = "VM_KERNEL_PAGE_SIZE_4K=1";
100*5e3eaea3SApple OSS Distributions 		if (vm_kernel_page_shift != 12 && strcmp(VM_KERNEL_PAGE_SHIFT_ENV, envp[i]) == 0) {
101*5e3eaea3SApple OSS Distributions 			vm_kernel_page_shift = 12;
102*5e3eaea3SApple OSS Distributions 			vm_kernel_page_size = 1 << vm_kernel_page_shift;
103*5e3eaea3SApple OSS Distributions 			vm_kernel_page_mask = vm_kernel_page_size - 1;
104*5e3eaea3SApple OSS Distributions 		}
105*5e3eaea3SApple OSS Distributions #endif /* defined(__i386__) || defined(__x86_64__) */
106*5e3eaea3SApple OSS Distributions 	}
107*5e3eaea3SApple OSS Distributions #endif /* TARGET_OS_OSX */
108*5e3eaea3SApple OSS Distributions }
109*5e3eaea3SApple OSS Distributions 
110*5e3eaea3SApple OSS Distributions void
__libkernel_init_late(_libkernel_late_init_config_t config)111*5e3eaea3SApple OSS Distributions __libkernel_init_late(_libkernel_late_init_config_t config)
112*5e3eaea3SApple OSS Distributions {
113*5e3eaea3SApple OSS Distributions 	if (config->version >= 1) {
114*5e3eaea3SApple OSS Distributions #if SYSTEM_VERSION_COMPAT_ENABLED
115*5e3eaea3SApple OSS Distributions #if TARGET_OS_OSX && !defined(__i386__)
116*5e3eaea3SApple OSS Distributions 		if (config->enable_system_version_compat) {
117*5e3eaea3SApple OSS Distributions 			/* enable the version compatibility shim for this process (macOS only) */
118*5e3eaea3SApple OSS Distributions 
119*5e3eaea3SApple OSS Distributions 			/* first hook up the shims we reference from open{at}() */
120*5e3eaea3SApple OSS Distributions 			system_version_compat_check_path_suffix = _system_version_compat_check_path_suffix;
121*5e3eaea3SApple OSS Distributions 			system_version_compat_open_shim = _system_version_compat_open_shim;
122*5e3eaea3SApple OSS Distributions 
123*5e3eaea3SApple OSS Distributions 			system_version_compat_mode = SYSTEM_VERSION_COMPAT_MODE_MACOSX;
124*5e3eaea3SApple OSS Distributions 
125*5e3eaea3SApple OSS Distributions 			/*
126*5e3eaea3SApple OSS Distributions 			 * tell the kernel the shim is enabled for this process so it can shim any
127*5e3eaea3SApple OSS Distributions 			 * necessary sysctls
128*5e3eaea3SApple OSS Distributions 			 */
129*5e3eaea3SApple OSS Distributions 			int enable = 1;
130*5e3eaea3SApple OSS Distributions 			__sysctlbyname("kern.system_version_compat", strlen("kern.system_version_compat"),
131*5e3eaea3SApple OSS Distributions 			    NULL, NULL, &enable, sizeof(enable));
132*5e3eaea3SApple OSS Distributions 		} else if ((config->version >= 2) && config->enable_ios_version_compat) {
133*5e3eaea3SApple OSS Distributions 			/* enable the iOS ProductVersion compatibility shim for this process */
134*5e3eaea3SApple OSS Distributions 
135*5e3eaea3SApple OSS Distributions 			/* first hook up the shims we reference from open{at}() */
136*5e3eaea3SApple OSS Distributions 			system_version_compat_check_path_suffix = _system_version_compat_check_path_suffix;
137*5e3eaea3SApple OSS Distributions 			system_version_compat_open_shim = _system_version_compat_open_shim;
138*5e3eaea3SApple OSS Distributions 
139*5e3eaea3SApple OSS Distributions 			system_version_compat_mode = SYSTEM_VERSION_COMPAT_MODE_IOS;
140*5e3eaea3SApple OSS Distributions 
141*5e3eaea3SApple OSS Distributions 			/*
142*5e3eaea3SApple OSS Distributions 			 * We don't currently shim any sysctls for iOS apps running on macOS so we
143*5e3eaea3SApple OSS Distributions 			 * don't need to inform the kernel that this app has the SystemVersion shim enabled.
144*5e3eaea3SApple OSS Distributions 			 */
145*5e3eaea3SApple OSS Distributions 		}
146*5e3eaea3SApple OSS Distributions #endif /* TARGET_OS_OSX && !defined(__i386__) */
147*5e3eaea3SApple OSS Distributions 
148*5e3eaea3SApple OSS Distributions #endif /* SYSTEM_VERSION_COMPAT_ENABLED */
149*5e3eaea3SApple OSS Distributions 
150*5e3eaea3SApple OSS Distributions #if POSIX_SPAWN_FILTERING_ENABLED
151*5e3eaea3SApple OSS Distributions 		if ((config->version >= 3) && config->enable_posix_spawn_filtering) {
152*5e3eaea3SApple OSS Distributions 			posix_spawn_with_filter = _posix_spawn_with_filter;
153*5e3eaea3SApple OSS Distributions 			execve_with_filter = _execve_with_filter;
154*5e3eaea3SApple OSS Distributions 		}
155*5e3eaea3SApple OSS Distributions #endif /* POSIX_SPAWN_FILTERING_ENABLED */
156*5e3eaea3SApple OSS Distributions 	}
157*5e3eaea3SApple OSS Distributions }
158*5e3eaea3SApple OSS Distributions 
159*5e3eaea3SApple OSS Distributions void
__libkernel_init_after_boot_tasks(_libkernel_init_after_boot_tasks_config_t config)160*5e3eaea3SApple OSS Distributions __libkernel_init_after_boot_tasks(
161*5e3eaea3SApple OSS Distributions 	_libkernel_init_after_boot_tasks_config_t config)
162*5e3eaea3SApple OSS Distributions {
163*5e3eaea3SApple OSS Distributions 	if (config->version >= 1) {
164*5e3eaea3SApple OSS Distributions #if POSIX_SPAWN_FILTERING_ENABLED
165*5e3eaea3SApple OSS Distributions 		if (config->enable_posix_spawn_filtering) {
166*5e3eaea3SApple OSS Distributions 			posix_spawn_with_filter = _posix_spawn_with_filter;
167*5e3eaea3SApple OSS Distributions 			execve_with_filter = _execve_with_filter;
168*5e3eaea3SApple OSS Distributions 		}
169*5e3eaea3SApple OSS Distributions #endif /* POSIX_SPAWN_FILTERING_ENABLED */
170*5e3eaea3SApple OSS Distributions 	}
171*5e3eaea3SApple OSS Distributions }
172