Repository navigation
Return EFAULT instead of crashing on an unreadable sysctl name or MIB - #16
Open
willmcginnis wants to merge 2 commits into
Open
willmcginnis wants to merge 2 commits into
willmcginnis wants to merge 2 commits into
Conversation
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.
Contributor
Author
|
I pushed one more small commit: a write to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is the sysctl follow-up mentioned in #12.
Cause
sysctlbynameand__sysctlread the guest's name and MIB in place, so an unreadable one crashed the runtime where macOS returnsEFAULT.Change
They now read them with
guestmem::readand, as XNU does, copy the name (exactlynamelenbytes) or the two-entry MIB once and fail withEFAULTif 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(whatuname -mreports) instead ofarm64. Anamelenof 0, or of 1024 and more (which XNU refuses withENAMETOOLONG), 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 (
newpandnewlenboth set) fails as it does on macOS 27, for all 62 names: withEPERM, or withENOENTforhw.optional.AdvSIMD, which macOS 27.0.1 does not have (it returnsENOENTfor 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-bitu_intXNU reads, so a count register with its upper half set findshw.machine, as natively; and one trailing dot is dropped before matching, as XNU'sname2oiddoes, sohw.machine.answersarm64too.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, andbad_name_pointers.cstill passes 7/7.The 24 rows
tests/guest/bad_sysctl_pointers.cruns 24 rows, each in its own child process, and compares them with the native answers: two ordinary lookups ofhw.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 uphw.machine20,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, twokern.ostypelookups 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 thatoldpandoldlenpare left untouched), a write tohw.optional.AdvSIMD(ENOENT), and two rows, by name and by MIB, that each set onlynewpand then onlynewlen, which macOS treats as reads (arm64). In the four racing rows every call must answerarm64or fail as the kernel does (EFAULTorENOENT), 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.