Skip to content

fix: declaration.h getters call get_small_value() without is_small() guard (13 locations) #13616

Closed

Description

@trackoor

This issue documents a class of unsafe narrowing conversions in the kernel's declaration metadata accessors. Found during a systematic security audit.

Prerequisites

Description

Summary

All 13 getter methods in src/kernel/declaration.h (and 1 in local_ctx.h) call nat::get_small_value() without first checking nat::is_small(). In release builds (where lean_assert is compiled out), if a non-scalar (heap-allocated bignum) nat value reaches these getters, they silently return garbage.

Affected locations

src/kernel/declaration.h:

Line Class Method
300 inductive_val get_nparams()
301 inductive_val get_nindices()
305 inductive_val get_nnested()
328 constructor_val get_cidx()
329 constructor_val get_nparams()
330 constructor_val get_nfields()
348 recursor_rule get_nfields()
378 recursor_val get_nparams()
379 recursor_val get_nindices()
380 recursor_val get_nmotives()
381 recursor_val get_nminors()
382 recursor_val get_major_idx() (sums 4 unguarded calls)

src/kernel/local_ctx.h:

Line Class Method
36 local_decl get_idx()

Root cause

In src/util/nat.h:34-35:

bool is_small() const { return is_scalar(raw()); }
size_t get_small_value() const { lean_assert(is_small()); return unbox(raw()); }

The internal lean_assert(is_small()) is compiled out in release. If the stored nat is a boxed bignum (heap pointer), unbox(raw()) shifts the pointer right by 1 — producing garbage. That garbage is then truncated to unsigned and used as:

  • Array indices (args[nparams + idx] in type_checker.cpp:371)
  • Loop bounds (for (i = 0; i < get_nparams(); i++) in type_checker.cpp:242)
  • Equality checks (args.size() != get_nparams() + get_nindices() in type_checker.cpp:237)

Contrast with correct pattern

type_checker.cpp uses the correct pattern for proj_idx in 3 places (lines 223, 378, 1015):

if (!proj_idx(e).is_small())
    throw exception(...);
unsigned idx = proj_idx(e).get_small_value();

The declaration.h getters never received this treatment.

Why the existing guard is insufficient

Even where is_small() IS checked (e.g., inductive.cpp:165-167), the check is wrong:

  • is_small() guarantees < 2^63 (on 64-bit)
  • unsigned can only hold < 2^32
  • Values in [2^32, 2^63) pass is_small() but silently truncate

Steps to Reproduce

The getters are normally safe because inductive_val/constructor_val/recursor_val objects are constructed from validated unsigned values. However, if a crafted .olean file injects a declaration with non-small nat fields, the getters would produce garbage in release builds.

The underlying structural issue is the inconsistency between:

  1. type_checker.cpp which guards with is_small() + exception
  2. declaration.h which guards with nothing (trusts construction provenance)
  3. The is_small() check itself which checks the wrong bound (2^63 vs 2^32)

Suggested Fix

Two complementary approaches:

Option A (defense-in-depth): Add is_small() checks to all 13 getters, throwing kernel_exception on failure. This mirrors the proj_idx pattern in type_checker.cpp.

Option B (correct the bound): Replace is_small() in the kernel's truncation patterns with an explicit comparison <= UINT32_MAX, since is_small() verifies < 2^63 but the storage type is unsigned (32-bit).

Ideally do both. The currently-vacant is_small() check in the getters' internal get_small_value() should also be converted from lean_assert (debug-only) to lean_always_assert (always active).

Versions

  • Lean 4 (current master as of 2026-05-02)
  • All platforms (but worse on 64-bit because size_t is 64-bit while unsigned is 32-bit)

Impact

Medium-High. In normal operation, these getters are safe because all declaration metadata originates from validated unsigned values. However, via a crafted .olean file or memory corruption, garbage values would propagate through the kernel causing out-of-bounds array access. This is a defense-in-depth gap: the codebase has the correct pattern (is_small() + guard) in one place but fails to apply it in 13 other places.

Activity

  1. nomeata commented on May 3, 2026

    @nomeata
    Collaborator

    Closing this as unhelpful AI generated. We know about the truncation is the code, and the single existing issue suffices. Also, an issue report is not a fix:. Please involve humans in issue reporting, and discuss on the Zulip if you are new to Lean.

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