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