Skip to content

Commit 63fc2ab

Browse files
William Robertscplappert
authored andcommitted
tcti-spi-helper: check callback pointers
NULL pointers will cause an NPD error, check all the pointers for sanity on initialization. Signed-off-by: William Roberts <william.c.roberts@intel.com>
1 parent 23390a2 commit 63fc2ab

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/tss2-tcti/tcti-spi-helper.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,24 @@ static void spi_tpm_helper_fifo_transfer(TSS2_TCTI_SPI_HELPER_CONTEXT* ctx, uint
430430
} while (handled_so_far != transfer_size);
431431
}
432432

433+
static TSS2_RC check_platform_conf(TSS2_TCTI_SPI_HELPER_PLATFORM *platform_conf)
434+
{
435+
436+
bool required_set = platform_conf->sleep_ms && platform_conf->spi_transfer \
437+
&& platform_conf->start_timeout && platform_conf->timeout_expired;
438+
if (!required_set) {
439+
LOG_ERROR("Expected sleep_ms, spi_transfer, start_timeout and timeout_expired to be set.");
440+
return TSS2_TCTI_RC_BAD_VALUE;
441+
}
442+
443+
if (!!platform_conf->spi_acquire != !!platform_conf->spi_release) {
444+
LOG_ERROR("Expected spi_acquire and spi_release to both be NULL or set.");
445+
return TSS2_TCTI_RC_BAD_VALUE;
446+
}
447+
448+
return TSS2_RC_SUCCESS;
449+
}
450+
433451
/*
434452
* This function wraps the "up-cast" of the opaque TCTI context type to the
435453
* type for the device TCTI context. The only safe-guard we have to ensure
@@ -675,6 +693,9 @@ TSS2_RC Tss2_Tcti_Spi_Helper_Init (TSS2_TCTI_CONTEXT* tcti_context, size_t* size
675693
memset (&tcti_common->header, 0, sizeof (tcti_common->header));
676694
tcti_common->locality = 0;
677695

696+
rc = check_platform_conf(platform_conf);
697+
return_if_error(rc, "platform_conf invalid");
698+
678699
// Copy platform struct into context
679700
tcti_spi_helper->platform = *platform_conf;
680701

test/unit/tcti-spi-helper.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,67 @@ tcti_spi_with_wait_state_success_test (void **state)
260260
free (tcti_ctx);
261261
}
262262

263+
static void
264+
tcti_spi_with_bad_callbacks_test (void **state)
265+
{
266+
TSS2_RC rc;
267+
size_t size;
268+
TSS2_TCTI_SPI_HELPER_PLATFORM tcti_platform = {};
269+
TSS2_TCTI_CONTEXT* tcti_ctx;
270+
271+
// Get requested TCTI context size
272+
rc = Tss2_Tcti_Spi_Helper_Init (NULL, &size, &tcti_platform);
273+
assert_int_equal (rc, TSS2_RC_SUCCESS);
274+
275+
// Allocate TCTI context size
276+
tcti_ctx = (TSS2_TCTI_CONTEXT*) calloc (1, size);
277+
assert_non_null (tcti_ctx);
278+
279+
// Initialize TCTI context
280+
tcti_platform = create_tcti_spi_helper_platform (false);
281+
tcti_platform.sleep_ms = NULL;
282+
rc = Tss2_Tcti_Spi_Helper_Init (tcti_ctx, &size, &tcti_platform);
283+
assert_int_equal (rc, TSS2_TCTI_RC_BAD_VALUE);
284+
285+
free (tcti_platform.user_data);
286+
free (tcti_ctx);
287+
}
288+
289+
static void
290+
tcti_spi_with_wait_state_bad_callbacks_test (void **state)
291+
{
292+
TSS2_RC rc;
293+
size_t size;
294+
TSS2_TCTI_SPI_HELPER_PLATFORM tcti_platform = {};
295+
TSS2_TCTI_CONTEXT* tcti_ctx;
296+
297+
// Get requested TCTI context size
298+
rc = Tss2_Tcti_Spi_Helper_Init (NULL, &size, &tcti_platform);
299+
assert_int_equal (rc, TSS2_RC_SUCCESS);
300+
301+
// Allocate TCTI context size
302+
tcti_ctx = (TSS2_TCTI_CONTEXT*) calloc (1, size);
303+
assert_non_null (tcti_ctx);
304+
305+
// Initialize TCTI context
306+
tcti_platform = create_tcti_spi_helper_platform (true);
307+
tcti_platform.spi_acquire = NULL;
308+
rc = Tss2_Tcti_Spi_Helper_Init (tcti_ctx, &size, &tcti_platform);
309+
assert_int_equal (rc, TSS2_TCTI_RC_BAD_VALUE);
310+
311+
free (tcti_platform.user_data);
312+
free (tcti_ctx);
313+
}
314+
263315
int
264316
main (int argc,
265317
char *argv[])
266318
{
267319
const struct CMUnitTest tests[] = {
268320
cmocka_unit_test (tcti_spi_no_wait_state_success_test),
269321
cmocka_unit_test (tcti_spi_with_wait_state_success_test),
322+
cmocka_unit_test (tcti_spi_with_bad_callbacks_test),
323+
cmocka_unit_test (tcti_spi_with_wait_state_bad_callbacks_test)
270324
};
271325
return cmocka_run_group_tests (tests, NULL, NULL);
272326
}

0 commit comments

Comments
 (0)