# Buffer overflow in PyCArg_repr in _ctypes/callproc.c for Python 3.x to 3.9.1

Unbounded sprintf in PyCArg_repr in _ctypes/callproc.c overflows a fixed buffer when formatting extreme doubles like 1e300, crashing Python and potentially enabling RCE.

- Payout: $1,500
- Severity: high
- Program: Internet Bug Bounty
- Found by: jordyzomer
- Disclosed: 2021-08-25
- CVE: CVE-2021-3177
- Write-up quality: 6/10
- Original: https://hackerone.com/reports/1084342
- Library page: https://bugbountylibrary.com/r/h1%3A1084342
- Tags: area:oss domain:oss impact:dos kind:bounty platform:hackerone sev:high tech:python technique:source-review vuln:memory-corruption

**Target:** CPython _ctypes/callproc.c PyCArg_repr (ctypes c_double.from_param)
**Root cause:** PyCArg_repr uses sprintf with %f into a fixed-size stack buffer without bounds checking, so extreme double values expand to hundreds of digits and overflow.
**Steps:**
1. Run `python3 -c "from ctypes import *; c_double.from_param(1e300)"`
2. Observe `*** buffer overflow detected ***: terminated` / abort
3. Trigger via any app passing untrusted floats into ctypes c_double.from_param
**How found:** Code reading of PyCArg_repr's sprintf call, noticing self->value.d is attacker-influenced and %f formatting of extreme values like 1e300 produces 300+ digits into a fixed buffer.
**Exploitation:** Not demonstrated beyond a crash PoC; RCE is only hypothesized for applications that pass untrusted floating-point input into ctypes.
**Insight:** Audit C extension repr/format paths that use sprintf with %f on attacker-controlled doubles; extreme magnitudes (1e300) expand to hundreds of chars and blow fixed stack buffers.

## Proof of concept (copied exactly from the original report)

**c_double.from_param(1e300) buffer overflow PoC**

```
>>> from ctypes import *
>>> c_double.from_param(1e300)
*** buffer overflow detected ***: terminated
Aborted
```

