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

Open
#16,893 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
64/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp
Domain
backend, security

Research direction

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.

Written by the indexing model from the issue text.

Description

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;
Dominant language
Lean
Stars
10.5k
Forks
1.2k
Avg merge
19h 36m
Merged PRs (30d)
323

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from leanprover/lean4

All issues in leanprover/lean4

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.