Skip to content

Missing check apparently allows proof of False after adversarial metaprogram #14484

Closed

Description

@phulin

A missing check on opaque declarations creates a situation in which an adversarial metaprogram can allow a proof of False.

This report was largely generated with AI, and so I apologize if I have missed something. I have endeavored to check everything I can of the results. I hope that this form of checking of the Lean kernel is valuable to the maintainers.

Prerequisites

Description

The checked kernel path for Declaration.opaqueDecl does not reject free or
metavariables in the declaration body. In combination with the kernel type
checker's inference cache, this allows a safe metaprogram to add an opaque
constant of type False whose body is an unbound free variable.

The example below uses the ordinary checked Lean.addDeclCore path. It does
not use unsafe, unsafeCast, sorry, axioms, debug.skipKernelTC,
addDeclWithoutChecking, FFI, or a modified .olean file.

Context

No apparent context.

Steps to Reproduce

import Lean
open Lean

private def cachePrimingType : Expr :=
  let falseE := mkConst ``False
  let falseToFalse := .forallE `h falseE falseE .default
  let identity := .lam `h falseE (.bvar 0) .default
  .app (.lam `_ falseToFalse falseE .default) identity

run_meta
  let name := `opaqueFVarCacheFalse
  addDecl <| .opaqueDecl {
    name
    levelParams := []
    type := cachePrimingType
    value := .fvar { name := .num `_kernel_fresh 2 }
    isUnsafe := false
  }
  logInfo m!"kernel accepted {name} : False"

theorem assumptionFreeFalse : False := opaqueFVarCacheFalse

#print axioms opaqueFVarCacheFalse
#print axioms assumptionFreeFalse

Compiling this file succeeds and prints:

kernel accepted opaqueFVarCacheFalse : False
'opaqueFVarCacheFalse' does not depend on any axioms
'assumptionFreeFalse' does not depend on any axioms

I also emitted an .olean file and replayed it with:

leanchecker --fresh OpaqueFVarCacheFalse

Fresh replay exits successfully.

Expected behavior

The checked declaration addition should fail with
Kernel.Exception.declHasFVars because the value of opaqueFVarCacheFalse
contains an unbound free variable.

Actual behavior

The kernel accepts the opaque declaration. An ordinary theorem can then use
the new constant to prove False, and both declarations report no axiom
dependencies.

Versions

live.lean.org nightly:
Lean 4.34.0, commit 3259610
Target: x86_64-unknown-linux-gnu

Additional Information

environment::add_opaque checks the OpaqueVal signature and type-checks its
value, but does not perform the closure check used for the other declaration
kinds with bodies:

environment environment::add_opaque(declaration const & d, bool check) const {
    scoped_diagnostics diag(*this, check);
    opaque_val const & v = d.to_opaque_val();
    if (check) {
        type_checker checker(*this, diag.get());
        check_constant_val(*this, v.to_constant_val(), checker);
        expr val_type = checker.check(v.get_value(), v.get_lparams());
        // ...
    }
}

The declared type in the reproducer is a closed beta redex definitionally
equal to False. While checking it, the kernel creates the temporary local
declaration _kernel_fresh.2 : False and caches its inferred type. The local
context is restored afterward, but the inference cache remains in the same
type_checker instance.

The opaque value is an Expr.fvar with that identifier. In
infer_type_core, the inference-cache lookup occurs before the FVar branch
checks membership in the current local context. The stale cache entry therefore
returns False, and the now-unbound variable is not rejected.

Suggested fix

The minimal fix appears to be making add_opaque apply the same closure check
as definitions and theorems before checking the value:

check_constant_val(*this, v.to_constant_val(), checker);
check_no_metavar_no_fvar(*this, v.get_name(), v.get_value());
expr val_type = checker.check(v.get_value(), v.get_lparams());

This should only reject malformed opaque declaration bodies: a valid
environment declaration cannot contain free or metavariables in its body.

As defense in depth, an inference-cache hit for an FVar could also verify
that the identifier is present in the current local context, or cache entries
depending on temporary free variables could be scoped to that context.

Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Activity

  1. added
    bug
    on Jul 22, 2026
  2. changed the title [-]Missing check apparently allows proof of False[/-] [+]Missing check apparently allows proof of False after adversarial metaprogram[/+] on Jul 22, 2026
  3. nomeata commented on Jul 22, 2026

    @nomeata
    Collaborator

    Good find! Can this even fool comparator? Or put differently, can you write a lean4export file that is able to prime the cache like this?

  4. nomeata commented on Jul 22, 2026

    @nomeata
    Collaborator

    Also I'm surprised that our previous AI checks didn't find this. Can you share more on which AI models you are using, and how you set them to the task?

  5. hargoniX commented on Jul 22, 2026

    @hargoniX
    Member

    Note that in order to fool comparator/export format based tools in general you will have to generate Expr's that are fvar/mvar free as the export format is not even capable of denoting an fvar.

  6. phulin commented on Jul 22, 2026

    @phulin
    Author

    That corresponds with my understanding (so this issue cannot appear in an export file).

    I used GPT-5.6 Sol at High thinking in Codex's /goal mode. I can't say my prompts were terribly sophisticated. Here was (essentially) the core prompt:

    i'd like to audit the lean kernel. see if you can find an assumption-free proof of false. your primary
    task should be reading the source code and docs and building an understanding of how the kernel
    works to find the seams. treat this like a cybersecurity task.
    

    I rejected the LLM's first two candidates (one was not a bug, the other involved such a strange initialization setup that I was not sure). It found this issue after about 3.5 hours of work.

  7. nomeata commented on Jul 22, 2026

    @nomeata
    Collaborator

    Ah, right. So - for unrelated reasons - those following the "gold standard" for proof checking are not affected. That's good.

  8. nomeata commented on Jul 22, 2026

    @nomeata
    Collaborator

    It also explains why my attempts at finding flaws did not turn this up: I was explicitly asking for a exported proof

  9. nomeata commented on Jul 22, 2026

    @nomeata
    Collaborator

    Fix en route.

    And just in case someone is curious (AI generated):

    The bug's history:

    1. 2019-03-15 (20ba4c4, "feat(kernel): opaque constants") — add_opaque is introduced. At this point, no declaration kind checks for stray metavariables/fvars in its value — add_definition and add_theorem don't have the check either, so add_opaque isn't an outlier yet.
    2. 2020-12-24 (17114cc, "feat: make sure kernel generates KernelException.declHasMVars instead of KernelException.other") — Leonardo de Moura adds check_no_metavar_no_fvar(...) calls to add_definition (both the unsafe and safe branches), add_theorem, and add_mutual. add_opaque is skipped — almost certainly an oversight, since its code is structurally near-identical to add_theorem (same shape, sits right next to it in the file) but simply wasn't touched by that patch.
    3. 2026-07-22 (your commit 981f633) — the omission is finally fixed, closing the soundness hole reported in Missing check apparently allows proof of False after adversarial metaprogram #14484.
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions