# Arbitary Memory Read via gdImageRotateInterpolated Array Index Out of Bounds

PHP imagerotate() failed to validate the $bgd_color palette index, allowing out-of-bounds reads of adjacent heap memory leaked via the rotated image's background color.

- Payout: $500
- Severity: medium
- Program: Internet Bug Bounty
- Found by: libnex
- Disclosed: 2019-11-12
- Write-up quality: 7/10
- Original: https://hackerone.com/reports/110720
- Library page: https://bugbountylibrary.com/r/h1%3A110720
- Tags: area:oss domain:oss impact:data-leak kind:bounty platform:hackerone sev:medium tech:php technique:source-review vuln:info-disclosure

**Target:** PHP ext/gd imagerotate() / gdImageRotateInterpolated
**Root cause:** $bgd_color is used directly as an index into fixed 256-entry red/green/blue/alpha arrays in gdImageStruct without bounds checking, so large values read memory past the palette.
**Steps:**
1. Build PHP with `--with-gd`.
2. Run `php -r "imagerotate(imagecreate(1,1),45,0x7ffffff9);"` to trigger SIGSEGV at gd_interpolation.c:2174.
3. In gdb, `ptype src` shows `int red[256]; int green[256]; int blue[256];` confirming OOB index.
4. Use the provided PoC script to pass crafted bgd_color values and read contiguous heap memory, then extract leaked bytes from the rotated image's background color.
**How found:** Noticed $bgd_color is documented as a palette index but not validated; hypothesized large values would index past the 256-entry arrays, confirmed via crash and gdb inspection of gdImageStruct.
**Exploitation:** Attacker controls the $bgd_color argument to imagerotate(); by choosing large indices, the function reads out-of-bounds heap memory and encodes those bytes as the background color of the rotated image, which the attacker then reads back to disclose memory contents.
**Insight:** When a parameter is documented as an index into a fixed-size palette/array, test values beyond the array length; the resulting OOB read can be turned into an info leak if the read value is reflected back in output (e.g., a rendered color).

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

**imagerotate with oversized bgd_color index**

```
php -r "imagerotate(imagecreate(1,1),45,0x7ffffff9);"
```

