diff mbox series

lib: utils/irqchip: plic: Fix the off-by-one error in priority save/restore helpers

Message ID 20221128132129.58558-1-bmeng@tinylab.org
State Superseded
Headers show
Series lib: utils/irqchip: plic: Fix the off-by-one error in priority save/restore helpers | expand

Commit Message

Bin Meng Nov. 28, 2022, 1:21 p.m. UTC
Interrupt source 0 is reserved. Hence the irq should start from 1.

Fixes: 2b79b694a805 ("lib: irqchip/plic: Add priority save/restore helpers")
Signed-off-by: Bin Meng <bmeng@tinylab.org>
---

 lib/utils/irqchip/plic.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Andreas Schwab Nov. 28, 2022, 1:36 p.m. UTC | #1
On Nov 28 2022, Bin Meng wrote:

> diff --git a/lib/utils/irqchip/plic.c b/lib/utils/irqchip/plic.c
> index 73d7788..4df9020 100644
> --- a/lib/utils/irqchip/plic.c
> +++ b/lib/utils/irqchip/plic.c
> @@ -38,13 +38,13 @@ static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val)
>  
>  void plic_priority_save(const struct plic_data *plic, u8 *priority)
>  {
> -	for (u32 i = 0; i < plic->num_src; i++)
> +	for (u32 i = 1; i <= plic->num_src; i++)
>  		priority[i] = plic_get_priority(plic, i);

That requires updating the size of the priority array.
Bin Meng Nov. 28, 2022, 1:50 p.m. UTC | #2
On 2022/11/28 21:36:24, "Andreas Schwab" <schwab@suse.de> wrote:

>On Nov 28 2022, Bin Meng wrote:
>
>>  diff --git a/lib/utils/irqchip/plic.c b/lib/utils/irqchip/plic.c
>>  index 73d7788..4df9020 100644
>>  --- a/lib/utils/irqchip/plic.c
>>  +++ b/lib/utils/irqchip/plic.c
>>  @@ -38,13 +38,13 @@ static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val)
>>
>>   void plic_priority_save(const struct plic_data *plic, u8 *priority)
>>   {
>>  -	for (u32 i = 0; i < plic->num_src; i++)
>>  +	for (u32 i = 1; i <= plic->num_src; i++)
>>   		priority[i] = plic_get_priority(plic, i);
>
>That requires updating the size of the priority array.
These 2 APIs are really poorly designed. The index to the priority array 
is controlled by plic->num_src which comes from DTS, so there is 
potentially a mismatch between the DTS property value and the size of 
the priority array.

Currently the only user of these 2 APIs in OpenSBI is D1 and the size of 
the priority array is set to 176. There is no problem if DTS provides a 
less than 176 value. Otherwise it can cause out-of-bound access in 
OpenSBI.

Regards,
Bin
diff mbox series

Patch

diff --git a/lib/utils/irqchip/plic.c b/lib/utils/irqchip/plic.c
index 73d7788..4df9020 100644
--- a/lib/utils/irqchip/plic.c
+++ b/lib/utils/irqchip/plic.c
@@ -38,13 +38,13 @@  static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val)
 
 void plic_priority_save(const struct plic_data *plic, u8 *priority)
 {
-	for (u32 i = 0; i < plic->num_src; i++)
+	for (u32 i = 1; i <= plic->num_src; i++)
 		priority[i] = plic_get_priority(plic, i);
 }
 
 void plic_priority_restore(const struct plic_data *plic, const u8 *priority)
 {
-	for (u32 i = 0; i < plic->num_src; i++)
+	for (u32 i = 1; i <= plic->num_src; i++)
 		plic_set_priority(plic, i, priority[i]);
 }