Skip to contentSkip to conversation

RDMA/hns: Fix memory corruption when allocating XRCDN

PATCH Applied Weihang Li 3 participants 3 messages Mar 22, 2021 – Mar 23, 2021

Subsystems (2)
lore
Share & tools
lore

Weihang Li

author
#0 lore raw

PATCH v2 1/1 RDMA/hns: Fix memory corruption when allocating XRCDN

It's incorrect to cast the type of pointer to xrcdn from (u32 *) to (unsigned long *), then pass it into hns_roce_bitmap_alloc(), this will lead to a memory corruption.

Fixes 32548870d438 ("RDMA/hns: Add support for XRC on HIP09")

---
Changes since v1:
- xrcdn won't be set if hns_roce_bitmap_alloc() fails.
- Link: https://patchwork.kernel.org/project/linux-rdma/patch/[email protected]/

 drivers/infiniband/hw/hns/hns_roce_pd.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/infiniband/hw/hns/hns_roce_pd.c +10 -2
index 3ca51ce..68a59ff 100644
@@ -140,8 +140,16 @@ void hns_roce_cleanup_uar_table(struct hns_roce_dev *hr_dev)
 
 static int hns_roce_xrcd_alloc(struct hns_roce_dev *hr_dev, u32 *xrcdn)
 {
-	return hns_roce_bitmap_alloc(&hr_dev->xrcd_bitmap,
-				     (unsigned long *)xrcdn);
+	unsigned long obj;
+	int ret;
+
+	ret = hns_roce_bitmap_alloc(&hr_dev->xrcd_bitmap, &obj);
+	if (ret)
+		return ret;
+
+	*xrcdn = (u32)obj;
+
+	return 0;
 }
 
 static void hns_roce_xrcd_free(struct hns_roce_dev *hr_dev,
Signature · 1 line
2.8.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 7 lines · Weihang Li

It's incorrect to cast the type of pointer to xrcdn from (u32 *) to (unsigned long *), then pass it into hns_roce_bitmap_alloc(), this will lead to a memory corruption.

Fixes 32548870d438 ("RDMA/hns: Add support for XRC on HIP09")
Quoted 8 lines

---
Changes since v1:
- xrcdn won't be set if hns_roce_bitmap_alloc() fails.
- Link: https://patchwork.kernel.org/project/linux-rdma/patch/[email protected]/

 drivers/infiniband/hw/hns/hns_roce_pd.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

Thanks,

maintainer Applied
#2 lore raw
In reply to Weihang Li
Quoted 8 lines · Weihang Li

It's incorrect to cast the type of pointer to xrcdn from (u32 *) to (unsigned long *), then pass it into hns_roce_bitmap_alloc(), this will lead to a memory corruption.

Fixes 32548870d438 ("RDMA/hns: Add support for XRC on HIP09")
Quoted 7 lines

---
Changes since v1:
- xrcdn won't be set if hns_roce_bitmap_alloc() fails.
- Link: https://patchwork.kernel.org/project/linux-rdma/patch/[email protected]/

 drivers/infiniband/hw/hns/hns_roce_pd.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

Applied to for-next

Quoted 8 lines
drivers/infiniband/hw/hns/hns_roce_pd.c +0 -0
index 3ca51ce..68a59ff 100644
@@ -140,8 +140,16 @@ void hns_roce_cleanup_uar_table(struct hns_roce_dev *hr_dev)
 
 static int hns_roce_xrcd_alloc(struct hns_roce_dev *hr_dev, u32 *xrcdn)
 {

drivers/infiniband/hw/hns/hns_roce_pd.c void hns_roce_cleanup_uar_table(struct hns_roce_dev *hr_dev) · line 150

Quoted 10 lines
drivers/infiniband/hw/hns/hns_roce_pd.c +8 -2
-	return hns_roce_bitmap_alloc(&hr_dev->xrcd_bitmap,
-				     (unsigned long *)xrcdn);
+	unsigned long obj;
+	int ret;
+
+	ret = hns_roce_bitmap_alloc(&hr_dev->xrcd_bitmap, &obj);
+	if (ret)
+		return ret;
+
+	*xrcdn = (u32)obj;

Though this cast is useless, I removed it

Thanks,
Jason