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