Repository navigation
Answer the runtime's own sysctl names when libc looks them up - #22
Open
willmcginnis wants to merge 6 commits into
Open
willmcginnis wants to merge 6 commits into
willmcginnis wants to merge 6 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.
libc's sysctlbyname() does not use the sysctlbyname system call. It
looks the name up with name2oid (sysctl with the MIB {0, 3} and the name
as the new value) and then asks by number, and sysctlnametomib() is that
lookup alone. The runtime answered its sysctl_override names only on the
system call and passed name2oid to the host kernel. XNU registers
hw.optional.arm64 and the hw.optional.arm.FEAT_* names only when it is
built for arm64, so an Intel kernel has none of them, and through libc
they read ENOENT, as if the feature were missing.
name2oid now asks the host kernel first. Only when the host answers
ENOENT for a name that sysctl_override answers does the runtime hand
back a synthetic MIB, and a sysctl on that MIB is answered from the same
table, so libc gets the value the system call gives. Names the host has,
including those the table also lists, still get the host's MIB and the
host's answer, as before.
A synthetic MIB has one entry per component of the name, as a real one
does, so sysctlnametomib() reports the size an arm64 kernel reports.
Each entry is -2 - i for the i-th such name the process looked up. XNU
numbers its OIDs from 0 up, so no host MIB starts with a negative number
and a synthetic one cannot hide a real OID. They are valid in the
process that looked the name up and in its forks. A write to one is
EPERM, as it is on an arm64 kernel.
The name is read with guestmem::read, exactly newlen bytes as XNU copies
it, so an unreadable name answers EFAULT instead of reaching the host,
and the host kernel is handed the runtime's copy. A NULL name, or a
newlen of 0 or of 1024 and more, goes to the host unchanged. As
natively, the name ends at its first NUL and loses one trailing dot;
with no buffer only the size is returned, and a buffer too small for the
whole MIB gets nothing, a size of 0 and ENOMEM.
tests/guest/sysctl_name_lookup.c checks fourteen rows, each in a child
process under a 60-second alarm and each passing natively:
hw.optional.arm.FEAT_LSE2 found through sysctlbyname() and through
sysctlnametomib() and sysctl(), with the system call's value whenever
the runtime answered the lookup itself; hw.optional.arm64 and
hw.optional.AdvSIMD answered alike by those two and the system call (the
same errno where they fail, the same length and value where they
succeed); hw.pagesize still given the host's MIB; a name that does not
exist (ENOENT); a name on, running into, and ending just before an
unreadable page (EFAULT, EFAULT, found); the size with no buffer and
with a short one (nothing copied); a write (EPERM, the old value
untouched); one trailing dot; and a NULL name with a non-zero newlen
(ENOENT). A unit test checks that a synthetic MIB answers only the name
it was made for.
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.
libc's sysctlbyname() looks the name up with name2oid and then writes by number. For hw.optional.AdvSIMD, which macOS 26 and 27 do not have, name2oid now hands out a synthetic MIB, and a write to it failed with EPERM, where macOS fails with ENOENT. It now fails with ENOENT, as the sysctlbyname system call does. Reads are unchanged. sysctl_name_lookup.c gains a row for it, making fifteen.
The synthetic MIBs are kept in a process-wide lock. A guest fork copies only the thread that forks, so if another thread held that lock at the instant of the fork, the child got it held with nobody to release it, and the child's next sysctlbyname() of such a name never returned. The guest's fork and vfork now take the lock before libc::fork and release it afterwards in both the parent and the child, as a pthread_atfork handler pair would, so the child always starts with it free. Guest forks and lookups share the lock, so a fork can wait for a lookup or for another fork. A lookup holds no other lock of the runtime's while it holds this one, and the lock is taken before libc::fork is called. fork's errno is kept across the release. A new unit test forks while another thread holds the lock and checks that the child can still look up the MIB its parent was given; with a plain fork, the child is still waiting when the test kills it after 3 seconds.
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.
On an Intel kernel, which has none of these names, a guest that looks up
hw.optional.arm64or ahw.optional.arm.FEAT_*name in the runtime's own table through libc getsENOENT, as if the feature were missing, though the runtime already answers those names on the system call. This change answers them on libc's route too. It builds on my #16, which changes the samesysctlcode insyscalls.rs, so this branch includes #16's commits (its second one merged in), and the changes here are the commits "Answer the runtime's own sysctl names when libc looks them up", "Answer ENOENT for a write to hw.optional.AdvSIMD through libc too" and "Keep the synthetic MIBs usable in a fork's child". Once #16 is merged I will rebase this ontomain, and if you would rather take this one first, I can rebase it the other way.libc's
sysctlbyname()does not use thesysctlbynamesystem call: it looks the name up with name2oid (sysctlwith the MIB{0, 3}) and then asks by number, andsysctlnametomib()is that lookup alone. The runtime answershw.optional.arm64,hw.optional.arm.FEAT_LSE2and its othersysctl_overridenames on the system call, but it passed name2oid to the host kernel. XNU registershw.optional.arm64and thehw.optional.arm.FEAT_*names only when it is built for arm64, so an Intel kernel has none of them, and a guest that asks for them through libc getsENOENT, as if the feature were missing. This change lets libc's route reach the same table.name2oid still asks the host kernel first. Only when the host answers
ENOENTfor a name thatsysctl_overrideanswers does the runtime hand back a synthetic MIB, and asysctlon that MIB is answered from the table, so libc gets the value the system call gives. Names the host has, including those the table also lists, still get the host's MIB and are answered as before.A synthetic MIB has one entry per component of the name, as a real one does, so
sysctlnametomib()reports the size an arm64 kernel reports. Each entry is-2 - ifor the i-th such name the process looked up; XNU numbers its OIDs from 0 up, so no host MIB starts with a negative number and a synthetic one cannot hide a real one. They are valid in the process that looked the name up and in its forks. They are kept in a process-wide lock, so the guest's fork and vfork take it beforelibc::forkand release it in the parent and in the child: otherwise a fork taken while another thread was looking a name up left the child's next lookup of such a name waiting forever. With a test program whose three threads lookhw.optional.AdvSIMDup while its main thread forks 300 times, 19, 15 and 21 of the 300 children hung in three runs without this, and none of 1200 in four runs with it. A write to one isEPERM, as on an arm64 kernel, except forhw.optional.AdvSIMD, which macOS 27.0.1 does not have: a write to it isENOENT, as macOS answers. The name is read withguestmem::read, exactlynewlenbytes as XNU copies it, so an unreadable name answersEFAULT, while a NULL name, or anewlenof 0 or of 1024 and more, goes to the host unchanged. The name ends at its first NUL and loses one trailing dot, and the output follows what macOS does: with no buffer only the size, and with a buffer too small for the whole MIB, nothing copied, a size of 0 andENOMEM.tests/guest/sysctl_name_lookup.cruns 15 rows, each in its own child process under a 60-second alarm and each passing natively, and two new unit tests check that a synthetic MIB answers only the name it was made for, and that a child forked while another thread holds the lock can still look a name up (with a plain fork, the child was still waiting when the test killed it after 3 seconds). Rows 3 and 4 check thatsysctlbyname(),sysctlnametomib()withsysctl(), and the system call agree: the same errno where they fail, and the same length and value where they succeed (natively, the same value or the sameENOENT); row 2 also checks that when the runtime answered the lookup itself, the value is the system call's; the others check macOS's answer, and row 15 checks that a write tohw.optional.AdvSIMDfails withENOENTthrough libc and through the system call, as natively. Natively and with this change all 15 rows pass, in 5 runs each for arm64 and arm64e. Onmain(2d2f5b3) 13 of 15 pass in every run: the twohw.optional.AdvSIMDrows fail, because libc getsENOENTwhere the system call answers 1, and the system call lets a write to it succeed. The test machine's kernel has the arm64 names, so there the new path is taken only byhw.optional.AdvSIMD, which the table lists and macOS 27.0.1 does not have. To exercise the rest, the rows were also run with a test-only change, not part of this PR (I can share it if useful), that makes name2oid answerENOENTfor arm64 names without asking the host, as an Intel kernel does: with this change all 15 rows pass, and onmainwith the same test-only change 5 of 15 pass in every run: 9 rows fail because libc getsENOENTwhen it looks the name up, and row 15 fails because the system call accepts the write, as it does onmain.cargo test -p maclator(21 tests) and the workspace tests pass,bad_name_pointers.cstill passes 7/7, and #16'sbad_sysctl_pointers.cstill passes 24/24.Differences from macOS: existing ones, and two new ones from the synthetic MIBs
Differences from macOS that remain: names in the table that the host kernel also has still get the host's value through libc (for example
hw.pagesizereads the host's 4096, not the table's 16384), and the answer is written as the system call writes it, 8 bytes when the buffer has room for 8, where macOS writes 4 for these names. Two are new, both from the synthetic MIBs: the kernel's other lookups by number do not know them (oidfmt,{0, 4}, answersENOENT, and oid2name,{0, 1}, gives back the numbers, such as-2.-2.-2.-2, instead of the name), where before the name had no MIB at all; andhw.optional.AdvSIMD, which the table lists but macOS 27.0.1 does not have, now answers 1 through libc as well, as it already did through the system call, where macOS answersENOENT.A note on the fork test above: of the 18 hung children sampled, 16 waited for the synthetic MIBs' lock and 2 in
paths::overlay_root, an older lock this PR does not change, which can leave a forked child waiting the same way.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. Not tested on an Intel Mac.This is one of a few small PRs I'm sending today, and there's no rush on any of them.