From 5646a917ff1f502c9198c2670ca31057d05fdb26 Mon Sep 17 00:00:00 2001 From: Wei Liu Date: Mon, 30 Jan 2023 22:14:35 +0000 Subject: [PATCH] tpm: handle short write There is no guarantee that the write can send the whole buffer at once. In those rare occasions, we should return a sensible error. Signed-off-by: Wei Liu --- tpm/src/emulator.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tpm/src/emulator.rs b/tpm/src/emulator.rs index f11c4bcfc..6398ce730 100644 --- a/tpm/src/emulator.rs +++ b/tpm/src/emulator.rs @@ -208,7 +208,7 @@ impl Emulator { buf.extend(converted_req); debug!("full Control request {:02X?}", buf); - let _res = self.control_socket.write(&buf).map_err(|e| { + let written = self.control_socket.write(&buf).map_err(|e| { Error::RunControlCmd(anyhow!( "Failed while running {:02X?} Control Cmd. Error: {:?}", cmd, @@ -216,6 +216,13 @@ impl Emulator { )) })?; + if written < buf.len() { + return Err(Error::RunControlCmd(anyhow!( + "Truncated write while running {:02X?} Control Cmd", + cmd, + ))); + } + // The largest response is 16 bytes so far. if msg_len_out > 16 { return Err(Error::RunControlCmd(anyhow!(