← Back to Pull requests

P2.5: put user data below user code so sysretq loads the right SS #147

Closed
unknown closed this pull request without merging on Oct 02, 2026 00:13 UTC
View on GitHub
unknown unknown commented on Oct 01, 2026 23:35 UTC
Author

The GDT put user code (0x2B) before user data (0x33). sysretq loads SS = STAR[63:48] + 8 and CS = STAR[63:48] + 16, so SS became 0x23, which is the upper half of the TSS descriptor. Nothing executes sysretq yet, so the bug is latent. Once ring 3 runs, the first iretq back to it would take #GP. This blocks P2.5 (#60).

GDT layout


  • Order: user data is now appended before user code: null, kcode 0x08, kdata 0x10, TSS 0x18/0x20, udata 0x2B, ucode 0x33. STAR's sysret base is 0x23, so sysretq loads SS 0x2B and CS 0x33.

  • Selectors::user_data: the field is used now, so its #[allow(dead_code)] is gone.
  • syscall::init


  • STAR: written with x86_64's Star::write, which checks both 8-byte offsets and the privilege levels. If the layout is wrong, the kernel panics at boot instead of faulting on the first return to ring 3:

  •   Star::write(sel.user_code, sel.user_data, sel.code, sel.data)
          .expect("GDT layout is incompatible with syscall/sysret");
      LStar::write(VirtAddr::from_ptr(syscall_entry as *const ()));
      SFMask::write(RFlags::INTERRUPT_FLAG | RFlags::DIRECTION_FLAG);
      

  • LSTAR and SFMASK: they use the crate's typed writers too, so all three MSRs are programmed the same way. syscall::init no longer has an unsafe block; CR4 setup keeps its own. The raw IA32_ and RFLAGS_DF constants are removed.
  • Tests (kernel/src/test.rs)


  • star_matches_gdt_layout_for_sysret: checks user_data + 8 == user_code and code + 8 == data, and that Star::read() decodes back to the GDT selectors. With the old descriptor order restored, boot panics with SysretOffset.

  • syscall_entry_msrs_are_programmed: checks that LSTAR points into the higher half and that SFMASK is exactly IF | DF.
  • Docs


  • CHANGELOG.md: new Fixed* entry.

  • docs/PLAN.md: #140 is no longer listed as blocking the P2.5 row in §8 or under open bugs in §11. The P2.5 row now also notes that EFER.SCE is never set, which is still open P2.5 work and not touched here.
  • task: P2.5

    Diff: 259 lines (+194 / -65) across 3 files Reviewer: garrickdabbs Review duration: 38m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.39) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.39 · 259 lines diff across 3 files
    0.39
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.333
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #147"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 39.2% (MEDIUM) · Higher = larger blast radius
    39.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17.0% · Safe to merge
    17.0%