Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
* [PATCH v2] RDMA/ucma: reject a second join of the same multicast address
@ 2026-11-20 17:44 Andrey Misiurov
  2026-11-20 17:52 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Andrey Misiurov @ 2026-11-20 17:44 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky
  Cc: linux-rdma, Quanye Yang, linux-kernel, stable, amisiurov

ucma_process_join() permits multiple joins of the same address on one
cm_id. Each join has its own ucma_multicast, but rdma_leave_multicast()
searches by address and removes the first match. A leave can therefore
destroy another join's cma_multicast and free the requested ucma_multicast
while its cma_multicast still refers to it. A later event then accesses
freed memory.

Reject duplicate addresses with -EADDRINUSE. Hold ctx->mutex across the
duplicate check, mc_list insertion and rdma_join_multicast(). Use
xa_alloc() to reserve the multicast ID; dropping the XArray lock during
allocation cannot let another join bypass the check while the mutex is
held.

On leave, erase the handle under the XArray lock to claim the multicast
object, but keep it on mc_list until ctx->mutex is acquired. Hold the
mutex continuously from list removal through rdma_leave_multicast(),
event cleanup and freeing the object. Otherwise a rejoin after unlinking
could create a new membership before the old address-only leave, which
would destroy the new membership and leave the old one referencing freed
memory.

Fixes: c8f6a362bf3e ("RDMA/cma: Add multicast communication support")
Cc: stable@vger.kernel.org
Signed-off-by: Andrey Misiurov <amisiurov@cloudlinux.com>
---
Changes in v2:
- Serialize duplicate detection, insertion and join with ctx->mutex.
- Keep leave's unlink, RDMA teardown, event cleanup and free under the
  same mutex; drop the XArray lock before acquiring the mutex.
- Keep join error unwinding serialized and use the normal XArray API.
- Correct the Fixes tag to c8f6a362bf3e, where the duplicate join and the
  address-only leave were introduced.

 drivers/infiniband/core/ucma.c | 38 ++++++++++++++++++++------------------
 1 file changed, 20 insertions(+), 18 deletions(-)

diff --git a/drivers/infiniband/core/ucma.c b/drivers/infiniband/core/ucma.c
index a15182f..9d94d19 100644
--- a/drivers/infiniband/core/ucma.c
+++ b/drivers/infiniband/core/ucma.c
@@ -1509,7 +1509,7 @@ static ssize_t ucma_process_join(struct ucma_file *file,
 {
 	struct rdma_ucm_create_id_resp resp;
 	struct ucma_context *ctx;
-	struct ucma_multicast *mc;
+	struct ucma_multicast *mc, *tmp;
 	struct sockaddr *addr;
 	int ret;
 	u8 join_state;
@@ -1543,23 +1543,25 @@ static ssize_t ucma_process_join(struct ucma_file *file,
 	mc->uid = cmd->uid;
 	memcpy(&mc->addr, addr, cmd->addr_size);
 
-	xa_lock(&multicast_table);
-	if (__xa_alloc(&multicast_table, &mc->id, NULL, xa_limit_32b,
+	mutex_lock(&ctx->mutex);
+	list_for_each_entry(tmp, &ctx->mc_list, list)
+		if (!memcmp(&tmp->addr, addr, cmd->addr_size)) {
+			ret = -EADDRINUSE;
+			goto err_free_mc;
+		}
+
+	if (xa_alloc(&multicast_table, &mc->id, NULL, xa_limit_32b,
 		     GFP_KERNEL)) {
 		ret = -ENOMEM;
 		goto err_free_mc;
 	}
 
 	list_add_tail(&mc->list, &ctx->mc_list);
-	xa_unlock(&multicast_table);
 
-	mutex_lock(&ctx->mutex);
 	ret = rdma_join_multicast(ctx->cm_id, (struct sockaddr *)&mc->addr,
 				  join_state, mc);
-	if (ret) {
-		mutex_unlock(&ctx->mutex);
+	if (ret)
 		goto err_xa_erase;
-	}
 
 	resp.id = mc->id;
 	if (copy_to_user(u64_to_user_ptr(cmd->response),
@@ -1567,24 +1570,22 @@ static ssize_t ucma_process_join(struct ucma_file *file,
 		ret = -EFAULT;
 		goto err_leave_multicast;
 	}
-	mutex_unlock(&ctx->mutex);
 
 	xa_store(&multicast_table, mc->id, mc, 0);
+	mutex_unlock(&ctx->mutex);
 
 	ucma_put_ctx(ctx);
 	return 0;
 
 err_leave_multicast:
 	rdma_leave_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr);
-	mutex_unlock(&ctx->mutex);
 	ucma_cleanup_mc_events(mc);
 err_xa_erase:
-	xa_lock(&multicast_table);
 	list_del(&mc->list);
-	__xa_erase(&multicast_table, mc->id);
+	xa_erase(&multicast_table, mc->id);
 err_free_mc:
-	xa_unlock(&multicast_table);
 	kfree(mc);
+	mutex_unlock(&ctx->mutex);
 err_put_ctx:
 	ucma_put_ctx(ctx);
 	return ret;
@@ -1634,6 +1635,7 @@ static ssize_t ucma_leave_multicast(struct ucma_file *file,
 {
 	struct rdma_ucm_destroy_id cmd;
 	struct rdma_ucm_destroy_id_resp resp;
+	struct ucma_context *ctx;
 	struct ucma_multicast *mc;
 	int ret = 0;
 
@@ -1658,19 +1660,20 @@ static ssize_t ucma_leave_multicast(struct ucma_file *file,
 		goto out;
 	}
 
-	list_del(&mc->list);
+	ctx = mc->ctx;
 	__xa_erase(&multicast_table, mc->id);
 	xa_unlock(&multicast_table);
 
-	mutex_lock(&mc->ctx->mutex);
-	rdma_leave_multicast(mc->ctx->cm_id, (struct sockaddr *) &mc->addr);
-	mutex_unlock(&mc->ctx->mutex);
+	mutex_lock(&ctx->mutex);
+	list_del(&mc->list);
+	rdma_leave_multicast(ctx->cm_id, (struct sockaddr *)&mc->addr);
 
 	ucma_cleanup_mc_events(mc);
 
-	ucma_put_ctx(mc->ctx);
 	resp.events_reported = mc->events_reported;
 	kfree(mc);
+	mutex_unlock(&ctx->mutex);
+	ucma_put_ctx(ctx);
 
 	if (copy_to_user(u64_to_user_ptr(cmd.response),
 			 &resp, sizeof(resp)))

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] RDMA/ucma: reject a second join of the same multicast address
  2026-11-20 17:44 [PATCH v2] RDMA/ucma: reject a second join of the same multicast address Andrey Misiurov
@ 2026-11-20 17:52 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-20 17:52 UTC (permalink / raw)
  To: Andrey Misiurov; +Cc: linux-rdma

> ucma_process_join() permits multiple joins of the same address on one
> cm_id. Each join has its own ucma_multicast, but rdma_leave_multicast()
> searches by address and removes the first match. A leave can therefore
> destroy another join's cma_multicast and free the requested ucma_multicast
> while its cma_multicast still refers to it. A later event then accesses
> freed memory.
> 
> Reject duplicate addresses with -EADDRINUSE. Hold ctx->mutex across the
> duplicate check, mc_list insertion and rdma_join_multicast(). Use
> xa_alloc() to reserve the multicast ID; dropping the XArray lock during
> allocation cannot let another join bypass the check while the mutex is
> held.
> 
> On leave, erase the handle under the XArray lock to claim the multicast
> object, but keep it on mc_list until ctx->mutex is acquired. Hold the
> mutex continuously from list removal through rdma_leave_multicast(),
> event cleanup and freeing the object. Otherwise a rejoin after unlinking
> could create a new membership before the old address-only leave, which
> would destroy the new membership and leave the old one referencing freed
> memory.
> 
> Fixes: c8f6a362bf3e ("RDMA/cma: Add multicast communication support")
> Cc: stable@vger.kernel.org
> Signed-off-by: Andrey Misiurov <amisiurov@cloudlinux.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120174405.2761262-1-amisiurov@cloudlinux.com?part=1


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-20 17:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 17:44 [PATCH v2] RDMA/ucma: reject a second join of the same multicast address Andrey Misiurov
2026-11-20 17:52 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox