Skip to content

Commit

Permalink
options: fix TCTI handling
Browse files Browse the repository at this point in the history
A command that should work with no options would fail:

./tools/tpm2 nvdefine
ERROR:esys:src/tss2-esys/api/Esys_GetCapability.c:164:Esys_GetCapability_Async() esyscontext is NULL.
ERROR:esys:src/tss2-esys/api/Esys_GetCapability.c:83:Esys_GetCapability() Error in async function ErrorCode (0x00070005)
ERROR: Esys_NV_ReadPublic(0x70005) - esapi:A pointer is NULL that isn't allowed to be NULL.
ERROR: Unable to run nvdefine

The handling for the optional and fake TCTI states would get conflated
and not initialize an ESAPI context. Clean up the handling and make
supporting a fake TCTI it's own option bit to make it less confusing to
code.

After this the tool works as expected:
./tools/tpm2 nvdefine
nv-index: 0x1000000

Fixes: tpm2-software#3314
Signed-off-by: Bill Roberts <[email protected]>
  • Loading branch information
williamcroberts committed Dec 11, 2023
1 parent 7156cae commit 8a1efe5
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 42 deletions.
37 changes: 17 additions & 20 deletions lib/tpm2_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,44 +434,43 @@ tpm2_option_code tpm2_handle_options(int argc, char **argv,
* SAPI
*/
bool is_sapi =
(!tool_opts || !tool_opts->flags);
!tool_opts || !(tool_opts->flags & TPM2_OPTIONS_NO_SAPI);

/*
* NO_SAPI
*/
bool is_no_sapi =
(tool_opts && tool_opts->flags & TPM2_OPTIONS_NO_SAPI);
/* tool doesn't use sapi, skip tcti checks and continue */
if (is_no_sapi) {
bool is_optional_sapi =
(tool_opts && (tool_opts->flags & TPM2_OPTIONS_OPTIONAL_SAPI));

/* tool doesn't REQUIRE the use sapi, skip tcti checks and continue */
if (!is_sapi && !is_optional_sapi) {
if (flags->tcti_none && !flags->quiet) {
LOG_WARN("Tool does not use SAPI. Continuing with tcti=none");
}
goto out;
}

/*
* OPTIONAL_SAPI
*/
bool is_optional_sapi =
(tool_opts && tool_opts->flags & TPM2_OPTIONS_OPTIONAL_SAPI);
bool is_fake_tcti = (flags->tcti_none && tool_opts &&
(tool_opts->flags & TPM2_OPTIONS_FAKE_TCTI));

/*
* Actions when tcti is "none"
* get the TCTI variable from the env if user didn't specify
* on command line. We cant' use flags->tcti_none until we
* check the env!
*/
bool is_tcti_from_env =
(!is_no_sapi && tcti_conf_option == 0);
((is_sapi || is_optional_sapi) && !tcti_conf_option);
if (is_tcti_from_env) {
tcti_conf_option = tpm2_util_getenv(TPM2TOOLS_ENV_TCTI);
flags->tcti_none = !strcmp(tcti_conf_option, "none");
}

if (flags->tcti_none && is_sapi) {
/* A tool the needs a SAPI (and not a fake one) should fail */
if (flags->tcti_none && !is_fake_tcti && !is_optional_sapi && is_sapi) {
LOG_ERR("Requested no tcti, but tool requires TCTI.");
rc = tpm2_option_code_err;
goto out;
}

/* tool doesn't request a sapi, don't initialize one */
if (flags->tcti_none && is_optional_sapi) {
if (flags->tcti_none && is_optional_sapi && !is_fake_tcti) {
if (!flags->quiet) {
LOG_WARN("Tool optionally uses SAPI. Continuing with tcti=none");
}
Expand All @@ -491,9 +490,7 @@ tpm2_option_code tpm2_handle_options(int argc, char **argv,
.finalize = tcti_fake_finalize
};

bool is_optional_fake_tcti = (flags->tcti_none && tool_opts &&
tool_opts->flags & TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI);
if (is_optional_fake_tcti) {
if (is_fake_tcti) {
if (!flags->quiet) {
LOG_WARN("Tool optionally uses SAPI. Continuing with tcti=fake");
}
Expand Down
6 changes: 3 additions & 3 deletions lib/tpm2_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ typedef bool (*tpm2_arg_handler)(int argc, char **argv);
* TPM2_OPTIONS_NO_SAPI:
* Skip SAPI initialization. Removes the "-T" common option.
*/
#define TPM2_OPTIONS_NO_SAPI 0x1
#define TPM2_OPTIONS_OPTIONAL_SAPI 0x2
#define TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI 0x4
#define TPM2_OPTIONS_NO_SAPI (1 << 0)
#define TPM2_OPTIONS_OPTIONAL_SAPI (1 << 1)
#define TPM2_OPTIONS_FAKE_TCTI (1 << 3)

struct tpm2_options {
struct {
Expand Down
4 changes: 2 additions & 2 deletions tools/tpm2_nvcertify.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ static tool_rc check_options(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
* none we fall back to the old behavior of reading from a define NV index
*
* Also, tcti is setup to a fake_tcti when tcti is specified "none" as the
* tool option affords TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI.
* tool option affords TPM2_OPTIONS_FAKE_TCTI.
*
* If NVindex name is not specified and tcti is not none, it is expected
* that the NV index is actually define. This behavior complies with the
Expand Down Expand Up @@ -535,7 +535,7 @@ static bool tpm2_tool_onstart(tpm2_options **opts) {
};

*opts = tpm2_options_new("C:P:c:p:g:s:f:o:q:S:n:", ARRAY_LEN(topts), topts,
on_option, on_arg, TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI);
on_option, on_arg, TPM2_OPTIONS_FAKE_TCTI);

return *opts != NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion tools/tpm2_nvdefine.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ static bool tpm2_tool_onstart(tpm2_options **opts) {
};

*opts = tpm2_options_new("S:C:s:a:P:p:L:g:", ARRAY_LEN(topts), topts,
on_option, on_arg, TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI);
on_option, on_arg, TPM2_OPTIONS_FAKE_TCTI);

return *opts != NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion tools/tpm2_nvextend.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ static bool tpm2_tool_onstart(tpm2_options **opts) {
};

*opts = tpm2_options_new("S:C:P:i:n:", ARRAY_LEN(topts), topts, on_option,
on_arg, TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI);
on_arg, TPM2_OPTIONS_FAKE_TCTI);

return *opts != NULL;
}
Expand Down
4 changes: 2 additions & 2 deletions tools/tpm2_nvincrement.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ static tool_rc check_options(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
* none we fall back to the old behavior of reading from a define NV index
*
* Also, tcti is setup to a fake_tcti when tcti is specified "none" as the
* tool option affords TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI.
* tool option affords TPM2_OPTIONS_FAKE_TCTI.
*
* If NVindex name is not specified and tcti is not none, it is expected
* that the NV index is actually define. This behavior complies with the
Expand Down Expand Up @@ -292,7 +292,7 @@ static bool tpm2_tool_onstart(tpm2_options **opts) {
};

*opts = tpm2_options_new("C:P:S:n:", ARRAY_LEN(topts), topts, on_option,
on_arg, TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI);
on_arg, TPM2_OPTIONS_FAKE_TCTI);

return *opts != NULL;
}
Expand Down
4 changes: 2 additions & 2 deletions tools/tpm2_nvread.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ static tool_rc check_options(tpm2_option_flags flags) {
* none we fall back to the old behavior of reading from a define NV index
*
* Also, tcti is setup to a fake_tcti when tcti is specified "none" as the
* tool option affords TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI.
* tool option affords TPM2_OPTIONS_FAKE_TCTI.
*
* If NVindex name is not specified and tcti is not none, it is expected
* that the NV index is actually define. This behavior complies with the
Expand Down Expand Up @@ -361,7 +361,7 @@ static bool tpm2_tool_onstart(tpm2_options **opts) {
};

*opts = tpm2_options_new("C:s:o:P:n:S:", ARRAY_LEN(topts), topts, on_option,
on_arg, TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI);
on_arg, TPM2_OPTIONS_FAKE_TCTI);

if (ctx.is_yaml) {
ctx.offset = 0;
Expand Down
4 changes: 2 additions & 2 deletions tools/tpm2_nvreadlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ static tool_rc check_options(tpm2_option_flags flags) {
* none we fall back to the old behavior of reading from a define NV index
*
* Also, tcti is setup to a fake_tcti when tcti is specified "none" as the
* tool option affords TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI.
* tool option affords TPM2_OPTIONS_FAKE_TCTI.
*
* If NVindex name is not specified and tcti is not none, it is expected
* that the NV index is actually define. This behavior complies with the
Expand Down Expand Up @@ -278,7 +278,7 @@ static bool tpm2_tool_onstart(tpm2_options **opts) {
};

*opts = tpm2_options_new("C:P:S:n:", ARRAY_LEN(topts), topts, on_option,
on_arg, TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI);
on_arg, TPM2_OPTIONS_FAKE_TCTI);

return *opts != NULL;
}
Expand Down
4 changes: 2 additions & 2 deletions tools/tpm2_nvreadpublic.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ static tool_rc check_options(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
* none we fall back to the old behavior of reading from a define NV index
*
* Also, tcti is setup to a fake_tcti when tcti is specified "none" as the
* tool option affords TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI.
* tool option affords TPM2_OPTIONS_FAKE_TCTI.
*
* If NVindex name is not specified and tcti is not none, it is expected
* that the NV index is actually define. This behavior complies with the
Expand Down Expand Up @@ -381,7 +381,7 @@ static bool tpm2_tool_onstart(tpm2_options **opts) {
};

*opts = tpm2_options_new("S:n:", ARRAY_LEN(topts), topts, on_option, on_arg,
TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI);
TPM2_OPTIONS_FAKE_TCTI);

return *opts != 0;
}
Expand Down
2 changes: 1 addition & 1 deletion tools/tpm2_nvsetbits.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ static bool tpm2_tool_onstart(tpm2_options **opts) {
};

*opts = tpm2_options_new("C:P:i:S:n:", ARRAY_LEN(topts), topts, on_option,
on_arg, TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI);
on_arg, TPM2_OPTIONS_FAKE_TCTI);

return *opts != NULL;
}
Expand Down
4 changes: 2 additions & 2 deletions tools/tpm2_nvundefine.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ static tool_rc check_options(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
* none we fall back to the old behavior of reading from a define NV index
*
* Also, tcti is setup to a fake_tcti when tcti is specified "none" as the
* tool option affords TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI.
* tool option affords TPM2_OPTIONS_FAKE_TCTI.
*
* If NVindex name is not specified and tcti is not none, it is expected
* that the NV index is actually define. This behavior complies with the
Expand Down Expand Up @@ -413,7 +413,7 @@ static bool tpm2_tool_onstart(tpm2_options **opts) {
};

*opts = tpm2_options_new("C:P:S:n:", ARRAY_LEN(topts), topts, on_option,
on_arg, TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI);
on_arg, TPM2_OPTIONS_FAKE_TCTI);

return *opts != NULL;
}
Expand Down
4 changes: 2 additions & 2 deletions tools/tpm2_nvwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ static tool_rc check_options(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
* none we fall back to the old behavior of reading from a define NV index
*
* Also, tcti is setup to a fake_tcti when tcti is specified "none" as the
* tool option affords TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI.
* tool option affords TPM2_OPTIONS_FAKE_TCTI.
*
* If NVindex name is not specified and tcti is not none, it is expected
* that the NV index is actually define. This behavior complies with the
Expand Down Expand Up @@ -432,7 +432,7 @@ static bool tpm2_tool_onstart(tpm2_options **opts) {
};

*opts = tpm2_options_new("C:P:i:S:n:", ARRAY_LEN(topts), topts, on_option,
on_arg, TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI);
on_arg, TPM2_OPTIONS_FAKE_TCTI);

return *opts != NULL;
}
Expand Down
4 changes: 2 additions & 2 deletions tools/tpm2_nvwritelock.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ static tool_rc check_options(tpm2_option_flags flags) {
* none we fall back to the old behavior of reading from a define NV index
*
* Also, tcti is setup to a fake_tcti when tcti is specified "none" as the
* tool option affords TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI.
* tool option affords TPM2_OPTIONS_FAKE_TCTI.
*
* If NVindex name is not specified and tcti is not none, it is expected
* that the NV index is actually define. This behavior complies with the
Expand Down Expand Up @@ -313,7 +313,7 @@ static bool tpm2_tool_onstart(tpm2_options **opts) {
};

*opts = tpm2_options_new("C:P:S:n:", ARRAY_LEN(topts), topts, on_option,
on_arg, TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI);
on_arg, TPM2_OPTIONS_FAKE_TCTI);

return *opts != NULL;
}
Expand Down

0 comments on commit 8a1efe5

Please sign in to comment.