`lean_compacted_region_read` SIGSEGV when `data_size < sizeof(object_offset)` and `num_closure_offsets == 0`

Aperta Adatta ai principianti
#16,892 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à
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c
Ambito
backend, security

Direzione di ricerca

Leggi la convalida del framing delle sezioni in src/library/module.cpp, intorno alle righe 600–626, in particolare i controlli di lean_compacted_region_read per data_size e num_closure_offsets. Riproduci il crash con il caso fornito TruncatedDataSize.lean, poi verifica che il reader rifiuti i dati più piccoli di un object_offset con un IO.Error invece di andare in crash.

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

Descrizione

bug
Prerequisites
Description

PR #16737 (fix: report truncated .olean files as errors) added section-framing validation in lean_compacted_region_read (src/library/module.cpp:600–626) to reject truncated .olean files cleanly instead of reading out of bounds.

However, lean_compacted_region_read checks data_size > static_cast<size_t>(end - p) (line 609), while the lower-bound check data_size < sizeof(void*) only appears inside if (num_closure_offsets > 0) (line 622).

When num_closure_offsets == 0 and data_size < sizeof(object_offset) (for example data_size = 0 or 4), framing validation succeeds and region_reader::read() immediately dereferences an 8-byte root object_offset at m_begin, reading past the data section / buffer and crashing with SIGSEGV.

Context

Found while reviewing the .olean section-framing check introduced in PR #16737.

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

unsafe def main : IO Unit := do
  IO.FS.withTempFile fun _ file => do
    let _ ← CompactedRegion.save file `EdgeTest ("hello" : String) #[] none false
    let bytes ← IO.FS.readBinFile file
    -- Keep 88-byte header, set data_size = 4 (< sizeof(object_offset) = 8), num_closure_offsets = 0
    let mut bad := bytes.extract 0 88
    for b in [4, 0, 0, 0, 0, 0, 0, 0] do bad := bad.push b.toUInt8
    for b in [0xef, 0xbe, 0xad, 0xde] do bad := bad.push b.toUInt8
    for _ in [0:4] do bad := bad.push 0
    IO.FS.writeBinFile file bad
    let (s, _) ← CompactedRegion.read (α := String) file #[]
    IO.println s.length
  1. Run with Lean nightly:
lean --run TruncatedDataSize.lean

Expected behavior: CompactedRegion.read rejects the file with an IO.Error reporting that the compacted region data section is too small / truncated (data_size < sizeof(object_offset)), consistent with PR #16737.

Actual behavior: lean crashes with Segmentation fault (core dumped) (exit code 139).

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 2-line fix in src/library/module.cpp right after if (data_size > static_cast<size_t>(end - p)):

if (data_size < sizeof(object_offset))
    throw exception("compacted region data section too small");
Lingua principale
Lean
Stelle
10.5k
Fork
1.2k
Merge medio
17h 55m
PR unite (30g)
356

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.