Skip to content

Security: defend against unsafeCast and elaboration-time attacks#19

Merged
GasStationManager merged 4 commits intomainfrom
pr7-rebase
Mar 6, 2026
Merged

Security: defend against unsafeCast and elaboration-time attacks#19
GasStationManager merged 4 commits intomainfrom
pr7-rebase

Conversation

@GasStationManager
Copy link
Owner

Follow-up security hardening after #18 (merged).

Attacks defended against

1. Import superset check (commit bea2fbd)

Submissions must import at least everything the target imports. Prevents redefining types by omitting imports (e.g. def FermatLastTheorem := True).

2. Nat literal validation (commits 73196d3, f06ecb5)

Recursively scans all sub-expressions of new declarations for corrupted Nat literals (stored natVal renders as negative). Catches unsafeCast + run_elab patterns that launder corrupted values through compile-time evaluation.

3. Level object rebuild (commit 5cafc55)

Deep-copies all Level nodes (.sort, .const level lists) before kernel replay. Catches unsafeCast on Level objects that create definitions with undeclared universe parameters (e.g. the magic/isProp attack proving False via universe inconsistency).

Test results (11 cases)

# Test Expected Result
1 Prelude attack Reject ❌ no imports
2 Missing Mathlib import Reject ❌ missing imports
3 unsafeCast Nat attack Reject ❌ suspicious Nat literal
4 Benign helper def Pass
5 Helper lemma Pass
6 Large valid Nat (10^18) Pass
7 Structure with Nats Pass
8 Wrong theorem type Reject ❌ sorryAx
9 Missing declaration Reject ❌ not found
10 Matching def+theorem Pass
11 Level unsafeCast attack Reject ❌ undefined level param

user added 4 commits March 6, 2026 19:08
For submission definitions not present in the target, add a Nat literal
sanity check: if a stored natVal renders as a negative numeral, reject.
This catches unsafeCast/run_elab corruption patterns (e.g. badNat exploit)
while still allowing normal helper definitions.
Scan all sub-expressions (not just top-level values) of new declarations
for corrupted Nat literals. Catches unsafeCast corruption hidden inside
lists, structures, pairs, or any compound type.
Add rebuildLevel to deep-copy Level nodes (zero, succ, max, imax, param, mvar).
Update rebuildExpr to rebuild levels in .sort and .const expressions.

This catches attacks that use unsafeCast on Level objects to create
definitions with undeclared universe parameters (e.g., the magic/isProp
attack that proves False via universe-polymorphism inconsistency).

The kernel rejects the rebuilt expressions because free level params
become visible after breaking compacted-region references.
…recursors

Scan types of inductInfo, ctorInfo, and recInfo for corrupted Nat literals,
not just defnInfo/thmInfo/opaqueInfo.
@GasStationManager GasStationManager merged commit f26870b into main Mar 6, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant