Skip to content

Commit c0f8d04

Browse files
hyperfinitismJuergenReppSIT
authored andcommitted
fix(tpm2_send): validate command_size before computing data_size
Signed-off-by: Takuma IMAMURA <209989118+hyperfinitism@users.noreply.github.com>
1 parent 14b9445 commit c0f8d04

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

tools/tpm2_send.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,19 @@ static int read_command_from_file(FILE *f, tpm2_command_header **c,
4747
const tpm2_command_header *header = tpm2_command_header_from_bytes(buffer);
4848

4949
UINT32 command_size = tpm2_command_header_get_size(header, true);
50-
UINT32 data_size = tpm2_command_header_get_size(header, false);
51-
52-
if (command_size > TPM2_MAX_SIZE || command_size < data_size) {
53-
LOG_ERR("Command buffer %"PRIu32" bytes cannot be smaller then the "
54-
"encapsulated data %"PRIu32" bytes, and can not be bigger than"
55-
" the maximum buffer size", command_size, data_size);
50+
if (command_size < TPM2_COMMAND_HEADER_SIZE) {
51+
LOG_ERR("Command buffer size %"PRIu32" is smaller than command header "
52+
"size %zu", command_size, TPM2_COMMAND_HEADER_SIZE);
53+
return -1;
54+
}
55+
if (command_size > TPM2_MAX_SIZE) {
56+
LOG_ERR("Command buffer size %"PRIu32" exceeds maximum buffer size %u",
57+
command_size, TPM2_MAX_SIZE);
5658
return -1;
5759
}
5860

61+
UINT32 data_size = tpm2_command_header_get_size(header, false);
62+
5963
tpm2_command_header *command = (tpm2_command_header *) malloc(command_size);
6064
if (!command) {
6165
LOG_ERR("oom");

0 commit comments

Comments
 (0)