Skip to contentSkip to conversation

mailbox: th1520: Fix out of bounds write

PATCH-next Advait Dhamorikar 2 participants 2 messages Nov 25, 2024 – Jan 18, 2025

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

The loop in the function iterates up to TH_1520_MBOX_CHANS, but the ctx->intr_mask array only has 3 elements. When TH_1520_MBOX_CHANS is set to a value larger than 3, this causes an out-of-bounds write at ctx->intr_mask[3]. This could cause an immediate crash or incorrect computations.

---
 drivers/mailbox/mailbox-th1520.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/mailbox/mailbox-th1520.c +3 -0
index 4e84640ac3b8..759634a4fb72 100644
@@ -532,6 +532,9 @@ static int __maybe_unused th1520_mbox_suspend_noirq(struct device *dev)
 	 * INFO data all assumed to be lost.
 	 */
 	for (i = 0; i < TH_1520_MBOX_CHANS; i++) {
+		if (i >= ARRAY_SIZE(ctx->intr_mask))
+			break;
+
 		ctx->intr_mask[i] =
 			ioread32(priv->local_icu[i] + TH_1520_MBOX_MASK);
 	}
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 17 lines · Advait Dhamorikar

The loop in the function iterates up to TH_1520_MBOX_CHANS, but the ctx->intr_mask array only has 3 elements. When TH_1520_MBOX_CHANS is set to a value larger than 3, this causes an out-of-bounds write at ctx->intr_mask[3]. This could cause an immediate crash or incorrect computations.

---
 drivers/mailbox/mailbox-th1520.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/mailbox/mailbox-th1520.c +0 -0
index 4e84640ac3b8..759634a4fb72 100644

drivers/mailbox/mailbox-th1520.c static int __maybe_unused th1520_mbox_suspend_noirq(struct device *dev) · line 537

Quoted 6 lines
drivers/mailbox/mailbox-th1520.c +3 -0
@@ -532,6 +532,9 @@ static int __maybe_unused th1520_mbox_suspend_noirq(struct device *dev)
         * INFO data all assumed to be lost.
         */
        for (i = 0; i < TH_1520_MBOX_CHANS; i++) {
+               if (i >= ARRAY_SIZE(ctx->intr_mask))
+                       break;
+

Maybe simply reduce the loop by one OR iterate for ARRAY_SIZE(ctx->intr_mask), here and elsewhere?
I have a gut feeling that intr_mask is declared one element short. CC'ing the author.

Thanks