tpm2: adapt NVRAM offsets and check structure sizes

Make sure that the NVRAM offsets and structure sizes are the same
on all architectures so that we can fill up the NVRAM on one system
and migrate the state to another architecture and it will fit into
the NVRAM space there.

We leave some space for the first few data structures in the NVRAM to
grow. We do this by rounding up the NV_INDEX_RAM_DATA location to the
next kb boundary. This moves it from offset 4356 to 5120 on x86_64 and
from offset 4332 to 5120 on i386. This now leaves us with the same
amount of space for user dynamic NVRAM, which starts beyond offset 5120.

We also pad the OBJECT structure with 4 bytes so that it is the same
size on 32 and 64 bit architectures. This is a data structure that
is used in user dynamic NVRAM and should be the same size on all
architectures so that a full NVRAM always fits.

Also test the size of the NV_INDEX structure, which already has the
same size on all tested architectures (x86_64, i386, arm32, ppc64).

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
This commit is contained in:
Stefan Berger 2018-06-02 09:54:49 -04:00
parent 48dabdbdef
commit eed97cf008
4 changed files with 81 additions and 3 deletions

1
.gitignore vendored
View File

@ -32,6 +32,7 @@ Makefile
/tests/base64decode
/tests/fuzz
/tests/freebl_sha1flattensize
/tests/nvram_offsets
/debian/*debhelper*
/debian/*substvars
/debian/files

View File

@ -210,6 +210,11 @@ typedef struct OBJECT
// handle of an object slot.
TPM2B_NAME name; // Name of the object name. Kept here
// to avoid repeatedly computing it.
// libtpms: OBJECT lies in NVRAM; to avoid that it needs different number
// of bytes on 32 bit and 64 bit architectures, we need to make sure it's the
// same size; simple padding at the end works here
UINT32 _pad;
} OBJECT;
/* This structure holds a hash sequence object or an event sequence object. */
/* The first four components of this structure are manually set to be the same as the first four
@ -883,11 +888,15 @@ extern STATE_RESET_DATA gr;
/* c) a STATE_CLEAR_DATA structure */
/* d) an ORDERLY_DATA structure */
/* e) the user defined NV index space */
#define NV_ROUNDUP(VAL, SIZE) \
( ( (VAL) + (SIZE) - 1 ) / (SIZE) ) * (SIZE)
#define NV_PERSISTENT_DATA (0)
#define NV_STATE_RESET_DATA (NV_PERSISTENT_DATA + sizeof(PERSISTENT_DATA))
#define NV_STATE_CLEAR_DATA (NV_STATE_RESET_DATA + sizeof(STATE_RESET_DATA))
#define NV_ORDERLY_DATA (NV_STATE_CLEAR_DATA + sizeof(STATE_CLEAR_DATA))
#define NV_INDEX_RAM_DATA (NV_ORDERLY_DATA + sizeof(ORDERLY_DATA))
#define NV_INDEX_RAM_DATA NV_ROUNDUP(NV_ORDERLY_DATA + sizeof(ORDERLY_DATA),\
1024)
#define NV_USER_DYNAMIC (NV_INDEX_RAM_DATA + sizeof(s_indexOrderlyRam))
#define NV_USER_DYNAMIC_END NV_MEMORY_SIZE
/* 5.10.13 Global Macro Definitions */

View File

@ -7,8 +7,18 @@
AM_CFLAGS = -I../include $(SANITIZERS)
AM_LDFLAGS = -ltpms -L../src/.libs $(SANITIZERS)
check_PROGRAMS = base64decode
TESTS = base64decode.sh
check_PROGRAMS = base64decode nvram_offsets
TESTS = base64decode.sh nvram_offsets
nvram_offsets_SOURCES = nvram_offsets.c
nvram_offsets_CFLAGS = $(AM_CFLAGS) \
-I$(top_srcdir)/include/libtpms \
-I$(top_srcdir)/src \
-I$(top_srcdir)/src/tpm2 \
-I$(top_srcdir)/src/tpm2/crypto \
-I$(top_srcdir)/src/tpm2/crypto/openssl \
-DTPM_POSIX
nvram_offsets_LDFLAGS = $(AM_LDFLAGS)
if WITH_FUZZER
check_PROGRAMS += fuzz

58
tests/nvram_offsets.c Normal file
View File

@ -0,0 +1,58 @@
#include <assert.h>
#include <stdlib.h>
#include "Implementation.h"
#include "Tpm.h"
/* from Global.h */
extern BYTE s_indexOrderlyRam[RAM_INDEX_SPACE];
int main(void)
{
/* ensure that the NVRAM offset of NV_USER_DYNAMIC is at the expected
location so that there's enough memory for re-constructing NVRAM
indices etc. into the NVRAM */
#define NV_INDEX_RAM_DATA_EXP_OFFSET 5120
if (NV_INDEX_RAM_DATA != NV_INDEX_RAM_DATA_EXP_OFFSET) {
/* If this ever changes due to growth of the preceding data
* structure, we need to adjust the total NVRAM memory size
* for the architecture where this changed (or have all
* architectures use the same offset.
*/
fprintf(stderr,
"NV_INDEX_RAM_DATA not at expected offset %u but at %u\n",
NV_INDEX_RAM_DATA_EXP_OFFSET, (unsigned int)NV_INDEX_RAM_DATA);
return EXIT_FAILURE;
}
#define NV_USER_DYNAMIC_EXP_OFFSET (5120 + 512)
if (NV_USER_DYNAMIC != NV_USER_DYNAMIC_EXP_OFFSET) {
fprintf(stderr,
"NV_USER_DYNAMIC not at expected offset %u but at %u\n",
NV_USER_DYNAMIC_EXP_OFFSET, (unsigned int)NV_USER_DYNAMIC);
return EXIT_FAILURE;
}
/*
* OBJECTs are directly written into NVRAM. We have to make sure that the
* size of the OBJECT is the same on all architectures so that a full
* NVRAM fits on all architectures
*/
#define OBJECT_EXP_SIZE 1896
if (sizeof(OBJECT) != OBJECT_EXP_SIZE) {
fprintf(stderr, "sizeof(OBJECT) does not have expected size of %u bytes"
"but %zu bytes\n", OBJECT_EXP_SIZE, sizeof(OBJECT));
return EXIT_FAILURE;
}
/* Same for NV_INDEX */
#define NV_INDEX_EXP_SIZE 148
if (sizeof(NV_INDEX) != NV_INDEX_EXP_SIZE) {
fprintf(stderr,
"sizeof(NV_INDEX) does not have expected size of %u bytes"
"but %zu bytes\n", NV_INDEX_EXP_SIZE, sizeof(NV_INDEX));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}