Skip to content

fix: .olean files lack integrity checks and bypass kernel type checking on import #13615

Closed

Description

@trackoor

This issue documents a soundness vulnerability in the .olean import path, discovered during a systematic security audit of the Lean 4 kernel.

Prerequisites

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

  1. No payload integrity: .olean files 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.

  2. No re-typechecking on import: In Environment.lean:finalizeImport (lines ~2304-2314), constants from deserialized ModuleData are placed directly into the kernel environment's constants field via SMap.fromHashMap. The kernel type checker is never called for imported declarations.

  3. isUnsafe bypass: In the replay path (Environment.lean:2539-2544), declarations marked isUnsafe are added via add_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:

  1. Inject an axiom proving False
  2. Inject definitional equality rules that conflict with the existing environment
  3. Corrupt the ConstMap causing 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 .olean in 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 import

For 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_other mismatch (compact.cpp:420-429): In release builds where lean_assert is compiled out, a constructor with m_cs_sz=8 (tiny) and m_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_mpz unchecked overflow (compact.cpp:460-469): The deserialization path lacks lean_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

  1. 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.
  2. Re-typecheck imports: Run imported declarations through the kernel type checker during finalizeImport, or at minimum verify that their types are well-formed.
  3. Remove isUnsafe kernel 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.
  4. Add a trust-level check: Make LEAN_BELIEVER_TRUST_LEVEL (currently documented but unused at environment.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

  1. nomeata commented on May 3, 2026

    @nomeata
    Collaborator

    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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions