Skip to content

Answer the runtime's own sysctl names when libc looks them up - #22

Open
willmcginnis wants to merge 6 commits into
linuxkid473:mainfrom
willmcginnis:fix-sysctl-name-lookup
Open

willmcginnis wants to merge 6 commits into
linuxkid473:mainfrom
willmcginnis:fix-sysctl-name-lookup

Conversation

@willmcginnis

@willmcginnis willmcginnis commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

On an Intel kernel, which has none of these names, a guest that looks up hw.optional.arm64 or a hw.optional.arm.FEAT_* name in the runtime's own table through libc gets ENOENT, 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 same sysctl code in syscalls.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 onto main, and if you would rather take this one first, I can rebase it the other way.

libc's sysctlbyname() does not use the sysctlbyname system call: it looks the name up with name2oid (sysctl with the MIB {0, 3}) and then asks by number, and sysctlnametomib() is that lookup alone. The runtime answers hw.optional.arm64, hw.optional.arm.FEAT_LSE2 and its other sysctl_override names on the system call, but it 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 a guest that asks for them through libc gets ENOENT, 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 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 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 - 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 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 before libc::fork and 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 look hw.optional.AdvSIMD up 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 is EPERM, as on an arm64 kernel, except for hw.optional.AdvSIMD, which macOS 27.0.1 does not have: a write to it is ENOENT, as macOS answers. The name is read with guestmem::read, exactly newlen bytes as XNU copies it, so an unreadable name answers EFAULT, while a NULL name, or a newlen of 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 and ENOMEM.

tests/guest/sysctl_name_lookup.c runs 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 that sysctlbyname(), sysctlnametomib() with sysctl(), 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 same ENOENT); 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 to hw.optional.AdvSIMD fails with ENOENT through 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. On main (2d2f5b3) 13 of 15 pass in every run: the two hw.optional.AdvSIMD rows fail, because libc gets ENOENT where 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 by hw.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 answer ENOENT for arm64 names without asking the host, as an Intel kernel does: with this change all 15 rows pass, and on main with the same test-only change 5 of 15 pass in every run: 9 rows fail because libc gets ENOENT when it looks the name up, and row 15 fails because the system call accepts the write, as it does on main. cargo test -p maclator (21 tests) and the workspace tests pass, bad_name_pointers.c still passes 7/7, and #16's bad_sysctl_pointers.c still 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.pagesize reads 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}, answers ENOENT, 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; and hw.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 answers ENOENT.

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.

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.
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