tpm2_send: Validate command_size before computing data_size#3549
Merged
JuergenReppSIT merged 1 commit intoFeb 18, 2026
Merged
Conversation
dfcf038 to
f3e05a6
Compare
Signed-off-by: Takuma IMAMURA <209989118+hyperfinitism@users.noreply.github.com>
f3e05a6 to
fdd9b7f
Compare
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.
Background
In
lib/tpm2_header.h, TPM command buffers are represented by thetpm2_command_headerunion, which exposes both a raw byte view and a structured header view (tag,size,command_code,data[]).tpm2-tools/lib/tpm2_header.h
Lines 18 to 26 in 14b9445
TPM2_COMMAND_HEADER_SIZEis defined assizeof(tpm2_command_header), i.e., the fixed-size header portion (excluding the variable-lengthdata[]). The helpertpm2_command_header_get_size()returns either the full command size (include_header=true) or payload size (include_header=false, computed assize - TPM2_COMMAND_HEADER_SIZE).tpm2-tools/lib/tpm2_header.h
Lines 82 to 87 in 14b9445
In
tools/tpm2_send.c, the code currently derives bothcommand_sizeanddata_sizefrom the input header, and then validates size relationships before allocating/parsing the command body.tpm2-tools/tools/tpm2_send.c
Lines 47 to 57 in 14b9445
Problem
If malformed input sets
tpm2_command_header.sizeto a value smaller thanTPM2_COMMAND_HEADER_SIZE, computingdata_size(size - TPM2_COMMAND_HEADER_SIZE) causes unsigned overflow.As a result, later checks may report a confusing error path/message based on overflowed values rather than the actual root cause (invalid header-size field).
For example:
Current behavior can produce an error like:
This message is misleading when the real issue is that the encoded command size is smaller than the required header size.
Fix
This PR changes validation order in
tpm2_send:command_size.command_size >= TPM2_COMMAND_HEADER_SIZE.command_size <= TPM2_MAX_SIZE.data_size.With this order, overflow during
data_sizederivation is avoided for malformed short-size headers. Sincedata_sizeis then derived from a validatedcommand_size, thecommand_size < data_sizecheck becomes unnecessary and is removed.After those changes, the command
yields a relevant error message: