Skip to content

Commit ce608d4

Browse files
authored
Merge pull request #390 from ionut-arm/handles-fix
Fix handle management for static resources
2 parents a0d39dd + c030ba6 commit ce608d4

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ impl HandleManager {
4040
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
4141
}
4242

43-
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));
43+
// The TSS might return the same handle, see #383
44+
if let Some(stored_handle_drop_action) = self.open_handles.get(&handle) {
45+
if handle_drop_action != *stored_handle_drop_action {
46+
error!("Handle drop action inconsistency");
47+
return Err(Error::local_error(WrapperErrorKind::InconsistentParams));
48+
}
4649
}
4750
let _ = self.open_handles.insert(handle, handle_drop_action);
4851
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: 10 additions & 4 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();
@@ -81,12 +80,15 @@ fn list() {
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+
.unwrap_or_else(|_| owner_nv_index_handle.into());
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();
@@ -98,8 +100,12 @@ fn read_full() {
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+
.unwrap_or_else(|_| owner_nv_index_handle.into());
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)