Skip to content

Commit dc61c00

Browse files
committed
optee-utee: fix TA session parameter lifetime and cleanup
1 parent 1110f77 commit dc61c00

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

crates/optee-utee/src/ta_session.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,14 @@ impl<'a> TaSessionBuilder<'a> {
5050
pub fn build(mut self) -> Result<TaSession> {
5151
let mut err_origin: u32 = 0;
5252
let mut raw_session: raw::TEE_TASessionHandle = core::ptr::null_mut();
53-
// Check if the parameters are provided and prepare them for the C API call.
54-
let (raw_param_types, raw_params_ptr, raw_params_opt) =
55-
if let Some(params) = &mut self.params {
56-
let mut raw_params = params.as_raw();
57-
let raw_ptr = raw_params.as_mut_ptr();
58-
(params.raw_param_types(), raw_ptr, Some(raw_params))
59-
} else {
60-
(0, core::ptr::null_mut(), None)
61-
};
53+
// Store the raw parameters in their final location before taking a pointer to them.
54+
// `TEE_Param` is `Copy`, so taking the pointer before moving the array into the
55+
// `Option` could leave the pointer referring to the old stack location.
56+
let raw_param_types = self.params.as_ref().map_or(0, TeeParams::raw_param_types);
57+
let mut raw_params_opt = self.params.as_mut().map(|params| params.as_raw());
58+
let raw_params_ptr = raw_params_opt
59+
.as_mut()
60+
.map_or(core::ptr::null_mut(), |raw_params| raw_params.as_mut_ptr());
6261

6362
// SAFETY:
6463
// self.target_uuid.as_raw_ptr() provides a valid pointer to the UUID.
@@ -76,11 +75,13 @@ impl<'a> TaSessionBuilder<'a> {
7675
)
7776
} {
7877
raw::TEE_SUCCESS => {
78+
// From this point on, ensure every error path closes the opened session.
79+
let session = TaSession { raw: raw_session };
7980
if let (Some(params), Some(raw_params)) = (&mut self.params, raw_params_opt) {
8081
params.update_from_raw(&raw_params)?;
8182
}
8283

83-
Ok(TaSession { raw: raw_session })
84+
Ok(session)
8485
}
8586
code => Err(Error::from_raw_error(code).with_origin(err_origin.into())),
8687
}

0 commit comments

Comments
 (0)