Repository navigation
kernel: SMEP and SMAP, exact timeouts, #! scripts - #116
Conversation
The kernel only reported the two CPU features. It now sets CR4.SMEP and CR4.SMAP on every CPU that has them, so it can no longer execute user pages, nor read or write them outside an explicit access window. All kernel accesses to user memory already go through the fixup routines (probe_*, copy_from_user, copy_to_user); those now open the window with stac and close it with clac on both the normal and the recovery path. The interrupt entry clears RFLAGS.AC (SYSCALL already masks it), so handlers run with SMAP in force. A kernel fault on a present user page outside a window is no longer handed to copy-on-write: it recovers through a fixup or stops the system. A new self-test maps a user page in its own address space and checks that the copy routines reach it while a plain kernel read faults. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01Wky8K1hAgSGYFWjvQ1gDqK
|
Caution Review failedThe pull request is closed. Pre-merge checks:
|
… are late The user check "sleep blocks for the requested time" failed now and then on four CPUs: a 50 ms sleep measured 48.6 ms. The tick count falls behind when CPU 0's timer interrupt is late and is raised in one step afterwards. monotonic_ns() interpolated at most one tick past the last counted one, so the clock stood still meanwhile. User space re-anchors its TSC clock on CLOCK_MONOTONIC once a second; anchored on a clock that was behind, and held by its own monotonic guard, it then measured the next interval too short by the delay. The interpolation now follows the TSC for up to a second. Deadlines of sleep, futex and wait-queue timeouts were counted from the same late tick count and expired early by the delay once it was caught up. They now start from ticks_now(), which adds the ticks the TSC says have passed. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01Wky8K1hAgSGYFWjvQ1gDqK
Merging this PR will improve performance by 10.28%2 warnings
Performance Changes
Tip Curious why performance improved? Comment Comparing |
… a paired reading Timeouts still ended early now and then after the previous change (sleep, futex and condition variable timeouts in the user checks, on one CPU and on four). Three more causes, all in how the clocks follow each other: - record_tick added the ticks a late interrupt had lost and then one more for the interrupt that had become pending behind it, and one on top of what another CPU had already caught up: the same time was counted twice, the tick clock ran ahead of the TSC (51 ticks in 48.1 ms) and the windowed TSC rate it feeds came out up to 1.8 % low. The count is now set from the time since the last counted tick, rounded to whole ticks. ticks_now() rounds the same way, so a deadline is never reached before the requested ticks have passed. - The last tick and its TSC reading were two separate atomics; a reader on another CPU could pair the tick of one interrupt with the TSC of the next. They are read under a sequence counter now. - User space re-anchored its TSC clock with RDTSC followed by a CLOCK call; a preemption between the two shifted the clock by its length. TIME_CALIB returns the kernel clock with the TSC reading it was computed from, and the resync uses that pair. A clock found ahead of the kernel's is no longer set back and held by the monotonic guard, which shortened every interval measured meanwhile; it keeps its reading and runs slower until the next resync. The first resync comes after 100 ms instead of a second. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01Wky8K1hAgSGYFWjvQ1gDqK
What does this change?
Three separate changes, each in its own commits.
SMEP and SMAP
The kernel only reported SMEP and SMAP in the CPU feature line. It now enables both on every CPU that has them.
cpu::enable_supervisor_protectionsetsCR4.SMEPandCR4.SMAPon the boot CPU and on each AP.fixup.rsroutines (probe_*,copy_from_user,copy_to_user) are the only kernel code that touches user memory. They now open an access window withstacand close it withclacon both the normal and the recovery path. The instructions are skipped on CPUs without SMAP.RFLAGS.AC.SYSCALLalready masked it, so user code cannot hand the kernel an open window.RFLAGS.ACis clear afterwards.docs/kernel.mddescribes the mechanism.Timeouts that ended early
The user checks
sleep blocks for the requested time,futex: value mismatch and timeoutandCondvar: wait_timeout expiresfailed now and then (a 50 ms sleep measured 48.6 ms). This also happens onmain. Causes, all in how the tick count, the kernel clock and the user-space TSC clock follow each other:record_tickadded the ticks a late timer interrupt had lost, then one more for the interrupt that had become pending behind it, and one on top of what another CPU had already caught up. The same time was counted twice: 51 ticks in 48.1 ms of TSC time, and the windowed TSC rate came out up to 1.8 % low. The count is now set from the time since the last counted tick, rounded to whole ticks.monotonic_nsinterpolated at most one tick past the last counted one, so it stood still while ticks were late. It now follows the TSC for up to a second.ticks_now(), which rounds the same way asrecord_tick.os-sysre-anchored its clock with RDTSC followed by aCLOCKcall; a preemption between the two shifted the clock.TIME_CALIBnow returns the kernel clock with the TSC reading it was computed from, and the resync uses that pair. A user clock found ahead of the kernel's is no longer set back and held still by the monotonic guard; it keeps its reading and runs slower until the next resync. The first resync comes after 100 ms instead of 1 s.#!scriptsspawnandexecverefused every file that was not an ELF executable. A file that starts with#!now runs the interpreter named on its first line (#!interpreter [argument], at most 255 bytes).argv= interpreter, its argument, the script's canonical path, then the caller's arguments afterargv[0]. The process keeps the script's name./binand/usr/bin) and may be a script itself, four levels deep; a longer chain fails withLoop, a malformed line withBadExecutable./bin/axisalready takes a script path, so AXIS scripts can be started directly.docs/kernel.mdanddocs/syscall-abi.mdare updated; "#!interpreter scripts" leaves the "Not implemented yet" list.scripts/test.ps1: the expected number of exec checks goes from 13 to 14.How was it tested?
QEMU 8.2.2 (TCG) with OVMF on Linux, test image built from the same steps as
build.ps1 -NoZen -Cmdline 'test.exit boot.fast init.args=tests'.SMEP/SMAP (first commit):
main,-cpu qemu64,...(baseline)+smep,+smapThe boot log shows
cpu: SMEP on, SMAP on.Timeouts, repeated boots after the timeout commits; "clean" means 35 self-tests passed and 92 of 93 user checks passed with only the known failure below:
+smep,+smap+smep,+smap+smep,+smapBefore the timeout commits the sleep check failed in 6 of 6 four-CPU runs with
+smep,+smapin one session and in 1 of 6 one-CPU runs in another; intermediate versions still failed in 2 of 40 and 5 of 40 runs.Scripts, head of this branch, 1 CPU and 2 CPUs with
+smep,+smap: 35 self-tests passed, 93 of 94 user checks passed. The self-test "program loader" covers the line parser, the argument order, a missing interpreter, a script that interprets itself and a non-ELF interpreter. The new user checkexec: #! scripts run their interpreterruns AXIS scripts by path and by name and reads back the file they write; all 14 exec checks pass.Also run:
cargo test --locked -p os-std(52 passed), and the desktop session (init.args=desktop, 2 CPUs,+smep,+smap) for 70 s with PRISM composing frames and no fault in the serial log (before the script commit).Limitations or screenshots
maintoo:ipc: ordinary users cannot register services, take the display or read input. It may come from this Linux test image and is not addressed here.scripts/test.ps1itself (PowerShell, Windows host), the Win32, network and Zen suites, WHPX, real hardware. The timing changes were only measured under TCG.CR4bit is read back. SMAP is tested by a real fault.AT_EXECFNis the interpreter's path. AXIS ignores arguments after the script path, so a script cannot read its own arguments yet.Labels
Labels: security, kernel
🤖 Generated with Claude Code
https://claude.ai/code/session_01Wky8K1hAgSGYFWjvQ1gDqK