Skip to content

Return EFAULT instead of crashing on an unreadable sysctl name or MIB - #16

Open
willmcginnis wants to merge 2 commits into
linuxkid473:mainfrom
willmcginnis:fix-unreadable-sysctl-name
Open

willmcginnis wants to merge 2 commits into
linuxkid473:mainfrom
willmcginnis:fix-unreadable-sysctl-name

Conversation

@willmcginnis

@willmcginnis willmcginnis commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

This is the sysctl follow-up mentioned in #12.

Cause

sysctlbyname and __sysctl read the guest's name and MIB in place, so an unreadable one crashed the runtime where macOS returns EFAULT.

Change

They now read them with guestmem::read and, as XNU does, copy the name (exactly namelen bytes) or the two-entry MIB once and fail with EFAULT if they cannot.

One difference from #12's path names: a name the runtime cannot read is not passed on to the host kernel, and a name it did read is passed on as the runtime's copy rather than the guest's pointer. Otherwise a second thread that restores access to the page, or changes the name, between the runtime's check and the host's read gets the host's answer for hw.machine (what uname -m reports) instead of arm64. A namelen of 0, or of 1024 and more (which XNU refuses with ENAMETOOLONG), and MIBs of other lengths go to the host kernel unchanged, which answers as XNU does.

The names the runtime answers itself now also behave as they do natively in three more cases: a write to one of them (newp and newlen both set) fails as it does on macOS 27, for all 62 names: with EPERM, or with ENOENT for hw.optional.AdvSIMD, which macOS 27.0.1 does not have (it returns ENOENT for reads of it too, while the runtime still answers reads from its table; this change leaves reads alone); __sysctl's MIB count is compared as the 32-bit u_int XNU reads, so a count register with its upper half set finds hw.machine, as natively; and one trailing dot is dropped before matching, as XNU's name2oid does, so hw.machine. answers arm64 too.

Testing

Natively and with this change all 24 rows pass, in 5 runs each for arm64 and arm64e. On main (2d2f5b3) 7 pass in every run: 9 rows kill the process, and 8 succeed where macOS fails or give a different answer. cargo test -p maclator (19 tests) and the workspace tests pass, and bad_name_pointers.c still passes 7/7.

The 24 rows

tests/guest/bad_sysctl_pointers.c runs 24 rows, each in its own child process, and compares them with the native answers: two ordinary lookups of hw.machine, a name that ends exactly at an unreadable page (also a success), six unreadable, unmapped or page-crossing names and MIBs (EFAULT), four rows that look up hw.machine 20,000 times while a second thread takes away and restores access to its page or switches it to a name or MIB that does not exist, two kern.ostype lookups that only the host can answer (Darwin), the MIB count with its upper half set, hw.machine., four writes (EPERM; two of them also check that oldp and oldlenp are left untouched), a write to hw.optional.AdvSIMD (ENOENT), and two rows, by name and by MIB, that each set only newp and then only newlen, which macOS treats as reads (arm64). In the four racing rows every call must answer arm64 or fail as the kernel does (EFAULT or ENOENT), and the lookups start only once the second thread has made its first change.

Tested on an Apple Silicon Mac on macOS 27.0.1, running the x86_64 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.

sysctlbyname read its name with CStr::from_ptr and sysctl read its MIB
with slice::from_raw_parts, both on the guest's pointer. An unreadable
name or MIB therefore crashed the runtime where macOS returns EFAULT.

XNU copies a name of 1 to 1023 bytes, or a two-entry MIB, once, and
fails with EFAULT if it cannot. The runtime now does the same: it reads
exactly namelen bytes with guestmem::read and uses them up to the first
NUL, reads a two-entry MIB the same way, and answers EFAULT when the
copy faults. When the call is passed on, the host kernel is handed the
runtime's copy instead of the guest's pointer, so a second thread
changing the name or its page between the two reads cannot reach the
host's own hw.machine. A namelen of 0 (XNU copies nothing and finds no
such name), one of 1024 or more (which XNU refuses) and MIBs of other
lengths go to the host kernel unchanged. Reading namelen bytes also
means a name whose NUL lies past namelen is matched as XNU sees it.

The names the runtime answers itself also follow XNU where they
differed: a write to one of them (newp and newlen both set) fails with
EPERM, as it does natively for all of them but hw.optional.AdvSIMD,
which macOS 27 does not have; sysctl's MIB count is compared as XNU's
32-bit u_int; and sysctlbyname drops one trailing dot before matching,
as XNU's name2oid does, so "hw.machine." answers arm64 too.

tests/guest/bad_sysctl_pointers.c checks twenty-three rows against the
native answers: two ordinary successes, one boundary success, six EFAULT
cases (unreadable, unmapped and page-crossing names and MIBs), four rows
that look up hw.machine 20000 times while another thread takes away and
restores access to its page or changes it to a name or MIB that does
not exist, two kern.ostype lookups that only the host can answer, and
eight rows for the count, the trailing dot, writes (two of which also
check that oldp and oldlenp are left untouched) and calls that set only
one of newp and newlen, which are reads. A lookup that succeeds
must return the expected string ("arm64", or "Darwin" for kern.ostype),
so one that reaches the host and gets its x86_64 machine fails the row,
and so does any result other than 0 or -1 or a row whose child process
cannot be forked or reaped. The race rows start their lookups only
after the other thread has made its first change.
macOS 26 and 27 have no hw.optional.AdvSIMD (the feature is
hw.optional.neon and hw.optional.arm.AdvSIMD there), so a write to it
fails with ENOENT. The previous commit made it EPERM, like the other
names the runtime answers. It is now ENOENT, so all 62 names answer a
write as macOS 27 does. Reads of the name are unchanged.

bad_sysctl_pointers.c gains a row for it, making twenty-four.
@willmcginnis

Copy link
Copy Markdown
Contributor Author

I pushed one more small commit: a write to hw.optional.AdvSIMD now fails with ENOENT, as it does on macOS, instead of EPERM. Nothing else changed; the test has one more row for it (24 now), and I updated the description to match.

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.

1 participant