Skip to content

Commit

Permalink
FAPI: Fix error handling in state machines 3.2.x
Browse files Browse the repository at this point in the history
* In some sub state machines, which had no async function, the
  state was not reset to the initial state in error cases.
* In some FAPI commands in the async part it was not checked
  whether the command is state is equal _FAPI_STATE_INIT.
* In several FAPI commands the current state of the FAPI
  context was not reset to _FAPI_STATE_INIT in error cases.
  Thus it was possible that a sequence error did occur after
  a normal error case.

Signed-off-by: Juergen Repp <[email protected]>
  • Loading branch information
JuergenReppSIT authored and AndreasFuchsTPM committed Jan 24, 2024
1 parent d0e8252 commit 77d254e
Show file tree
Hide file tree
Showing 30 changed files with 93 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/tss2-fapi/api/Fapi_AuthorizePolicy.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,6 @@ Fapi_AuthorizePolicy_Finish(
r = ifapi_cleanup_session(context);
try_again_or_error_goto(r, "Cleanup", cleanup);

context->state = _FAPI_STATE_INIT;
break;

statecasedefault(context->state);
Expand All @@ -383,6 +382,7 @@ Fapi_AuthorizePolicy_Finish(
ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
SAFE_FREE(command->policyPath);
SAFE_FREE(command->signingKeyPath);
context->state = _FAPI_STATE_INIT;
LOG_TRACE("finished");
return r;
}
2 changes: 1 addition & 1 deletion src/tss2-fapi/api/Fapi_ChangeAuth.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,6 @@ Fapi_ChangeAuth_Finish(
r = ifapi_cleanup_session(context);
try_again_or_error_goto(r, "Cleanup", error_cleanup);

context->state = _FAPI_STATE_INIT;
LOG_TRACE("success");
break;

Expand Down Expand Up @@ -609,5 +608,6 @@ Fapi_ChangeAuth_Finish(
SAFE_FREE(command->pathlist);
}
LOG_TRACE("finished");
context->state = _FAPI_STATE_INIT;
return r;
}
1 change: 1 addition & 0 deletions src/tss2-fapi/api/Fapi_CreateKey.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ Fapi_CreateKey_Finish(
ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
ifapi_cleanup_ifapi_object(context->loadKey.key_object);
ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
context->state = _FAPI_STATE_INIT;
LOG_TRACE("finished");
return r;
}
2 changes: 1 addition & 1 deletion src/tss2-fapi/api/Fapi_Decrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,6 @@ Fapi_Decrypt_Finish(
ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
ifapi_cleanup_ifapi_object(context->loadKey.key_object);

context->state = _FAPI_STATE_INIT;
return r;
}
11 changes: 6 additions & 5 deletions src/tss2-fapi/api/Fapi_Delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ Fapi_Delete_Finish(

/* Load the object metadata from the keystore. */
r = ifapi_keystore_load_async(&context->keystore, &context->io, path);
return_if_error2(r, "Could not open: %s", path);
goto_if_error2(r, "Could not open: %s", error_cleanup, path);

fallthrough;

Expand All @@ -559,7 +559,7 @@ Fapi_Delete_Finish(
TPM operations; e.g. persistent key or NV index. */
r = ifapi_keystore_load_finish(&context->keystore, &context->io, object);
return_try_again(r);
return_if_error_reset_state(r, "read_finish failed");
goto_if_error(r, "read_finish failed", error_cleanup);

/* Initialize the ESYS object for the persistent key or NV Index. */
r = ifapi_initialize_object(context->esys, object);
Expand All @@ -579,7 +579,7 @@ Fapi_Delete_Finish(
/* Check whether hierarchy file has been read. */
if (authObject->objectType == IFAPI_OBJ_NONE) {
r = ifapi_keystore_load_async(&context->keystore, &context->io, "/HS");
return_if_error2(r, "Could not open hierarchy /HS");
goto_if_error(r, "Could not open hierarchy /HS", error_cleanup);

command->auth_index = ESYS_TR_RH_OWNER;
} else {
Expand Down Expand Up @@ -624,15 +624,15 @@ Fapi_Delete_Finish(
statecase(context->state, ENTITY_DELETE_KEY);
if (object->misc.key.persistent_handle) {
r = ifapi_keystore_load_async(&context->keystore, &context->io, "/HS");
return_if_error2(r, "Could not open hierarchy /HS");
goto_if_error(r, "Could not open hierarchy /HS", error_cleanup);
}
fallthrough;

statecase(context->state, ENTITY_DELETE_KEY_WAIT_FOR_HIERARCHY);
if (object->misc.key.persistent_handle) {
r = ifapi_keystore_load_finish(&context->keystore, &context->io, authObject);
return_try_again(r);
return_if_error(r, "read_finish failed");
goto_if_error(r, "read_finish failed", error_cleanup);

r = ifapi_initialize_object(context->esys, authObject);
goto_if_error_reset_state(r, "Initialize hierarchy object", error_cleanup);
Expand Down Expand Up @@ -786,5 +786,6 @@ Fapi_Delete_Finish(
SAFE_FREE(command->pathlist);
ifapi_session_clean(context);
ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
context->state = _FAPI_STATE_INIT;
return r;
}
4 changes: 2 additions & 2 deletions src/tss2-fapi/api/Fapi_ExportPolicy.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,14 @@ Fapi_ExportPolicy_Finish(
/* Load the key meta data from the keystore. */
r = ifapi_keystore_load_async(&context->keystore, &context->io,
command->path);
return_if_error2(r, "Could not open: %s", command->path);
goto_if_error2(r, "Could not open: %s", error_cleanup, command->path);
fallthrough;

statecase(context->state, POLICY_EXPORT_READ_OBJECT_FINISH);
r = ifapi_keystore_load_finish(&context->keystore, &context->io,
&command->object);
return_try_again(r);
return_if_error_reset_state(r, "read_finish failed");
goto_if_error(r, "read_finish failed", error_cleanup);

goto_if_null2(command->object.policy,
"Object has no policy",
Expand Down
2 changes: 1 addition & 1 deletion src/tss2-fapi/api/Fapi_GetAppData.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ Fapi_GetAppData_Finish(
if (appDataSize)
*appDataSize = objAppData->size;

context->state = _FAPI_STATE_INIT;
r = TSS2_RC_SUCCESS;
break;

Expand All @@ -228,6 +227,7 @@ Fapi_GetAppData_Finish(
ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
ifapi_cleanup_ifapi_object(context->loadKey.key_object);
ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
context->state = _FAPI_STATE_INIT;
LOG_TRACE("finished");
return r;
}
4 changes: 4 additions & 0 deletions src/tss2-fapi/api/Fapi_GetCertificate.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ Fapi_GetCertificate_Async(
check_not_null(context);
check_not_null(path);

if (context->state != _FAPI_STATE_INIT) {
return_error(TSS2_FAPI_RC_BAD_SEQUENCE, "Invalid State");
}

r = ifapi_non_tpm_mode_init(context);
return_if_error(r, "Initialize GetCertificate");

Expand Down
4 changes: 4 additions & 0 deletions src/tss2-fapi/api/Fapi_GetDescription.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ Fapi_GetDescription_Async(
check_not_null(context);
check_not_null(path);

if (context->state != _FAPI_STATE_INIT) {
return_error(TSS2_FAPI_RC_BAD_SEQUENCE, "Invalid State");
}

/* Load the object metadata from keystore. */
r = ifapi_keystore_load_async(&context->keystore, &context->io, path);
return_if_error2(r, "Could not open: %s", path);
Expand Down
1 change: 1 addition & 0 deletions src/tss2-fapi/api/Fapi_GetEsysBlob.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,5 +401,6 @@ Fapi_GetEsysBlob_Finish(
ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
ifapi_cleanup_ifapi_object(context->loadKey.key_object);
ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
context->state = _FAPI_STATE_INIT;
return r;
}
1 change: 1 addition & 0 deletions src/tss2-fapi/api/Fapi_GetRandom.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ Fapi_GetRandom_Finish(
ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
ifapi_session_clean(context);
SAFE_FREE(context->get_random.data);
context->state = _FAPI_STATE_INIT;
LOG_TRACE("finished");
return r;
}
5 changes: 5 additions & 0 deletions src/tss2-fapi/api/Fapi_GetTpmBlobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ Fapi_GetTpmBlobs_Async(
check_not_null(context);
check_not_null(path);

if (context->state != _FAPI_STATE_INIT) {
return_error(TSS2_FAPI_RC_BAD_SEQUENCE, "Invalid State");
}

/* Load the object from the key store. */
r = ifapi_keystore_load_async(&context->keystore, &context->io, path);
return_if_error2(r, "Could not open: %s", path);
Expand Down Expand Up @@ -270,5 +274,6 @@ Fapi_GetTpmBlobs_Finish(
ifapi_cleanup_ifapi_object(context->loadKey.key_object);
ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
LOG_TRACE("finished");
context->state = _FAPI_STATE_INIT;
return r;
}
1 change: 1 addition & 0 deletions src/tss2-fapi/api/Fapi_Import.c
Original file line number Diff line number Diff line change
Expand Up @@ -668,5 +668,6 @@ Fapi_Import_Finish(
ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
ifapi_cleanup_ifapi_object(context->loadKey.key_object);
ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
context->state = _FAPI_STATE_INIT;
return r;
}
8 changes: 4 additions & 4 deletions src/tss2-fapi/api/Fapi_NvExtend.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ Fapi_NvExtend_Finish(
/* Compute Digest of the current event */
hashAlg = object->misc.nv.public.nvPublic.nameAlg;
r = ifapi_crypto_hash_start(&cryptoContext, hashAlg);
return_if_error(r, "crypto hash start");
goto_if_error(r, "crypto hash start", error_cleanup);

HASH_UPDATE_BUFFER(cryptoContext,
&auxData->buffer[0], auxData->size,
Expand All @@ -380,7 +380,7 @@ Fapi_NvExtend_Finish(
(uint8_t *)
&event->digests.digests[0].digest,
&hashSize);
return_if_error(r, "crypto hash finish");
goto_if_error(r, "crypto hash finish", error_cleanup);

event->digests.digests[0].hashAlg = hashAlg;
event->digests.count = 1;
Expand Down Expand Up @@ -448,15 +448,14 @@ Fapi_NvExtend_Finish(
/* Finish writing the NV object to the key store */
r = ifapi_keystore_store_finish(&context->io);
return_try_again(r);
return_if_error_reset_state(r, "write_finish failed");
goto_if_error(r, "write_finish failed", error_cleanup);
fallthrough;

statecase(context->state, NV_EXTEND_CLEANUP)
/* Cleanup the session. */
r = ifapi_cleanup_session(context);
try_again_or_error_goto(r, "Cleanup", error_cleanup);

context->state = _FAPI_STATE_INIT;
r = TSS2_RC_SUCCESS;

break;
Expand All @@ -483,5 +482,6 @@ Fapi_NvExtend_Finish(
SAFE_FREE(object->misc.nv.event_log);
ifapi_session_clean(context);
LOG_TRACE("finished");
context->state = _FAPI_STATE_INIT;
return r;
}
2 changes: 1 addition & 1 deletion src/tss2-fapi/api/Fapi_NvIncrement.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ Fapi_NvIncrement_Finish(
r = ifapi_cleanup_session(context);
try_again_or_error_goto(r, "Cleanup", error_cleanup);

context->state = _FAPI_STATE_INIT;
break;

statecasedefault(context->state);
Expand All @@ -351,6 +350,7 @@ Fapi_NvIncrement_Finish(
SAFE_FREE(command->nvPath);
SAFE_FREE(jso);
ifapi_session_clean(context);
context->state = _FAPI_STATE_INIT;
LOG_TRACE("finished");
return r;
}
2 changes: 1 addition & 1 deletion src/tss2-fapi/api/Fapi_NvRead.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ Fapi_NvRead_Finish(
*data = command->rdata;
if (size)
*size = command->size;
context->state = _FAPI_STATE_INIT;
break;

statecasedefault(context->state);
Expand All @@ -345,6 +344,7 @@ Fapi_NvRead_Finish(
SAFE_FREE(command->nvPath);
//SAFE_FREE(context->nv_cmd.tes);
ifapi_session_clean(context);
context->state = _FAPI_STATE_INIT;
LOG_TRACE("finished");
return r;
}
5 changes: 2 additions & 3 deletions src/tss2-fapi/api/Fapi_NvSetBits.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ Fapi_NvSetBits_Finish(
/* Finish writing the NV object to the key store */
r = ifapi_keystore_store_finish(&context->io);
return_try_again(r);
return_if_error_reset_state(r, "write_finish failed");
goto_if_error(r, "write_finish failed", error_cleanup);

fallthrough;

Expand All @@ -345,9 +345,7 @@ Fapi_NvSetBits_Finish(
r = ifapi_cleanup_session(context);
try_again_or_error_goto(r, "Cleanup", error_cleanup);

context->state = _FAPI_STATE_INIT;
LOG_DEBUG("success");

break;

statecasedefault(context->state);
Expand All @@ -362,6 +360,7 @@ Fapi_NvSetBits_Finish(
ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
ifapi_cleanup_ifapi_object(context->loadKey.key_object);
ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
context->state = _FAPI_STATE_INIT;
LOG_TRACE("finished");
return r;
}
3 changes: 1 addition & 2 deletions src/tss2-fapi/api/Fapi_NvWrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ Fapi_NvWrite_Finish(
r = ifapi_cleanup_session(context);
try_again_or_error_goto(r, "Cleanup", error_cleanup);

context->state = _FAPI_STATE_INIT;
break;

statecasedefault(context->state);
Expand All @@ -307,7 +306,7 @@ Fapi_NvWrite_Finish(
SAFE_FREE(command->data);
SAFE_FREE(jso);
ifapi_session_clean(context);

context->state = _FAPI_STATE_INIT;
LOG_TRACE("finished");
return r;
}
6 changes: 3 additions & 3 deletions src/tss2-fapi/api/Fapi_PcrExtend.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ Fapi_PcrExtend_Finish(
/* Construct the filename for the eventlog file */
r = ifapi_asprintf(&command->event_log_file, "%s/%s%i",
context->eventlog.log_dir, IFAPI_PCR_LOG_FILE, command->pcrIndex);
return_if_error(r, "Out of memory.");
return_if_error_reset_state(r, "Out of memory.");

/* Check wheter the event log has to be read. */
if (ifapi_io_path_exists(command->event_log_file)) {
Expand Down Expand Up @@ -296,7 +296,7 @@ Fapi_PcrExtend_Finish(
r = Esys_PCR_Event_Async(context->esys, command->pcrIndex,
context->session1, ESYS_TR_NONE, ESYS_TR_NONE,
&command->event);
return_if_error(r, "Esys_PCR_Event_Async");
goto_if_error(r, "Esys_PCR_Event_Async", error_cleanup);
command->event_digests = NULL;

fallthrough;
Expand Down Expand Up @@ -333,7 +333,6 @@ Fapi_PcrExtend_Finish(
r = ifapi_cleanup_session(context);
try_again_or_error_goto(r, "Cleanup", error_cleanup);

context->state = _FAPI_STATE_INIT;
break;

statecasedefault(context->state);
Expand All @@ -350,6 +349,7 @@ Fapi_PcrExtend_Finish(
ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
ifapi_cleanup_event(pcrEvent);
ifapi_session_clean(context);
context->state = _FAPI_STATE_INIT;
LOG_TRACE("finished");
return r;
}
2 changes: 1 addition & 1 deletion src/tss2-fapi/api/Fapi_PcrRead.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ Fapi_PcrRead_Finish(
*pcrValue = command->pcrValue;
if (pcrValueSize)
*pcrValueSize = command->pcrValueSize;
context->state = _FAPI_STATE_INIT;
break;

statecasedefault(context->state);
Expand All @@ -279,6 +278,7 @@ Fapi_PcrRead_Finish(
SAFE_FREE(command->pcrValue);
}
SAFE_FREE(command->pcrValues);
context->state = _FAPI_STATE_INIT;
LOG_TRACE("finished");
return r;
}
7 changes: 4 additions & 3 deletions src/tss2-fapi/api/Fapi_Provision.c
Original file line number Diff line number Diff line change
Expand Up @@ -906,9 +906,9 @@ Fapi_Provision_Finish(FAPI_CONTEXT *context)
if (command->auth_state & TPMA_PERMANENT_LOCKOUTAUTHSET) {
hierarchy_lockout->misc.hierarchy.with_auth = TPM2_YES;
r = ifapi_get_description(hierarchy_lockout, &description);
return_if_error(r, "Get description");
goto_if_error(r, "Get description", error_cleanup);
r = ifapi_set_auth(context, hierarchy_lockout, description);
return_if_error(r, "Set auth value");
goto_if_error(r, "Set auth value", error_cleanup);
} else {
hierarchy_lockout->misc.hierarchy.with_auth = TPM2_NO;
}
Expand Down Expand Up @@ -1269,7 +1269,7 @@ Fapi_Provision_Finish(FAPI_CONTEXT *context)
/* Finish writing the endorsement hierarchy to the key store */
r = ifapi_keystore_store_finish(&context->io);
return_try_again(r);
return_if_error_reset_state(r, "write_finish failed");
goto_if_error_reset_state(r, "write_finish failed", error_cleanup);

/* Write all endorsement hierarchies. */
command->hierarchy = hierarchy_he;
Expand Down Expand Up @@ -1541,6 +1541,7 @@ Fapi_Provision_Finish(FAPI_CONTEXT *context)
}
SAFE_FREE(command->pathlist);
}
context->state = _FAPI_STATE_INIT;
LOG_TRACE("finished");
return r;
}
Loading

0 comments on commit 77d254e

Please sign in to comment.