Skip to content

Commit

Permalink
core: fix race condition on single instance TA loading
Browse files Browse the repository at this point in the history
Fix race condition on single instance TA creation where several
instances of a single instance TA could be created if invoked close
enough that they are both created after tee_ta_init_session() calls
tee_ta_init_session_with_context().

Closes: OP-TEE#6801
Signed-off-by: Etienne Carriere <etienne.carriere@foss.st.com>
  • Loading branch information
etienne-lms committed May 14, 2024
1 parent 9b1d1cf commit bc192f6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
3 changes: 3 additions & 0 deletions core/include/kernel/tee_ta_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ extern struct tee_ta_ctx_head tee_ctxes;
extern struct mutex tee_ta_mutex;
extern struct condvar tee_ta_init_cv;

TEE_Result tee_ta_init_session_with_context(struct tee_ta_session *s,
const TEE_UUID *uuid);

TEE_Result tee_ta_open_session(TEE_ErrorOrigin *err,
struct tee_ta_session **sess,
struct tee_ta_session_head *open_sessions,
Expand Down
4 changes: 2 additions & 2 deletions core/kernel/tee_ta_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,8 @@ TEE_Result tee_ta_close_session(struct tee_ta_session *csess,
return TEE_SUCCESS;
}

static TEE_Result tee_ta_init_session_with_context(struct tee_ta_session *s,
const TEE_UUID *uuid)
TEE_Result tee_ta_init_session_with_context(struct tee_ta_session *s,
const TEE_UUID *uuid)
{
struct tee_ta_ctx *ctx = NULL;

Expand Down
14 changes: 14 additions & 0 deletions core/kernel/user_ta.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,20 @@ TEE_Result tee_ta_init_user_ta_session(const TEE_UUID *uuid,
#endif

mutex_lock(&tee_ta_mutex);

/*
* Before updating the context list check again if a context
* already exists in the list for a single instance TA, maybe
* created recently enough so that it was not found at entry
* in tee_ta_init_session().
*/
res = tee_ta_init_session_with_context(s, uuid);
if (res != TEE_ERROR_ITEM_NOT_FOUND) {
free(utc);
mutex_unlock(&tee_ta_mutex);
return res;
}

s->ts_sess.ctx = &utc->ta_ctx.ts_ctx;
s->ts_sess.handle_scall = s->ts_sess.ctx->ops->handle_scall;
/*
Expand Down

0 comments on commit bc192f6

Please sign in to comment.