Skip to contentSkip to conversation

RDMA/hns: Fix memory corruption when allocating XRCDN

PATCH Superseded Weihang Li 2 participants 3 messages Mar 19, 2021 – Mar 22, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Weihang Li

author
#0 lore raw

PATCH 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")
---
 drivers/infiniband/hw/hns/hns_roce_pd.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/infiniband/hw/hns/hns_roce_pd.c +8 -2
index 3ca51ce..16d6b69 100644
@@ -140,8 +140,14 @@ 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);
+
+	*xrcdn = (u32)obj;
+
+	return ret;
 }
 
 static void hns_roce_xrcd_free(struct hns_roce_dev *hr_dev,
Signature · 1 line
2.8.1
#1 lore raw
Quoted 17 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")
---
 drivers/infiniband/hw/hns/hns_roce_pd.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/infiniband/hw/hns/hns_roce_pd.c +0 -0
index 3ca51ce..16d6b69 100644
@@ -140,8 +140,14 @@ void hns_roce_cleanup_uar_table(struct hns_roce_dev *hr_dev)
 

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

Quoted 10 lines
drivers/infiniband/hw/hns/hns_roce_pd.c +6 -2
 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);
+
+	*xrcdn = (u32)obj;

NIT, it will be safer if you don't set set xrcdn after hns_roce_bitmap_alloc() failed.

Thanks

Quoted 8 lines
drivers/infiniband/hw/hns/hns_roce_pd.c +2 -0
+
+	return ret;
 }
 
 static void hns_roce_xrcd_free(struct hns_roce_dev *hr_dev,
Signature · 1 line
2.8.1
author
#2 lore raw
Quoted 33 lines · Leon Romanovsky
Quoted 17 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")
---
 drivers/infiniband/hw/hns/hns_roce_pd.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/infiniband/hw/hns/hns_roce_pd.c +0 -0
index 3ca51ce..16d6b69 100644
@@ -140,8 +140,14 @@ void hns_roce_cleanup_uar_table(struct hns_roce_dev *hr_dev)
 

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

Quoted 10 lines
drivers/infiniband/hw/hns/hns_roce_pd.c +6 -2
 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);
+
+	*xrcdn = (u32)obj;

NIT, it will be safer if you don't set set xrcdn after hns_roce_bitmap_alloc() failed.

Thanks

Quoted 8 lines
Quoted 8 lines
drivers/infiniband/hw/hns/hns_roce_pd.c +2 -0
+
+	return ret;
 }
 
 static void hns_roce_xrcd_free(struct hns_roce_dev *hr_dev,
Signature · 1 line
2.8.1

Thank you, I will fix it.

Weihang