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"
26 #include <TargetConditionals.h>
32 #include <sys/errno.h>
35 #include <mach/mach.h>
36 #if !TARGET_OS_SIMULATOR && !TARGET_OS_DRIVERKIT
38 #include <sandbox/private.h>
40 #include <TargetConditionals.h>
41 #include "MachOFile.h"
42 #include "MachOAnalyzer.h"
44 using dyld3::closure::FileSystemPhysical
;
46 bool FileSystemPhysical::getRealPath(const char possiblePath
[MAXPATHLEN
], char realPath
[MAXPATHLEN
]) const {
47 __block
bool success
= false;
48 // first pass: open file and ask kernel for canonical path
49 forEachPath(possiblePath
, ^(const char* aPath
, unsigned prefixLen
, bool& stop
) {
50 int fd
= dyld3::open(aPath
, O_RDONLY
, 0);
52 char tempPath
[MAXPATHLEN
];
53 success
= (fcntl(fd
, F_GETPATH
, tempPath
) == 0);
56 // if prefix was used, remove it
57 strcpy(realPath
, &tempPath
[prefixLen
]);
65 // second pass: file does not exist but may be a symlink to a non-existent file
66 // This is only for use on-device on platforms where dylibs are removed
67 if ( _overlayPath
== nullptr && _rootPath
== nullptr ) {
68 realpath(possiblePath
, realPath
);
69 int realpathErrno
= errno
;
70 // If realpath() resolves to a path which does not exist on disk, errno is set to ENOENT
71 success
= (realpathErrno
== ENOENT
) || (realpathErrno
== 0);
76 static bool sandboxBlocked(const char* path
, const char* kind
)
78 #if TARGET_OS_SIMULATOR || TARGET_OS_DRIVERKIT
79 // sandbox calls not yet supported in dyld_sim
82 sandbox_filter_type filter
= (sandbox_filter_type
)(SANDBOX_FILTER_PATH
| SANDBOX_CHECK_NO_REPORT
);
83 return ( sandbox_check(getpid(), kind
, filter
, path
) > 0 );
87 static bool sandboxBlockedMmap(const char* path
)
89 return sandboxBlocked(path
, "file-map-executable");
92 static bool sandboxBlockedOpen(const char* path
)
94 return sandboxBlocked(path
, "file-read-data");
97 static bool sandboxBlockedStat(const char* path
)
99 return sandboxBlocked(path
, "file-read-metadata");
102 void FileSystemPhysical::forEachPath(const char* path
, void (^handler
)(const char* fullPath
, unsigned prefixLen
, bool& stop
)) const
105 char altPath
[PATH_MAX
];
106 if ( _overlayPath
!= nullptr ) {
107 strlcpy(altPath
, _overlayPath
, PATH_MAX
);
108 strlcat(altPath
, path
, PATH_MAX
);
109 handler(altPath
, (unsigned)strlen(_overlayPath
), stop
);
113 if ( _rootPath
!= nullptr ) {
114 strlcpy(altPath
, _rootPath
, PATH_MAX
);
115 if ( path
[0] != '/' )
116 strlcat(altPath
, "/", PATH_MAX
);
117 strlcat(altPath
, path
, PATH_MAX
);
118 handler(altPath
, (unsigned)strlen(_rootPath
), stop
);
123 handler(path
, 0, stop
);
127 static bool isFileRelativePath(const char* path
)
129 if ( path
[0] == '/' )
131 if ( path
[0] != '.' )
133 if ( path
[1] == '/' )
135 if ( (path
[1] == '.') && (path
[2] == '/') )
140 // Returns true on success. If an error occurs the given callback will be called with the reason.
141 // On success, info is filled with info about the loaded file. If the path supplied includes a symlink,
142 // the supplier realerPath is filled in with the real path of the file, otherwise it is set to the empty string.
143 bool FileSystemPhysical::loadFile(const char* path
, LoadedFileInfo
& info
, char realerPath
[MAXPATHLEN
], void (^error
)(const char* format
, ...)) const {
144 if ( !_allowRelativePaths
&& isFileRelativePath(path
) ) {
145 error("relative file paths not allowed '%s'", path
);
150 __block
struct stat statBuf
;
151 forEachPath(path
, ^(const char* aPath
, unsigned prefixLen
, bool& stop
) {
152 fd
= dyld3::open(aPath
, O_RDONLY
, 0);
154 int openErrno
= errno
;
155 if ( (openErrno
== EPERM
) && sandboxBlockedOpen(path
) )
156 error("file system sandbox blocked open(\"%s\", O_RDONLY)", path
);
157 else if ( (openErrno
!= ENOENT
) && (openErrno
!= ENOTDIR
) )
158 error("open(\"%s\", O_RDONLY) failed with errno=%d", path
, openErrno
);
162 #if TARGET_OS_SIMULATOR
163 if ( ::stat(aPath
, &statBuf
) != 0 ) {
165 if ( ::fstat(fd
, &statBuf
) != 0 ) {
168 if ( (statErr
== EPERM
) && sandboxBlockedStat(path
) )
169 error("file system sandbox blocked stat(\"%s\")", path
);
171 error("stat(\"%s\") failed with errno=%d", path
, errno
);
176 // Get the realpath of the file if it is a symlink
177 char tempPath
[MAXPATHLEN
];
178 if ( fcntl(fd
, F_GETPATH
, tempPath
) == 0 ) {
179 const char* realPathWithin
= &tempPath
[prefixLen
];
180 // Don't set the realpath if it is just the same as the regular path
181 if ( strcmp(path
, realPathWithin
) == 0 ) {
182 // zero out realerPath if path is fine as-is
183 // <rdar://45018392> don't trash input 'path' if realerPath is same buffer as path
184 if ( realerPath
!= path
)
185 realerPath
[0] = '\0';
188 strcpy(realerPath
, realPathWithin
);
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
.isOSBinary
= false;
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 // if this is an arm64e mach-o or a fat file with an arm64e slice we need to record if it is an OS binary
244 #if TARGET_OS_OSX && __arm64e__
245 const MachOAnalyzer
* ma
= (MachOAnalyzer
*)wholeFile
;
246 if ( ma
->hasMachOMagic() ) {
247 if ( (ma
->cputype
== CPU_TYPE_ARM64
) && ((ma
->cpusubtype
& ~CPU_SUBTYPE_MASK
) == CPU_SUBTYPE_ARM64E
) ) {
248 if ( ma
->isOSBinary(fd
, 0, info
.fileContentLen
) )
249 info
.isOSBinary
= true;
252 else if ( const FatFile
* fat
= FatFile::isFatFile(wholeFile
) ) {
254 fat
->forEachSlice(diag
, info
.fileContentLen
, ^(uint32_t sliceCpuType
, uint32_t sliceCpuSubType
, const void* sliceStart
, uint64_t sliceSize
, bool& stop
) {
255 if ( (sliceCpuType
== CPU_TYPE_ARM64
) && ((sliceCpuSubType
& ~CPU_SUBTYPE_MASK
) == CPU_SUBTYPE_ARM64E
) ) {
256 uint64_t sliceOffset
= (uint8_t*)sliceStart
-(uint8_t*)wholeFile
;
257 const MachOAnalyzer
* sliceMA
= (MachOAnalyzer
*)((uint8_t*)wholeFile
+ sliceOffset
);
258 if ( sliceMA
->isOSBinary(fd
, sliceOffset
, sliceSize
) )
259 info
.isOSBinary
= true;
264 // Set unmap as the unload method.
265 info
.unload
= [](const LoadedFileInfo
& info
) {
266 ::munmap((void*)info
.fileContent
, (size_t)info
.fileContentLen
);
273 void FileSystemPhysical::unloadFile(const LoadedFileInfo
& info
) const {
278 void FileSystemPhysical::unloadPartialFile(LoadedFileInfo
& info
, uint64_t keepStartOffset
, uint64_t keepLength
) const {
279 // Unmap from 0..keepStartOffset and (keepStartOffset+keepLength)..info.fileContentLen
281 ::munmap((void*)info
.fileContent
, (size_t)trunc_page(keepStartOffset
));
282 if ((keepStartOffset
+ keepLength
) != info
.fileContentLen
) {
283 uintptr_t start
= round_page((uintptr_t)info
.fileContent
+ keepStartOffset
+ keepLength
);
284 uintptr_t end
= (uintptr_t)info
.fileContent
+ info
.fileContentLen
;
285 ::munmap((void*)start
, end
- start
);
287 info
.fileContent
= (const void*)((char*)info
.fileContent
+ keepStartOffset
);
288 info
.fileContentLen
= keepLength
;
291 bool FileSystemPhysical::fileExists(const char* path
, uint64_t* inode
, uint64_t* mtime
,
292 bool* issetuid
, bool* inodesMatchRuntime
) const {
293 __block
bool result
= false;
294 forEachPath(path
, ^(const char* aPath
, unsigned prefixLen
, bool& stop
) {
296 if ( dyld3::stat(aPath
, &statBuf
) == 0 ) {
298 *inode
= statBuf
.st_ino
;
300 *mtime
= statBuf
.st_mtime
;
302 *issetuid
= (statBuf
.st_mode
& (S_ISUID
|S_ISGID
));
303 if (inodesMatchRuntime
)
304 *inodesMatchRuntime
= true;