Skip to content

Commit 5fe23f2

Browse files
congwangdledford
authored andcommitted
ucma: fix a use-after-free in ucma_resolve_ip()
There is a race condition between ucma_close() and ucma_resolve_ip(): CPU0 CPU1 ucma_resolve_ip(): ucma_close(): ctx = ucma_get_ctx(file, cmd.id); list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) { mutex_lock(&mut); idr_remove(&ctx_idr, ctx->id); mutex_unlock(&mut); ... mutex_lock(&mut); if (!ctx->closing) { mutex_unlock(&mut); rdma_destroy_id(ctx->cm_id); ... ucma_free_ctx(ctx); ret = rdma_resolve_addr(); ucma_put_ctx(ctx); Before idr_remove(), ucma_get_ctx() could still find the ctx and after rdma_destroy_id(), rdma_resolve_addr() may still access id_priv pointer. Also, ucma_put_ctx() may use ctx after ucma_free_ctx() too. ucma_close() should call ucma_put_ctx() too which tests the refcnt and waits for the last one releasing it. The similar pattern is already used by ucma_destroy_id(). Reported-and-tested-by: [email protected] Reported-by: [email protected] Cc: Jason Gunthorpe <[email protected]> Cc: Doug Ledford <[email protected]> Cc: Leon Romanovsky <[email protected]> Signed-off-by: Cong Wang <[email protected]> Reviewed-by: Leon Romanovsky <[email protected]> Signed-off-by: Doug Ledford <[email protected]>
1 parent 67e3816 commit 5fe23f2

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

drivers/infiniband/core/ucma.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,6 +1759,8 @@ static int ucma_close(struct inode *inode, struct file *filp)
17591759
mutex_lock(&mut);
17601760
if (!ctx->closing) {
17611761
mutex_unlock(&mut);
1762+
ucma_put_ctx(ctx);
1763+
wait_for_completion(&ctx->comp);
17621764
/* rdma_destroy_id ensures that no event handlers are
17631765
* inflight for that id before releasing it.
17641766
*/

0 commit comments

Comments
 (0)