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

FAPI: fix problems detected by scanner. #2906

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
5 changes: 2 additions & 3 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ task:
env:
CFLAGS: "-I/usr/local/include -I/usr/local/openssl/include"
LDFLAGS: -L/usr/local/lib
ibmtpm_name: ibmtpm1637
libusb_version: v1.0.26
freebsd_instance:
matrix:
image_family: freebsd-14-0
image_family: freebsd-14-1
install_script:
- IGNORE_OSVERSION=yes
- pkg update -f
Expand All @@ -23,5 +22,5 @@ task:
# Due to a race condition that only occurs in the cirrus ci, "make distcheck" has been replaced by "make check".
#
./bootstrap &&
./configure --enable-self-generated-certificate --enable-unit=yes --enable-integration=yes --with-crypto=ossl --disable-doxygen-doc --enable-tcti-swtpm=yes --enable-tcti-libtpms=no --enable-tcti-mssim=no --disable-dependency-tracking &&
./configure --enable-self-generated-certificate --enable-unit=yes --enable-integration=yes --with-crypto=ossl --disable-doxygen-doc --enable-tcti-swtpm=yes --enable-tcti-libtpms=no --enable-tcti-mssim=no --enable-nodl --disable-dependency-tracking &&
gmake -j check || { cat /tmp/cirrus-ci-build/tpm2-tss-*/_build/sub/test-suite.log; exit 1; }
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ src_tss2_esys_libtss2_esys_la_SOURCES = $(TSS2_ESYS_SRC) $(TSS2_ESYS_SRC_CRYPTO)
src/tss2-tcti/tctildr.c src/tss2-tcti/tctildr.h \
src/tss2-tcti/tctildr-interface.h
if NO_DL
src_tss2_esys_libtss2_esys_la_LIBADD += $(libtss2_tcti_device) $(libtss2_tcti_mssim) $(libtss2_tcti_cmd)
src_tss2_esys_libtss2_esys_la_LIBADD += $(libtss2_tcti_device) $(libtss2_tcti_mssim) $(libtss2_tcti_cmd) $(libtss2_tcti_swtpm)
src_tss2_esys_libtss2_esys_la_SOURCES += src/tss2-tcti/tctildr-nodl.c src/tss2-tcti/tctildr-nodl.h
else
src_tss2_esys_libtss2_esys_la_LIBADD += $(LIBADD_DL)
Expand Down
15 changes: 10 additions & 5 deletions src/tss2-fapi/api/Fapi_WriteAuthorizeNV.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ Fapi_WriteAuthorizeNv_Finish(

TSS2_RC r;
const size_t maxNvSize = sizeof(TPMU_HA) + sizeof(TPMI_ALG_HASH);
BYTE nvBuffer[maxNvSize];
size_t offset = 0;

/* Check for NULL parameters */
Expand All @@ -252,6 +251,7 @@ Fapi_WriteAuthorizeNv_Finish(

switch (context->state) {
statecase(context->state, WRITE_AUTHORIZE_NV_READ_NV)
nvCmd->nv_buffer = NULL;
/* First check whether the file in object store can be updated. */
r = ifapi_keystore_check_writeable(&context->keystore, nvCmd->nvPath);
goto_if_error_reset_state(r,
Expand Down Expand Up @@ -285,25 +285,29 @@ Fapi_WriteAuthorizeNv_Finish(

statecase(context->state, WRITE_AUTHORIZE_NV_WRITE_NV_RAM_PREPARE)

nvCmd->nv_buffer = malloc(maxNvSize);
if (!nvCmd->nv_buffer) {
goto_error(r, TSS2_FAPI_RC_MEMORY, "Out of memory", error_cleanup);
}

/* Copy hash alg followed by digest into a buffer to be written to NV ram */
r = Tss2_MU_TPMI_ALG_HASH_Marshal(
object->misc.nv.public.nvPublic.nameAlg,
&nvBuffer[0], maxNvSize, &offset);
&nvCmd->nv_buffer[0], maxNvSize, &offset);
goto_if_error_reset_state(r, "FAPI marshal hash alg", error_cleanup);

void * currentDigest =
&policy->policyDigests.digests[command->digest_idx].digest;
memcpy(&nvBuffer[offset], currentDigest, command->hash_size);
memcpy(&nvCmd->nv_buffer[offset], currentDigest, command->hash_size);

/* Store these data in the context to be used for re-entry on nv_write. */
nvCmd->data = &nvBuffer[0];
nvCmd->numBytes = command->hash_size + sizeof(TPMI_ALG_HASH);
fallthrough;

statecase(context->state, WRITE_AUTHORIZE_NV_WRITE_NV_RAM)
/* Perform the actual NV Write operation. */
r = ifapi_nv_write(context, nvCmd->nvPath, 0,
nvCmd->data, context->nv_cmd.numBytes);
nvCmd->nv_buffer, context->nv_cmd.numBytes);
return_try_again(r);
goto_if_error_reset_state(r, " FAPI NV Write", error_cleanup);

Expand Down Expand Up @@ -355,6 +359,7 @@ Fapi_WriteAuthorizeNv_Finish(
/* Cleanup any intermediate results and state stored in the context. */
SAFE_FREE(command->policyPath);
SAFE_FREE(nvCmd->nvPath);
SAFE_FREE(nvCmd->nv_buffer);
ifapi_session_clean(context);
ifapi_cleanup_policy(policy);
ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
Expand Down
1 change: 1 addition & 0 deletions src/tss2-fapi/fapi_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ typedef struct {
UINT16 offset; /**< Offset in TPM memory TPM */
size_t data_idx; /**< Offset in the read buffer */
const uint8_t *data; /**< Buffer for data to be written */
uint8_t *nv_buffer; /**< Buffer for data to be written */
uint8_t *rdata; /**< Buffer for data to be read */
size_t size; /**< size of rdata */
IFAPI_OBJECT auth_object; /**< Object used for authentication */
Expand Down
8 changes: 4 additions & 4 deletions src/tss2-fapi/ifapi_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ ifapi_set_key_flags(const char *type, bool policy, IFAPI_KEY_TEMPLATE *template)
} else if (strcasecmp(flag, "noda") == 0) {
attributes |= TPMA_OBJECT_NODA;
} else if (strncmp(flag, "0x", 2) == 0) {
sscanf(&flag[2], "%"SCNx32 "%n", &handle, &pos);
if ((size_t)pos != strlen(flag) - 2) {
if (sscanf(&flag[2], "%"SCNx32 "%n", &handle, &pos) < 1 ||
(size_t)pos != strlen(flag) - 2) {
goto_error(r, TSS2_FAPI_RC_BAD_VALUE, "Invalid flag: %s",
error, flag);
}
Expand Down Expand Up @@ -182,8 +182,8 @@ ifapi_set_nv_flags(const char *type, IFAPI_NV_TEMPLATE *template,
} else if (strcasecmp(flag, "noda") == 0) {
attributes |= TPMA_NV_NO_DA;
} else if (strncmp(flag, "0x", 2) == 0) {
sscanf(&flag[2], "%"SCNx32 "%n", &handle, &pos);
if ((size_t)pos != strlen(flag) - 2) {
if (sscanf(&flag[2], "%"SCNx32 "%n", &handle, &pos) < 1 ||
(size_t)pos != strlen(flag) - 2) {
goto_error(r, TSS2_FAPI_RC_BAD_VALUE, "Invalid flag: %s",
error, flag);
}
Expand Down
Loading