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"
29 #include <sandbox/private.h>
31 #include <sys/errno.h>
35 using dyld3::closure::FileSystemPhysical
;
37 bool FileSystemPhysical::getRealPath(const char possiblePath
[MAXPATHLEN
], char realPath
[MAXPATHLEN
]) const {
39 int fd
= ::open(possiblePath
, O_RDONLY
);
41 success
= fcntl(fd
, F_GETPATH
, realPath
) == 0;
46 realpath(possiblePath
, realPath
);
47 int realpathErrno
= errno
;
48 // If realpath() resolves to a path which does not exist on disk, errno is set to ENOENT
49 return (realpathErrno
== ENOENT
) || (realpathErrno
== 0);
52 static bool sandboxBlocked(const char* path
, const char* kind
)
54 #if TARGET_IPHONE_SIMULATOR
55 // sandbox calls not yet supported in dyld_sim
58 sandbox_filter_type filter
= (sandbox_filter_type
)(SANDBOX_FILTER_PATH
| SANDBOX_CHECK_NO_REPORT
);
59 return ( sandbox_check(getpid(), kind
, filter
, path
) > 0 );
63 static bool sandboxBlockedMmap(const char* path
)
65 return sandboxBlocked(path
, "file-map-executable");
68 static bool sandboxBlockedOpen(const char* path
)
70 return sandboxBlocked(path
, "file-read-data");
73 static bool sandboxBlockedStat(const char* path
)
75 return sandboxBlocked(path
, "file-read-metadata");
78 // Returns true on success. If an error occurs the given callback will be called with the reason.
79 // On success, info is filled with info about the loaded file. If the path supplied includes a symlink,
80 // the supplier realerPath is filled in with the real path of the file, otherwise it is set to the empty string.
81 bool FileSystemPhysical::loadFile(const char* path
, LoadedFileInfo
& info
, char realerPath
[MAXPATHLEN
], void (^error
)(const char* format
, ...)) const {
83 const char* originalPath
= path
;
84 char altPath
[PATH_MAX
];
86 if ( _fileSystemPrefix
!= nullptr ) {
87 strlcpy(altPath
, _fileSystemPrefix
, PATH_MAX
);
88 strlcat(altPath
, path
, PATH_MAX
);
89 fd
= ::open(altPath
, O_RDONLY
, 0);
94 fd
= ::open(path
, O_RDONLY
, 0);
96 int openErrno
= errno
;
97 if ( (openErrno
== EPERM
) && sandboxBlockedOpen(path
) )
98 error("file system sandbox blocked open(\"%s\", O_RDONLY)", path
);
99 else if ( (openErrno
!= ENOENT
) && (openErrno
!= ENOTDIR
) )
100 error("open(\"%s\", O_RDONLY) failed with errno=%d", path
, openErrno
);
105 // Get the realpath of the file if it is a symlink
106 if ( fcntl(fd
, F_GETPATH
, realerPath
) == 0 ) {
107 // Don't set the realpath if it is just the same as the regular path
108 if ( strcmp(originalPath
, realerPath
) == 0 )
109 realerPath
[0] = '\0';
111 error("Could not get real path for \"%s\"\n", path
);
118 #if TARGET_IPHONE_SIMULATOR
119 if ( ::stat(path
, &statBuf
) != 0 ) {
121 if ( ::fstat(fd
, &statBuf
) != 0 ) {
124 if ( (statErr
== EPERM
) && sandboxBlockedStat(path
) )
125 error("file system sandbox blocked stat(\"%s\")", path
);
127 error("stat(\"%s\") failed with errno=%d", path
, errno
);
132 // only regular files can be loaded
133 if ( !S_ISREG(statBuf
.st_mode
) ) {
134 error("not a file for %s", path
);
139 // mach-o files must be at list one page in size
140 if ( statBuf
.st_size
< 4096 ) {
141 error("file too short %s", path
);
146 info
.fileContent
= nullptr;
147 info
.fileContentLen
= statBuf
.st_size
;
148 info
.sliceOffset
= 0;
149 info
.sliceLen
= statBuf
.st_size
;
150 info
.inode
= statBuf
.st_ino
;
151 info
.mtime
= statBuf
.st_mtime
;
152 info
.path
= originalPath
;
155 void* wholeFile
= ::mmap(nullptr, (size_t)statBuf
.st_size
, PROT_READ
, MAP_PRIVATE
|MAP_RESILIENT_CODESIGN
, fd
, 0);
156 if ( wholeFile
== MAP_FAILED
) {
158 if ( mmapErr
== EPERM
) {
159 if ( sandboxBlockedMmap(path
) )
160 error("file system sandbox blocked mmap() of '%s'", path
);
162 error("code signing blocked mmap() of '%s'", path
);
165 error("mmap() failed with errno=%d for %s", errno
, path
);
170 info
.fileContent
= wholeFile
;
172 // Set unmap as the unload method.
173 info
.unload
= [](const LoadedFileInfo
& info
) {
174 ::munmap((void*)info
.fileContent
, (size_t)info
.fileContentLen
);
181 void FileSystemPhysical::unloadFile(const LoadedFileInfo
& info
) const {
186 void FileSystemPhysical::unloadPartialFile(LoadedFileInfo
& info
, uint64_t keepStartOffset
, uint64_t keepLength
) const {
187 // Unmap from 0..keepStartOffset and (keepStartOffset+keepLength)..info.fileContentLen
189 ::munmap((void*)info
.fileContent
, (size_t)keepStartOffset
);
190 if ((keepStartOffset
+ keepLength
) != info
.fileContentLen
) {
191 // Round up to page alignment
192 keepLength
= (keepLength
+ PAGE_SIZE
- 1) & (-PAGE_SIZE
);
193 ::munmap((void*)((char*)info
.fileContent
+ keepStartOffset
+ keepLength
), (size_t)(info
.fileContentLen
- (keepStartOffset
+ keepLength
)));
195 info
.fileContent
= (const void*)((char*)info
.fileContent
+ keepStartOffset
);
196 info
.fileContentLen
= keepLength
;
199 bool FileSystemPhysical::fileExists(const char* path
, uint64_t* inode
, uint64_t* mtime
, bool* issetuid
) const {
201 if ( _fileSystemPrefix
!= nullptr ) {
202 char altPath
[PATH_MAX
];
203 strlcpy(altPath
, _fileSystemPrefix
, PATH_MAX
);
204 strlcat(altPath
, path
, PATH_MAX
);
205 if ( ::stat(altPath
, &statBuf
) == 0 ) {
207 *inode
= statBuf
.st_ino
;
209 *mtime
= statBuf
.st_mtime
;
211 *issetuid
= (statBuf
.st_mode
& (S_ISUID
|S_ISGID
));
215 if ( ::stat(path
, &statBuf
) != 0 )
218 *inode
= statBuf
.st_ino
;
220 *mtime
= statBuf
.st_mtime
;
222 *issetuid
= (statBuf
.st_mode
& (S_ISUID
|S_ISGID
));