RDMA/hns: Fix memory corruption when allocating XRCDN
Subsystems (2)
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.
--- 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
@@ -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
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")Reported-by Dan Carpenter <[email protected]>Signed-off-by Weihang Li <[email protected]>--- 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@@ -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
-2static 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
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")Reported-by Dan Carpenter <[email protected]>Signed-off-by Weihang Li <[email protected]>--- 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@@ -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
-2static 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