regmap-irq: Fix dereference of a potentially null d->virt_buf
authorColin Ian King <colin.king@canonical.com>
Tue, 6 Apr 2021 16:40:02 +0000 (17:40 +0100)
committerMark Brown <broonie@kernel.org>
Wed, 7 Apr 2021 15:58:33 +0000 (16:58 +0100)
The clean up of struct d can potentiallly index into a null array
d->virt_buf causing errorenous pointer dereferencing issues on
kfree calls.  Fix this by adding a null check on d->virt_buf before
attempting to traverse the array to kfree the objects.

Addresses-Coverity: ("Dereference after null check")
Fixes: 4c5014456305 ("regmap-irq: Introduce virtual regs to handle more config regs")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
Link: https://lore.kernel.org/r/20210406164002.430221-1-colin.king@canonical.com
Signed-off-by: Mark Brown <broonie@kernel.org>
drivers/base/regmap/regmap-irq.c

index e6343cc..760296a 100644 (file)
@@ -901,9 +901,11 @@ err_alloc:
        kfree(d->mask_buf);
        kfree(d->status_buf);
        kfree(d->status_reg_buf);
-       for (i = 0; i < chip->num_virt_regs; i++)
-               kfree(d->virt_buf[i]);
-       kfree(d->virt_buf);
+       if (d->virt_buf) {
+               for (i = 0; i < chip->num_virt_regs; i++)
+                       kfree(d->virt_buf[i]);
+               kfree(d->virt_buf);
+       }
        kfree(d);
        return ret;
 }