A TPM reply's length is taken from the device and only upper-capped, then
several callers use it unchecked: the TPM1 OIAP helpers and the TPM2
GetCapability parser subtract a fixed header length from it, so a reply too
short underflows the subtraction into a huge memcpy; the GetCapability
parser then copies that many bytes into the caller's buffer, so a reply
longer than the buffer overruns it. Because a TPM sits on a physically
accessible bus, a cheap bus interposer (the kind used to sniff
disk-encryption keys) can inject such a reply, and on the TPM2 path this
parsing runs during measured boot, which is exactly the physical attacker
that measured boot is meant to resist.

Patch 1 checks the reported length once in tpm_sendrecv_command(), so every
command that reads a reply rejects a too-short response, and passes the
caller's buffer size to tpm2_get_capability() so its copy cannot overrun for
any capability. Patch 2 adds regression tests driving the GetCapability
parser and the TPM1 OIAP helpers with truncated and over-long replies
through the sandbox emulator.

Based on v2026.07 (fdfe2ec48d5c). A reproducer is available on request.

Signed-off-by: Shahriyar Jalayeri <[email protected]>
---
Changes in v4:
- tpm2_get_capability(): bound the copy by the caller's buffer size, passed
  in as a new buf_size argument, instead of a prop_count-derived guess; this
  covers every capability, not just the properties query. Document @buf_size
  and @prop_count. (Simon Glass)
- Add regression tests for the TPM1 OIAP tpm1_load_key2_oiap() and
  tpm1_get_pub_key_oiap() length checks; enable CONFIG_TPM_AUTH_SESSIONS in
  the sandbox config so they build. (Simon Glass)

Changes in v3:
- Rework per review: check the response length once in
  tpm_sendrecv_command() instead of at each call site, so every command
  that reads a reply is guarded. Drop the per-site lower-bound checks; keep
  the GetCapability buffer bound.
- Drop Miquel's Acked-by, as the fix changed substantially.

Changes in v2:
- Use my real name in the From and Signed-off-by (Miquel Raynal)

---
Shahriyar Jalayeri (2):
      tpm: bounds-check the device-reported response length
      test: tpm: check malformed capability and OIAP responses are rejected

 cmd/tpm-v2.c                   |  4 +-
 configs/sandbox_defconfig      |  1 +
 drivers/tpm/tpm2_tis_sandbox.c | 28 ++++++++++++++
 drivers/tpm/tpm_tis_sandbox.c  |  9 +++++
 include/tpm-v2.h               | 15 ++++++-
 lib/efi_loader/efi_tcg2.c      |  9 +++--
 lib/tpm-common.c               | 16 ++++++--
 lib/tpm-utils.h                |  8 +++-
 lib/tpm-v1.c                   | 59 ++++++++++++++++------------
 lib/tpm-v2.c                   | 60 ++++++++++++++++------------
 lib/tpm_api.c                  |  4 +-
 test/dm/tpm.c                  | 88 ++++++++++++++++++++++++++++++++++++++++++
 12 files changed, 242 insertions(+), 59 deletions(-)
---
base-commit: fdfe2ec48d5c1c2ed03073d73edd3fdd3fe1ffa1
change-id: 20260723-tpm-trunc-resp-664a3babf1cf

Best regards,
--  
Shahriyar Jalayeri <[email protected]>

Reply via email to