Skip to content

Commit c1caa59

Browse files
authored
Merge pull request #583 from NuSkooler/dll-boundary-fixes
Fix Windows DLL boundary issues
2 parents 40899e5 + eb95e00 commit c1caa59

18 files changed

+100
-80
lines changed

tss-esapi/src/context.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use handle_manager::HandleManager;
1515
use log::{debug, error};
1616
use malloced::Malloced;
1717
use std::collections::HashMap;
18-
use std::ptr::null_mut;
18+
use std::{ffi::c_void, ptr, ptr::null_mut};
1919

2020
/// Safe abstraction over an ESYS_CONTEXT.
2121
///
@@ -454,12 +454,16 @@ impl Context {
454454

455455
/// Private function for handling that has been allocated with
456456
/// C memory allocation functions in TSS.
457-
fn ffi_data_to_owned<T: Copy>(data_ptr: *mut T) -> T {
458-
let out = unsafe { *data_ptr };
457+
fn ffi_data_to_owned<T: Copy>(data_ptr: *mut T) -> Result<T> {
458+
if data_ptr.is_null() {
459+
error!("Null pointer received from TSS");
460+
return Err(Error::local_error(ErrorKind::WrongValueFromTpm));
461+
}
462+
463+
let out = unsafe { ptr::read(data_ptr) };
464+
unsafe { Esys_Free(data_ptr.cast::<c_void>()) };
459465

460-
// Free the malloced data.
461-
drop(unsafe { Malloced::from_raw(data_ptr) });
462-
out
466+
Ok(out)
463467
}
464468
}
465469

tss-esapi/src/context/general_esys_tr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl Context {
139139
error!("Error in getting name: {:#010X}", ret);
140140
},
141141
)?;
142-
Name::try_from(Context::ffi_data_to_owned(name_ptr))
142+
Name::try_from(Context::ffi_data_to_owned(name_ptr)?)
143143
}
144144

145145
/// Used to construct an esys object from the resources inside the TPM.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl Context {
3939
error!("Error when performing RSA encryption: {:#010X}", ret);
4040
},
4141
)?;
42-
PublicKeyRsa::try_from(Context::ffi_data_to_owned(out_data_ptr))
42+
PublicKeyRsa::try_from(Context::ffi_data_to_owned(out_data_ptr)?)
4343
}
4444

4545
/// Perform an asymmetric RSA decryption.
@@ -69,7 +69,7 @@ impl Context {
6969
error!("Error when performing RSA decryption: {:#010X}", ret);
7070
},
7171
)?;
72-
PublicKeyRsa::try_from(Context::ffi_data_to_owned(message_ptr))
72+
PublicKeyRsa::try_from(Context::ffi_data_to_owned(message_ptr)?)
7373
}
7474

7575
/// Generate an ephemeral key pair.
@@ -199,8 +199,8 @@ impl Context {
199199
},
200200
)?;
201201

202-
let z_point = Context::ffi_data_to_owned(z_point_ptr);
203-
let pub_point = Context::ffi_data_to_owned(pub_point_ptr);
202+
let z_point = Context::ffi_data_to_owned(z_point_ptr)?;
203+
let pub_point = Context::ffi_data_to_owned(pub_point_ptr)?;
204204
Ok((
205205
EccPoint::try_from(z_point.point)?,
206206
EccPoint::try_from(pub_point.point)?,
@@ -335,7 +335,7 @@ impl Context {
335335
error!("Error when performing ECDH ZGen: {:#010X}", ret);
336336
},
337337
)?;
338-
let out_point = Context::ffi_data_to_owned(out_point_ptr);
338+
let out_point = Context::ffi_data_to_owned(out_point_ptr)?;
339339
EccPoint::try_from(out_point.point)
340340
}
341341

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ impl Context {
147147
},
148148
)?;
149149

150-
let certify_info = Context::ffi_data_to_owned(certify_info_ptr);
151-
let signature = Context::ffi_data_to_owned(signature_ptr);
150+
let certify_info = Context::ffi_data_to_owned(certify_info_ptr)?;
151+
let signature = Context::ffi_data_to_owned(signature_ptr)?;
152152
Ok((
153153
Attest::try_from(AttestBuffer::try_from(certify_info)?)?,
154154
Signature::try_from(signature)?,
@@ -272,8 +272,8 @@ impl Context {
272272
},
273273
)?;
274274

275-
let certify_info = Context::ffi_data_to_owned(certify_info_ptr);
276-
let signature = Context::ffi_data_to_owned(signature_ptr);
275+
let certify_info = Context::ffi_data_to_owned(certify_info_ptr)?;
276+
let signature = Context::ffi_data_to_owned(signature_ptr)?;
277277
Ok((
278278
Attest::try_from(AttestBuffer::try_from(certify_info)?)?,
279279
Signature::try_from(signature)?,
@@ -313,8 +313,8 @@ impl Context {
313313
},
314314
)?;
315315

316-
let quoted = Context::ffi_data_to_owned(quoted_ptr);
317-
let signature = Context::ffi_data_to_owned(signature_ptr);
316+
let quoted = Context::ffi_data_to_owned(quoted_ptr)?;
317+
let signature = Context::ffi_data_to_owned(signature_ptr)?;
318318
Ok((
319319
Attest::try_from(AttestBuffer::try_from(quoted)?)?,
320320
Signature::try_from(signature)?,
@@ -426,8 +426,8 @@ impl Context {
426426
},
427427
)?;
428428

429-
let timeinfo = Context::ffi_data_to_owned(timeinfo_ptr);
430-
let signature = Context::ffi_data_to_owned(signature_ptr);
429+
let timeinfo = Context::ffi_data_to_owned(timeinfo_ptr)?;
430+
let signature = Context::ffi_data_to_owned(signature_ptr)?;
431431
Ok((
432432
Attest::try_from(AttestBuffer::try_from(timeinfo)?)?,
433433
Signature::try_from(signature)?,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl Context {
6969
)?;
7070

7171
Ok((
72-
CapabilityData::try_from(Context::ffi_data_to_owned(capability_data_ptr))?,
72+
CapabilityData::try_from(Context::ffi_data_to_owned(capability_data_ptr)?)?,
7373
YesNo::try_from(more_data)?.into(),
7474
))
7575
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl Context {
2626
error!("Error in saving context: {:#010X}", ret);
2727
},
2828
)?;
29-
SavedTpmContext::try_from(Context::ffi_data_to_owned(context_ptr))
29+
SavedTpmContext::try_from(Context::ffi_data_to_owned(context_ptr)?)
3030
}
3131

3232
/// Load a previously saved context into the TPM and return the object handle.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,9 @@ impl Context {
327327
)?;
328328

329329
Ok((
330-
Data::try_from(Context::ffi_data_to_owned(encryption_key_out_ptr))?,
331-
Private::try_from(Context::ffi_data_to_owned(duplicate_ptr))?,
332-
EncryptedSecret::try_from(Context::ffi_data_to_owned(out_sym_seed_ptr))?,
330+
Data::try_from(Context::ffi_data_to_owned(encryption_key_out_ptr)?)?,
331+
Private::try_from(Context::ffi_data_to_owned(duplicate_ptr)?)?,
332+
EncryptedSecret::try_from(Context::ffi_data_to_owned(out_sym_seed_ptr)?)?,
333333
))
334334
}
335335

@@ -683,6 +683,6 @@ impl Context {
683683
error!("Error when performing import: {:#010X}", ret);
684684
},
685685
)?;
686-
Private::try_from(Context::ffi_data_to_owned(out_private_ptr))
686+
Private::try_from(Context::ffi_data_to_owned(out_private_ptr)?)
687687
}
688688
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ impl Context {
6464
},
6565
)?;
6666
Ok((
67-
Timeout::try_from(Context::ffi_data_to_owned(out_timeout_ptr))?,
68-
AuthTicket::try_from(Context::ffi_data_to_owned(out_policy_ticket_ptr))?,
67+
Timeout::try_from(Context::ffi_data_to_owned(out_timeout_ptr)?)?,
68+
AuthTicket::try_from(Context::ffi_data_to_owned(out_policy_ticket_ptr)?)?,
6969
))
7070
}
7171

@@ -106,8 +106,8 @@ impl Context {
106106
},
107107
)?;
108108
Ok((
109-
Timeout::try_from(Context::ffi_data_to_owned(out_timeout_ptr))?,
110-
AuthTicket::try_from(Context::ffi_data_to_owned(out_policy_ticket_ptr))?,
109+
Timeout::try_from(Context::ffi_data_to_owned(out_timeout_ptr)?)?,
110+
AuthTicket::try_from(Context::ffi_data_to_owned(out_policy_ticket_ptr)?)?,
111111
))
112112
}
113113

@@ -533,7 +533,7 @@ impl Context {
533533
},
534534
)?;
535535

536-
Digest::try_from(Context::ffi_data_to_owned(policy_digest_ptr))
536+
Digest::try_from(Context::ffi_data_to_owned(policy_digest_ptr)?)
537537
}
538538

539539
/// Cause conditional gating of a policy based on NV written state.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ impl Context {
7070
error!("Error in creating primary key: {:#010X}", ret);
7171
},
7272
)?;
73-
let out_public_owned = Context::ffi_data_to_owned(out_public_ptr);
74-
let creation_data_owned = Context::ffi_data_to_owned(creation_data_ptr);
75-
let creation_hash_owned = Context::ffi_data_to_owned(creation_hash_ptr);
76-
let creation_ticket_owned = Context::ffi_data_to_owned(creation_ticket_ptr);
73+
let out_public_owned = Context::ffi_data_to_owned(out_public_ptr)?;
74+
let creation_data_owned = Context::ffi_data_to_owned(creation_data_ptr)?;
75+
let creation_hash_owned = Context::ffi_data_to_owned(creation_hash_ptr)?;
76+
let creation_ticket_owned = Context::ffi_data_to_owned(creation_ticket_ptr)?;
7777
let primary_key_handle = KeyHandle::from(object_handle);
7878
self.handle_manager
7979
.add_handle(primary_key_handle.into(), HandleDropAction::Flush)?;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ impl Context {
175175

176176
Ok((
177177
pcr_update_counter,
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))?,
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)?)?,
180180
))
181181
}
182182

0 commit comments

Comments
 (0)