Merge tag 'amdtee-fix-for-v6.6' of https://git.linaro.org/people/jens.wiklander/linux...
authorArnd Bergmann <arnd@arndb.de>
Fri, 6 Oct 2023 20:45:02 +0000 (22:45 +0200)
committerArnd Bergmann <arnd@arndb.de>
Fri, 6 Oct 2023 20:45:03 +0000 (22:45 +0200)
AMDTEE fix possible use-after-free

* tag 'amdtee-fix-for-v6.6' of https://git.linaro.org/people/jens.wiklander/linux-tee:
  tee: amdtee: fix use-after-free vulnerability in amdtee_close_session

Link: https://lore.kernel.org/r/20231003171835.GA669924@rayden
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
drivers/tee/amdtee/core.c

index 372d64756ed64b10c3dd9096aa20886320837ff9..3c15f6a9e91c0a2c24dceecea1815b6bd2600068 100644 (file)
@@ -217,12 +217,12 @@ unlock:
        return rc;
 }
 
+/* mutex must be held by caller */
 static void destroy_session(struct kref *ref)
 {
        struct amdtee_session *sess = container_of(ref, struct amdtee_session,
                                                   refcount);
 
-       mutex_lock(&session_list_mutex);
        list_del(&sess->list_node);
        mutex_unlock(&session_list_mutex);
        kfree(sess);
@@ -272,7 +272,8 @@ int amdtee_open_session(struct tee_context *ctx,
        if (arg->ret != TEEC_SUCCESS) {
                pr_err("open_session failed %d\n", arg->ret);
                handle_unload_ta(ta_handle);
-               kref_put(&sess->refcount, destroy_session);
+               kref_put_mutex(&sess->refcount, destroy_session,
+                              &session_list_mutex);
                goto out;
        }
 
@@ -290,7 +291,8 @@ int amdtee_open_session(struct tee_context *ctx,
                pr_err("reached maximum session count %d\n", TEE_NUM_SESSIONS);
                handle_close_session(ta_handle, session_info);
                handle_unload_ta(ta_handle);
-               kref_put(&sess->refcount, destroy_session);
+               kref_put_mutex(&sess->refcount, destroy_session,
+                              &session_list_mutex);
                rc = -ENOMEM;
                goto out;
        }
@@ -331,7 +333,7 @@ int amdtee_close_session(struct tee_context *ctx, u32 session)
        handle_close_session(ta_handle, session_info);
        handle_unload_ta(ta_handle);
 
-       kref_put(&sess->refcount, destroy_session);
+       kref_put_mutex(&sess->refcount, destroy_session, &session_list_mutex);
 
        return 0;
 }