tpm: v2: Fix the size of the random data in tpm2_get_random()
PATCH 1/1 tpm: v2: Fix the size of the random data in tpm2_get_random()
tpm2_get_random() reads the size of the returned random bytes with the "w" format of unpack_byte_string(), which stores a u16, but data_size is a u32. Only its lower half is written, so its upper half keeps whatever was on the stack, and the "data_size > this_bytes" check fails with TPM_LIB_ERROR whenever that is not zero.
On qemu-riscv64_smode with a swtpm TPM, this breaks the kaslr-seed fixup on every bootefi:
tpm-rng tpm-rng: dm_rng_read failed: -1
Under gdb, data_size was 0xffffffff before the call and 0xffff0008 after it, for an 8-byte request. Whether it fails depends on the stack contents, so unrelated changes can hide it.
The size field of a TPM2B is a UINT16, so declare data_size as u16.
--- lib/tpm-v2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/tpm-v2.c +1 -1
@@ -977,7 +977,7 @@ u32 tpm2_get_random(struct udevice *dev, void *data, u32 count)const size_t data_size_offset =10 ;const size_t data_offset =12 ;size_t response_length =sizeof (response); -u32 data_size; +u16 data_size;u8 *out = data;while (count >0 ) {
Signature · 1 line
2.43.0
Hello Hung-Chun
Quoted 9 lines · Hung-Chun Tseng
tpm2_get_random()reads the size of the returned random bytes with the "w" format ofunpack_byte_string(), which stores a u16, butdata_sizeis a u32. Only its lower half is written, so its upper half keeps whatever was on the stack, and the "data_size>this_bytes" check fails withTPM_LIB_ERRORwhenever that is not zero.On qemu-riscv64_smode with a swtpm TPM, this breaks the kaslr-seed fixup on every bootefi:
Just a not here, kaslr-seed is not what you want for KASLR. Having the EFI rng protocol is better since it randomizes both the physical and virtual placement of the kernel.
Quoted 7 lines
tpm-rng tpm-rng:
dm_rng_readfailed: -1Under gdb,
data_sizewas0xffffffffbefore the call and0xffff0008after it, for an 8-byte request. Whether it fails depends on the stack contents, so unrelated changes can hide it.
Quoted 5 lines
The size field of a TPM2B is a UINT16, so declare
data_sizeas u16.Fixes 06bea4980980 ("tpm: add TPM2_GetRandom command support")Signed-off-by Hung-Chun Tseng <[email protected]>---
Quoted 19 lines
lib/tpm-v2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/tpm-v2.c +1
-2@@ -977,7 +977,7 @@ u32 tpm2_get_random(struct udevice *dev, void *data, u32 count)const size_t data_size_offset =10 ;const size_t data_offset =12 ;size_t response_length =sizeof (response); -u32 data_size; +u16 data_size;u8 *out = data;while (count >0 ) { --2.43.0