usb: host: ehci-sched: Use struct_size() in kzalloc()
authorGustavo A. R. Silva <gustavoars@kernel.org>
Tue, 11 Jan 2022 07:54:27 +0000 (01:54 -0600)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 26 Jan 2022 12:50:07 +0000 (13:50 +0100)
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 worse scenario, could lead to heap overflows.

Also, address the following sparse warning:
drivers/usb/host/ehci-sched.c:1168:40: warning: using sizeof on a flexible structure

Link: https://github.com/KSPP/linux/issues/160
Link: https://github.com/KSPP/linux/issues/174
Acked-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Link: https://lore.kernel.org/r/20220111075427.GA76390@embeddedor
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/usb/host/ehci-sched.c

index 0f85aa9b2fb1a379652be80c0dd6bb2c73fab910..bd542b6fc46bdab1f42721b682ed49a269f090f3 100644 (file)
@@ -1165,10 +1165,8 @@ static struct ehci_iso_sched *
 iso_sched_alloc(unsigned packets, gfp_t mem_flags)
 {
        struct ehci_iso_sched   *iso_sched;
-       int                     size = sizeof(*iso_sched);
 
-       size += packets * sizeof(struct ehci_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);