1 /*
2 * Copyright (c) 2020 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <fcntl.h>
25 #include <stdarg.h>
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <TargetConditionals.h>
29 #include "system-version-compat-support.h"
30
31 #if !defined(__i386__)
32
33 #if SYSTEM_VERSION_COMPAT_ENABLED
34 #include <stdbool.h>
35
36 extern bool (*system_version_compat_check_path_suffix)(const char *orig_path);
37 extern int (*system_version_compat_open_shim)(int opened_fd, int openat_fd, const char *orig_path, int oflag, mode_t mode,
38 int (*close_syscall)(int), int (*open_syscall)(const char *, int, mode_t),
39 int (*openat_syscall)(int, const char *, int, mode_t),
40 int (*fcntl_syscall)(int, int, long));
41 #endif /* SYSTEM_VERSION_COMPAT_ENABLED */
42
43 #ifdef VARIANT_CANCELABLE
44 int __open(const char *path, int oflag, mode_t mode);
45 int __openat(int fd, const char *path, int oflag, mode_t mode);
46
47 #define OPEN_SYSCALL __open
48 #define OPENAT_SYSCALL __openat
49
50 #if SYSTEM_VERSION_COMPAT_ENABLED
51 int __fcntl(int fd, int cmd, long arg);
52 int close(int fd);
53
54 #define FCNTL_SYSCALL __fcntl
55 #define CLOSE_SYSCALL close
56 #endif /* SYSTEM_VERSION_COMPAT_ENABLED */
57
58 #else /* VARIANT_CANCELABLE */
59 int __open_nocancel(const char *path, int oflag, mode_t mode);
60 int __openat_nocancel(int fd, const char *path, int oflag, mode_t mode);
61
62 #define OPEN_SYSCALL __open_nocancel
63 #define OPENAT_SYSCALL __openat_nocancel
64
65 #if SYSTEM_VERSION_COMPAT_ENABLED
66 int __fcntl_nocancel(int fd, int cmd, long arg);
67 int __close_nocancel(int fd);
68
69 #define FCNTL_SYSCALL __fcntl_nocancel
70 #define CLOSE_SYSCALL __close_nocancel
71 #endif /* SYSTEM_VERSION_COMPAT_ENABLED */
72 #endif /* VARIANT_CANCELABLE */
73
74 #ifdef VARIANT_CANCELABLE
75 int
open(const char * path,int oflag,...)76 open(const char *path, int oflag, ...)
77 #else /* VARIANT_CANCELABLE */
78 int
79 open$NOCANCEL(const char *path, int oflag, ...)
80 #endif
81 {
82 int opened_fd = 0;
83 mode_t mode = 0;
84
85 if (oflag & O_CREAT) {
86 va_list ap;
87 va_start(ap, oflag);
88 /* compiler warns to pass int (not mode_t) to va_arg */
89 mode = va_arg(ap, int);
90 va_end(ap);
91 }
92
93 opened_fd = OPEN_SYSCALL(path, oflag, mode);
94 #if !SYSTEM_VERSION_COMPAT_ENABLED
95 return opened_fd;
96 #else /* SYSTEM_VERSION_COMPAT_ENABLED */
97 if (opened_fd < 0) {
98 return opened_fd;
99 }
100
101 /* check to see if system_version_compat is enabled for this process */
102 if (system_version_compat_check_path_suffix == NULL) {
103 return opened_fd;
104 }
105
106 /* check to see if the suffix of the path we opened matches one we are shimming */
107 if (!system_version_compat_check_path_suffix(path)) {
108 return opened_fd;
109 }
110
111 /* at this point we call into the version compat open shim and return values from there */
112 return system_version_compat_open_shim(opened_fd, -1, path, oflag, mode, CLOSE_SYSCALL, OPEN_SYSCALL,
113 NULL, FCNTL_SYSCALL);
114 #endif /* !SYSTEM_VERSION_COMPAT_ENABLED */
115 }
116
117 #ifdef VARIANT_CANCELABLE
118 int
openat(int fd,const char * path,int oflag,...)119 openat(int fd, const char *path, int oflag, ...)
120 #else /* VARIANT_CANCELABLE */
121 int
122 openat$NOCANCEL(int fd, const char *path, int oflag, ...)
123 #endif
124 {
125 int opened_fd = 0;
126 mode_t mode = 0;
127
128 if (oflag & O_CREAT) {
129 va_list ap;
130 va_start(ap, oflag);
131 // compiler warns to pass int (not mode_t) to va_arg
132 mode = va_arg(ap, int);
133 va_end(ap);
134 }
135
136 opened_fd = OPENAT_SYSCALL(fd, path, oflag, mode);
137 #if !SYSTEM_VERSION_COMPAT_ENABLED
138 return opened_fd;
139 #else
140 if (opened_fd < 0) {
141 return opened_fd;
142 }
143
144 /* check to see if system_version_compat is enabled for this process */
145 if (system_version_compat_check_path_suffix == NULL) {
146 return opened_fd;
147 }
148
149 /* check to see if the suffix of the path we opened matches one we are shimming */
150 if (!system_version_compat_check_path_suffix(path)) {
151 return opened_fd;
152 }
153
154 /* at this point we call into the version compat open shim and return values from there */
155 return system_version_compat_open_shim(opened_fd, fd, path, oflag, mode, CLOSE_SYSCALL, NULL,
156 OPENAT_SYSCALL, FCNTL_SYSCALL);
157 #endif /* !SYSTEM_VERSION_COMPAT_ENABLED */
158 }
159 #endif /* !defined(__i386__) */
160