genirq/generic_chip: Use struct_size() in kzalloc()
authorGustavo A. R. Silva <gustavoars@kernel.org>
Thu, 13 May 2021 21:27:29 +0000 (16:27 -0500)
committerThomas Gleixner <tglx@linutronix.de>
Tue, 10 Aug 2021 13:35:20 +0000 (15:35 +0200)
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.

This code was detected with the help of Coccinelle and, audited and
fixed manually.

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20210513212729.GA214145@embeddedor
kernel/irq/generic-chip.c

index f8f23af6ab0dc4533e8fe94ca6a2c5dfc6166084..cc7cdd26e23e6dd39bca25b664c3c20cd4413545 100644 (file)
@@ -240,9 +240,8 @@ irq_alloc_generic_chip(const char *name, int num_ct, unsigned int irq_base,
                       void __iomem *reg_base, irq_flow_handler_t handler)
 {
        struct irq_chip_generic *gc;
-       unsigned long sz = sizeof(*gc) + num_ct * sizeof(struct irq_chip_type);
 
-       gc = kzalloc(sz, GFP_KERNEL);
+       gc = kzalloc(struct_size(gc, chip_types, num_ct), GFP_KERNEL);
        if (gc) {
                irq_init_generic_chip(gc, name, num_ct, irq_base, reg_base,
                                      handler);
@@ -288,8 +287,11 @@ int __irq_alloc_domain_generic_chips(struct irq_domain *d, int irqs_per_chip,
 {
        struct irq_domain_chip_generic *dgc;
        struct irq_chip_generic *gc;
-       int numchips, sz, i;
        unsigned long flags;
+       int numchips, i;
+       size_t dgc_sz;
+       size_t gc_sz;
+       size_t sz;
        void *tmp;
 
        if (d->gc)
@@ -300,8 +302,9 @@ int __irq_alloc_domain_generic_chips(struct irq_domain *d, int irqs_per_chip,
                return -EINVAL;
 
        /* Allocate a pointer, generic chip and chiptypes for each chip */
-       sz = sizeof(*dgc) + numchips * sizeof(gc);
-       sz += numchips * (sizeof(*gc) + num_ct * sizeof(struct irq_chip_type));
+       gc_sz = struct_size(gc, chip_types, num_ct);
+       dgc_sz = struct_size(dgc, gc, numchips);
+       sz = dgc_sz + numchips * gc_sz;
 
        tmp = dgc = kzalloc(sz, GFP_KERNEL);
        if (!dgc)
@@ -314,7 +317,7 @@ int __irq_alloc_domain_generic_chips(struct irq_domain *d, int irqs_per_chip,
        d->gc = dgc;
 
        /* Calc pointer to the first generic chip */
-       tmp += sizeof(*dgc) + numchips * sizeof(gc);
+       tmp += dgc_sz;
        for (i = 0; i < numchips; i++) {
                /* Store the pointer to the generic chip */
                dgc->gc[i] = gc = tmp;
@@ -331,7 +334,7 @@ int __irq_alloc_domain_generic_chips(struct irq_domain *d, int irqs_per_chip,
                list_add_tail(&gc->list, &gc_list);
                raw_spin_unlock_irqrestore(&gc_lock, flags);
                /* Calc pointer to the next generic chip */
-               tmp += sizeof(*gc) + num_ct * sizeof(struct irq_chip_type);
+               tmp += gc_sz;
        }
        return 0;
 }