2 * Copyright (c) 2017 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #include "ClosureFileSystemPhysical.h"
30 #if BUILDING_UPDATE_DYLD_CACHE_BUILDER
33 #include <sys/errno.h>
36 #include <mach/mach.h>
37 #if !TARGET_OS_SIMULATOR && !TARGET_OS_DRIVERKIT
39 #include <sandbox/private.h>
42 using dyld3::closure::FileSystemPhysical
;
44 bool FileSystemPhysical::getRealPath(const char possiblePath
[MAXPATHLEN
], char realPath
[MAXPATHLEN
]) const {
45 __block
bool success
= false;
46 // first pass: open file and ask kernel for canonical path
47 forEachPath(possiblePath
, ^(const char* aPath
, unsigned prefixLen
, bool& stop
) {
48 int fd
= ::open(aPath
, O_RDONLY
, 0);
50 char tempPath
[MAXPATHLEN
];
51 success
= (fcntl(fd
, F_GETPATH
, tempPath
) == 0);
54 // if prefix was used, remove it
55 strcpy(realPath
, &tempPath
[prefixLen
]);
63 // second pass: file does not exist but may be a symlink to a non-existent file
64 // This is only for use on-device on platforms where dylibs are removed
65 if ( _overlayPath
== nullptr && _rootPath
== nullptr ) {
66 realpath(possiblePath
, realPath
);
67 int realpathErrno
= errno
;
68 // If realpath() resolves to a path which does not exist on disk, errno is set to ENOENT
69 success
= (realpathErrno
== ENOENT
) || (realpathErrno
== 0);
74 static bool sandboxBlocked(const char* path
, const char* kind
)
76 #if TARGET_OS_SIMULATOR || TARGET_OS_DRIVERKIT
77 // sandbox calls not yet supported in dyld_sim
80 sandbox_filter_type filter
= (sandbox_filter_type
)(SANDBOX_FILTER_PATH
| SANDBOX_CHECK_NO_REPORT
);
81 return ( sandbox_check(getpid(), kind
, filter
, path
) > 0 );
85 static bool sandboxBlockedMmap(const char* path
)
87 return sandboxBlocked(path
, "file-map-executable");
90 static bool sandboxBlockedOpen(const char* path
)
92 return sandboxBlocked(path
, "file-read-data");
95 static bool sandboxBlockedStat(const char* path
)
97 return sandboxBlocked(path
, "file-read-metadata");
100 void FileSystemPhysical::forEachPath(const char* path
, void (^handler
)(const char* fullPath
, unsigned prefixLen
, bool& stop
)) const
103 char altPath
[PATH_MAX
];
104 if ( _overlayPath
!= nullptr ) {
105 strlcpy(altPath
, _overlayPath
, PATH_MAX
);
106 strlcat(altPath
, path
, PATH_MAX
);
107 handler(altPath
, (unsigned)strlen(_overlayPath
), stop
);
111 if ( _rootPath
!= nullptr ) {
112 strlcpy(altPath
, _rootPath
, PATH_MAX
);
113 strlcat(altPath
, path
, PATH_MAX
);
114 handler(altPath
, (unsigned)strlen(_rootPath
), stop
);
119 handler(path
, 0, stop
);
123 static bool isFileRelativePath(const char* path
)
125 if ( path
[0] == '/' )
127 if ( path
[0] != '.' )
129 if ( path
[1] == '/' )
131 if ( (path
[1] == '.') && (path
[2] == '/') )
136 // Returns true on success. If an error occurs the given callback will be called with the reason.
137 // On success, info is filled with info about the loaded file. If the path supplied includes a symlink,
138 // the supplier realerPath is filled in with the real path of the file, otherwise it is set to the empty string.
139 bool FileSystemPhysical::loadFile(const char* path
, LoadedFileInfo
& info
, char realerPath
[MAXPATHLEN
], void (^error
)(const char* format
, ...)) const {
140 if ( !_allowRelativePaths
&& isFileRelativePath(path
) ) {
141 error("relative file paths not allowed '%s'", path
);
146 __block
struct stat statBuf
;
147 __block
bool sipProtected
= false;
148 forEachPath(path
, ^(const char* aPath
, unsigned prefixLen
, bool& stop
) {
149 fd
= ::open(aPath
, O_RDONLY
, 0);
151 int openErrno
= errno
;
152 if ( (openErrno
== EPERM
) && sandboxBlockedOpen(path
) )
153 error("file system sandbox blocked open(\"%s\", O_RDONLY)", path
);
154 else if ( (openErrno
!= ENOENT
) && (openErrno
!= ENOTDIR
) )
155 error("open(\"%s\", O_RDONLY) failed with errno=%d", path
, openErrno
);
159 #if TARGET_OS_SIMULATOR
160 if ( ::stat(aPath
, &statBuf
) != 0 ) {
162 if ( ::fstat(fd
, &statBuf
) != 0 ) {
165 if ( (statErr
== EPERM
) && sandboxBlockedStat(path
) )
166 error("file system sandbox blocked stat(\"%s\")", path
);
168 error("stat(\"%s\") failed with errno=%d", path
, errno
);
173 // Get the realpath of the file if it is a symlink
174 char tempPath
[MAXPATHLEN
];
175 if ( fcntl(fd
, F_GETPATH
, tempPath
) == 0 ) {
176 const char* realPathWithin
= &tempPath
[prefixLen
];
177 // Don't set the realpath if it is just the same as the regular path
178 if ( strcmp(path
, realPathWithin
) == 0 ) {
179 // zero out realerPath if path is fine as-is
180 // <rdar://45018392> don't trash input 'path' if realerPath is same buffer as path
181 if ( realerPath
!= path
)
182 realerPath
[0] = '\0';
185 strcpy(realerPath
, realPathWithin
);
186 #if BUILDING_UPDATE_DYLD_CACHE_BUILDER
187 sipProtected
= (rootless_check_trusted_fd(fd
) == 0);
192 error("Could not get real path for \"%s\"\n", path
);
202 // only regular files can be loaded
203 if ( !S_ISREG(statBuf
.st_mode
) ) {
204 error("not a file for %s", path
);
209 // mach-o files must be at list one page in size
210 if ( statBuf
.st_size
< 4096 ) {
211 error("file too short %s", path
);
216 info
.fileContent
= nullptr;
217 info
.fileContentLen
= statBuf
.st_size
;
218 info
.sliceOffset
= 0;
219 info
.sliceLen
= statBuf
.st_size
;
220 info
.isSipProtected
= sipProtected
;
221 info
.inode
= statBuf
.st_ino
;
222 info
.mtime
= statBuf
.st_mtime
;
226 void* wholeFile
= ::mmap(nullptr, (size_t)statBuf
.st_size
, PROT_READ
, MAP_PRIVATE
|MAP_RESILIENT_CODESIGN
, fd
, 0);
227 if ( wholeFile
== MAP_FAILED
) {
229 if ( mmapErr
== EPERM
) {
230 if ( sandboxBlockedMmap(path
) )
231 error("file system sandbox blocked mmap() of '%s'", path
);
233 error("code signing blocked mmap() of '%s'", path
);
236 error("mmap() failed with errno=%d for %s", errno
, path
);
241 info
.fileContent
= wholeFile
;
243 // Set unmap as the unload method.
244 info
.unload
= [](const LoadedFileInfo
& info
) {
245 ::munmap((void*)info
.fileContent
, (size_t)info
.fileContentLen
);
252 void FileSystemPhysical::unloadFile(const LoadedFileInfo
& info
) const {
257 void FileSystemPhysical::unloadPartialFile(LoadedFileInfo
& info
, uint64_t keepStartOffset
, uint64_t keepLength
) const {
258 // Unmap from 0..keepStartOffset and (keepStartOffset+keepLength)..info.fileContentLen
260 ::munmap((void*)info
.fileContent
, (size_t)keepStartOffset
);
261 if ((keepStartOffset
+ keepLength
) != info
.fileContentLen
) {
262 // Round up to page alignment
263 keepLength
= (keepLength
+ PAGE_SIZE
- 1) & (-PAGE_SIZE
);
264 ::munmap((void*)((char*)info
.fileContent
+ keepStartOffset
+ keepLength
), (size_t)(info
.fileContentLen
- (keepStartOffset
+ keepLength
)));
266 info
.fileContent
= (const void*)((char*)info
.fileContent
+ keepStartOffset
);
267 info
.fileContentLen
= keepLength
;
270 bool FileSystemPhysical::fileExists(const char* path
, uint64_t* inode
, uint64_t* mtime
,
271 bool* issetuid
, bool* inodesMatchRuntime
) const {
272 __block
bool result
= false;
273 forEachPath(path
, ^(const char* aPath
, unsigned prefixLen
, bool& stop
) {
275 if ( ::stat(aPath
, &statBuf
) == 0 ) {
277 *inode
= statBuf
.st_ino
;
279 *mtime
= statBuf
.st_mtime
;
281 *issetuid
= (statBuf
.st_mode
& (S_ISUID
|S_ISGID
));
282 if (inodesMatchRuntime
)
283 *inodesMatchRuntime
= true;