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

swtpm: Search for all state files and use abstracted names in JSON #557

Merged
merged 1 commit into from
Sep 25, 2021
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
65 changes: 32 additions & 33 deletions src/swtpm/swtpm_nvstore.c
Original file line number Diff line number Diff line change
Expand Up @@ -1314,49 +1314,48 @@ TPM_RESULT SWTPM_NVRAM_SetStateBlob(unsigned char *data,
}

/* Example JSON output:
* { "type": "swtpm", "states":
* [ { "name": "tpm2-00.permall" } ]
* { "type": "swtpm",
* "states": [ "permall", "volatilestate", "savestate" ]
* }
*/
int SWTPM_NVRAM_PrintJson(void)
{
TPM_RESULT rc = 0;
int ret = 0, n;
uint32_t tpm_number = 0;
char filename[FILENAME_MAX];
char *state_str = NULL;
const char *backend_uri = NULL;

if (rc == 0)
rc = SWTPM_NVRAM_GetFilenameForName(filename, sizeof(filename),
tpm_number, TPM_PERMANENT_ALL_NAME,
false);
if (rc == 0)
rc = SWTPM_NVRAM_Init();
const char *backend_uri;
const char *states[] = {
TPM_PERMANENT_ALL_NAME,
TPM_VOLATILESTATE_NAME,
TPM_SAVESTATE_NAME,
};
char state_str[64] = "";
size_t i, n, o;
int ret = -1;

rc = SWTPM_NVRAM_Init();
if (rc == 0) {
o = 0;
backend_uri = tpmstate_get_backend_uri();
rc = g_nvram_backend_ops->check_state(backend_uri, TPM_PERMANENT_ALL_NAME);
if (rc == TPM_SUCCESS) {
n = asprintf(&state_str, " { \"name\": \"%s\" } ", filename);
if (n < 0) {
logprintf(STDERR_FILENO, "Out of memory\n");
state_str = NULL;
ret = -1;
goto cleanup;

for (i = 0; i < ARRAY_LEN(states); i++) {
rc = g_nvram_backend_ops->check_state(backend_uri, states[i]);
if (rc == TPM_SUCCESS) {
n = snprintf(&state_str[o], sizeof(state_str) - o,
"%s \"%s\"",
(o > 0) ? "," : "",
states[i]);
if (n >= sizeof(state_str) - o)
goto exit;
o += n;
} else if (rc != TPM_RETRY) {
/* Error other than ENOENT */
goto exit;
}
} else if (rc != TPM_RETRY) {
/* Error other than ENOENT */
ret = -1;
goto cleanup;
}
printf("{ \"type\": \"swtpm\", \"states\": [%s%s] }",
state_str, (o > 0) ? " ": "");
ret = 0;
}

printf("{ \"type\": \"swtpm\", \"states\": [%s] }", state_str ? state_str : "");
} else
ret = -1;

cleanup:
free(state_str);

exit:
return ret;
}
12 changes: 5 additions & 7 deletions src/swtpm_setup/swtpm_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <glib-object.h>
#include <json-glib/json-glib.h>

#include <libtpms/tpm_nvfilename.h>

#include "swtpm.h"
#include "swtpm_setup_conf.h"
#include "swtpm_setup_utils.h"
Expand Down Expand Up @@ -696,7 +698,6 @@ static int init_tpm(unsigned long flags, gchar **swtpm_prg_l, const gchar *confi
static int check_state_overwrite(gchar **swtpm_prg_l, unsigned int flags,
const char *tpm_state_path)
{
const char *statefile;
gboolean success;
g_autofree gchar *standard_output = NULL;
int exit_status = 0;
Expand All @@ -712,11 +713,8 @@ static int check_state_overwrite(gchar **swtpm_prg_l, unsigned int flags,
NULL
}, NULL, FALSE);

if (flags & SETUP_TPM2_F) {
statefile = "tpm2-00.permall";
if (flags & SETUP_TPM2_F)
my_argv = concat_arrays(my_argv, (gchar*[]) { "--tpm2", NULL }, TRUE);
} else
statefile = "tpm-00.permall";

argv = concat_arrays(swtpm_prg_l, my_argv, FALSE);
success = g_spawn_sync(NULL, argv, NULL, G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL,
Expand All @@ -732,15 +730,15 @@ static int check_state_overwrite(gchar **swtpm_prg_l, unsigned int flags,
return 1;
}

if (g_strstr_len(standard_output, -1, statefile) != NULL) {
if (g_strstr_len(standard_output, -1, TPM_PERMANENT_ALL_NAME) != NULL) {
/* State file exists */
if (flags & SETUP_STATE_NOT_OVERWRITE_F) {
logit(gl_LOGFILE, "Not overwriting existing state file.\n");
return 2;
}
if (flags & SETUP_STATE_OVERWRITE_F)
return 0;
logerr(gl_LOGFILE, "Found existing TPM state file %s.\n", statefile);
logerr(gl_LOGFILE, "Found existing TPM state '%s'.\n", TPM_PERMANENT_ALL_NAME);
return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/_test_print_states
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ if [ $? -ne 0 ]; then
exit 1
fi

exp='\{ "type": "swtpm", "states": \[ \{ "name": "tpm-00.permall" \} \] \}'
exp='\{ "type": "swtpm", "states": \[ "permall" \] \}'
if ! [[ ${msg} =~ ${exp} ]]; then
echo "Unexpected response from ${SWTPM_IFACE} TPM to --print-states:"
echo "Actual : ${msg}"
Expand Down
2 changes: 1 addition & 1 deletion tests/_test_tpm2_print_states
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ if [ $? -ne 0 ]; then
exit 1
fi

exp='\{ "type": "swtpm", "states": \[ \{ "name": "tpm2-00.permall" \} \] \}'
exp='\{ "type": "swtpm", "states": \[ "permall" \] \}'
if ! [[ ${msg} =~ ${exp} ]]; then
echo "Unexpected response from ${SWTPM_IFACE} TPM to --print-states:"
echo "Actual : ${msg}"
Expand Down