Skip to contentSkip to conversation

RDMA/core: Fix unsafe linked list traversal after failing to allocate CQ

PATCH Applied Weihang Li 2 participants 2 messages Sep 1, 2020 – Sep 2, 2020 InfiniBand

lore
Share & tools
lore

Weihang Li

author
#0 lore raw

PATCH 1/1 RDMA/core: Fix unsafe linked list traversal after failing to allocate CQ

From: Xi Wang <[email protected]>

It's not safe to access the next CQ in list_for_each_entry() after invoking ib_free_cq(), because the CQ has already been freed in current iteration. It should be replaced by list_for_each_entry_safe().

Fixes c7ff819aefea ("RDMA/core: Introduce shared CQ pool API")
---
 drivers/infiniband/core/cq.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/infiniband/core/cq.c +2 -2
index 513825e..a92fc3f 100644
@@ -379,7 +379,7 @@ static int ib_alloc_cqs(struct ib_device *dev, unsigned int nr_cqes,
 {
 	LIST_HEAD(tmp_list);
 	unsigned int nr_cqs, i;
-	struct ib_cq *cq;
+	struct ib_cq *cq, *n;
 	int ret;
 
 	if (poll_ctx > IB_POLL_LAST_POOL_TYPE) {
@@ -412,7 +412,7 @@ static int ib_alloc_cqs(struct ib_device *dev, unsigned int nr_cqes,
 	return 0;
 
 out_free_cqs:
-	list_for_each_entry(cq, &tmp_list, pool_entry) {
+	list_for_each_entry_safe(cq, n, &tmp_list, pool_entry) {
 		cq->shared = false;
 		ib_free_cq(cq);
 	}
Signature · 1 line
2.8.1
maintainer Applied
#1 lore raw
Quoted 6 lines · Weihang Li

From: Xi Wang <[email protected]>

It's not safe to access the next CQ in list_for_each_entry() after invoking ib_free_cq(), because the CQ has already been freed in current iteration. It should be replaced by list_for_each_entry_safe().

Quoted 6 lines
Fixes c7ff819aefea ("RDMA/core: Introduce shared CQ pool API")
---
 drivers/infiniband/core/cq.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Applied to for-rc, thanks

Jason