Skip to content

Commit

Permalink
tee-supplicant: Allow for TA load path to be specified at runtime
Browse files Browse the repository at this point in the history
Add a new `--ta-path` CLI flag for overriding the default load path used
by tee-supplicant. The given path string can be a set of colon (':')
separated paths, each being a full path used when searching for TAs.
When this option is not used, the existing behavior of loading TAs from
a subdirectory "ta-dir" under TEEC_LOAD_PATH is retained.

Signed-off-by: Jared Baur <jaredbaur@fastmail.com>
Reviewed-by: Etienne Carriere <etienne.carriere@foss.st.com>
Reviewed-by: Jerome Forissier <jerome.forissier@linaro.org>
  • Loading branch information
jmbaur authored and jforissier committed Nov 27, 2023
1 parent c84206b commit f3845d8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 25 deletions.
49 changes: 40 additions & 9 deletions tee-supplicant/src/tee_supplicant.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@

#define RPC_NUM_PARAMS 5

#define DEFAULT_TA_DIR "optee_armtz"

#define RPC_BUF_SIZE (sizeof(struct tee_iocl_supp_send_arg) + \
RPC_NUM_PARAMS * sizeof(struct tee_ioctl_param))

char **ta_path;
char *ta_path_str;

union tee_rpc_invoke {
uint64_t buf[(RPC_BUF_SIZE - 1) / sizeof(uint64_t) + 1];
Expand Down Expand Up @@ -103,7 +104,6 @@ static pthread_mutex_t shm_mutex = PTHREAD_MUTEX_INITIALIZER;
static struct tee_shm *shm_head;

struct tee_supplicant_params supplicant_params = {
.ta_dir = "optee_armtz",
#ifdef TEE_PLUGIN_LOAD_PATH
.plugin_load_path = TEE_PLUGIN_LOAD_PATH,
#endif
Expand Down Expand Up @@ -300,7 +300,7 @@ static uint32_t load_ta(size_t num_params, struct tee_ioctl_param *params)
uuid_from_octets(&uuid, (void *)val_cmd);

size = shm_ta.size;
ta_found = TEECI_LoadSecureModule(supplicant_params.ta_dir, &uuid, shm_ta.buffer, &size);
ta_found = TEECI_LoadSecureModule(&uuid, shm_ta.buffer, &size);
if (ta_found != TA_BINARY_FOUND) {
EMSG(" TA not found");
return TEEC_ERROR_ITEM_NOT_FOUND;
Expand Down Expand Up @@ -495,8 +495,10 @@ static int usage(int status)
"after child has opened the TEE device or on error)\n");
fprintf(stderr, "\t-f, --fs-parent-path: secure fs parent path [%s]\n",
supplicant_params.fs_parent_path);
fprintf(stderr, "\t-t, --ta-dir: TAs dirname under %s [%s]\n", TEEC_LOAD_PATH,
supplicant_params.ta_dir);
fprintf(stderr, "\t-l, --ta-path: TA load path\n");
fprintf(stderr, "\t-t, --ta-dir: TAs dirname under %s [%s]"
" (deprecated, cannot be used with --ta-path)\n",
TEEC_LOAD_PATH, DEFAULT_TA_DIR);
fprintf(stderr, "\t-p, --plugin-path: plugin load path [%s]\n",
supplicant_params.plugin_load_path);
fprintf(stderr, "\t-r, --rpmb-cid: RPMB device identification register "
Expand Down Expand Up @@ -698,10 +700,15 @@ static void *thread_main(void *a)

static void set_ta_path(void)
{
char *ta_path_str = NULL;
char *p = NULL;
char *saveptr = NULL;
const char *path = (char *)TEEC_LOAD_PATH;
char *new_path = NULL;
size_t n = 0;
const char *path = supplicant_params.ta_load_path;

if (!path)
path = TEEC_LOAD_PATH;

ta_path_str = strdup(path);
if (!ta_path_str)
Expand All @@ -721,8 +728,22 @@ static void set_ta_path(void)
n = 0;
strcpy(ta_path_str, path);
p = ta_path_str;
while ((ta_path[n++] = strtok_r(p, ":", &saveptr)))
p = NULL;

while ((new_path = strtok_r(p, ":", &saveptr))) {
if (!supplicant_params.ta_load_path) {
char full_path[PATH_MAX] = { 0 };

snprintf(full_path, PATH_MAX, "%s/%s", new_path,
supplicant_params.ta_dir);
ta_path[n++] = strdup(full_path);
} else {
ta_path[n++] = strdup(new_path);
}

p = NULL;
}

free(ta_path_str);

return;
err:
Expand Down Expand Up @@ -803,13 +824,14 @@ int main(int argc, char *argv[])
{ "help", no_argument, 0, 'h' },
{ "daemonize", no_argument, 0, 'd' },
{ "fs-parent-path", required_argument, 0, 'f' },
{ "ta-path", required_argument, 0, 'l' },
{ "ta-dir", required_argument, 0, 't' },
{ "plugin-path", required_argument, 0, 'p' },
{ "rpmb-cid", required_argument, 0, 'r' },
{ 0, 0, 0, 0 }
};

while ((opt = getopt_long(argc, argv, "hdf:t:p:r:",
while ((opt = getopt_long(argc, argv, "hdf:l:t:p:r:",
long_options, &long_index )) != -1) {
switch (opt) {
case 'h' :
Expand All @@ -821,6 +843,9 @@ int main(int argc, char *argv[])
case 'f':
supplicant_params.fs_parent_path = optarg;
break;
case 'l':
supplicant_params.ta_load_path = optarg;
break;
case 't':
supplicant_params.ta_dir = optarg;
break;
Expand All @@ -846,6 +871,12 @@ int main(int argc, char *argv[])
}
}

if (!supplicant_params.ta_dir && !supplicant_params.ta_load_path) {
supplicant_params.ta_dir = DEFAULT_TA_DIR;
} else if (supplicant_params.ta_dir && supplicant_params.ta_load_path) {
fprintf(stderr, "Cannot use --ta-path and --ta-dir at the same time\n");
return usage(EXIT_FAILURE);
}

set_ta_path();

Expand Down
1 change: 1 addition & 0 deletions tee-supplicant/src/tee_supplicant.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct tee_ioctl_param;
/* Global tee-supplicant parameters */
struct tee_supplicant_params {
const char *ta_dir;
const char *ta_load_path;
const char *plugin_load_path;
const char *fs_parent_path;
const char *rpmb_cid;
Expand Down
18 changes: 7 additions & 11 deletions tee-supplicant/src/teec_ta_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ struct tee_rpc_cmd {
* Based on the uuid this function will try to find a TA-binary on the
* filesystem and return it back to the caller in the parameter ta.
*
* @param: prefix Prefix for TA load path
* @param: dev_path Where to load the TA from. The full path to the TA
* binary is @prefix/@dev_path/@destination.ta.
* @param: ta_load_path Where to load the TA from. The full path to the TA
* binary is @ta_load_path/@destination.ta.
* @param: destination The uuid of the TA we are searching for.
* @param: ta A pointer which this function will allocate and copy
* the TA from the filesystem to the pointer itself. It is
Expand All @@ -63,8 +62,7 @@ struct tee_rpc_cmd {
*
* @return 0 if TA was found, otherwise -1.
*/
static int try_load_secure_module(const char* prefix,
const char* dev_path,
static int try_load_secure_module(const char *ta_load_path,
const TEEC_UUID *destination, void *ta,
size_t *ta_size)
{
Expand All @@ -88,8 +86,8 @@ static int try_load_secure_module(const char* prefix,
*/
again:
n = snprintf(fname, PATH_MAX,
"%s/%s/%08x-%04x-%04x-%02x%02x%s%02x%02x%02x%02x%02x%02x.ta",
prefix, dev_path,
"%s/%08x-%04x-%04x-%02x%02x%s%02x%02x%02x%02x%02x%02x.ta",
ta_load_path,
destination->timeLow,
destination->timeMid,
destination->timeHiAndVersion,
Expand Down Expand Up @@ -159,16 +157,14 @@ static int try_load_secure_module(const char* prefix,
return TA_BINARY_FOUND;
}

int TEECI_LoadSecureModule(const char* dev_path,
const TEEC_UUID *destination, void *ta,
int TEECI_LoadSecureModule(const TEEC_UUID *destination, void *ta,
size_t *ta_size)
{
int res = TA_BINARY_NOT_FOUND;
char **path = NULL;

for (path = ta_path; *path; path++) {
res = try_load_secure_module(*path, dev_path, destination, ta,
ta_size);
res = try_load_secure_module(*path, destination, ta, ta_size);
if (res == TA_BINARY_FOUND)
break;
}
Expand Down
7 changes: 2 additions & 5 deletions tee-supplicant/src/teec_ta_load.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
#define TA_BINARY_FOUND 0
#define TA_BINARY_NOT_FOUND -1

/* Heap copy of TA load paths, separated by '\0' (access via ta_path) */
extern char *ta_path_str;
/* NULL-terminated list of paths (pointers into ta_path_str) */
/* NULL-terminated list of paths */
extern char **ta_path;

/**
Expand All @@ -49,7 +47,6 @@ extern char **ta_path;
*
* @return 0 if TA was found, otherwise -1.
*/
int TEECI_LoadSecureModule(const char *name,
const TEEC_UUID *destination, void *ta,
int TEECI_LoadSecureModule(const TEEC_UUID *destination, void *ta,
size_t *ta_size);
#endif

0 comments on commit f3845d8

Please sign in to comment.