Skip to contentSkip to conversation

RDMA/core: fix refcount leak in __ib_alloc_pd()

PATCH In review Wentao Liang 2 participants 2 messages Jun 8

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 RDMA/core: fix refcount leak in __ib_alloc_pd()

The error handling in __ib_alloc_pd() has a refcount leak. When get_dma_mr() fails it calls ib_dealloc_pd() which invokes ib_dealloc_pd_user(). If the driver's dealloc_pd operation returns an error, ib_dealloc_pd_user() returns early and skips both rdma_restrack_del() and kfree(pd). This leaves the resource tracking kref held and the pd memory unfreed. Because ib_dealloc_pd() has a void return, __ib_alloc_pd() cannot detect the failure, so the leak persists.

Fix it by always calling rdma_restrack_del() and kfree(pd) in ib_dealloc_pd_user(), even when the driver callback fails. This ensures the software state is cleaned up regardless of the hardware operation result.

Fixes 91a7c58fce06 ("RDMA: Restore ability to fail on PD deallocate")
---
 drivers/infiniband/core/verbs.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/infiniband/core/verbs.c +4 -1
index bac87de9cc67..6437ede11908 100644
@@ -398,8 +398,11 @@ int ib_dealloc_pd_user(struct ib_pd *pd, struct ib_udata *udata)
 	}
 
 	ret = pd->device->ops.dealloc_pd(pd, udata);
-	if (ret)
+	if (ret) {
+		rdma_restrack_del(&pd->res);
+		kfree(pd);
 		return ret;
+	}
 
 	rdma_restrack_del(&pd->res);
 	kfree(pd);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 1 line · Wentao Liang

The error handling in __ib_alloc_pd() has a refcount leak.

No it doesn't, the error handling is as designed.

Quoted 7 lines

When get_dma_mr() fails it calls ib_dealloc_pd() which invokes ib_dealloc_pd_user(). If the driver's dealloc_pd operation returns an error, ib_dealloc_pd_user() returns early and skips both rdma_restrack_del() and kfree(pd). This leaves the resource tracking kref held and the pd memory unfreed. Because ib_dealloc_pd() has a void return, __ib_alloc_pd() cannot detect the failure, so the leak persists.

Yes, we can't handle failures on this path, the kernel verb ib_dealloc_pd() could gain a WARN_ON to make these clearer, but the correct thing for the user facing API is to just leave it as is untouched.

If you mess with this you break the uverbs stuff pretty badly.

Jason