# BN_hex2bn/BN_dec2bn NULL pointer deref/heap corruption (CVE-2016-0797)

Integer overflow in OpenSSL BN_hex2bn/BN_dec2bn lets large untrusted hex/dec input cause NULL deref or undersized heap allocation and corruption.

- Payout: hidden
- Severity: low
- Program: Internet Bug Bounty
- Found by: guido
- Disclosed: 2016-03-27
- CVE: CVE-2016-0797
- Write-up quality: 6/10
- Original: https://hackerone.com/reports/119873
- Library page: https://bugbountylibrary.com/r/h1%3A119873
- Tags: area:oss domain:oss impact:dos impact:integrity kind:bounty platform:hackerone sev:low tech:linux vuln:dos vuln:memory-corruption

**Target:** OpenSSL BIGNUM parsing functions BN_hex2bn/BN_dec2bn
**Root cause:** Digit count stored in int |i|; bn_expand called with |i*4| which can overflow negative (no allocation, NULL field) or wrap to a small positive value (undersized allocation).
**Steps:**
1. Call BN_hex2bn (or BN_dec2bn) with a very large untrusted hex/dec string so digit count |i| is large.
2. Observe bn_expand(|i*4|): if |i*4| is negative, no memory allocated, BIGNUM data field stays NULL -> NULL pointer deref.
3. If |i*4| wraps to a positive value smaller than |i|, memory is allocated but undersized -> heap corruption.
**Insight:** Audit integer-width assumptions where a length is multiplied before being passed to an allocator: a signed int length times a per-element size can wrap negative (skip allocation) or small-positive (undersized buffer).

