xref: /xnu-8796.141.3/libsyscall/wrappers/system-version-compat.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789) !
1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions  * Copyright (c) 2020 Apple Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions  *
4*1b191cb5SApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions  *
6*1b191cb5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions  * compliance with the License. Please obtain a copy of the License at
10*1b191cb5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this
11*1b191cb5SApple OSS Distributions  * file.
12*1b191cb5SApple OSS Distributions  *
13*1b191cb5SApple OSS Distributions  * The Original Code and all software distributed under the License are
14*1b191cb5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*1b191cb5SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*1b191cb5SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*1b191cb5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*1b191cb5SApple OSS Distributions  * Please see the License for the specific language governing rights and
19*1b191cb5SApple OSS Distributions  * limitations under the License.
20*1b191cb5SApple OSS Distributions  *
21*1b191cb5SApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
22*1b191cb5SApple OSS Distributions  */
23*1b191cb5SApple OSS Distributions 
24*1b191cb5SApple OSS Distributions #include "system-version-compat-support.h"
25*1b191cb5SApple OSS Distributions 
26*1b191cb5SApple OSS Distributions #if SYSTEM_VERSION_COMPAT_ENABLED
27*1b191cb5SApple OSS Distributions 
28*1b191cb5SApple OSS Distributions 
29*1b191cb5SApple OSS Distributions #include <fcntl.h>
30*1b191cb5SApple OSS Distributions #include <stdbool.h>
31*1b191cb5SApple OSS Distributions #include <strings.h>
32*1b191cb5SApple OSS Distributions #include <sys/errno.h>
33*1b191cb5SApple OSS Distributions #include <sys/param.h>
34*1b191cb5SApple OSS Distributions #include <unistd.h>
35*1b191cb5SApple OSS Distributions 
36*1b191cb5SApple OSS Distributions #define PLAT_PREFIX_IOS "iOS"
37*1b191cb5SApple OSS Distributions #define PLAT_PREFIX_MACOS ""
38*1b191cb5SApple OSS Distributions 
39*1b191cb5SApple OSS Distributions #define COMPAT_SUFFIX_MACOS "Compat"
40*1b191cb5SApple OSS Distributions #define COMPAT_SUFFIX_IOS ""
41*1b191cb5SApple OSS Distributions 
42*1b191cb5SApple OSS Distributions #define SYSTEM_VERSION_PLIST_FILENAME "SystemVersion.plist"
43*1b191cb5SApple OSS Distributions #define SYSTEM_VERSION_PLIST_PATH ("/System/Library/CoreServices/" SYSTEM_VERSION_PLIST_FILENAME)
44*1b191cb5SApple OSS Distributions 
45*1b191cb5SApple OSS Distributions #define SYSTEM_VERSION_COMPAT_PLIST_FILENAME(platform_prefix, compat_suffix) (platform_prefix "SystemVersion" compat_suffix ".plist")
46*1b191cb5SApple OSS Distributions 
47*1b191cb5SApple OSS Distributions #define SYSTEM_VERSION_PLIST_FILENAMELEN strlen(SYSTEM_VERSION_PLIST_FILENAME)
48*1b191cb5SApple OSS Distributions 
49*1b191cb5SApple OSS Distributions #define SYSTEM_VERSION_COMPAT_PLIST_FILENAMELEN(platform_prefix, compat_suffix) strlen(SYSTEM_VERSION_COMPAT_PLIST_FILENAME(platform_prefix, compat_suffix))
50*1b191cb5SApple OSS Distributions 
51*1b191cb5SApple OSS Distributions #define SYSTEM_VERSION_PLIST_PATHLEN strlen(SYSTEM_VERSION_PLIST_PATH)
52*1b191cb5SApple OSS Distributions 
53*1b191cb5SApple OSS Distributions extern system_version_compat_mode_t system_version_compat_mode;
54*1b191cb5SApple OSS Distributions 
55*1b191cb5SApple OSS Distributions /*
56*1b191cb5SApple OSS Distributions  * This routine determines whether the path specified matches the path of the SystemVersion plist file
57*1b191cb5SApple OSS Distributions  * we are shimming accesses to. If the file name suffix matches, it's expected we'll call into the
58*1b191cb5SApple OSS Distributions  * version_compat_open_shim() routine below which will do a full comparison on the expanded path.
59*1b191cb5SApple OSS Distributions  *
60*1b191cb5SApple OSS Distributions  * Parameters:	orig_path The path suffix that was provided to the open{at} call.
61*1b191cb5SApple OSS Distributions  *
62*1b191cb5SApple OSS Distributions  * Returns:		true if the path suffix matches the SystemVersion plist path we're shimming
63*1b191cb5SApple OSS Distributions  *				false otherwise
64*1b191cb5SApple OSS Distributions  */
65*1b191cb5SApple OSS Distributions __attribute__((visibility("hidden")))
66*1b191cb5SApple OSS Distributions bool
_system_version_compat_check_path_suffix(const char * orig_path)67*1b191cb5SApple OSS Distributions _system_version_compat_check_path_suffix(const char *orig_path)
68*1b191cb5SApple OSS Distributions {
69*1b191cb5SApple OSS Distributions 	size_t path_str_len = strnlen(orig_path, MAXPATHLEN);
70*1b191cb5SApple OSS Distributions 	/*
71*1b191cb5SApple OSS Distributions 	 * If the length of the filename we're opening is shorter than
72*1b191cb5SApple OSS Distributions 	 * SYSTEM_VERSION_PLIST_FILENAME, bail.
73*1b191cb5SApple OSS Distributions 	 */
74*1b191cb5SApple OSS Distributions 	if (path_str_len < SYSTEM_VERSION_PLIST_FILENAMELEN) {
75*1b191cb5SApple OSS Distributions 		return false;
76*1b191cb5SApple OSS Distributions 	}
77*1b191cb5SApple OSS Distributions 
78*1b191cb5SApple OSS Distributions 	/* If the path we're accessing doesn't end in SYSTEM_VERSION_PLIST_FILENAME, bail. */
79*1b191cb5SApple OSS Distributions 	if (strncmp(&orig_path[path_str_len - SYSTEM_VERSION_PLIST_FILENAMELEN], SYSTEM_VERSION_PLIST_FILENAME,
80*1b191cb5SApple OSS Distributions 	    SYSTEM_VERSION_PLIST_FILENAMELEN) != 0) {
81*1b191cb5SApple OSS Distributions 		return false;
82*1b191cb5SApple OSS Distributions 	}
83*1b191cb5SApple OSS Distributions 
84*1b191cb5SApple OSS Distributions 	/* If modifying the path specified would exceed MAXPATHLEN, bail */
85*1b191cb5SApple OSS Distributions 	if (path_str_len == MAXPATHLEN) {
86*1b191cb5SApple OSS Distributions 		return false;
87*1b191cb5SApple OSS Distributions 	}
88*1b191cb5SApple OSS Distributions 
89*1b191cb5SApple OSS Distributions 	size_t compat_len = (system_version_compat_mode == SYSTEM_VERSION_COMPAT_MODE_IOS) ? SYSTEM_VERSION_COMPAT_PLIST_FILENAMELEN(PLAT_PREFIX_IOS, COMPAT_SUFFIX_IOS) : SYSTEM_VERSION_COMPAT_PLIST_FILENAMELEN(PLAT_PREFIX_MACOS, COMPAT_SUFFIX_MACOS);
90*1b191cb5SApple OSS Distributions 	if ((compat_len - SYSTEM_VERSION_PLIST_FILENAMELEN) > (MAXPATHLEN - path_str_len - 1)) {
91*1b191cb5SApple OSS Distributions 		return false;
92*1b191cb5SApple OSS Distributions 	}
93*1b191cb5SApple OSS Distributions 
94*1b191cb5SApple OSS Distributions 	/* Indicate that we should */
95*1b191cb5SApple OSS Distributions 	return true;
96*1b191cb5SApple OSS Distributions }
97*1b191cb5SApple OSS Distributions 
98*1b191cb5SApple OSS Distributions /*
99*1b191cb5SApple OSS Distributions  * This routine determines whether we are trying to open the SystemVersion plist at SYSTEM_VERSION_PLIST_PATH.
100*1b191cb5SApple OSS Distributions  * It's only used on targets that have the compatibility shim enabled (mainly older binaries).
101*1b191cb5SApple OSS Distributions  *
102*1b191cb5SApple OSS Distributions  * Note that this routine should * ABSOLUTELY NOT * be used as a general shim for accesses at all paths. We replace
103*1b191cb5SApple OSS Distributions  * what the developer generally expected to be one system call with multiple additional system calls. We're ok
104*1b191cb5SApple OSS Distributions  * with doing this here because we only do it for calls to open against files that match this very specific pattern
105*1b191cb5SApple OSS Distributions  * (named SystemVersion.plist), but doing so for all calls to open could result in various issues. Specifically it's
106*1b191cb5SApple OSS Distributions  * difficult to ensure the same cancellation semantics (around EINTR etc) that developers generally expect when replacing
107*1b191cb5SApple OSS Distributions  * a single system call with multiple.
108*1b191cb5SApple OSS Distributions  *
109*1b191cb5SApple OSS Distributions  * This routine should return with the same semantics as the general open system calls that it is shimming - specifically
110*1b191cb5SApple OSS Distributions  * it should leave errno and the return value matching what developers expect.
111*1b191cb5SApple OSS Distributions  *
112*1b191cb5SApple OSS Distributions  * It's expected that _version_compat_check_path_suffix() above was called prior to this call and returned true.
113*1b191cb5SApple OSS Distributions  *
114*1b191cb5SApple OSS Distributions  * We take the close, open and fcntl syscalls as parameters to make sure the variant we call matches the original call
115*1b191cb5SApple OSS Distributions  * to open{at}.
116*1b191cb5SApple OSS Distributions  *
117*1b191cb5SApple OSS Distributions  * Parameters:  opened_fd        The file descriptor that was opened in the original open{at} call
118*1b191cb5SApple OSS Distributions  *              openat_fd        The file descriptor passed to the original openat call (only used when use_openat is true)
119*1b191cb5SApple OSS Distributions  *              orig_path        The original path suffix passed to open{at}
120*1b191cb5SApple OSS Distributions  *              oflag            The original oflag passed to open{at}
121*1b191cb5SApple OSS Distributions  *              mode             The original mode passed to open{at}
122*1b191cb5SApple OSS Distributions  *              close_syscall    The appropriate syscall to use for closing file descriptors
123*1b191cb5SApple OSS Distributions  *              open_syscall     The syscall that should be used for a new call to open.
124*1b191cb5SApple OSS Distributions  *              fctnl_syscall    The appopriate syscall to use for fcntl.
125*1b191cb5SApple OSS Distributions  *
126*1b191cb5SApple OSS Distributions  * Returns:     The original file descriptor if the open{at} access wasn't to SYSTEM_VERSION_PLIST_PATH
127*1b191cb5SApple OSS Distributions  *              A new file descriptor (with the original closed) if the expanded path matches SYSTEM_VERSION_PLIST_PATH
128*1b191cb5SApple OSS Distributions  *              The original file descriptor if the full path suffix does not match SYSTEM_VERSION_PLIST_PATH
129*1b191cb5SApple OSS Distributions  *              -1 (with errno set to EINTR) if the new open or fcntl calls received EINTR (with all new fds closed)
130*1b191cb5SApple OSS Distributions  */
131*1b191cb5SApple OSS Distributions __attribute__((visibility("hidden")))
132*1b191cb5SApple OSS Distributions int
_system_version_compat_open_shim(int opened_fd,int openat_fd,const char * orig_path,int oflag,mode_t mode,int (* close_syscall)(int),int (* open_syscall)(const char *,int,mode_t),int (* openat_syscall)(int,const char *,int,mode_t),int (* fcntl_syscall)(int,int,long))133*1b191cb5SApple OSS Distributions _system_version_compat_open_shim(int opened_fd, int openat_fd, const char *orig_path, int oflag, mode_t mode,
134*1b191cb5SApple OSS Distributions     int (*close_syscall)(int), int (*open_syscall)(const char *, int, mode_t),
135*1b191cb5SApple OSS Distributions     int (*openat_syscall)(int, const char *, int, mode_t),
136*1b191cb5SApple OSS Distributions     int (*fcntl_syscall)(int, int, long))
137*1b191cb5SApple OSS Distributions {
138*1b191cb5SApple OSS Distributions 	/* stash the errno from the original open{at} call */
139*1b191cb5SApple OSS Distributions 	int stashed_errno = errno;
140*1b191cb5SApple OSS Distributions 	char new_path[MAXPATHLEN];
141*1b191cb5SApple OSS Distributions 	size_t path_str_len = strnlen(orig_path, sizeof(new_path));
142*1b191cb5SApple OSS Distributions 
143*1b191cb5SApple OSS Distributions 	/* Resolve the full path of the file we've opened */
144*1b191cb5SApple OSS Distributions 	if (fcntl_syscall(opened_fd, F_GETPATH, new_path)) {
145*1b191cb5SApple OSS Distributions 		if (errno == EINTR) {
146*1b191cb5SApple OSS Distributions 			/* If we got EINTR, we close the file that was opened and return -1 & EINTR */
147*1b191cb5SApple OSS Distributions 			close_syscall(opened_fd);
148*1b191cb5SApple OSS Distributions 			errno = EINTR;
149*1b191cb5SApple OSS Distributions 			return -1;
150*1b191cb5SApple OSS Distributions 		} else {
151*1b191cb5SApple OSS Distributions 			/* otherwise we return the original file descriptor that was requested */
152*1b191cb5SApple OSS Distributions 			errno = stashed_errno;
153*1b191cb5SApple OSS Distributions 			return opened_fd;
154*1b191cb5SApple OSS Distributions 		}
155*1b191cb5SApple OSS Distributions 	}
156*1b191cb5SApple OSS Distributions 
157*1b191cb5SApple OSS Distributions 	/* Check to see whether the path matches SYSTEM_VERSION_PLIST_PATH */
158*1b191cb5SApple OSS Distributions 	size_t newpathlen = strnlen(new_path, MAXPATHLEN);
159*1b191cb5SApple OSS Distributions 	if (newpathlen != SYSTEM_VERSION_PLIST_PATHLEN) {
160*1b191cb5SApple OSS Distributions 		errno = stashed_errno;
161*1b191cb5SApple OSS Distributions 		return opened_fd;
162*1b191cb5SApple OSS Distributions 	}
163*1b191cb5SApple OSS Distributions 
164*1b191cb5SApple OSS Distributions 	if (strncmp(new_path, SYSTEM_VERSION_PLIST_PATH, SYSTEM_VERSION_PLIST_PATHLEN) != 0) {
165*1b191cb5SApple OSS Distributions 		errno = stashed_errno;
166*1b191cb5SApple OSS Distributions 		return opened_fd;
167*1b191cb5SApple OSS Distributions 	}
168*1b191cb5SApple OSS Distributions 
169*1b191cb5SApple OSS Distributions 	new_path[0] = '\0';
170*1b191cb5SApple OSS Distributions 
171*1b191cb5SApple OSS Distributions 	/*
172*1b191cb5SApple OSS Distributions 	 * It looks like we're trying to access the SystemVersion plist. Let's try to open
173*1b191cb5SApple OSS Distributions 	 * the compatibility plist and return that instead if it exists.
174*1b191cb5SApple OSS Distributions 	 */
175*1b191cb5SApple OSS Distributions 	size_t prefix_str_len = path_str_len - SYSTEM_VERSION_PLIST_FILENAMELEN;
176*1b191cb5SApple OSS Distributions 	strlcpy(new_path, orig_path, (prefix_str_len + 1));
177*1b191cb5SApple OSS Distributions 	if (system_version_compat_mode == SYSTEM_VERSION_COMPAT_MODE_IOS) {
178*1b191cb5SApple OSS Distributions 		strlcat(new_path, SYSTEM_VERSION_COMPAT_PLIST_FILENAME(PLAT_PREFIX_IOS, COMPAT_SUFFIX_IOS), MAXPATHLEN);
179*1b191cb5SApple OSS Distributions 	} else {
180*1b191cb5SApple OSS Distributions 		strlcat(new_path, SYSTEM_VERSION_COMPAT_PLIST_FILENAME(PLAT_PREFIX_MACOS, COMPAT_SUFFIX_MACOS), MAXPATHLEN);
181*1b191cb5SApple OSS Distributions 	}
182*1b191cb5SApple OSS Distributions 
183*1b191cb5SApple OSS Distributions 	int new_fd = -1;
184*1b191cb5SApple OSS Distributions 	if (openat_syscall != NULL) {
185*1b191cb5SApple OSS Distributions 		new_fd = openat_syscall(openat_fd, new_path, oflag, mode);
186*1b191cb5SApple OSS Distributions 	} else {
187*1b191cb5SApple OSS Distributions 		new_fd = open_syscall(new_path, oflag, mode);
188*1b191cb5SApple OSS Distributions 	}
189*1b191cb5SApple OSS Distributions 	if ((new_fd == -1) && (errno == ENOENT)) {
190*1b191cb5SApple OSS Distributions 		/* The file doesn't exist, so return the original fd and errno. */
191*1b191cb5SApple OSS Distributions 		errno = stashed_errno;
192*1b191cb5SApple OSS Distributions 		return opened_fd;
193*1b191cb5SApple OSS Distributions 	}
194*1b191cb5SApple OSS Distributions 
195*1b191cb5SApple OSS Distributions 	/*
196*1b191cb5SApple OSS Distributions 	 * Otherwise we close the first file we opened and populate errno
197*1b191cb5SApple OSS Distributions 	 * with errno from the call to open{at}. (Note this covers the EINTR
198*1b191cb5SApple OSS Distributions 	 * case and other failures).
199*1b191cb5SApple OSS Distributions 	 */
200*1b191cb5SApple OSS Distributions 	stashed_errno = errno;
201*1b191cb5SApple OSS Distributions 	close_syscall(opened_fd);
202*1b191cb5SApple OSS Distributions 	errno = stashed_errno;
203*1b191cb5SApple OSS Distributions 	return new_fd;
204*1b191cb5SApple OSS Distributions }
205*1b191cb5SApple OSS Distributions 
206*1b191cb5SApple OSS Distributions #endif /* SYSTEM_VERSION_COMPAT_ENABLED */
207