From 3606cec3c00b9b32e4d0be4692ceeb7aa1865697 Mon Sep 17 00:00:00 2001 From: Hasini Gunasinghe Date: Thu, 13 Oct 2022 23:04:44 +0000 Subject: [PATCH] Process large messages from TA This CL adds the capability to the HAL to process reponses from the TA that are larger than the capacity of the channel from HAL to TA. Bug: 253501976 Test: with Trusty KM which has a smaller limit than some responses Change-Id: I2fe056143f18718eb10bdd2d0559f3d171b14c96 --- trusty/keymint/src/keymint_hal_main.rs | 41 ++++++++++++++++---------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/trusty/keymint/src/keymint_hal_main.rs b/trusty/keymint/src/keymint_hal_main.rs index d2d5f2750..cfa859f63 100644 --- a/trusty/keymint/src/keymint_hal_main.rs +++ b/trusty/keymint/src/keymint_hal_main.rs @@ -14,7 +14,9 @@ // limitations under the License. //! This module implements the HAL service for Keymint (Rust) in Trusty. -use kmr_hal::{keymint, rpc, secureclock, send_hal_info, sharedsecret, SerializedChannel}; +use kmr_hal::{ + extract_rsp, keymint, rpc, secureclock, send_hal_info, sharedsecret, SerializedChannel, +}; use log::{error, info}; use std::{ ffi::CString, @@ -41,6 +43,7 @@ struct HalServiceError(String); struct TipcChannel(trusty::TipcChannel); impl SerializedChannel for TipcChannel { + const MAX_SIZE: usize = 4000; fn execute(&mut self, serialized_req: &[u8]) -> binder::Result> { self.0.send(serialized_req).map_err(|e| { binder::Status::new_exception( @@ -54,21 +57,27 @@ impl SerializedChannel for TipcChannel { ), ) })?; - let mut recv_buf = Vec::new(); - // TODO(b/253501976): cope with fragmentation of responses - self.0.recv(&mut recv_buf).map_err(|e| { - binder::Status::new_exception( - binder::ExceptionCode::TRANSACTION_FAILED, - Some( - &CString::new(format!( - "Failed to receive the response via tipc channel because of {:?}", - e - )) - .unwrap(), - ), - ) - })?; - Ok(recv_buf) + let mut expect_more_msgs = true; + let mut full_rsp = Vec::new(); + while expect_more_msgs { + let mut recv_buf = Vec::new(); + self.0.recv(&mut recv_buf).map_err(|e| { + binder::Status::new_exception( + binder::ExceptionCode::TRANSACTION_FAILED, + Some( + &CString::new(format!( + "Failed to receive the response via tipc channel because of {:?}", + e + )) + .unwrap(), + ), + ) + })?; + let current_rsp_content; + (expect_more_msgs, current_rsp_content) = extract_rsp(&recv_buf)?; + full_rsp.extend_from_slice(current_rsp_content); + } + Ok(full_rsp) } }