`deriving Inhabited` on private Extension handles enables type confusion in safe Lean
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
Read the four handle definitions named in src/Lean/Environment.lean, src/Lean/Elab/Command.lean, src/Lean/Meta/Sym/SymM.lean, and src/Lean/Meta/Tactic/Grind/Types.lean, then run the supplied reproductions with lean --trust=0. Check whether removing deriving Inhabited or using a safe sentinel preserves valid handle behavior and prevents the demonstrated cross type casts; both reproductions should stop producing the stated false results.
索引モデルが issue の本文から書いたものです。
説明
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”) - When reporting a perceived soundness bug: Does it affect
lake check --paranoid?
If in doubt, please reach out on [email protected] for responsible disclosure.
Description
Four typed handle structures in Lean 4 core use private mk :: to ensure that only their register* functions can allocate valid, unique slot indices (idx / id). These indices are used by getState, setState, and prevState, whose implementations rely on unsafeCast being applied to values of the expected type.
However, all four structures expose a public deriving Inhabited instance, which allows safe user code to construct default handles with idx = 0:
Lean.EnvExtension(src/Lean/Environment.lean:1549-1575)Lean.Elab.Command.StatefulLinter(src/Lean/Elab/Command.lean:98-115)Lean.Meta.Sym.SymExtension(src/Lean/Meta/Sym/SymM.lean:40-43)Lean.Meta.Grind.SolverExtension(src/Lean/Meta/Tactic/Grind/Types.lean:2466-2486)
Consequently, in pure safe Lean (without unsafe, sorry, @[extern], @[implemented_by], or native_decide), (default : EnvExtension A) and (default : EnvExtension B) share slot 0. Using one handle to write a value and the other to read it triggers an internal unsafeCast from A to B, allowing arbitrary cross-type value fabrication and potentially causing a native SIGSEGV.
Similarly, StatefulLinter.prevState (default : StatefulLinter σ Unit) #[default] can cast an internal dummy LinterState to an arbitrary [Inhabited σ] type, violating its type invariants.
Context
Discovered while auditing private mk + unsafeCast invariants across src/Lean/ on nightly-2026-12-02 (5e756ba2).
Steps to Reproduce
EnvExtensionpure-safeunsafeCast— paste directly intohttps://live.lean-lang.org/#project=lean-nightly(or runlean --trust=0 ReproEnvExt.lean):
import Lean.Environment
open Lean
structure BoxBool where
val : Bool
tag : Nat
deriving Inhabited
structure BoxTrue where
val : Bool
tag : Nat
h : val = true
instance : Inhabited BoxTrue := ⟨⟨true, 0, rfl⟩⟩
def forgedTrue : IO BoxTrue := do
let env ← mkEmptyEnvironment
let extA : EnvExtension BoxBool := default
let extB : EnvExtension BoxTrue := default
let env' := extA.setState env (BoxBool.mk false 0) (asyncMode := .local)
return extB.getState env' (asyncMode := .local)
theorem always_true (b : BoxTrue) : b.val = true := b.h
#eval do return (← forgedTrue).val
StatefulLinterpure-safe contradiction — paste directly intohttps://live.lean-lang.org/#project=lean-nightly(or runlean --trust=0 ReproLinter.lean):
import Lean.Elab.Command
open Lean.Elab.Command
abbrev TrueBool := { b : Bool // b = true }
instance : Inhabited TrueBool := ⟨⟨true, rfl⟩⟩
def forged : TrueBool :=
StatefulLinter.prevState (default : StatefulLinter TrueBool Unit) #[default]
theorem kernel_says_true : forged.val = true := forged.property
#eval forged.val
Expected behavior: Safe user code cannot fabricate typed registry handles via default (Inhabited) that alias slot 0 across distinct types σ.
Actual behavior: Both snippets compile cleanly with lean --trust=0 and #eval prints false, contradicting the kernel-proved val = true invariant (and casting Nat to a boxed type crashes native code with SIGSEGV).
Versions
"4.38.0-nightly-2026-12-02"
Lean (version 4.38.0-nightly-2026-12-02, x86_64-unknown-linux-gnu, commit 5e756ba2526bd88b18fa8b01588964fe848e0f91, Release)
Ubuntu 24.04.4 LTS (Linux 6.17.0-1022-azure x86_64)
Suggested fix
Remove public deriving Inhabited from EnvExtension, StatefulLinter, SymExtension, and SolverExtension (or replace with a sentinel/unregistered index that causes getState/setState/prevState to return mkInitial or panic safely instead of indexing slot 0 and calling unsafeCast on an unrelated extension's state).
- 主要言語
- Lean
- スター
- 10.5k
- フォーク
- 1.2k
- 平均マージ
- 19時間 36分
- マージ済み PR(30日)
- 323
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
leanprover/lean4 のほかの issue
-
P-medium
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
leanprover/lean4#16958 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
leanprover/lean4#16897 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
leanprover/lean4#16892 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1〜3時間 初心者へのやさしさ 86/100
leanprover/lean4#16886 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
leanprover/lean4#16820 ·
メンテナーはふだん 1 日以内に返信
leanprover/lean4 の issue をすべて見る
似ている issue
-
chat compiler P1
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
HerringtonDarkholme/hd-lang#2793 ·
メンテナーはふだん 1 日以内に返信
-
area-dart-model gardening
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 87/100
メンテナーはふだん 1 日以内に返信
-
`shift(x, y)` with a uint256 shift amount >= 2**255 shifts in the wrong direction対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 3 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 71/100
facebookexperimental/semcode#195 ·
メンテナーはふだん 2 日以内に返信