Skip to contentSkip to conversation

RDMA/ucma: Fix use-after-free in ucma_create_uevent

PATCH In review shaikh.kamal 2 participants 2 messages May 3 – May 11 InfiniBand

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 RDMA/ucma: Fix use-after-free in ucma_create_uevent

ucma_create_uevent() dereferences event->param.ud.private_data as a struct ucma_multicast for multicast events. However, this pointer may refer to memory that has already been freed, leading to a use-after-free.

Fix this by avoiding dereferencing private_data for multicast events and instead using ctx for uid and id, which has a well-defined lifetime.

Tested by running the reproducer under KASAN with slub_debug enabled for several hours with no crashes observed.

Reported-by [email protected]
Fixes c8f6a362bf3e ("RDMA/cma: Add multicast communication support")
---
 drivers/infiniband/core/ucma.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/infiniband/core/ucma.c +6 -4
index 6e700b974033..8eef10352af7 100644
@@ -270,10 +270,12 @@ static struct ucma_event *ucma_create_uevent(struct ucma_context *ctx,
 	switch (event->event) {
 	case RDMA_CM_EVENT_MULTICAST_JOIN:
 	case RDMA_CM_EVENT_MULTICAST_ERROR:
-		uevent->mc = (struct ucma_multicast *)
-			     event->param.ud.private_data;
-		uevent->resp.uid = uevent->mc->uid;
-		uevent->resp.id = uevent->mc->id;
+		/*
+		 * event->param.ud.private_data may point to a ucma_multicast
+		 * that has already been freed, so use ctx instead.
+		 */
+		uevent->resp.uid = ctx->uid;
+		uevent->resp.id = ctx->id;
 		break;
 	default:
 		uevent->resp.uid = ctx->uid;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 29 lines · shaikh.kamal

ucma_create_uevent() dereferences event->param.ud.private_data as a struct ucma_multicast for multicast events. However, this pointer may refer to memory that has already been freed, leading to a use-after-free.

Fix this by avoiding dereferencing private_data for multicast events and instead using ctx for uid and id, which has a well-defined lifetime.

Tested by running the reproducer under KASAN with slub_debug enabled for several hours with no crashes observed.

Reported-by [email protected]
Fixes c8f6a362bf3e ("RDMA/cma: Add multicast communication support")
---
 drivers/infiniband/core/ucma.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/infiniband/core/ucma.c +0 -0
index 6e700b974033..8eef10352af7 100644
@@ -270,10 +270,12 @@ static struct ucma_event *ucma_create_uevent(struct ucma_context *ctx,
 	switch (event->event) {
 	case RDMA_CM_EVENT_MULTICAST_JOIN:
 	case RDMA_CM_EVENT_MULTICAST_ERROR:

drivers/infiniband/core/ucma.c static struct ucma_event *ucma_create_uevent(struct ucma_context *ctx, · line 278

Quoted 10 lines
drivers/infiniband/core/ucma.c +6 -4
-		uevent->mc = (struct ucma_multicast *)
-			     event->param.ud.private_data;
-		uevent->resp.uid = uevent->mc->uid;
-		uevent->resp.id = uevent->mc->id;
+		/*
+		 * event->param.ud.private_data may point to a ucma_multicast
+		 * that has already been freed, so use ctx instead.
+		 */
+		uevent->resp.uid = ctx->uid;
+		uevent->resp.id = ctx->id;

I believe your analysis is correct, but the implementation is not.
Are you certain that ctx->uid == uevent->mc->uid and ctx->id == uevent->mc->id?

Thanks

Quoted 7 lines
drivers/infiniband/core/ucma.c +0 -0
 		break;
 	default:
 		uevent->resp.uid = ctx->uid;
Signature · 1 line
2.43.0