Skip to content

Bound header_length and check shape-product overflow when loading .npy (fix #44)#45

Open
ValheruEldarr wants to merge 2 commits into
llohse:masterfrom
ValheruEldarr:fix/npy-header-bounds-checks
Open

Bound header_length and check shape-product overflow when loading .npy (fix #44)#45
ValheruEldarr wants to merge 2 commits into
llohse:masterfrom
ValheruEldarr:fix/npy-header-bounds-checks

Conversation

@ValheruEldarr

Copy link
Copy Markdown

Fixes the unbounded allocation and shape-product integer overflow reported in #44 (CWE-789, CWE-190).

Changes:

  • read_header: cap the attacker-controlled v2 header_length (16 MiB, well above any real header) before allocating, so a tiny crafted file can no longer force a multi-gigabyte allocation.
  • comp_size: reject shape-dimension products that overflow ndarray_len_t (the wrap previously produced a silent undersized buffer or a giant allocation downstream).

Verified with AddressSanitizer: the PoCs from #44 are now rejected with a clean exception, and a valid .npy (ndim=3, nelem=24) still loads. This is denial-of-service and undersized-allocation class; no code execution is involved.

@llohse

llohse commented Jun 6, 2026

Copy link
Copy Markdown
Owner

Thank you for your contribution.
Could you please rebase against master so that the CI checks will run through.

Adds input validation to read_header/comp_size (reported in llohse#44):
- cap the attacker-controlled v2 header_length (16 MiB, well above any real header) before
  allocating, so a tiny crafted file can no longer force a multi-gigabyte allocation (CWE-789)
- reject shape-dimension products that overflow ndarray_len_t (CWE-190)
@ValheruEldarr ValheruEldarr force-pushed the fix/npy-header-bounds-checks branch from c8c614c to d05cd28 Compare June 6, 2026 13:25
@llohse

llohse commented Jun 6, 2026

Copy link
Copy Markdown
Owner

Tests are failing. It looks like an import is missing.

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.

2 participants