Skip to content

Avoid crashes when reading from truncated/deleted MMAP on macos. - #164

Open
roblabla wants to merge 1 commit into
RazrFalcon:masterfrom
roblabla:no-mmap-crash-macos
Open

roblabla wants to merge 1 commit into
RazrFalcon:masterfrom
roblabla:no-mmap-crash-macos

Conversation

@roblabla

@roblabla roblabla commented Feb 18, 2026 •

Copy link
Copy Markdown

macOS provides us with two flags that makes reading from an mmap'd file more resilient against crashes:

  • MAP_RESILIENT_CODESIGN avoids crashes when mapping a macho binary with an invalid signature. Apple has some weird shenanigans going on that makes it validate the signature of mmaped file (even when read-only) under some undocumented circumstances, and this flag avoids this. See Crash on macOS (EXC_BAD_ACCESS (Code Signature Invalid)) VirusTotal/yara#1309 for more information

  • MAP_RESILIENT_MEDIA avoids crashes when the underlying data becomes inaccessible - either because the file is no longer present (think removable media getting unplugged) or because the file got truncated to a smaller size. When this happens, it returns zeroes instead.

Note that MAP_RESILIENT_MEDIA only works for MAP_PRIVATE mappings.

These aren't very well documented, but can be found in the XNU source code: https://github.com/apple-oss-distributions/xnu/blob/xnu-12377.61.12/bsd/sys/mman.h

This PR adds both to every MMAP to make them more robust. Arguably, the second one (MAP_RESILIENT_MEDIA) does change the semantics of mmap compared to how it works on every other platform, and as such we may want to move it to be a flag that the user must provide?

macOS provides us with two flags that makes reading from an mmap'd file
more resilient against crashes:

- MAP_RESILIENT_CODESIGN avoids crashes when mapping a macho binary with an
  invalid signature. Apple has some weird shenanigans going on that
  makes it validate the signature of mmaped file (even when read-only)
  under some undocumented circumstances, and this flag avoids this.
- MAP_RESILIENT_MEDIA avoids crashes when the underlying data becomes
  inaccessible - either because the file is no longer present (think
  removable media getting unplugged) or because the file got truncated
  to a smaller size. When this happens, it returns zeroes instead.
@roblabla
roblabla force-pushed the no-mmap-crash-macos branch from 6eea7fb to 229ed8b Compare February 18, 2026 20:04
@mat-gas

mat-gas commented Aug 20, 2026

Copy link
Copy Markdown

@de-vri-es while you're at it, small up on this change that seems relevant too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants