Skip to contentSkip to conversation

platform: generic: allwinner: Fix PLIC array bounds

PATCH New Samuel Holland 3 participants 3 messages Dec 27, 2022 – Jan 13, 2023 Arm/Allwinner sunXi SoC support

lore
Share & tools
lore

Samuel Holland

author maintainer
#0 lore raw

PATCH 1/1 platform: generic: allwinner: Fix PLIC array bounds

The two referenced commits passed incorrect bounds to the PLIC save/ restore functions, causing out-of-bounds memory access. The functions expect "num" to be the 1-based number of interrupt sources, equivalent to the "riscv,ndev" devicetree property. Thus, "num" must be strictly smaller than the 0-based size of the array storing the register values.

However, the referenced commits incorrectly passed in the unmodified size of the array as "num". Fix this by reducing PLIC_SOURCES (matching "riscv,ndev" on this platform), while keeping the same array sizes.

Addresses-Coverity-ID: 1530251 ("Out-of-bounds access")
Addresses-Coverity-ID: 1530252 ("Out-of-bounds access")

Fixes 8509e46ca63a ("lib: utils/irqchip: plic: Ensure no out-of-bound access in priority save/restore helpers")
Fixes 9a2eeb4aaeac ("lib: utils/irqchip: plic: Ensure no out-of-bound access in context save/restore helpers")

---

 platform/generic/allwinner/sun20i-d1.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
platform/generic/allwinner/sun20i-d1.c +3 -3
index 1da9e5b..e2b76a3 100644
@@ -69,10 +69,10 @@ static void sun20i_d1_csr_restore(void)
  * PLIC
  */
 
-#define PLIC_SOURCES			176
-#define PLIC_IE_WORDS			((PLIC_SOURCES + 31) / 32)
+#define PLIC_SOURCES			175
+#define PLIC_IE_WORDS			(PLIC_SOURCES / 32 + 1)
 
-static u8 plic_priority[PLIC_SOURCES];
+static u8 plic_priority[1 + PLIC_SOURCES];
 static u32 plic_sie[PLIC_IE_WORDS];
 static u32 plic_threshold;
 
Signature · 1 line
2.37.4
#1 lore raw

PATCH 1/1 platform: generic: allwinner: Fix PLIC array bounds

platform/generic/allwinner/sun20i-d1.c static void sun20i_d1_csr_restore(void) · line 75

Quoted 2 lines · Samuel Holland
platform/generic/allwinner/sun20i-d1.c +1 -1
-static u8 plic_priority[PLIC_SOURCES];
+static u8 plic_priority[1 + PLIC_SOURCES];

This is unnecessarily wasting space.

Signature · 3 lines
Andreas Schwab, schwab at linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."
Applied
#2 lore raw

PATCH 1/1 platform: generic: allwinner: Fix PLIC array bounds

In reply to Samuel Holland
Quoted 11 lines · Samuel Holland

The two referenced commits passed incorrect bounds to the PLIC save/ restore functions, causing out-of-bounds memory access. The functions expect "num" to be the 1-based number of interrupt sources, equivalent to the "riscv,ndev" devicetree property. Thus, "num" must be strictly smaller than the 0-based size of the array storing the register values.

However, the referenced commits incorrectly passed in the unmodified size of the array as "num". Fix this by reducing PLIC_SOURCES (matching "riscv,ndev" on this platform), while keeping the same array sizes.

Quoted 5 lines

Addresses-Coverity-ID: 1530251 ("Out-of-bounds access")
Addresses-Coverity-ID: 1530252 ("Out-of-bounds access")

Fixes 8509e46ca63a ("lib: utils/irqchip: plic: Ensure no out-of-bound access in priority save/restore helpers")
Fixes 9a2eeb4aaeac ("lib: utils/irqchip: plic: Ensure no out-of-bound access in context save/restore helpers")

This looks similar to Heinrich's patch. I am merging this since it came after enough discussion on Heinrich's patch.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 31 lines

---

 platform/generic/allwinner/sun20i-d1.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
platform/generic/allwinner/sun20i-d1.c +3 -4
index 1da9e5b..e2b76a3 100644
@@ -69,10 +69,10 @@ static void sun20i_d1_csr_restore(void)
  * PLIC
  */
 
-#define PLIC_SOURCES                   176
-#define PLIC_IE_WORDS                  ((PLIC_SOURCES + 31) / 32)
+#define PLIC_SOURCES                   175
+#define PLIC_IE_WORDS                  (PLIC_SOURCES / 32 + 1)
 
-static u8 plic_priority[PLIC_SOURCES];
+static u8 plic_priority[1 + PLIC_SOURCES];
 static u32 plic_sie[PLIC_IE_WORDS];
 static u32 plic_threshold;
 
--

2.37.4

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi