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