Skip to content

fix: nparams + idx unsigned wraparound in reduce_proj_core #13618

Closed as duplicate

Description

@trackoor

Prerequisites

Description

In src/kernel/type_checker.cpp:369-371, the bounds check in reduce_proj_core can be bypassed via unsigned integer wraparound:

unsigned nparams = mk_info.to_constructor_val().get_nparams();  
if (nparams + idx < args.size())  // UNSIGNED WRAP: if idx near UINT_MAX, sum wraps to small value
    return some_expr(args[nparams + idx]);  // indexes wrong element

On 64-bit systems, unsigned is 32-bit (UINT_MAX = 2^32 - 1 ≈ 4.29 × 10^9). If idx is near UINT_MAX (crafted via a Proj or via corrupted metadata), nparams + idx wraps to a value < args.size(). The check passes but args[nparams + idx] reads the wrong element — kernel extracts the wrong struct field during projection reduction.

This is distinct from #12746/#13602 which are about the proj_idx truncation from size_t to unsigned (the narrowing conversion). This finding is about the subsequent arithmetic on the already-converted unsigned value.

nparams itself comes from declaration.h:300 (get_nparams()) which also lacks an is_small() guard (see #13616). Two issues compound: garbage nparams + unsigned-wrapping check.

Suggested Fix

// Instead of:
if (nparams + idx < args.size())

// Use:
if (nparams < args.size() && idx < args.size() - nparams)

Or use size_t arithmetic for the comparison to avoid the 32-bit wrap:

if (static_cast<size_t>(nparams) + idx < args.size())

Versions

  • Lean 4 (all versions)
  • All 64-bit platforms (LP64 data model: unsigned=32-bit)

Impact

Low. Requires idx near UINT_MAX, which is practically unreachable through normal term construction (terms would need >4 billion fields). However, in combination with the .olean corruption vectors documented in #13615, this becomes exploitable for reading out-of-bounds kernel objects.

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