# Heap BufferOver Flow in escapeshellargs and escapeshellcmd functions

Integer overflow in PHP escapeshellarg/escapeshellcmd when 4*l+2 wraps for ~1GB input, causing heap buffer overflow writing attacker data past allocation.

- Payout: $500
- Severity: high
- Program: Internet Bug Bounty
- Found by: libnex
- Disclosed: 2019-11-12
- Write-up quality: 7/10
- Original: https://hackerone.com/reports/110722
- Library page: https://bugbountylibrary.com/r/h1%3A110722
- Tags: area:oss domain:oss impact:rce kind:bounty platform:hackerone sev:high tech:php technique:fuzzing technique:source-review vuln:memory-corruption

**Target:** PHP ext/standard/exec.c escapeshellarg/escapeshellcmd
**Root cause:** Length l is cast to int and 4*l+2 overflows for large strings, allocating a tiny zend_string while the loop writes up to 4*l bytes.
**Steps:**
1. Set memory_limit high (e.g. ini_set('memory_limit', -1) or >1024MB).
2. Create a ~1GB string of 'A' (l=0x40000000).
3. Call `escapeshellarg($str)`; observe `4*l+2` wraps to 0x2 in zend_string_alloc.
4. Loop writes 0x41 filler bytes past the 2-byte buffer, corrupting heap.
**How found:** Author wrote a custom PHP engine fuzzer; found the overflow, then used gdb to trace strlen length into zend_string_alloc and confirm 4*l+2 integer wrap and out-of-bounds writes.
**Exploitation:** Attacker-controlled string written past a 2-byte heap allocation with 0x41 filler; requires memory_limit >~1024MB to reach the integer overflow.
**Insight:** Audit size computations like `4*l+2` for int overflow when input length can approach 2^30; fuzz with huge strings under raised memory_limit, since default limits mask the bug.

