tpm2_util: fix buffer overflow in string validation checks#3563
Merged
JuergenReppSIT merged 1 commit intoMar 4, 2026
Merged
Conversation
Signed-off-by: Takuma IMAMURA <209989118+hyperfinitism@users.noreply.github.com>
JuergenReppSIT
approved these changes
Mar 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
strlen()validation checks intpm2_pem_encoded_key_to_fingerprint()used> 1024instead of>= 1024, which allowed strings of exactly 1024 characters to pass validation and be copied into 1024-byte stack buffers. Sincestrcpy()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:
str[1024]base64[1024]Current exploitability
In the existing call path,
pem_encoded_keyis 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
> 1024to>= 1024in both length checks so that an input of exactly 1024 characters (+ null termination) is correctly rejectedstrcpywithstrncpy+ explicit null termination as defence-in-depth, ensuring no overflow occurs even if the length guard were somehow bypassed