Skip to content

Commit

Permalink
ESYS: Add reference counting for Esys_TR_FromTPMPublic.
Browse files Browse the repository at this point in the history
For every call of Esys_TR_FromTPMPublic the reference counter is incremented
to ensure that the created esys object is only freed by Esys_Close if
the reference count will be zero or decreased to zero.
Fixes: #2693

Signed-off-by: Juergen Repp <[email protected]>
  • Loading branch information
JuergenReppSIT authored and AndreasFuchsTPM committed Nov 29, 2023
1 parent 8fe6bcf commit 4bebc57
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
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 @@ -23,6 +23,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

0 comments on commit 4bebc57

Please sign in to comment.