Skip to content

Commit 36fe4fe

Browse files
committed
Fix handle management for static resources
Making "duplicate" handles acceptable, as per the convo in #383. Also fixing tests that are impacted by this, and documenting it. Co-authored-by: Ionut Mihalcea <[email protected]> Co-authored-by: Jesper Brynolf <[email protected]> Signed-off-by: Ionut Mihalcea <[email protected]>
1 parent a0d39dd commit 36fe4fe

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

tss-esapi/src/abstraction/nv.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ pub fn read_full(
4242
}
4343

4444
/// Returns the NvPublic and Name associated with an NV index TPM handle
45+
///
46+
/// NOTE: This call _may_ close existing ESYS handles to the NV Index.
4547
fn get_nv_index_info(
4648
context: &mut Context,
4749
nv_index_tpm_handle: NvIndexTpmHandle,
@@ -63,6 +65,8 @@ fn get_nv_index_info(
6365
}
6466

6567
/// Lists all the currently defined NV Indexes' names and public components
68+
///
69+
/// NOTE: This call _may_ close existing ESYS handles to the existing NV Indexes.
6670
pub fn list(context: &mut Context) -> Result<Vec<(NvPublic, Name)>> {
6771
context.execute_without_session(|ctx| {
6872
ctx.get_capability(
@@ -166,6 +170,8 @@ pub fn max_nv_buffer_size(ctx: &mut Context) -> Result<usize> {
166170
/// Provides methods and trait implementations to interact with a non-volatile storage index that has been opened.
167171
///
168172
/// Use [`NvOpenOptions::open`] to obtain an [`NvReaderWriter`] object.
173+
///
174+
/// NOTE: When the `NvReaderWriter` is dropped, any existing ESYS handles to NV Indexes _may_ be closed.
169175
#[derive(Debug)]
170176
pub struct NvReaderWriter<'a> {
171177
context: &'a mut Context,

tss-esapi/src/context/handle_manager.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ impl HandleManager {
4141
}
4242

4343
if self.open_handles.contains_key(&handle) {
44-
error!("Handle({}) is already open", ESYS_TR::from(handle));
45-
return Err(Error::local_error(WrapperErrorKind::InvalidHandleState));
44+
// It is safe to call unwrap because the existance of the key has already been verified.
45+
let stored_handle_drop_action = self.open_handles.get(&handle).unwrap();
46+
if handle_drop_action != *stored_handle_drop_action {
47+
error!("Handle drop action inconsistency");
48+
return Err(Error::local_error(WrapperErrorKind::InconsistentParams));
49+
}
4650
}
4751
let _ = self.open_handles.insert(handle, handle_drop_action);
4852
Ok(())

tss-esapi/tests/integration_tests/abstraction_tests/ek_tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use tss_esapi::{
1010

1111
use crate::common::create_ctx_without_session;
1212

13-
#[cfg_attr(tpm2_tss_version = "4", ignore = "issues with tpm2-tss")]
1413
#[test]
1514
fn test_retrieve_ek_pubcert() {
1615
let mut context = create_ctx_without_session();

tss-esapi/tests/integration_tests/abstraction_tests/nv_tests.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ fn write_nv_index(context: &mut Context, nv_index: NvIndexTpmHandle) -> NvIndexH
6060
owner_nv_index_handle
6161
}
6262

63-
#[cfg_attr(tpm2_tss_version = "4", ignore = "issues with tpm2-tss")]
6463
#[test]
6564
fn list() {
6665
let mut context = create_ctx_with_session();
@@ -73,33 +72,40 @@ fn list() {
7372
.map(|(public, _)| public.nv_index())
7473
.any(|x| x == nv_index));
7574

76-
let owner_nv_index_handle = write_nv_index(&mut context, nv_index);
75+
let _owner_nv_index_handle = write_nv_index(&mut context, nv_index);
7776

7877
assert!(nv::list(&mut context)
7978
.unwrap()
8079
.iter()
8180
.map(|(public, _)| public.nv_index())
8281
.any(|x| x == nv_index));
8382

83+
// Need to get the ESYS handle again, as it was closed by nv::list above
84+
let owner_nv_index_handle = context
85+
.tr_from_tpm_public(nv_index.into())
86+
.expect("Failed to get ObjectHandle for NV Index");
8487
context
85-
.nv_undefine_space(Provision::Owner, owner_nv_index_handle)
88+
.nv_undefine_space(Provision::Owner, owner_nv_index_handle.into())
8689
.expect("Call to nv_undefine_space failed");
8790
}
8891

89-
#[cfg_attr(tpm2_tss_version = "4", ignore = "issues with tpm2-tss")]
9092
#[test]
9193
fn read_full() {
9294
let mut context = create_ctx_with_session();
9395

9496
let nv_index = NvIndexTpmHandle::new(0x01500015).unwrap();
9597

96-
let owner_nv_index_handle = write_nv_index(&mut context, nv_index);
98+
let _owner_nv_index_handle = write_nv_index(&mut context, nv_index);
9799

98100
// Now read it back
99101
let read_result = nv::read_full(&mut context, NvAuth::Owner, nv_index);
100102

103+
// Need to get the ESYS handle again, as it was closed by nv::read_full above
104+
let owner_nv_index_handle = context
105+
.tr_from_tpm_public(nv_index.into())
106+
.expect("Failed to get ObjectHandle for NV Index");
101107
context
102-
.nv_undefine_space(Provision::Owner, owner_nv_index_handle)
108+
.nv_undefine_space(Provision::Owner, owner_nv_index_handle.into())
103109
.expect("Call to nv_undefine_space failed");
104110

105111
let read_result = read_result.unwrap();

0 commit comments

Comments
 (0)