Skip to content

tpm2_util: fix buffer overflow in string validation checks#3563

Merged
JuergenReppSIT merged 1 commit into
tpm2-software:masterfrom
hyperfinitism:fix/overflow-pemkey
Mar 4, 2026
Merged

tpm2_util: fix buffer overflow in string validation checks#3563
JuergenReppSIT merged 1 commit into
tpm2-software:masterfrom
hyperfinitism:fix/overflow-pemkey

Conversation

@hyperfinitism
Copy link
Copy Markdown
Contributor

The strlen() validation checks in tpm2_pem_encoded_key_to_fingerprint() used > 1024 instead of >= 1024, which allowed strings of exactly 1024 characters to pass validation and be copied into 1024-byte stack buffers. Since strcpy() also writes the null terminator, a 1024-character input causes 1025 bytes to be written, overflowing the buffer by one byte.

The same off-by-one existed in two places within the function:

  • The initial PEM string length check against str[1024]
  • The per-line base64 accumulation check against base64[1024]

Current exploitability

In the existing call path, pem_encoded_key is the PEM-encoded public key supplied by the FAPI auth callback (tss2_template.c). All key types currently supported by FAPI (P-256, P-384, P-521, RSA-2048/3072/4096) produce PEM public keys well below 1024 bytes in length, so the overflow cannot be triggered in practice with standard inputs.

Nonetheless, the validation logic is incorrect as written. This fix closes the gap to prevent the issue from becoming exploitable if the function is called with a larger key in the future, or if it is reached through a different code path.

Changes

  • Change > 1024 to >= 1024 in both length checks so that an input of exactly 1024 characters (+ null termination) is correctly rejected
  • Replace strcpy with strncpy + explicit null termination as defence-in-depth, ensuring no overflow occurs even if the length guard were somehow bypassed

Signed-off-by: Takuma IMAMURA <209989118+hyperfinitism@users.noreply.github.com>
@JuergenReppSIT JuergenReppSIT merged commit 17e4d6b into tpm2-software:master Mar 4, 2026
40 of 41 checks passed
@hyperfinitism hyperfinitism deleted the fix/overflow-pemkey branch March 4, 2026 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants