Skip to content
Open
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
3 changes: 3 additions & 0 deletions src/swtpm/ctrlchannel.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ static int ctrlchannel_return_state(ptm_getstate *pgs, int fd,
if (!blobname)
res = TPM_FAIL;

if (res == 0 && blobtype == PTM_BLOB_TYPE_PERMANENT)
res = SWTPM_NVRAM_Store_Permanent();

if (res == 0 && blobtype == PTM_BLOB_TYPE_VOLATILE)
res = SWTPM_NVRAM_Store_Volatile();

Expand Down
31 changes: 31 additions & 0 deletions src/swtpm/swtpm_nvstore.c
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,37 @@ TPM_RESULT SWTPM_NVRAM_Store_Volatile(void)
return rc;
}

/*
* Flush libtpms's in-memory PERMANENT state to disk so that the next
* SWTPM_NVRAM_GetStateBlob(PERMANENT) returns a blob consistent with what
* libtpms is actually running. Counterpart to SWTPM_NVRAM_Store_Volatile().
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense and it shouldn't affect anything negatively. However, I was under the impression that the PERMANENT state was always stored whenever a command 'affected' it, e.g. when an NVRAM space was created or undefined etc., and therefore the latest state was always on disk. So I am a bit surprised that this is necessary and that nobody else complained about it so far.

*
* Without this, GET_STATEBLOB(PERMANENT) on the migration-out path reads a
* file that may be missing, empty, or stale relative to libtpms RAM (the
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't happen to have a reproducer for how we could generate a 'missing' or 'empty file' situation other than using rm intentionally, do you?

* file is only written by _plat__NvCommit() on UT_NV-class commands), and
* an empty/short-read blob then poisons the destination's state restore.
*/
TPM_RESULT SWTPM_NVRAM_Store_Permanent(void)
{
TPM_RESULT rc = 0;
char *name = TPM_PERMANENT_ALL_NAME;
uint32_t tpm_number = 0;
unsigned char *buffer = NULL;
uint32_t buflen = 0;

TPM_DEBUG(" SWTPM_Store_Permanent: Name %s\n", name);
if (rc == 0) {
rc = TPMLIB_GetState(TPMLIB_STATE_PERMANENT, &buffer, &buflen);
}
if (rc == 0 && buflen > 0) {
rc = SWTPM_NVRAM_StoreData(buffer, buflen, tpm_number, name);
}

free(buffer);

return rc;
}

static TPM_RESULT
SWTPM_NVRAM_KeyParamCheck(uint32_t keylen,
enum encryption_mode encmode)
Expand Down
1 change: 1 addition & 0 deletions src/swtpm/swtpm_nvstore.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ TPM_RESULT SWTPM_NVRAM_DeleteName(uint32_t tpm_number,
const char *name,
TPM_BOOL mustExist);
TPM_RESULT SWTPM_NVRAM_Store_Volatile(void);
TPM_RESULT SWTPM_NVRAM_Store_Permanent(void);

TPM_RESULT SWTPM_NVRAM_Set_FileKey(const unsigned char *data,
uint32_t length,
Expand Down
Loading