1855239e5SApple OSS Distributions /*
2855239e5SApple OSS Distributions  * Copyright (c) 2010 Apple Inc. All rights reserved.
3855239e5SApple OSS Distributions  *
4855239e5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5855239e5SApple OSS Distributions  *
6855239e5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7855239e5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8855239e5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9855239e5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10855239e5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11855239e5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12855239e5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13855239e5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14855239e5SApple OSS Distributions  *
15855239e5SApple OSS Distributions  * Please obtain a copy of the License at
16855239e5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17855239e5SApple OSS Distributions  *
18855239e5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19855239e5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20855239e5SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21855239e5SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22855239e5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23855239e5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24855239e5SApple OSS Distributions  * limitations under the License.
25855239e5SApple OSS Distributions  *
26855239e5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27855239e5SApple OSS Distributions  */
28855239e5SApple OSS Distributions 
2976e12aa3SApple OSS Distributions #include <TargetConditionals.h>
3076e12aa3SApple OSS Distributions #include <stdbool.h>
3176e12aa3SApple OSS Distributions #include <strings.h>
3276e12aa3SApple OSS Distributions #include <unistd.h>
33e6231be0SApple OSS Distributions #include <mach/vm_page_size.h>
34*1031c584SApple OSS Distributions #include <spawn_filtering_private.h>
35855239e5SApple OSS Distributions #include "_libkernel_init.h"
36e6231be0SApple OSS Distributions #include "system-version-compat-support.h"
37855239e5SApple OSS Distributions 
38186b8fceSApple OSS Distributions extern int mach_init(void);
39855239e5SApple OSS Distributions 
40e6231be0SApple OSS Distributions #if SYSTEM_VERSION_COMPAT_ENABLED
41bb611c8fSApple OSS Distributions 
42bb611c8fSApple OSS Distributions #include <sys/sysctl.h>
43bb611c8fSApple OSS Distributions 
44bb611c8fSApple OSS Distributions extern bool _system_version_compat_check_path_suffix(const char *orig_path);
45bb611c8fSApple OSS Distributions extern int _system_version_compat_open_shim(int opened_fd, int openat_fd, const char *orig_path, int oflag, mode_t mode,
46bb611c8fSApple OSS Distributions     int (*close_syscall)(int), int (*open_syscall)(const char *, int, mode_t),
47bb611c8fSApple OSS Distributions     int (*openat_syscall)(int, const char *, int, mode_t),
48bb611c8fSApple OSS Distributions     int (*fcntl_syscall)(int, int, long));
49bb611c8fSApple OSS Distributions 
50bb611c8fSApple OSS Distributions extern bool (*system_version_compat_check_path_suffix)(const char *orig_path);
51bb611c8fSApple OSS Distributions extern int (*system_version_compat_open_shim)(int opened_fd, int openat_fd, const char *orig_path, int oflag, mode_t mode,
52bb611c8fSApple OSS Distributions     int (*close_syscall)(int), int (*open_syscall)(const char *, int, mode_t),
53bb611c8fSApple OSS Distributions     int (*openat_syscall)(int, const char *, int, mode_t),
54bb611c8fSApple OSS Distributions     int (*fcntl_syscall)(int, int, long));
55bb611c8fSApple OSS Distributions 
56bb611c8fSApple OSS Distributions extern system_version_compat_mode_t system_version_compat_mode;
57bb611c8fSApple OSS Distributions 
58*1031c584SApple OSS Distributions int  __sysctlbyname(const char *name, size_t namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
59*1031c584SApple OSS Distributions #endif /* SYSTEM_VERSION_COMPAT_ENABLED */
60*1031c584SApple OSS Distributions 
61*1031c584SApple OSS Distributions #if POSIX_SPAWN_FILTERING_ENABLED
62e6231be0SApple OSS Distributions struct _posix_spawn_args_desc;
63e6231be0SApple OSS Distributions extern bool (*posix_spawn_with_filter)(pid_t *pid, const char *fname, char * const *argp,
64e6231be0SApple OSS Distributions     char * const *envp, struct _posix_spawn_args_desc *adp, int *ret);
65e6231be0SApple OSS Distributions extern bool _posix_spawn_with_filter(pid_t *pid, const char *fname, char * const *argp,
66e6231be0SApple OSS Distributions     char * const *envp, struct _posix_spawn_args_desc *adp, int *ret);
67e6231be0SApple OSS Distributions extern int (*execve_with_filter)(char *fname, char **argp, char **envp);
68e6231be0SApple OSS Distributions extern int _execve_with_filter(char *fname, char **argp, char **envp);
69*1031c584SApple OSS Distributions #endif /* POSIX_SPAWN_FILTERING_ENABLED */
70e6231be0SApple OSS Distributions 
71e6231be0SApple OSS Distributions #if TARGET_OS_OSX
7276e12aa3SApple OSS Distributions __attribute__((visibility("default")))
7376e12aa3SApple OSS Distributions extern bool _os_xbs_chrooted;
7476e12aa3SApple OSS Distributions bool _os_xbs_chrooted;
75bb611c8fSApple OSS Distributions #endif /* TARGET_OS_OSX */
7676e12aa3SApple OSS Distributions 
77855239e5SApple OSS Distributions /* dlsym() funcptr is for legacy support in exc_catcher */
78aca3beaaSApple OSS Distributions void* (*LIBKERNEL_FUNCTION_PTRAUTH(_dlsym))(void*, const char*) __attribute__((visibility("hidden")));
79855239e5SApple OSS Distributions 
80186b8fceSApple OSS Distributions __attribute__((visibility("hidden")))
81186b8fceSApple OSS Distributions _libkernel_functions_t _libkernel_functions;
82186b8fceSApple OSS Distributions 
83cc9a6355SApple OSS Distributions 
84855239e5SApple OSS Distributions void
__libkernel_init(_libkernel_functions_t fns,const char * envp[],const char * apple[],const struct ProgramVars * vars)85186b8fceSApple OSS Distributions __libkernel_init(_libkernel_functions_t fns,
86186b8fceSApple OSS Distributions     const char *envp[] __attribute__((unused)),
87186b8fceSApple OSS Distributions     const char *apple[] __attribute__((unused)),
88186b8fceSApple OSS Distributions     const struct ProgramVars *vars __attribute__((unused)))
89855239e5SApple OSS Distributions {
90186b8fceSApple OSS Distributions 	_libkernel_functions = fns;
91186b8fceSApple OSS Distributions 	if (fns->dlsym) {
92186b8fceSApple OSS Distributions 		_dlsym = fns->dlsym;
93186b8fceSApple OSS Distributions 	}
94186b8fceSApple OSS Distributions 	mach_init();
95e6231be0SApple OSS Distributions #if TARGET_OS_OSX
96e6231be0SApple OSS Distributions 	for (size_t i = 0; envp[i]; i++) {
97e6231be0SApple OSS Distributions 
98e6231be0SApple OSS Distributions #if defined(__i386__) || defined(__x86_64__)
99e6231be0SApple OSS Distributions 		const char *VM_KERNEL_PAGE_SHIFT_ENV = "VM_KERNEL_PAGE_SIZE_4K=1";
100e6231be0SApple OSS Distributions 		if (vm_kernel_page_shift != 12 && strcmp(VM_KERNEL_PAGE_SHIFT_ENV, envp[i]) == 0) {
101e6231be0SApple OSS Distributions 			vm_kernel_page_shift = 12;
102e6231be0SApple OSS Distributions 			vm_kernel_page_size = 1 << vm_kernel_page_shift;
103e6231be0SApple OSS Distributions 			vm_kernel_page_mask = vm_kernel_page_size - 1;
104e6231be0SApple OSS Distributions 		}
105e6231be0SApple OSS Distributions #endif /* defined(__i386__) || defined(__x86_64__) */
106e6231be0SApple OSS Distributions 	}
107e6231be0SApple OSS Distributions #endif /* TARGET_OS_OSX */
108855239e5SApple OSS Distributions }
109bb611c8fSApple OSS Distributions 
110bb611c8fSApple OSS Distributions void
__libkernel_init_late(_libkernel_late_init_config_t config)111bb611c8fSApple OSS Distributions __libkernel_init_late(_libkernel_late_init_config_t config)
112bb611c8fSApple OSS Distributions {
113bb611c8fSApple OSS Distributions 	if (config->version >= 1) {
114*1031c584SApple OSS Distributions #if SYSTEM_VERSION_COMPAT_ENABLED
115bb611c8fSApple OSS Distributions #if TARGET_OS_OSX && !defined(__i386__)
116bb611c8fSApple OSS Distributions 		if (config->enable_system_version_compat) {
117bb611c8fSApple OSS Distributions 			/* enable the version compatibility shim for this process (macOS only) */
118bb611c8fSApple OSS Distributions 
119bb611c8fSApple OSS Distributions 			/* first hook up the shims we reference from open{at}() */
120bb611c8fSApple OSS Distributions 			system_version_compat_check_path_suffix = _system_version_compat_check_path_suffix;
121bb611c8fSApple OSS Distributions 			system_version_compat_open_shim = _system_version_compat_open_shim;
122bb611c8fSApple OSS Distributions 
123bb611c8fSApple OSS Distributions 			system_version_compat_mode = SYSTEM_VERSION_COMPAT_MODE_MACOSX;
124bb611c8fSApple OSS Distributions 
125bb611c8fSApple OSS Distributions 			/*
126bb611c8fSApple OSS Distributions 			 * tell the kernel the shim is enabled for this process so it can shim any
127bb611c8fSApple OSS Distributions 			 * necessary sysctls
128bb611c8fSApple OSS Distributions 			 */
129bb611c8fSApple OSS Distributions 			int enable = 1;
130bb611c8fSApple OSS Distributions 			__sysctlbyname("kern.system_version_compat", strlen("kern.system_version_compat"),
131bb611c8fSApple OSS Distributions 			    NULL, NULL, &enable, sizeof(enable));
132bb611c8fSApple OSS Distributions 		} else if ((config->version >= 2) && config->enable_ios_version_compat) {
133bb611c8fSApple OSS Distributions 			/* enable the iOS ProductVersion compatibility shim for this process */
134bb611c8fSApple OSS Distributions 
135bb611c8fSApple OSS Distributions 			/* first hook up the shims we reference from open{at}() */
136bb611c8fSApple OSS Distributions 			system_version_compat_check_path_suffix = _system_version_compat_check_path_suffix;
137bb611c8fSApple OSS Distributions 			system_version_compat_open_shim = _system_version_compat_open_shim;
138bb611c8fSApple OSS Distributions 
139bb611c8fSApple OSS Distributions 			system_version_compat_mode = SYSTEM_VERSION_COMPAT_MODE_IOS;
140bb611c8fSApple OSS Distributions 
141bb611c8fSApple OSS Distributions 			/*
142bb611c8fSApple OSS Distributions 			 * We don't currently shim any sysctls for iOS apps running on macOS so we
143bb611c8fSApple OSS Distributions 			 * don't need to inform the kernel that this app has the SystemVersion shim enabled.
144bb611c8fSApple OSS Distributions 			 */
145bb611c8fSApple OSS Distributions 		}
146*1031c584SApple OSS Distributions #endif /* TARGET_OS_OSX && !defined(__i386__) */
147e6231be0SApple OSS Distributions 
148*1031c584SApple OSS Distributions #endif /* SYSTEM_VERSION_COMPAT_ENABLED */
149*1031c584SApple OSS Distributions 
150*1031c584SApple OSS Distributions #if POSIX_SPAWN_FILTERING_ENABLED
151e6231be0SApple OSS Distributions 		if ((config->version >= 3) && config->enable_posix_spawn_filtering) {
152e6231be0SApple OSS Distributions 			posix_spawn_with_filter = _posix_spawn_with_filter;
153e6231be0SApple OSS Distributions 			execve_with_filter = _execve_with_filter;
154e6231be0SApple OSS Distributions 		}
155*1031c584SApple OSS Distributions #endif /* POSIX_SPAWN_FILTERING_ENABLED */
156bb611c8fSApple OSS Distributions 	}
157bb611c8fSApple OSS Distributions }
158e6231be0SApple OSS Distributions 
159e6231be0SApple OSS Distributions void
__libkernel_init_after_boot_tasks(_libkernel_init_after_boot_tasks_config_t config)160e6231be0SApple OSS Distributions __libkernel_init_after_boot_tasks(
161e6231be0SApple OSS Distributions 	_libkernel_init_after_boot_tasks_config_t config)
162e6231be0SApple OSS Distributions {
163e6231be0SApple OSS Distributions 	if (config->version >= 1) {
164*1031c584SApple OSS Distributions #if POSIX_SPAWN_FILTERING_ENABLED
165e6231be0SApple OSS Distributions 		if (config->enable_posix_spawn_filtering) {
166e6231be0SApple OSS Distributions 			posix_spawn_with_filter = _posix_spawn_with_filter;
167e6231be0SApple OSS Distributions 			execve_with_filter = _execve_with_filter;
168e6231be0SApple OSS Distributions 		}
169*1031c584SApple OSS Distributions #endif /* POSIX_SPAWN_FILTERING_ENABLED */
170e6231be0SApple OSS Distributions 	}
171e6231be0SApple OSS Distributions }
172