Repository navigation
Description
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
- 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
(You can also use the settings there to switch to “Lean nightly”)
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 assumptionFreeFalseCompiling 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
[-]Missing check apparently allows proof of False[/-][+]Missing check apparently allows proof of False after adversarial metaprogram[/+]nomeata commentedon Jul 22, 2026
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?
nomeata commentedon Jul 22, 2026
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?
hargoniX commentedon Jul 22, 2026
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.
phulin commentedon Jul 22, 2026
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 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.
nomeata commentedon Jul 22, 2026
Ah, right. So - for unrelated reasons - those following the "gold standard" for proof checking are not affected. That's good.
nomeata commentedon Jul 22, 2026
It also explains why my attempts at finding flaws did not turn this up: I was explicitly asking for a exported proof
nomeata commentedon Jul 22, 2026
Fix en route.
And just in case someone is curious (AI generated):
The bug's history:
fix: kernel to check opaque values for fvars (#14498)
fix: kernel to check opaque values for fvars (#14498)
fix: kernel to check opaque values for fvars (leanprover#14498)
fix: kernel to check opaque values for fvars (#14498)
Bump the pinned Lean toolchain to v4.32.2