Skip to content

Commit 743acde

Browse files
committed
Fixes potential memory leaks in context methods.
Adds a private context function 'ffi_data_to_owned' that is used to take ownership of the heap allocated data that is returned from TSS. Fixes potential memory leaks where heap allocated data returned from TSS might not have been freed properly. Signed-off-by: Jesper Brynolf <[email protected]>
1 parent b7718a0 commit 743acde

16 files changed

+90
-129
lines changed

tss-esapi/src/context.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,12 @@ impl Context {
431431
Error::local_error(ErrorKind::MissingAuthSession)
432432
})
433433
}
434+
435+
/// Private function for handling that has been allocated with
436+
/// C memory allocation functions in TSS.
437+
fn ffi_data_to_owned<T>(data_ptr: *mut T) -> T {
438+
MBox::into_inner(unsafe { MBox::from_raw(data_ptr) })
439+
}
434440
}
435441

436442
impl Drop for Context {

tss-esapi/src/context/general_esys_tr.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::{
1010
Context, Error, Result,
1111
};
1212
use log::error;
13-
use mbox::MBox;
1413
use std::convert::TryFrom;
1514
use std::ptr::null_mut;
1615
use zeroize::Zeroize;
@@ -37,8 +36,7 @@ impl Context {
3736
unsafe { Esys_TR_GetName(self.mut_context(), object_handle.into(), &mut name_ptr) };
3837
let ret = Error::from_tss_rc(ret);
3938
if ret.is_success() {
40-
let name = unsafe { MBox::from_raw(name_ptr) };
41-
Ok(Name::try_from(*name)?)
39+
Name::try_from(Context::ffi_data_to_owned(name_ptr))
4240
} else {
4341
error!("Error in getting name: {}", ret);
4442
Err(ret)

tss-esapi/src/context/tpm_commands/asymmetric_primitives.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
Context, Error, Result,
99
};
1010
use log::error;
11-
use std::convert::{TryFrom, TryInto};
11+
use std::convert::TryFrom;
1212
use std::ptr::null_mut;
1313

1414
impl Context {
@@ -37,8 +37,7 @@ impl Context {
3737
let ret = Error::from_tss_rc(ret);
3838

3939
if ret.is_success() {
40-
let data = unsafe { PublicKeyRsa::try_from(*out_data_ptr)? };
41-
Ok(data)
40+
PublicKeyRsa::try_from(Context::ffi_data_to_owned(out_data_ptr))
4241
} else {
4342
error!("Error when performing RSA encryption: {}", ret);
4443
Err(ret)
@@ -70,8 +69,7 @@ impl Context {
7069
let ret = Error::from_tss_rc(ret);
7170

7271
if ret.is_success() {
73-
let data = unsafe { PublicKeyRsa::try_from(*message_ptr)? };
74-
Ok(data)
72+
PublicKeyRsa::try_from(Context::ffi_data_to_owned(message_ptr))
7573
} else {
7674
error!("Error when performing RSA decryption: {}", ret);
7775
Err(ret)
@@ -202,12 +200,12 @@ impl Context {
202200
let ret = Error::from_tss_rc(ret);
203201

204202
if ret.is_success() {
205-
Ok(unsafe {
206-
(
207-
(*z_point_ptr).point.try_into()?,
208-
(*pub_point_ptr).point.try_into()?,
209-
)
210-
})
203+
let z_point = Context::ffi_data_to_owned(z_point_ptr);
204+
let pub_point = Context::ffi_data_to_owned(pub_point_ptr);
205+
Ok((
206+
EccPoint::try_from(z_point.point)?,
207+
EccPoint::try_from(pub_point.point)?,
208+
))
211209
} else {
212210
error!("Error when generating ECDH keypair: {}", ret);
213211
Err(ret)
@@ -339,7 +337,8 @@ impl Context {
339337
let ret = Error::from_tss_rc(ret);
340338

341339
if ret.is_success() {
342-
Ok(unsafe { (*out_point_ptr).point.try_into()? })
340+
let out_point = Context::ffi_data_to_owned(out_point_ptr);
341+
EccPoint::try_from(out_point.point)
343342
} else {
344343
error!("Error when performing ECDH ZGen: {}", ret);
345344
Err(ret)

tss-esapi/src/context/tpm_commands/attestation_commands.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::{
77
Context, Error, Result,
88
};
99
use log::error;
10-
use mbox::MBox;
1110
use std::convert::TryFrom;
1211
use std::ptr::null_mut;
1312

@@ -142,11 +141,11 @@ impl Context {
142141
let ret = Error::from_tss_rc(ret);
143142

144143
if ret.is_success() {
145-
let certify_info = unsafe { MBox::from_raw(certify_info_ptr) };
146-
let signature = unsafe { MBox::from_raw(signature_ptr) };
144+
let certify_info = Context::ffi_data_to_owned(certify_info_ptr);
145+
let signature = Context::ffi_data_to_owned(signature_ptr);
147146
Ok((
148-
Attest::try_from(AttestBuffer::try_from(*certify_info)?)?,
149-
Signature::try_from(*signature)?,
147+
Attest::try_from(AttestBuffer::try_from(certify_info)?)?,
148+
Signature::try_from(signature)?,
150149
))
151150
} else {
152151
error!("Error in certifying: {}", ret);
@@ -186,11 +185,11 @@ impl Context {
186185
let ret = Error::from_tss_rc(ret);
187186

188187
if ret.is_success() {
189-
let quoted = unsafe { MBox::from_raw(quoted_ptr) };
190-
let signature = unsafe { MBox::from_raw(signature_ptr) };
188+
let quoted = Context::ffi_data_to_owned(quoted_ptr);
189+
let signature = Context::ffi_data_to_owned(signature_ptr);
191190
Ok((
192-
Attest::try_from(AttestBuffer::try_from(*quoted)?)?,
193-
Signature::try_from(*signature)?,
191+
Attest::try_from(AttestBuffer::try_from(quoted)?)?,
192+
Signature::try_from(signature)?,
194193
))
195194
} else {
196195
error!("Error in quoting PCR: {}", ret);

tss-esapi/src/context/tpm_commands/capability_commands.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::{
88
Context, Error, Result,
99
};
1010
use log::{error, warn};
11-
use mbox::MBox;
1211
use std::convert::TryFrom;
1312
use std::ptr::null_mut;
1413

@@ -66,9 +65,10 @@ impl Context {
6665
let ret = Error::from_tss_rc(ret);
6766

6867
if ret.is_success() {
69-
let capability_data = unsafe { MBox::from_raw(capability_data_ptr) };
70-
let capabilities = CapabilityData::try_from(*capability_data)?;
71-
Ok((capabilities, YesNo::try_from(more_data)?.into()))
68+
Ok((
69+
CapabilityData::try_from(Context::ffi_data_to_owned(capability_data_ptr))?,
70+
YesNo::try_from(more_data)?.into(),
71+
))
7272
} else {
7373
error!("Error when getting capabilities: {}", ret);
7474
Err(ret)

tss-esapi/src/context/tpm_commands/context_management.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use crate::{
99
Context, Error, Result,
1010
};
1111
use log::error;
12-
use mbox::MBox;
13-
use std::convert::TryInto;
12+
use std::convert::{TryFrom, TryInto};
1413
use std::ptr::null_mut;
1514

1615
impl Context {
@@ -24,8 +23,7 @@ impl Context {
2423
let ret = unsafe { Esys_ContextSave(self.mut_context(), handle.into(), &mut context_ptr) };
2524
let ret = Error::from_tss_rc(ret);
2625
if ret.is_success() {
27-
let context = unsafe { MBox::from_raw(context_ptr) };
28-
Ok((*context).try_into()?)
26+
TpmsContext::try_from(Context::ffi_data_to_owned(context_ptr))
2927
} else {
3028
error!("Error in saving context: {}", ret);
3129
Err(ret)

tss-esapi/src/context/tpm_commands/duplication_commands.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,11 @@ impl Context {
323323
let ret = Error::from_tss_rc(ret);
324324

325325
if ret.is_success() {
326-
let encryption_key_out = unsafe { Data::try_from(*encryption_key_out_ptr)? };
327-
let duplicate = unsafe { Private::try_from(*duplicate_ptr)? };
328-
let out_sym_seed = unsafe { EncryptedSecret::try_from(*out_sym_seed_ptr)? };
329-
Ok((encryption_key_out, duplicate, out_sym_seed))
326+
Ok((
327+
Data::try_from(Context::ffi_data_to_owned(encryption_key_out_ptr))?,
328+
Private::try_from(Context::ffi_data_to_owned(duplicate_ptr))?,
329+
EncryptedSecret::try_from(Context::ffi_data_to_owned(out_sym_seed_ptr))?,
330+
))
330331
} else {
331332
error!("Error when performing duplication: {}", ret);
332333
Err(ret)
@@ -681,7 +682,7 @@ impl Context {
681682
let ret = Error::from_tss_rc(ret);
682683

683684
if ret.is_success() {
684-
Ok(unsafe { Private::try_from(*out_private_ptr)? })
685+
Private::try_from(Context::ffi_data_to_owned(out_private_ptr))
685686
} else {
686687
error!("Error when performing import: {}", ret);
687688
Err(ret)

tss-esapi/src/context/tpm_commands/enhanced_authorization_ea_commands.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use crate::{
1919
Context, Error, Result, WrapperErrorKind as ErrorKind,
2020
};
2121
use log::error;
22-
use mbox::MBox;
2322
use std::convert::{TryFrom, TryInto};
2423
use std::ptr::null_mut;
2524
use std::time::Duration;
@@ -61,12 +60,10 @@ impl Context {
6160
};
6261
let ret = Error::from_tss_rc(ret);
6362
if ret.is_success() {
64-
let out_timeout = unsafe { MBox::from_raw(out_timeout_ptr) };
65-
let out_timeout = Timeout::try_from(*out_timeout)?;
66-
let out_policy_ticket = unsafe { MBox::from_raw(out_policy_ticket_ptr) };
67-
let out_policy_ticket = AuthTicket::try_from(*out_policy_ticket)?;
68-
69-
Ok((out_timeout, out_policy_ticket))
63+
Ok((
64+
Timeout::try_from(Context::ffi_data_to_owned(out_timeout_ptr))?,
65+
AuthTicket::try_from(Context::ffi_data_to_owned(out_policy_ticket_ptr))?,
66+
))
7067
} else {
7168
error!("Error when sending policy signed: {}", ret);
7269
Err(ret)
@@ -106,12 +103,10 @@ impl Context {
106103
};
107104
let ret = Error::from_tss_rc(ret);
108105
if ret.is_success() {
109-
let out_timeout = unsafe { MBox::from_raw(out_timeout_ptr) };
110-
let out_timeout = Timeout::try_from(*out_timeout)?;
111-
let out_policy_ticket = unsafe { MBox::from_raw(out_policy_ticket_ptr) };
112-
let out_policy_ticket = AuthTicket::try_from(*out_policy_ticket)?;
113-
114-
Ok((out_timeout, out_policy_ticket))
106+
Ok((
107+
Timeout::try_from(Context::ffi_data_to_owned(out_timeout_ptr))?,
108+
AuthTicket::try_from(Context::ffi_data_to_owned(out_policy_ticket_ptr))?,
109+
))
115110
} else {
116111
error!("Error when sending policy secret: {}", ret);
117112
Err(ret)
@@ -546,8 +541,7 @@ impl Context {
546541
};
547542
let ret = Error::from_tss_rc(ret);
548543
if ret.is_success() {
549-
let policy_digest = unsafe { MBox::from_raw(policy_digest_ptr) };
550-
Ok(Digest::try_from(*policy_digest)?)
544+
Digest::try_from(Context::ffi_data_to_owned(policy_digest_ptr))
551545
} else {
552546
error!(
553547
"Error failed to perform policy get digest operation: {}.",

tss-esapi/src/context/tpm_commands/hierarchy_commands.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::{
1515
Context, Error, Result,
1616
};
1717
use log::error;
18-
use mbox::MBox;
1918
use std::convert::{TryFrom, TryInto};
2019
use std::ptr::null_mut;
2120

@@ -77,20 +76,20 @@ impl Context {
7776
let ret = Error::from_tss_rc(ret);
7877

7978
if ret.is_success() {
80-
let out_public_owned = unsafe { MBox::from_raw(out_public_ptr) };
81-
let creation_data_owned = unsafe { MBox::from_raw(creation_data_ptr) };
82-
let creation_hash_owned = unsafe { MBox::from_raw(creation_hash_ptr) };
83-
let creation_ticket_owned = unsafe { MBox::from_raw(creation_ticket_ptr) };
84-
79+
let out_public_owned = Context::ffi_data_to_owned(out_public_ptr);
80+
let creation_data_owned = Context::ffi_data_to_owned(creation_data_ptr);
81+
let creation_hash_owned = Context::ffi_data_to_owned(creation_hash_ptr);
82+
let creation_ticket_owned = Context::ffi_data_to_owned(creation_ticket_ptr);
8583
let primary_key_handle = KeyHandle::from(object_handle);
8684
self.handle_manager
8785
.add_handle(primary_key_handle.into(), HandleDropAction::Flush)?;
86+
8887
Ok(CreatePrimaryKeyResult {
8988
key_handle: primary_key_handle,
90-
out_public: Public::try_from(*out_public_owned)?,
91-
creation_data: CreationData::try_from(*creation_data_owned)?,
92-
creation_hash: Digest::try_from(*creation_hash_owned)?,
93-
creation_ticket: CreationTicket::try_from(*creation_ticket_owned)?,
89+
out_public: Public::try_from(out_public_owned)?,
90+
creation_data: CreationData::try_from(creation_data_owned)?,
91+
creation_hash: Digest::try_from(creation_hash_owned)?,
92+
creation_ticket: CreationTicket::try_from(creation_ticket_owned)?,
9493
})
9594
} else {
9695
error!("Error in creating primary key: {}", ret);

tss-esapi/src/context/tpm_commands/integrity_collection_pcr.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::{
77
Context, Error, Result,
88
};
99
use log::error;
10-
use mbox::MBox;
1110
use std::convert::{TryFrom, TryInto};
1211
use std::ptr::null_mut;
1312

@@ -174,12 +173,10 @@ impl Context {
174173
let ret = Error::from_tss_rc(ret);
175174

176175
if ret.is_success() {
177-
let pcr_selection_out = unsafe { MBox::from_raw(pcr_selection_out_ptr) };
178-
let pcr_values = unsafe { MBox::from_raw(pcr_values_ptr) };
179176
Ok((
180177
pcr_update_counter,
181-
PcrSelectionList::try_from(*pcr_selection_out)?,
182-
DigestList::try_from(*pcr_values)?,
178+
PcrSelectionList::try_from(Context::ffi_data_to_owned(pcr_selection_out_ptr))?,
179+
DigestList::try_from(Context::ffi_data_to_owned(pcr_values_ptr))?,
183180
))
184181
} else {
185182
error!("Error when reading PCR: {}", ret);

0 commit comments

Comments
 (0)