Skip to content

Return EFAULT instead of crashing on an unreadable path name - #12

Merged
linuxkid473 merged 1 commit into
linuxkid473:mainfrom
willmcginnis:fix-unreadable-path-name
Nov 10, 2026
Merged

linuxkid473 merged 1 commit into
linuxkid473:mainfrom
willmcginnis:fix-unreadable-path-name

Conversation

@willmcginnis

Copy link
Copy Markdown
Contributor

When an arm64 program passes a path name that the runtime cannot read (an unmapped address, a PROT_NONE page, or a name whose bytes run into an unmapped page before the NUL), Maclator currently crashes with SIGSEGV or SIGBUS. On macOS the same call simply fails with EFAULT. This PR makes the translated call behave like the native one, so a stray pointer in one call no longer takes the whole program down.

Cause

bsd() reads the path argument of path-taking calls (open, openat, stat, access, ...) with cstr_at, which calls CStr::from_ptr directly on the guest's pointer. The kernel never sees the call, because the runtime faults first.

Change

  • A small guest_cstr helper reads the name with guestmem::read, the fault-safe copy the runtime already uses for other guest memory. It reads in pieces that stop at each 4 KiB boundary, so a name that ends just before an unmapped page is still read correctly, and it stops at 8 KiB (well above MAXPATHLEN).
  • If any byte before the NUL cannot be read, or no NUL comes within 8 KiB, bsd() passes the call to the host kernel unchanged and lets it return the appropriate error (EFAULT for an unreadable name). The existing path handling is untouched for every readable name up to that limit.

Two files: crates/maclator/src/syscalls.rs (+75 -2, including two unit tests for the helper) and the new guest test tests/guest/bad_name_pointers.c.

Testing

tests/guest/bad_name_pointers.c runs seven rows, each in its own child process so a crash shows up as that row's failure: open, openat, stat and access on a PROT_NONE page, open on an unmapped address, a name running into a PROT_NONE page, and a name whose NUL is the last readable byte before one (a no-crash control; the read up to the boundary itself is covered by the unit tests). A row whose child cannot be forked or reaped counts as a failure, never a pass.

Build arm64e arm64
native 7/7 PASS 7/7 PASS
main (debddd6) 1/7 PASS, 6 rows killed by SIGBUS/SIGSEGV 1/7 PASS, 6 rows killed
this PR 7/7 PASS 7/7 PASS

The unit tests pass (cargo test -p maclator, 10 passed), and so does the workspace. As a regression check, tests/guest/hello.c, forktest.c, thread_create.c and timer.c, plus a small program doing uname, sysctlbyname, stat and open through libc, print byte-identical output under main and under this change.

Test setup, to be clear about its limits: an Apple Silicon Mac, running the x86_64 Maclator build under Rosetta 2, with a macOS 26.6.2 (25G83) arm64e shared cache as the sysroot and dyld from 26.7.1, --no-aot. I have not yet run it on an Intel host.

Not in this PR

sysctlbyname and sysctl read their name or MIB the same way and crash the same way on an unreadable pointer. I have a separate small follow-up for those that reuses this helper's approach, and will open it after this one if it is welcome. Separately, the sysctl output helpers (write_sysctl_str, write_sysctl_int) still read and write the guest's oldp and oldlenp directly, so a bad output pointer can still crash; that would be a later, equally small change.

bsd() read a path argument with CStr::from_ptr on the guest's pointer.
A name the guest cannot read (NULL-adjacent, unmapped, PROT_NONE, or
running into an unmapped page before its NUL) therefore crashed the
runtime with SIGSEGV/SIGBUS, where macOS returns EFAULT from the call.

Read the name with guestmem::read, in pieces that stop at each 4 KiB
boundary, so a name that ends just before an unmapped page is still
read, up to 8 KiB. If any byte before the NUL cannot be read, or no NUL
comes within 8 KiB, the call is passed to the host kernel unchanged,
which returns the appropriate error (EFAULT for an unreadable name).

tests/guest/bad_name_pointers.c checks seven rows (open, openat, stat,
access on an unreadable page, an unmapped address, a name running into
an unreadable page, and, as a no-crash control, a name ending just
before one) against the native answers. A row whose child process
cannot be forked or reaped counts as a failure. The read up to the
boundary itself is covered by the unit tests.
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