Skip to contentSkip to conversation

usb: xhci-mtk: Use struct_size() helper in create_sch_ep()

PATCH In review Gustavo A. R. Silva 3 participants 3 messages Jan 20, 2022 – Jan 24, 2022

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

PATCH 1/1 usb: xhci-mtk: Use struct_size() helper in create_sch_ep()

Make use of the struct_size() helper instead of an open-coded version, in order to avoid any potential type mistakes or integer overflows that, in the worst scenario, could lead to heap overflows.

Also, address the following sparse warnings:
drivers/usb/host/xhci-mtk-sch.c:265:20: warning: using sizeof on a flexible structure

---
 drivers/usb/host/xhci-mtk-sch.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/usb/host/xhci-mtk-sch.c +3 -4
index edbfa82c6565..f3139ce7b0a9 100644
@@ -248,7 +248,6 @@ create_sch_ep(struct xhci_hcd_mtk *mtk, struct usb_device *udev,
 	struct mu3h_sch_bw_info *bw_info;
 	struct mu3h_sch_tt *tt = NULL;
 	u32 len_bw_budget_table;
-	size_t mem_size;
 
 	bw_info = get_bw_info(mtk, udev, ep);
 	if (!bw_info)
@@ -262,9 +261,9 @@ create_sch_ep(struct xhci_hcd_mtk *mtk, struct usb_device *udev,
 	else
 		len_bw_budget_table = 1;
 
-	mem_size = sizeof(struct mu3h_sch_ep_info) +
-			len_bw_budget_table * sizeof(u32);
-	sch_ep = kzalloc(mem_size, GFP_KERNEL);
+	sch_ep = kzalloc(struct_size(sch_ep, bw_budget_table,
+				     len_bw_budget_table),
+			 GFP_KERNEL);
 	if (!sch_ep)
 		return ERR_PTR(-ENOMEM);
 
Signature · 1 line
2.27.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 34 lines · Gustavo A. R. Silva

Make use of the struct_size() helper instead of an open-coded version,
in order to avoid any potential type mistakes or integer overflows that,
in the worst scenario, could lead to heap overflows.

Also, address the following sparse warnings:
drivers/usb/host/xhci-mtk-sch.c:265:20: warning: using sizeof on a flexible structure

---
 drivers/usb/host/xhci-mtk-sch.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/usb/host/xhci- renamed from drivers/usb/host/xhci-mtk-sch.c +0 -0

mtk-sch.c
index edbfa82c6565..f3139ce7b0a9 100644

drivers/usb/host/xhci-mtk-sch.c +0 -1
@@ -248,7 +248,6 @@ create_sch_ep(struct xhci_hcd_mtk *mtk, struct
 usb_device *udev,
 	struct mu3h_sch_bw_info *bw_info;
 	struct mu3h_sch_tt *tt = NULL;
 	u32 len_bw_budget_table;
-	size_t mem_size;
 
 	bw_info = get_bw_info(mtk, udev, ep);
drivers/usb/host/xhci-mtk-sch.c +0 -0
 	if (!bw_info)
@@ -262,9 +261,9 @@ create_sch_ep(struct xhci_hcd_mtk *mtk, struct
 usb_device *udev,
 	else

drivers/usb/host/xhci-mtk-sch.c create_sch_ep(struct xhci_hcd_mtk *mtk, struct · line 269

Quoted 10 lines
drivers/usb/host/xhci-mtk-sch.c +3 -3
 		len_bw_budget_table = 1;
 
-	mem_size = sizeof(struct mu3h_sch_ep_info) +
-			len_bw_budget_table * sizeof(u32);
-	sch_ep = kzalloc(mem_size, GFP_KERNEL);
+	sch_ep = kzalloc(struct_size(sch_ep, bw_budget_table,
+				     len_bw_budget_table),
+			 GFP_KERNEL);
 	if (!sch_ep)
 		return ERR_PTR(-ENOMEM);

Thanks a lot

Quoted 1 line
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Gustavo A. R. Silva

Il 20/01/22 02:55, Gustavo A. R. Silva ha scritto:

Quoted 11 lines

Make use of the struct_size() helper instead of an open-coded version, in order to avoid any potential type mistakes or integer overflows that, in the worst scenario, could lead to heap overflows.

Also, address the following sparse warnings:
drivers/usb/host/xhci-mtk-sch.c:265:20: warning: using sizeof on a flexible structure