Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESYS: Add reference counting for Esys_TR_FromTPMPublic #2707

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/tss2-esys/api/Esys_FlushContext.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ Esys_FlushContext_Finish(
ESYS_CONTEXT *esysContext)
{
TSS2_RC r;
RSRC_NODE_T *flushHandleNode;
LOG_TRACE("context=%p",
esysContext);

Expand Down Expand Up @@ -244,6 +245,12 @@ Esys_FlushContext_Finish(
"Received error from SAPI unmarshaling" );

/* The ESYS_TR object has to be invalidated */

r = esys_GetResourceObject(esysContext, esysContext->in.FlushContext.flushHandle,
&flushHandleNode);
return_state_if_error(r, _ESYS_STATE_INIT, "flushHandle unknown.");

flushHandleNode->reference_count = 0;
r = Esys_TR_Close(esysContext, &esysContext->in.FlushContext.flushHandle);
return_if_error(r, "invalidate object");

Expand Down
1 change: 1 addition & 0 deletions src/tss2-esys/esys_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef struct RSRC_NODE_T {
to reference this entry. */
TPM2B_AUTH auth; /**< The authValue for this resource object. */
IESYS_RESOURCE rsrc; /**< The meta data for this resource object. */
size_t reference_count; /**< Reference Count for Esys_TR_FromTPMPublic */
struct RSRC_NODE_T * next; /**< The next object in the linked list. */
} RSRC_NODE_T;

Expand Down
5 changes: 5 additions & 0 deletions src/tss2-esys/esys_tr.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ Esys_TR_FromTPMPublic_Finish(ESYS_CONTEXT * esys_context, ESYS_TR * object)
return_if_error(r, "Error TR FromTPMPublic");
return TSS2_ESYS_RC_TRY_AGAIN;
} else {
objectHandleNode->reference_count++;
*object = objectHandle;
return TSS2_RC_SUCCESS;
}
Expand Down Expand Up @@ -425,6 +426,10 @@ Esys_TR_Close(ESYS_CONTEXT * esys_context, ESYS_TR * object)
node != NULL;
update_ptr = &node->next, node = node->next) {
if (node->esys_handle == *object) {
if (node->reference_count > 1) {
node->reference_count--;
return TSS2_RC_SUCCESS;
}
*update_ptr = node->next;
SAFE_FREE(node);
*object = ESYS_TR_NONE;
Expand Down
10 changes: 9 additions & 1 deletion test/integration/esys-tr-fromTpmPublic-key.int.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,21 @@ test_esys_tr_fromTpmPublic_key(ESYS_CONTEXT * ectx)
goto_if_error(r, "Flushing primary", error_name1);

r = Esys_TR_Close(ectx, &keyHandle);
goto_if_error(r, "TR close on nv object", error_name1);
goto_if_error(r, "TR close on object", error_name1);

r = Esys_TR_FromTPMPublic(ectx, TPM2_PERSISTENT_FIRST,
ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
&keyHandle);
goto_if_error(r, "TR from TPM public", error_name1);

r = Esys_TR_FromTPMPublic(ectx, TPM2_PERSISTENT_FIRST,
ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
&keyHandle);
goto_if_error(r, "TR from TPM public", error_name1);

r = Esys_TR_Close(ectx, &keyHandle);
goto_if_error(r, "TR close on object", error_name1)

LOG_ERROR("Key handle (2) 0x%x", keyHandle);

r = Esys_TR_GetName(ectx, keyHandle, &name2);
Expand Down
Loading