Skip to contentSkip to conversation

ucma: fix a use-after-free in ucma_resolve_ip()

PATCH Applied Cong Wang 3 participants 3 messages Sep 12, 2018 – Sep 13, 2018 InfiniBand

lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 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]>
---
 drivers/infiniband/core/ucma.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/infiniband/core/ucma.c +2 -0
index 5f437d1570fb..21863ddde63e 100644
@@ -1759,6 +1759,8 @@ static int ucma_close(struct inode *inode, struct file *filp)
 		mutex_lock(&mut);
 		if (!ctx->closing) {
 			mutex_unlock(&mut);
+			ucma_put_ctx(ctx);
+			wait_for_completion(&ctx->comp);
 			/* rdma_destroy_id ensures that no event handlers are
 			 * inflight for that id before releasing it.
 			 */
Signature · 1 line
2.14.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 37 lines · Cong Wang

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]>
Quoted 4 lines
---
 drivers/infiniband/core/ucma.c | 2 ++
 1 file changed, 2 insertions(+)

Thanks,

Applied
#2 lore raw
In reply to Cong Wang
Quoted 31 lines · Cong Wang

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

Quoted 6 lines
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]>

Thanks, applied to for-rc.

Signature · 3 lines
Doug Ledford <[email protected]>
    GPG KeyID: B826A3330E572FDD
    Key fingerprint = AE6B 1BDA 122B 23B4 265B  1274 B826 A333 0E57 2FDD