Skip to content

Commit

Permalink
tctildr: refactor loading to use list of file name formats
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Holland <[email protected]>
  • Loading branch information
joholl authored and AndreasFuchsTPM committed Nov 29, 2023
1 parent d3fd5ea commit c024caa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 60 deletions.
73 changes: 22 additions & 51 deletions src/tss2-tcti/tctildr-dl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = {
/* <name> */
"%s",
/* libtss2-tcti-<name>.so.0 */
FMT_TCTI_PREFIX "%s" FMT_LIB_SUFFIX_0,
/* libtss2-tcti-<name>.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-<name>.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-<name>.so.0 didn't work, try libtss2-tcti-<name>.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,
Expand Down
15 changes: 8 additions & 7 deletions src/tss2-tcti/tctildr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions test/unit/tctildr-dl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down

0 comments on commit c024caa

Please sign in to comment.