Repository navigation
Description
This issue documents a soundness vulnerability in the .olean import path, discovered during a systematic security audit of the Lean 4 kernel.
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
Description
Summary
.olean files, when loaded via import, have zero payload integrity protection and their deserialized declarations are placed directly into the kernel environment without re-typechecking. This means a crafted .olean file can inject arbitrary declarations—including those proving False—into the kernel.
Root causes
-
No payload integrity:
.oleanfiles have only a 5-byte magic marker ("olean"), a version byte, and a flags byte as header validation. The git hash check (LEAN_CHECK_OLEAN_VERSION) is not enabled by default. There is no checksum, cryptographic hash, or digital signature of the data payload. -
No re-typechecking on import: In
Environment.lean:finalizeImport(lines ~2304-2314), constants from deserializedModuleDataare placed directly into the kernel environment'sconstantsfield viaSMap.fromHashMap. The kernel type checker is never called for imported declarations. -
isUnsafebypass: In the replay path (Environment.lean:2539-2544), declarations markedisUnsafeare added viaadd_core(C++environment.cpp:144), which performs zero type checking.
Impact
Any entity that can place a crafted .olean file where Lean's importer finds it can:
- Inject an axiom proving
False - Inject definitional equality rules that conflict with the existing environment
- Corrupt the
ConstMapcausing the kernel to use wrong types for existing constants
This is exploitable through:
- Dependency supply-chain attacks: A malicious package in a monorepo or dependency
- CI cache poisoning: Placing a crafted
.oleanin a CI build cache - Shared filesystem attacks: In multi-user environments
Steps to Reproduce
The most direct POC uses the isUnsafe bypass. Create a .lean file:
import Lean
open Lean
-- This declaration is marked isUnsafe, so when it's added to the environment,
-- addDeclCore skips type checking entirely (Environment.lean:2539-2544)
unsafe axiom falseAxiom : False
-- The kernel accepted this because isUnsafe declarations use add_core
-- (environment.cpp:144) which does NO checking
-- In a real attack, this would be in a compiled .olean file loaded by importFor the binary .olean attack, the attacker crafts a file containing serialized ConstantInfo objects (using the compacted region format from src/runtime/compact.cpp). The file header only needs:
- Magic:
"olean"(5 bytes) - Version:
2(1 byte) - Flags: matching the build's GMP setting (1 byte)
- Version string: any 33 bytes (never checked on read)
Concrete exploit sketch
The compacted_region::read() deserializer has additional memory corruption vectors:
- Constructor
m_cs_sz/m_othermismatch (compact.cpp:420-429): In release builds wherelean_assertis compiled out, a constructor withm_cs_sz=8(tiny) andm_other=255(huge num_objs) causes 2040 bytes of pointer fixups to be written past the object boundary — type confusion. - Array
m_size > m_capacity(compact.cpp:431-438): Loop writes past allocated slots fix_mpzunchecked overflow (compact.cpp:460-469): The deserialization path lackslean_usize_mul_checked(unlike the serialization path at lines 290-291), allowing size overflow
All protective checks in compacted_region::read() are lean_assert — compiled out in release builds.
Suggested Fix
- Add payload integrity: Compute a cryptographic hash (e.g., BLAKE3) over the compacted region data and store it in the .olean header. Verify on read.
- Re-typecheck imports: Run imported declarations through the kernel type checker during
finalizeImport, or at minimum verify that their types are well-formed. - Remove
isUnsafekernel bypass: Unsafe declarations should still have their types checked by the kernel. Only the value of unsafe definitions should be allowed to be partial/nonterminating — not the type. - Add a trust-level check: Make
LEAN_BELIEVER_TRUST_LEVEL(currently documented but unused atenvironment.h:23) actually gate whether re-typechecking is required. Set it to require checking by default.
Versions
- Lean 4 (current master as of 2026-05-02)
- All platforms
Impact
High. This can lead to proving False in any Lean project that imports a compromised dependency.
Activity
nomeata commentedon May 3, 2026
This report is clearly AI generated, and just describes the design of Lean in misleading terms. Also it is almost insulting to assume that the Lean community doesn't know it's system well enough that this report brings any new insights. Please do not paste GenAI reports into our issue tracker without human oversight.
(Oleans are mmapped and trusted by design; dishonest use of oleans is not a supported workflow, see https://lean-lang.org/doc/reference/latest/ValidatingProofs/#validating-comparator how do deal with possibly malicious proofs.)