Skip to contentSkip to conversation

usb: host: fotg210: Use struct_size() helper in kzalloc()

PATCH In review Gustavo A. R. Silva 2 participants 2 messages Jan 20, 2022 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: host: fotg210: Use struct_size() helper in kzalloc()

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/fotg210-hcd.c:4017:20: warning: using sizeof on a flexible structure

---
 drivers/usb/host/fotg210-hcd.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/usb/host/fotg210-hcd.c +1 -3
index 7af17c8e069b..c3fd375b4778 100644
@@ -4014,10 +4014,8 @@ static struct fotg210_iso_sched *iso_sched_alloc(unsigned packets,
 		gfp_t mem_flags)
 {
 	struct fotg210_iso_sched *iso_sched;
-	int size = sizeof(*iso_sched);
 
-	size += packets * sizeof(struct fotg210_iso_packet);
-	iso_sched = kzalloc(size, mem_flags);
+	iso_sched = kzalloc(struct_size(iso_sched, packet, packets), mem_flags);
 	if (likely(iso_sched != NULL))
 		INIT_LIST_HEAD(&iso_sched->td_list);
 
Signature · 1 line
2.27.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 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/fotg210-hcd.c:4017:20: warning: using sizeof on a flexible structure

Good-bye more open-coded calculations. :)

Signature · 1 line
Kees Cook