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