# Out-of-bounds read in iconv.c:_php_iconv_mime_decode() due to integer overflow

Integer underflow of size_t str_left in PHP's iconv MIME header decoder causes heap out-of-bounds read, leaking memory or crashing apps parsing crafted emails.

- Payout: $1,500
- Severity: high
- Program: Internet Bug Bounty
- Found by: neural_x
- Disclosed: 2020-10-12
- CVE: CVE-2019-11039
- Write-up quality: 8/10
- Original: https://hackerone.com/reports/593229
- Library page: https://bugbountylibrary.com/r/h1%3A593229
- Tags: area:oss domain:oss impact:data-leak impact:dos kind:bounty platform:hackerone sev:high tech:php technique:source-review vuln:dos vuln:info-disclosure vuln:memory-corruption

**Target:** PHP ext/iconv _php_iconv_mime_decode() MIME header decoder
**Root cause:** str_left is size_t; when the '?'-skip loop exits with str_left==1 and the following '=' check decrements it to 0, the for-loop's str_left-- underflows to SIZE_MAX, so the loop reads far past the buffer.
**Steps:**
1. Craft MIME header with an encoded word using an unrecognized charset and ICONV_MIME_DECODE_CONTINUE_ON_ERROR (flag 2)
2. Ensure the encoded word's '?'-scan leaves str_left==1 and next char is '=' so str_left hits 0
3. Call `iconv_mime_decode_headers(file_get_contents("poc"),2)`
4. Observe ASAN heap-buffer-overflow READ in _php_iconv_mime_decode at iconv.c:1965
**How found:** Code review of the for-loop condition and the inner '?'-skip/`=`-check branches, noticing str_left could reach 0 before the loop's decrement on an unsigned size_t.
**Exploitation:** Attacker submits a crafted MIME email; when the app decodes headers with CONTINUE_ON_ERROR, the decoder reads out-of-bounds heap memory into the decoded header, causing crash or info leak depending on adjacent memory.
**Insight:** When auditing parsers, trace unsigned loop counters through every early-exit branch; a counter reaching 0 before a `for(...; i--; ...)` decrement underflows and turns a bounded scan into an unbounded OOB read.

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

**Hex PoC file piped through xxd to create poc**

```
$ echo "53754c743b2020304a70616100000d0d0d0d0d0d0d0d0d6563743a203d3f69730d0d0d0d0d0d0d0d0d0d0d0d0d0d0d6563743a203d3f6973754c743b2020304a70616100000d0d0d0d0d0d0d0d0d6563743a203d3f6f2d383835392d313f713f3c334633463d33463f3da2" | xxd -r -p - > poc
```

**PHP command triggering the OOB read**

```
$ USE_ZEND_ALLOC=0 sapi/cli/php -r '$hdr = iconv_mime_decode_headers(file_get_contents("poc"),2);'
```

