Skip to content

kernel: SMEP and SMAP, exact timeouts, #! scripts - #116

Merged
v3nn7 merged 3 commits into
mainfrom
v3nn7/practical-euler-ot7vt8
Nov 11, 2026
Merged

v3nn7 merged 3 commits into
mainfrom
v3nn7/practical-euler-ot7vt8

Conversation

@v3nn7

@v3nn7 v3nn7 commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

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_protection sets CR4.SMEP and CR4.SMAP on the boot CPU and on each AP.
  • The fixup.rs routines (probe_*, copy_from_user, copy_to_user) are the only kernel code that touches user memory. They now open an access window with stac and close it with clac on both the normal and the recovery path. The instructions are skipped on CPUs without SMAP.
  • The interrupt entry clears RFLAGS.AC. SYSCALL already masked it, so user code cannot hand the kernel an open window.
  • A kernel page 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 with the faulting address.
  • New self-test "supervisor protection (SMEP, SMAP)": maps a user page in its own address space, checks that the copy routines reach it, that a plain kernel read faults under SMAP, and that RFLAGS.AC is clear afterwards.
  • docs/kernel.md describes the mechanism.

Timeouts that ended early

The user checks sleep blocks for the requested time, futex: value mismatch and timeout and Condvar: wait_timeout expires failed now and then (a 50 ms sleep measured 48.6 ms). This also happens on main. Causes, all in how the tick count, the kernel clock and the user-space TSC clock follow each other:

  • record_tick added 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_ns interpolated 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.
  • Sleep, futex and wait-queue deadlines were counted from a tick count that could be behind. They start from the new ticks_now(), which rounds the same way as record_tick.
  • The last tick and its TSC reading were two separate atomics. They are read under a sequence counter.
  • os-sys re-anchored its clock with RDTSC followed by a CLOCK call; a preemption between the two shifted the clock. TIME_CALIB now 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.

#! scripts

spawn and execve refused 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).

  • The interpreter gets argv = interpreter, its argument, the script's canonical path, then the caller's arguments after argv[0]. The process keeps the script's name.
  • The interpreter is found like any program (a bare name is searched in /bin and /usr/bin) and may be a script itself, four levels deep; a longer chain fails with Loop, a malformed line with BadExecutable.
  • /bin/axis already takes a script path, so AXIS scripts can be started directly.
  • docs/kernel.md and docs/syscall-abi.md are 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):

Run Kernel self-tests User checks
main, -cpu qemu64,... (baseline) 34 passed, 0 failed 92 passed, 1 failed
first commit, same CPU (no SMEP/SMAP) 35 passed, 0 failed 92 passed, 1 failed
first commit, +smep,+smap 35 passed, 0 failed 92 passed, 1 failed

The 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:

Configuration Clean runs
1 CPU, +smep,+smap 20 of 20
2 CPUs, +smep,+smap 20 of 20
4 CPUs, +smep,+smap 20 of 20
4 CPUs, default CPU model 10 of 10

Before the timeout commits the sleep check failed in 6 of 6 four-CPU runs with +smep,+smap in 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 check exec: #! scripts run their interpreter runs 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

  • One user check fails in every run, on main too: 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.
  • Not run: scripts/test.ps1 itself (PowerShell, Windows host), the Win32, network and Zen suites, WHPX, real hardware. The timing changes were only measured under TCG.
  • SMEP has no fault-injection test; only the CR4 bit is read back. SMAP is tested by a real fault.
  • A user clock that is ahead of the kernel's now slews (at most to half rate until the next resync) instead of stopping.
  • Scripts: AT_EXECFN is 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

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
@v3nn7 v3nn7 added kernel Area: kernel security Area: security labels Nov 11, 2026 — with Claude
@coderabbitai

coderabbitai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

Pre-merge checks: Inconclusive 2

Failed Failed checks: 2 inconclusive

Check name Status Explanation Resolution
Title check Pending This assessment has not completed. The check will be retried on the next pre-merge check run.
Description check Pending This assessment has not completed. The check will be retried on the next pre-merge check run.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

… 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
@codspeed

codspeed Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 10.28%

2 warnings

⚠️ 2 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 2 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

⚡ 2 improved benchmarks
✅ 34 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ tcp_build_segment_1460 8.9 µs 8 µs +10.55%
⚡ ipv4_build_1500 8.3 µs 7.5 µs +10.01%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing v3nn7/practical-euler-ot7vt8 (f52861a) with main (7745b7a)

Open in CodSpeed

… 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
@v3nn7 v3nn7 changed the title kernel: enable SMEP and SMAP kernel: enable SMEP and SMAP; timeouts no longer end early Nov 11, 2026
@github-actions github-actions Bot added the bug Something isn't working label Nov 11, 2026
@v3nn7 v3nn7 removed the bug Something isn't working label Nov 11, 2026 — with Claude
@v3nn7
v3nn7 marked this pull request as ready for review November 11, 2026 23:45
@v3nn7
v3nn7 merged commit 9e08ce6 into main Nov 11, 2026
7 checks passed
@v3nn7 v3nn7 changed the title kernel: enable SMEP and SMAP; timeouts no longer end early kernel: SMEP and SMAP, exact timeouts, #! scripts Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kernel Area: kernel security Area: security

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants