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 --- 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)))