`region_reader::sort_and_validate_dep_regions` `base_addr + size` overflow bypasses region-overlap validation

Aperta
#16,893 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
64/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp
Ambito
backend, security

Direzione di ricerca

Start by reading region_reader::sort_and_validate_dep_regions in src/runtime/compact.cpp around lines 500–516, then inspect fix_object_ptr as the issue suggests. Reproduce with the supplied RegionWrap.lean program and lean --run RegionWrap.lean. Done means overflowing region endpoints are rejected with a clear error before interval checks or pointer resolution.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug
Prerequisites
  • Check that your issue is not already filed:
    https://github.com/leanprover/lean4/issues
  • Reduce the issue to a minimal, self-contained, reproducible test case.
    Avoid dependencies to Mathlib or Batteries.
  • Test your test case against the latest nightly release, for example on
    https://live.lean-lang.org/#project=lean-nightly
    (You can also use the settings there to switch to “Lean nightly”)
  • When reporting a perceived soundness bug: Does it affect lake check --paranoid?
    If in doubt, please reach out on [email protected] for responsible disclosure.
Description

region_reader::sort_and_validate_dep_regions (src/runtime/compact.cpp:500–516) validates that neither dependency regions nor the reader's own region have overlapping [base_addr, base_addr + size) intervals before fix_object_ptr resolves cross-region pointers.

However, all endpoint computations (reinterpret_cast<size_t>(prev.base_addr) + prev.size, self_base + m_size, and dep_base + dep.size) use unchecked unsigned size_t addition. When base_addr + size wraps modulo $2^{64}$ (for example base_addr = 2^64 - 160 on a 70 KB region), self_end wraps to a small address (~69,840), so a dependency region at 0x1000 (4096) satisfies dep_base < self_end only on the wrapped tail or fails self_base < dep_end, bypassing the overlap/bounds check and subsequently aborting with 'unreachable' code was reached inside fix_object_ptr.

Context

Discovered while auditing region_reader bounds and interval checks in src/runtime/compact.cpp.

Steps to Reproduce
  1. Save the following self-contained file as RegionWrap.lean:
import Lean
open Lean

def setUInt64LE (bs : ByteArray) (pos v : Nat) : ByteArray := Id.run do
  let mut bs := bs
  for i in [0:8] do bs := bs.set! (pos + i) (v >>> (8 * i)).toUInt8
  return bs

def relocateSelfRoot (bs : ByteArray) (newBaseAddr : Nat) : ByteArray :=
  let bs := setUInt64LE bs 80 newBaseAddr
  setUInt64LE bs 96 ((newBaseAddr + 96 + 8) % (2^64))

unsafe def main : IO Unit := do
  IO.FS.withTempFile fun _ f1 =>
  IO.FS.withTempFile fun _ f2 => do
    let _ ← CompactedRegion.save f1 `DepMod ("dep" : String) #[] none false
    let _ ← CompactedRegion.save f2 `SelfMod (String.ofList (List.replicate 70000 'x')) #[] none false
    IO.FS.writeBinFile f1 (relocateSelfRoot (← IO.FS.readBinFile f1) 0x1000)
    let (_, reg1) ← CompactedRegion.read (α := String) f1 #[]
    IO.FS.writeBinFile f2 (relocateSelfRoot (← IO.FS.readBinFile f2) (2^64 - 160))
    let _ ← CompactedRegion.read (α := String) f2 #[reg1]
  1. Run with Lean nightly:
lean --run RegionWrap.lean

Expected behavior: sort_and_validate_dep_regions rejects regions where size > SIZE_MAX - base_addr with a clean region_reader overflow error before comparing intervals or resolving pointers.

Actual behavior: sort_and_validate_dep_regions does not detect the wrapped [base_addr, base_addr + size) range, and fix_object_ptr later fails with uncaught exception: failed to read '...': 'unreachable' code was reached.

Versions
Lean (version 4.38.0-nightly-2026-11-26, x86_64-unknown-linux-gnu, commit e1228b8b45918ece1a00d68e5fc5e6a685ca59ee, Release)
Lean (version 4.36.0, x86_64-unknown-linux-gnu, commit 611c117bfaf7b7ae1cfcf246ce6fba31e8f509e9, Release)
Ubuntu 24.04.4 LTS (Linux 6.17.0-1022-azure x86_64)
Additional Information

Suggested fix in src/runtime/compact.cpp (region_reader::sort_and_validate_dep_regions): check m_size > std::numeric_limits<size_t>::max() - self_base and dep.size > std::numeric_limits<size_t>::max() - dep_base before computing self_base + m_size, prev.base_addr + prev.size, and dep_base + dep.size (and similarly guard self_base + m_size in fix_object_ptr).

diff --git a/src/runtime/compact.cpp b/src/runtime/compact.cpp
index ed68cbb1..c081a0a8 100644
--- a/src/runtime/compact.cpp
+++ b/src/runtime/compact.cpp
@@ -497,6 +497,15 @@ void region_reader::sort_and_validate_dep_regions() {
     // by binary-searching `base_addr`s, and would silently translate via the wrong region if
     // two deps overlap (or if a dep overlaps our own range). This should not happen with regular
     // .olean use as we use only use `read`'s `prev` instead of `dep_regions` there.
+    size_t self_base = reinterpret_cast<size_t>(m_base_addr);
+    if (m_size > std::numeric_limits<size_t>::max() - self_base)
+        throw exception("region_reader: own region `base_addr + size` overflows address space");
+    size_t self_end = self_base + m_size;
+    for (region_view const & dep : m_dep_regions) {
+        size_t dep_base = reinterpret_cast<size_t>(dep.base_addr);
+        if (dep.size > std::numeric_limits<size_t>::max() - dep_base)
+            throw exception("region_reader: dep region `base_addr + size` overflows address space");
+    }
     for (size_t i = 1; i < m_dep_regions.size(); i++) {
         region_view const & prev = m_dep_regions[i - 1];
         region_view const & curr = m_dep_regions[i];
@@ -505,8 +514,6 @@ void region_reader::sort_and_validate_dep_regions() {
             throw exception("region_reader: dep regions have overlapping `base_addr` ranges");
         }
     }
-    size_t self_base = reinterpret_cast<size_t>(m_base_addr);
-    size_t self_end = self_base + m_size;
     for (region_view const & dep : m_dep_regions) {
         size_t dep_base = reinterpret_cast<size_t>(dep.base_addr);
         size_t dep_end = dep_base + dep.size;
Lingua principale
Lean
Stelle
10.5k
Fork
1.2k
Merge medio
19h 36m
PR unite (30g)
323

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leanprover/lean4

Tutte le issue di leanprover/lean4

Issue simili

Altre issue su Backend & API Design

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.