From 3a634eef021c2af94918746df37b523fc491910d Mon Sep 17 00:00:00 2001 From: Johannes Holland Date: Sun, 8 Oct 2023 11:41:54 +0200 Subject: [PATCH] tctildr: refactor loading to use list of file name formats Signed-off-by: Johannes Holland --- src/tss2-tcti/tctildr-dl.c | 73 ++++++++++++-------------------------- src/tss2-tcti/tctildr.h | 15 ++++---- test/unit/tctildr-dl.c | 4 +-- 3 files changed, 32 insertions(+), 60 deletions(-) diff --git a/src/tss2-tcti/tctildr-dl.c b/src/tss2-tcti/tctildr-dl.c index 363d32b66..491d404bc 100644 --- a/src/tss2-tcti/tctildr-dl.c +++ b/src/tss2-tcti/tctildr-dl.c @@ -78,65 +78,36 @@ TSS2_RC handle_from_name(const char *file, void **handle) { - char *file_xfrm = NULL; size_t size; - size_t len; + char file_xfrm[PATH_MAX]; + const char *formats[] = { + /* */ + "%s", + /* libtss2-tcti-.so.0 */ + FMT_TCTI_PREFIX "%s" FMT_LIB_SUFFIX_0, + /* libtss2-tcti-.so */ + FMT_TCTI_PREFIX "%s" FMT_LIB_SUFFIX, + }; if (handle == NULL) { return TSS2_TCTI_RC_BAD_REFERENCE; } - *handle = dlopen(file, RTLD_NOW); - if (*handle != NULL) { - return TSS2_RC_SUCCESS; - } else { - LOG_DEBUG("Could not load TCTI file: \"%s\": %s", file, dlerror()); - } - len = snprintf(NULL, 0, TCTI_NAME_TEMPLATE_0, file); - if (len >= PATH_MAX) { - LOG_ERROR("TCTI name truncated in transform."); - return TSS2_TCTI_RC_BAD_VALUE; - } - file_xfrm = calloc(len + 1, sizeof(char)); - if (file_xfrm == NULL) { - return TSS2_TCTI_RC_MEMORY; - } - /* 'name' alone didn't work, try libtss2-tcti-.so.0 */ - size = snprintf(file_xfrm, - len + 1, - TCTI_NAME_TEMPLATE_0, - file); - if (size >= (len + 1)) { - LOG_ERROR("TCTI name truncated in transform."); - SAFE_FREE(file_xfrm); - return TSS2_TCTI_RC_BAD_VALUE; - } - *handle = dlopen(file_xfrm, RTLD_NOW); - if (*handle != NULL) { - SAFE_FREE(file_xfrm); - return TSS2_RC_SUCCESS; - } else { - LOG_DEBUG("Could not load TCTI file \"%s\": %s", file, dlerror()); - } - /* libtss2-tcti-.so.0 didn't work, try libtss2-tcti-.so */ - size = snprintf(file_xfrm, - len + 1, - TCTI_NAME_TEMPLATE, - file); - if (size >= (len + 1)) { - LOG_ERROR("TCTI name truncated in transform."); - SAFE_FREE(file_xfrm); - return TSS2_TCTI_RC_BAD_VALUE; - } - *handle = dlopen(file_xfrm, RTLD_NOW); - if (*handle == NULL) { - LOG_DEBUG("Failed to load TCTI for name \"%s\": %s", file, dlerror()); - SAFE_FREE(file_xfrm); - return TSS2_TCTI_RC_NOT_SUPPORTED; + for (size_t i = 0; i < ARRAY_SIZE(formats); i++) { + size = snprintf(file_xfrm, sizeof(file_xfrm), formats[i], file); + if (size >= sizeof(file_xfrm)) { + LOG_ERROR("TCTI name truncated in transform."); + return TSS2_TCTI_RC_BAD_VALUE; + } + *handle = dlopen(file_xfrm, RTLD_NOW); + if (*handle != NULL) { + return TSS2_RC_SUCCESS; + } else { + LOG_DEBUG("Could not load TCTI file \"%s\": %s", file, dlerror()); + } } - SAFE_FREE(file_xfrm); - return TSS2_RC_SUCCESS; + return TSS2_TCTI_RC_NOT_SUPPORTED; } TSS2_RC tcti_from_file(const char *file, diff --git a/src/tss2-tcti/tctildr.h b/src/tss2-tcti/tctildr.h index 12bab3035..02cd39405 100644 --- a/src/tss2-tcti/tctildr.h +++ b/src/tss2-tcti/tctildr.h @@ -9,13 +9,14 @@ #include "tss2_tpm2_types.h" #include "tss2_tcti.h" -#define TCTI_SUFFIX ".so" -#define TCTI_SUFFIX_0 TCTI_SUFFIX".0" -#define LIB_PREFIX "lib" -#define TCTI_PREFIX LIB_PREFIX"tss2-tcti" -#define TCTI_NAME_TEMPLATE TCTI_PREFIX"-%s"TCTI_SUFFIX -#define TCTI_NAME_TEMPLATE_0 TCTI_PREFIX"-%s"TCTI_SUFFIX_0 -#define DEFAULT_TCTI_LIBRARY_NAME TCTI_PREFIX"-default"TCTI_SUFFIX +#define FMT_LIB_PREFIX "lib" +#define FMT_TSS_PREFIX FMT_LIB_PREFIX"tss2-" +#define FMT_TCTI_PREFIX FMT_TSS_PREFIX"tcti-" + +#define FMT_LIB_SUFFIX_0 FMT_LIB_SUFFIX".0" +#define FMT_LIB_SUFFIX ".so" + +#define DEFAULT_TCTI_LIBRARY_NAME FMT_TCTI_PREFIX"-default"TCTI_SUFFIX #define TCTILDR_MAGIC 0xbc44a31ca74b4aafULL diff --git a/test/unit/tctildr-dl.c b/test/unit/tctildr-dl.c index 4279baee5..ab62aa74b 100644 --- a/test/unit/tctildr-dl.c +++ b/test/unit/tctildr-dl.c @@ -152,7 +152,7 @@ test_handle_from_name_first_dlopen_success (void **state) assert_int_equal (handle, TEST_HANDLE); } -#define TEST_TCTI_NAME_SO_0 TCTI_PREFIX"-"TEST_TCTI_NAME""TCTI_SUFFIX_0 +#define TEST_TCTI_NAME_SO_0 FMT_TCTI_PREFIX TEST_TCTI_NAME""FMT_LIB_SUFFIX_0 static void test_handle_from_name_second_dlopen_success (void **state) { @@ -171,7 +171,7 @@ test_handle_from_name_second_dlopen_success (void **state) assert_int_equal (rc, TSS2_RC_SUCCESS); assert_int_equal (handle, TEST_HANDLE); } -#define TEST_TCTI_NAME_SO TCTI_PREFIX"-"TEST_TCTI_NAME""TCTI_SUFFIX +#define TEST_TCTI_NAME_SO FMT_TCTI_PREFIX TEST_TCTI_NAME""FMT_LIB_SUFFIX static void test_handle_from_name_third_dlopen_success (void **state) {