Skip to content
This repository was archived by the owner on Jun 12, 2024. It is now read-only.

Commit 5fd5cd7

Browse files
committed
Use flags MAP_RESILIENT_MEDIA and MAP_RESILIENT_CODESIGN with mmap in MacOS.
These flags prevent crashes while reading from memory-mapped files in MacOS. MAP_RESILIENT_MEDIA prevents crashes while reading from a file in removable media that becomes unavailable, while MAP_RESILIENT_CODESIGN prevents crashes when reading binaries whose digital signature is invalid. Closes VirusTotal#1309
1 parent debd460 commit 5fd5cd7

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

libyara/filemap.c

+21-1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,26 @@ YR_API int yr_filemap_map_fd(
176176

177177
#else // POSIX
178178

179+
#define MAP_EXTRA_FLAGS 0
180+
181+
#if defined (__APPLE__)
182+
// MacOS defines some extra flags for mmap.The MAP_RESILIENT_CODESIGN allows
183+
// to read from binaries whose code signature is invalid, without this flags
184+
// any attempt to read from such binaries causes a crash, see:
185+
// https://github.com/VirusTotal/yara/issues/1309.
186+
//
187+
// Also, reading from files in removable media that becomes unavailable crashes
188+
// the program if the MAP_RESILIENT_MEDIA flag is not set.
189+
#if defined(MAP_RESILIENT_CODESIGN)
190+
#undef MAP_EXTRA_FLAGS
191+
#if defined(MAP_RESILIENT_MEDIA)
192+
#define MAP_EXTRA_FLAGS MAP_RESILIENT_CODESIGN | MAP_RESILIENT_MEDIA
193+
#else
194+
#define MAP_EXTRA_FLAGS MAP_RESILIENT_CODESIGN
195+
#endif
196+
#endif // #if defined(MAP_RESILIENT_CODESIGN)
197+
#endif // #if defined (__APPLE__)
198+
179199
YR_API int yr_filemap_map_fd(
180200
YR_FILE_DESCRIPTOR file,
181201
off_t offset,
@@ -209,7 +229,7 @@ YR_API int yr_filemap_map_fd(
209229
0,
210230
pmapped_file->size,
211231
PROT_READ,
212-
MAP_PRIVATE,
232+
MAP_PRIVATE | MAP_EXTRA_FLAGS,
213233
pmapped_file->file,
214234
offset);
215235

0 commit comments

Comments
 (0)