# Vulnerabilities in the TPM 2.0 reference implementation code

TPM 2.0 reference implementation's CryptParameterDecryption mishandles the cipherSize length field, yielding a 2-byte OOB read and OOB write triggerable via crafted TPM2_CreatePrimary commands.

- Payout: $20,000
- Severity: high
- Program: Microsoft, VMware, Google, IBM, Lenovo, Qemu, Nuvoton, Trusted Computing Group, STMicroelectronics, Aruba Networks, CERT/CC, libtpms
- Credited: Francisco Falcon
- Disclosed: 2023-03-14
- CVE: CVE-2023-1017, CVE-2023-1018
- Write-up quality: 8/10
- Original: https://blog.quarkslab.com/vulnerabilities-in-the-tpm-20-reference-implementation-code.html
- Library page: https://bugbountylibrary.com/r/blog%3Ablog-quarkslab-com-vulnerabilities-in-the-tpm-20-reference-implementation-code-html
- Tags: domain:infra impact:dos impact:privesc kind:bounty platform:blog sev:high tech:evm technique:incomplete-fix technique:source-review vuln:dos vuln:memory-corruption

**Target:** TPM 2.0 command parser (CryptParameterDecryption in CryptUtil.c)
**Root cause:** CryptParameterDecryption reads cipherSize via unchecked BYTE_ARRAY_TO_UINT16 without verifying parameterArea exists, and after advancing the buffer by 2 it fails to subtract 2 from bufferSize, so the cipherSize > bufferSize check passes with a value 2 bytes too large.
**Steps:**
1. Send TPM2_StartAuthSession with TPM_ALG_XOR symmetric algorithm; obtain session handle.
2. Send TPM2_CreatePrimary with the session handle in sessionArea and Decrypt flag set in sessionAttributes.
3. For OOB read: omit parameterArea entirely (minimal valid sessionArea, no trailing data).
4. For OOB write: set total command size to 0x1000, set cipherSize field to 0xfe5, fill 0xfe3 bytes of parameter data to reach 0x1000 bytes.
5. Observe 2-byte OOB read past command buffer (VMware leaks prior command bytes) or 2-byte OOB write past buffer end.
**How found:** Researchers noted Windows 11's TPM 2.0 requirement adds virtual TPM attack surface reachable from guests, motivating audit of the TCG reference implementation. Code review of ExecuteCommand/ParseSessionBuffer/CryptParameterDecryption revealed the authorizationSize-only sanity check and the missing bufferSize adjustment after the 2-byte size field.
**Exploitation:** Attacker with guest/user-mode access sends crafted TPM commands to a virtual or hardware TPM. The OOB write overwrites 2 bytes past the command buffer; usefulness depends on adjacent memory, which varies by implementation (VMware oversized 0x10000 heap buffer, Hyper-V static 0x1000 buffer, SWTPM malloc 0x1008). On a Nuvoton discrete TPM the OOB write caused the chip to stop responding until hard reboot, confirming vulnerability. No full EoP/RCE chain demonstrated.
**Escalation:** Potential VM escape from guest to hypervisor host via virtual TPM; cloud vTPMs (Azure Trusted Launch, GCP Shielded VMs, AWS NitroTPM, OCI Shielded Instances) widen reach. Microsoft rated Azure impact low since only 2 bytes overwritten and no consistent EoP/RCE found.
**Insight:** When auditing length-prefixed in-place decryption, check whether the code subtracts the size-field width from the remaining buffer length after advancing the pointer; a check like cipherSize > bufferSize is off-by-N whenever the pointer moved past the size field. Also check whether the parser validates that a parameterArea exists at all before reading its first bytes.
