lib: utils/irqchip: plic: Fix the off-by-one error in priority save/restore helpers
PATCH v3 1/5 lib: utils/irqchip: plic: Fix the off-by-one error in priority save/restore helpers
Interrupt source 0 is reserved. Hence the irq should start from 1.
---
(no changes since v1)
lib/utils/irqchip/plic.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/utils/irqchip/plic.c +2 -2
@@ -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]); }
Signature · 1 line
2.34.1
PATCH v3 2/5 lib: utils/irqchip: plic: Ensure no out-of-bound access in priority save/restore helpers
Currently the priority save/restore helpers writes/reads the provided array using an index whose maximum value is determined by PLIC, which potentially may disagree with the caller to these helpers.
Add a parameter to ask the caller to provide the size limit of the array to ensure no out-of-bound access happens.
---
Changes in v3:
- fix the size limit check
- move the size limit check to plic_priority_save/restore
- add parameter description to fdt_plic_priority_save/restore
Changes in v2:
- new patch: libs: utils/irqchip: plic: Ensure no out-of-bound access in priority save/restore helpers
include/sbi_utils/irqchip/fdt_irqchip_plic.h | 14 ++++++++++++-- include/sbi_utils/irqchip/plic.h | 5 +++-- lib/utils/irqchip/fdt_irqchip_plic.c | 8 ++++---- lib/utils/irqchip/plic.c | 15 +++++++++++---- platform/generic/allwinner/sun20i-d1.c | 4 ++-- 5 files changed, 32 insertions(+), 14 deletions(-)
include/sbi_utils/irqchip/fdt_irqchip_plic.h +12 -2
@@ -9,9 +9,19 @@#include <sbi/sbi_types.h> -void fdt_plic_priority_save (u8 *priority); +/** +* Save the PLIC priority state +* @param priority pointer to the memory region for the saved priority +* @param num size of the memory region including interrupt source 0 +*/ +void fdt_plic_priority_save (u8 *priority,u32 num); -void fdt_plic_priority_restore (const u8 *priority); +/** +* Restore the PLIC priority state +* @param priority pointer to the memory region for the saved priority +* @param num size of the memory region including interrupt source 0 +*/ +void fdt_plic_priority_restore (const u8 *priority,u32 num);void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold);
include/sbi_utils/irqchip/plic.h +3 -2
@@ -18,9 +18,10 @@ struct plic_data { };/* So far, priorities on all consumers of these functions fit in 8 bits. */ -void plic_priority_save (const struct plic_data *plic,u8 *priority); +void plic_priority_save (const struct plic_data *plic,u8 *priority,u32 num); -void plic_priority_restore (const struct plic_data *plic,const u8 *priority); +void plic_priority_restore (const struct plic_data *plic,const u8 *priority, +u32 num);void plic_context_save (const struct plic_data *plic,int context_id,u32 *enable,u32 *threshold);
lib/utils/irqchip/fdt_irqchip_plic.c +4 -4
@@ -24,18 +24,18 @@ static struct plic_data plic[PLIC_MAX_NR];static struct plic_data *plic_hartid2data[SBI_HARTMASK_MAX_BITS ];static int plic_hartid2context[SBI_HARTMASK_MAX_BITS ][2 ]; -void fdt_plic_priority_save (u8 *priority) +void fdt_plic_priority_save (u8 *priority,u32 num) {struct plic_data *plic = plic_hartid2data[current_hartid ()]; -plic_priority_save (plic, priority); +plic_priority_save (plic, priority, num); } -void fdt_plic_priority_restore (const u8 *priority) +void fdt_plic_priority_restore (const u8 *priority,u32 num) {struct plic_data *plic = plic_hartid2data[current_hartid ()]; -plic_priority_restore (plic, priority); +plic_priority_restore (plic, priority, num); }void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold)
lib/utils/irqchip/plic.c +11 -4
@@ -36,15 +36,22 @@ static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val)writel (val, plic_priority); } -void plic_priority_save (const struct plic_data *plic,u8 *priority) +void plic_priority_save (const struct plic_data *plic,u8 *priority,u32 num) { -for (u32 i =1 ; i <= plic->num_src; i++) +if (num > plic->num_src) + num = plic->num_src; + +for (u32 i =1 ; i <= num; i++) priority[i] =plic_get_priority (plic, i); } -void plic_priority_restore (const struct plic_data *plic,const u8 *priority) +void plic_priority_restore (const struct plic_data *plic,const u8 *priority, +u32 num) { -for (u32 i =1 ; i <= plic->num_src; i++) +if (num > plic->num_src) + num = plic->num_src; + +for (u32 i =1 ; i <= num; i++)plic_set_priority (plic, i, priority[i]); }
platform/generic/allwinner/sun20i-d1.c +2 -2
@@ -79,13 +79,13 @@ static u32 plic_threshold;static void sun20i_d1_plic_save (void ) {fdt_plic_context_save (true, plic_sie, &plic_threshold); -fdt_plic_priority_save (plic_priority); +fdt_plic_priority_save (plic_priority,PLIC_SOURCES ); }static void sun20i_d1_plic_restore (void ) {thead_plic_restore (); -fdt_plic_priority_restore (plic_priority); +fdt_plic_priority_restore (plic_priority,PLIC_SOURCES );fdt_plic_context_restore (true, plic_sie, plic_threshold); }
Signature · 1 line
2.34.1
PATCH v3 2/5 lib: utils/irqchip: plic: Ensure no out-of-bound access in priority save/restore helpers
Quoted 9 lines · Bin Meng
Currently the priority save/restore helpers writes/reads the provided array using an index whose maximum value is determined by PLIC, which potentially may disagree with the caller to these helpers.
Add a parameter to ask the caller to provide the size limit of the array to ensure no out-of-bound access happens.
Signed-off-by Bin Meng <[email protected]>
Looks good to me.
Regards,
Anup
Quoted 147 lines
---
Changes in v3:
- fix the size limit check
- move the size limit check to plic_priority_save/restore
- add parameter description to fdt_plic_priority_save/restoreChanges in v2:
- new patch: libs: utils/irqchip: plic: Ensure no out-of-bound access in priority save/restore helpersinclude/sbi_utils/irqchip/fdt_irqchip_plic.h | 14 ++++++++++++-- include/sbi_utils/irqchip/plic.h | 5 +++-- lib/utils/irqchip/fdt_irqchip_plic.c | 8 ++++---- lib/utils/irqchip/plic.c | 15 +++++++++++---- platform/generic/allwinner/sun20i-d1.c | 4 ++-- 5 files changed, 32 insertions(+), 14 deletions(-)include/sbi_utils/irqchip/fdt_irqchip_plic.h +12
-2@@ -9,9 +9,19 @@#include <sbi/sbi_types.h> -void fdt_plic_priority_save (u8 *priority); +/** +* Save the PLIC priority state +* @param priority pointer to the memory region for the saved priority +* @param num size of the memory region including interrupt source 0 +*/ +void fdt_plic_priority_save (u8 *priority,u32 num); -void fdt_plic_priority_restore (const u8 *priority); +/** +* Restore the PLIC priority state +* @param priority pointer to the memory region for the saved priority +* @param num size of the memory region including interrupt source 0 +*/ +void fdt_plic_priority_restore (const u8 *priority,u32 num);void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold);include/sbi_utils/irqchip/plic.h +3
-2@@ -18,9 +18,10 @@ struct plic_data { };/* So far, priorities on all consumers of these functions fit in 8 bits. */ -void plic_priority_save (const struct plic_data *plic,u8 *priority); +void plic_priority_save (const struct plic_data *plic,u8 *priority,u32 num); -void plic_priority_restore (const struct plic_data *plic,const u8 *priority); +void plic_priority_restore (const struct plic_data *plic,const u8 *priority, +u32 num);void plic_context_save (const struct plic_data *plic,int context_id,u32 *enable,u32 *threshold);lib/utils/irqchip/fdt_irqchip_plic.c +4
-4@@ -24,18 +24,18 @@ static struct plic_data plic[PLIC_MAX_NR];static struct plic_data *plic_hartid2data[SBI_HARTMASK_MAX_BITS ];static int plic_hartid2context[SBI_HARTMASK_MAX_BITS ][2 ]; -void fdt_plic_priority_save (u8 *priority) +void fdt_plic_priority_save (u8 *priority,u32 num) {struct plic_data *plic = plic_hartid2data[current_hartid ()]; -plic_priority_save (plic, priority); +plic_priority_save (plic, priority, num); } -void fdt_plic_priority_restore (const u8 *priority) +void fdt_plic_priority_restore (const u8 *priority,u32 num) {struct plic_data *plic = plic_hartid2data[current_hartid ()]; -plic_priority_restore (plic, priority); +plic_priority_restore (plic, priority, num); }void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold)lib/utils/irqchip/plic.c +11
-4@@ -36,15 +36,22 @@ static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val)writel (val, plic_priority); } -void plic_priority_save (const struct plic_data *plic,u8 *priority) +void plic_priority_save (const struct plic_data *plic,u8 *priority,u32 num) { -for (u32 i =1 ; i <= plic->num_src; i++) +if (num > plic->num_src) + num = plic->num_src; + +for (u32 i =1 ; i <= num; i++) priority[i] =plic_get_priority (plic, i); } -void plic_priority_restore (const struct plic_data *plic,const u8 *priority) +void plic_priority_restore (const struct plic_data *plic,const u8 *priority, +u32 num) { -for (u32 i =1 ; i <= plic->num_src; i++) +if (num > plic->num_src) + num = plic->num_src; + +for (u32 i =1 ; i <= num; i++)plic_set_priority (plic, i, priority[i]); }platform/generic/allwinner/sun20i-d1.c +2
-3@@ -79,13 +79,13 @@ static u32 plic_threshold;static void sun20i_d1_plic_save (void ) {fdt_plic_context_save (true, plic_sie, &plic_threshold); -fdt_plic_priority_save (plic_priority); +fdt_plic_priority_save (plic_priority,PLIC_SOURCES ); }static void sun20i_d1_plic_restore (void ) {thead_plic_restore (); -fdt_plic_priority_restore (plic_priority); +fdt_plic_priority_restore (plic_priority,PLIC_SOURCES );fdt_plic_context_restore (true, plic_sie, plic_threshold); } --2.34.1
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v3 2/5 lib: utils/irqchip: plic: Ensure no out-of-bound access in priority save/restore helpers
In reply to Bin MengQuoted 9 lines · Bin Meng
platform/generic/allwinner/sun20i-d1.c +0
-1@@ -79,13 +79,13 @@ static u32 plic_threshold;static void sun20i_d1_plic_save (void ) {fdt_plic_context_save (true, plic_sie, &plic_threshold); -fdt_plic_priority_save (plic_priority);
platform/generic/allwinner/sun20i-d1.c static u32 plic_threshold; · line 90
Quoted 10 lines
platform/generic/allwinner/sun20i-d1.c +2
-1+fdt_plic_priority_save (plic_priority,PLIC_SOURCES ); }static void sun20i_d1_plic_restore (void ) {thead_plic_restore (); -fdt_plic_priority_restore (plic_priority); +fdt_plic_priority_restore (plic_priority,PLIC_SOURCES );fdt_plic_context_restore (true, plic_sie, plic_threshold); }
That fails to update the size of the plic_priority arraay.
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."
Samuel Holland
maintainerPATCH v3 2/5 lib: utils/irqchip: plic: Ensure no out-of-bound access in priority save/restore helpers
Quoted 14 lines · Andreas Schwab
Quoted 12 lines · Bin Meng
platform/generic/allwinner/sun20i-d1.c +1
-1@@ -79,13 +79,13 @@ static u32 plic_threshold;static void sun20i_d1_plic_save (void ) {fdt_plic_context_save (true, plic_sie, &plic_threshold); -fdt_plic_priority_save (plic_priority); +fdt_plic_priority_save (plic_priority,PLIC_SOURCES ); }
Quoted 9 lines
platform/generic/allwinner/sun20i-d1.c static u32 plic_threshold; · line 90
Quoted 7 lines
platform/generic/allwinner/sun20i-d1.c +1
-1static void sun20i_d1_plic_restore (void ) {thead_plic_restore (); -fdt_plic_priority_restore (plic_priority); +fdt_plic_priority_restore (plic_priority,PLIC_SOURCES );fdt_plic_context_restore (true, plic_sie, plic_threshold); }That fails to update the size of the
plic_priorityarray.
PLIC_SOURCES is 176, and I later corrected riscv,ndev in the DT to 175, so technically this does not create a bug.
Regards,
Samuel
Samuel Holland
maintainerPATCH v3 2/5 lib: utils/irqchip: plic: Ensure no out-of-bound access in priority save/restore helpers
In reply to Bin MengQuoted 101 lines · Bin Meng
Currently the priority save/restore helpers writes/reads the provided array using an index whose maximum value is determined by PLIC, which potentially may disagree with the caller to these helpers.
Add a parameter to ask the caller to provide the size limit of the array to ensure no out-of-bound access happens.
Signed-off-by Bin Meng <[email protected]>---
Changes in v3:
- fix the size limit check
- move the size limit check to plic_priority_save/restore
- add parameter description to fdt_plic_priority_save/restoreChanges in v2:
- new patch: libs: utils/irqchip: plic: Ensure no out-of-bound access in priority save/restore helpersinclude/sbi_utils/irqchip/fdt_irqchip_plic.h | 14 ++++++++++++-- include/sbi_utils/irqchip/plic.h | 5 +++-- lib/utils/irqchip/fdt_irqchip_plic.c | 8 ++++---- lib/utils/irqchip/plic.c | 15 +++++++++++---- platform/generic/allwinner/sun20i-d1.c | 4 ++-- 5 files changed, 32 insertions(+), 14 deletions(-)include/sbi_utils/irqchip/fdt_irqchip_plic.h +12
-2@@ -9,9 +9,19 @@#include <sbi/sbi_types.h> -void fdt_plic_priority_save (u8 *priority); +/** +* Save the PLIC priority state +* @param priority pointer to the memory region for the saved priority +* @param num size of the memory region including interrupt source 0 +*/ +void fdt_plic_priority_save (u8 *priority,u32 num); -void fdt_plic_priority_restore (const u8 *priority); +/** +* Restore the PLIC priority state +* @param priority pointer to the memory region for the saved priority +* @param num size of the memory region including interrupt source 0 +*/ +void fdt_plic_priority_restore (const u8 *priority,u32 num);void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold);include/sbi_utils/irqchip/plic.h +3
-2@@ -18,9 +18,10 @@ struct plic_data { };/* So far, priorities on all consumers of these functions fit in 8 bits. */ -void plic_priority_save (const struct plic_data *plic,u8 *priority); +void plic_priority_save (const struct plic_data *plic,u8 *priority,u32 num); -void plic_priority_restore (const struct plic_data *plic,const u8 *priority); +void plic_priority_restore (const struct plic_data *plic,const u8 *priority, +u32 num);void plic_context_save (const struct plic_data *plic,int context_id,u32 *enable,u32 *threshold);lib/utils/irqchip/fdt_irqchip_plic.c +4
-4@@ -24,18 +24,18 @@ static struct plic_data plic[PLIC_MAX_NR];static struct plic_data *plic_hartid2data[SBI_HARTMASK_MAX_BITS ];static int plic_hartid2context[SBI_HARTMASK_MAX_BITS ][2 ]; -void fdt_plic_priority_save (u8 *priority) +void fdt_plic_priority_save (u8 *priority,u32 num) {struct plic_data *plic = plic_hartid2data[current_hartid ()]; -plic_priority_save (plic, priority); +plic_priority_save (plic, priority, num); } -void fdt_plic_priority_restore (const u8 *priority) +void fdt_plic_priority_restore (const u8 *priority,u32 num) {struct plic_data *plic = plic_hartid2data[current_hartid ()]; -plic_priority_restore (plic, priority); +plic_priority_restore (plic, priority, num); }void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold)lib/utils/irqchip/plic.c +0
-0
lib/utils/irqchip/plic.c static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val) · line 42
Quoted 9 lines
lib/utils/irqchip/plic.c +3
-2@@ -36,15 +36,22 @@ static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val)writel (val, plic_priority); } -void plic_priority_save (const struct plic_data *plic,u8 *priority) +void plic_priority_save (const struct plic_data *plic,u8 *priority,u32 num) { -for (u32 i =1 ; i <= plic->num_src; i++) +if (num > plic->num_src) + num = plic->num_src;
This check is not really necessary. If the caller tries to save/restore more sources then actually exist, then the extra register writes will just be ignored. We already access extra bits by doing word accesses.
For bounds checking, the only things that need to match are the array size and the loop. With your change to the loop condition, plic->num_src is not relevant at all anymore.
Regards,
Samuel
Quoted 37 lines
lib/utils/irqchip/plic.c +8
-2+ +for (u32 i =1 ; i <= num; i++) priority[i] =plic_get_priority (plic, i); } -void plic_priority_restore (const struct plic_data *plic,const u8 *priority) +void plic_priority_restore (const struct plic_data *plic,const u8 *priority, +u32 num) { -for (u32 i =1 ; i <= plic->num_src; i++) +if (num > plic->num_src) + num = plic->num_src; + +for (u32 i =1 ; i <= num; i++)plic_set_priority (plic, i, priority[i]); }platform/generic/allwinner/sun20i-d1.c +2
-2@@ -79,13 +79,13 @@ static u32 plic_threshold;static void sun20i_d1_plic_save (void ) {fdt_plic_context_save (true, plic_sie, &plic_threshold); -fdt_plic_priority_save (plic_priority); +fdt_plic_priority_save (plic_priority,PLIC_SOURCES ); }static void sun20i_d1_plic_restore (void ) {thead_plic_restore (); -fdt_plic_priority_restore (plic_priority); +fdt_plic_priority_restore (plic_priority,PLIC_SOURCES );fdt_plic_context_restore (true, plic_sie, plic_threshold); }
PATCH v3 2/5 lib: utils/irqchip: plic: Ensure no out-of-bound access in priority save/restore helpers
Quoted 117 lines · Samuel Holland
Quoted 101 lines · Bin Meng
Currently the priority save/restore helpers writes/reads the provided array using an index whose maximum value is determined by PLIC, which potentially may disagree with the caller to these helpers.
Add a parameter to ask the caller to provide the size limit of the array to ensure no out-of-bound access happens.
Signed-off-by Bin Meng <[email protected]>---
Changes in v3:
- fix the size limit check
- move the size limit check to plic_priority_save/restore
- add parameter description to fdt_plic_priority_save/restoreChanges in v2:
- new patch: libs: utils/irqchip: plic: Ensure no out-of-bound access in priority save/restore helpersinclude/sbi_utils/irqchip/fdt_irqchip_plic.h | 14 ++++++++++++-- include/sbi_utils/irqchip/plic.h | 5 +++-- lib/utils/irqchip/fdt_irqchip_plic.c | 8 ++++---- lib/utils/irqchip/plic.c | 15 +++++++++++---- platform/generic/allwinner/sun20i-d1.c | 4 ++-- 5 files changed, 32 insertions(+), 14 deletions(-)include/sbi_utils/irqchip/fdt_irqchip_plic.h +12
-2@@ -9,9 +9,19 @@#include <sbi/sbi_types.h> -void fdt_plic_priority_save (u8 *priority); +/** +* Save the PLIC priority state +* @param priority pointer to the memory region for the saved priority +* @param num size of the memory region including interrupt source 0 +*/ +void fdt_plic_priority_save (u8 *priority,u32 num); -void fdt_plic_priority_restore (const u8 *priority); +/** +* Restore the PLIC priority state +* @param priority pointer to the memory region for the saved priority +* @param num size of the memory region including interrupt source 0 +*/ +void fdt_plic_priority_restore (const u8 *priority,u32 num);void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold);include/sbi_utils/irqchip/plic.h +3
-2@@ -18,9 +18,10 @@ struct plic_data { };/* So far, priorities on all consumers of these functions fit in 8 bits. */ -void plic_priority_save (const struct plic_data *plic,u8 *priority); +void plic_priority_save (const struct plic_data *plic,u8 *priority,u32 num); -void plic_priority_restore (const struct plic_data *plic,const u8 *priority); +void plic_priority_restore (const struct plic_data *plic,const u8 *priority, +u32 num);void plic_context_save (const struct plic_data *plic,int context_id,u32 *enable,u32 *threshold);lib/utils/irqchip/fdt_irqchip_plic.c +4
-4@@ -24,18 +24,18 @@ static struct plic_data plic[PLIC_MAX_NR];static struct plic_data *plic_hartid2data[SBI_HARTMASK_MAX_BITS ];static int plic_hartid2context[SBI_HARTMASK_MAX_BITS ][2 ]; -void fdt_plic_priority_save (u8 *priority) +void fdt_plic_priority_save (u8 *priority,u32 num) {struct plic_data *plic = plic_hartid2data[current_hartid ()]; -plic_priority_save (plic, priority); +plic_priority_save (plic, priority, num); } -void fdt_plic_priority_restore (const u8 *priority) +void fdt_plic_priority_restore (const u8 *priority,u32 num) {struct plic_data *plic = plic_hartid2data[current_hartid ()]; -plic_priority_restore (plic, priority); +plic_priority_restore (plic, priority, num); }void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold)lib/utils/irqchip/plic.c +0
-0lib/utils/irqchip/plic.c static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val) · line 42
Quoted 9 lines
lib/utils/irqchip/plic.c +3
-2@@ -36,15 +36,22 @@ static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val)writel (val, plic_priority); } -void plic_priority_save (const struct plic_data *plic,u8 *priority) +void plic_priority_save (const struct plic_data *plic,u8 *priority,u32 num) { -for (u32 i =1 ; i <= plic->num_src; i++) +if (num > plic->num_src) + num = plic->num_src;This check is not really necessary. If the caller tries to save/restore more sources then actually exist, then the extra register writes will just be ignored. We already access extra bits by doing word accesses.
Quoted 3 lines
For bounds checking, the only things that need to match are the array size and the loop. With your change to the loop condition,
plic->num_srcis not relevant at all anymore.
Okay, I will drop the bound checks at the time of merging this patch since the motivation of this patch is mainly to avoid out-of-bound access to the array passed by the caller.
If required, let's have a separate patch for this.
Regards,
Anup
Quoted 47 lines
Regards,
SamuelQuoted 27 lines
lib/utils/irqchip/plic.c +8
-2+ +for (u32 i =1 ; i <= num; i++) priority[i] =plic_get_priority (plic, i); } -void plic_priority_restore (const struct plic_data *plic,const u8 *priority) +void plic_priority_restore (const struct plic_data *plic,const u8 *priority, +u32 num) { -for (u32 i =1 ; i <= plic->num_src; i++) +if (num > plic->num_src) + num = plic->num_src; + +for (u32 i =1 ; i <= num; i++)plic_set_priority (plic, i, priority[i]); }platform/generic/allwinner/sun20i-d1.c +1
-1@@ -79,13 +79,13 @@ static u32 plic_threshold;static void sun20i_d1_plic_save (void ) {fdt_plic_context_save (true, plic_sie, &plic_threshold); -fdt_plic_priority_save (plic_priority); +fdt_plic_priority_save (plic_priority,PLIC_SOURCES );platform/generic/allwinner/sun20i-d1.c static u32 plic_threshold; · line 91
Quoted 10 lines
platform/generic/allwinner/sun20i-d1.c +1
-1}static void sun20i_d1_plic_restore (void ) {thead_plic_restore (); -fdt_plic_priority_restore (plic_priority); +fdt_plic_priority_restore (plic_priority,PLIC_SOURCES );fdt_plic_context_restore (true, plic_sie, plic_threshold); }--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
Anup Patel
AppliedPATCH v3 2/5 lib: utils/irqchip: plic: Ensure no out-of-bound access in priority save/restore helpers
In reply to Bin MengQuoted 9 lines · Bin Meng
Currently the priority save/restore helpers writes/reads the provided array using an index whose maximum value is determined by PLIC, which potentially may disagree with the caller to these helpers.
Add a parameter to ask the caller to provide the size limit of the array to ensure no out-of-bound access happens.
Signed-off-by Bin Meng <[email protected]>
Applied this patch to the riscv/opensbi repo.
Thanks,
Anup
Quoted 147 lines
---
Changes in v3:
- fix the size limit check
- move the size limit check to plic_priority_save/restore
- add parameter description to fdt_plic_priority_save/restoreChanges in v2:
- new patch: libs: utils/irqchip: plic: Ensure no out-of-bound access in priority save/restore helpersinclude/sbi_utils/irqchip/fdt_irqchip_plic.h | 14 ++++++++++++-- include/sbi_utils/irqchip/plic.h | 5 +++-- lib/utils/irqchip/fdt_irqchip_plic.c | 8 ++++---- lib/utils/irqchip/plic.c | 15 +++++++++++---- platform/generic/allwinner/sun20i-d1.c | 4 ++-- 5 files changed, 32 insertions(+), 14 deletions(-)include/sbi_utils/irqchip/fdt_irqchip_plic.h +12
-2@@ -9,9 +9,19 @@#include <sbi/sbi_types.h> -void fdt_plic_priority_save (u8 *priority); +/** +* Save the PLIC priority state +* @param priority pointer to the memory region for the saved priority +* @param num size of the memory region including interrupt source 0 +*/ +void fdt_plic_priority_save (u8 *priority,u32 num); -void fdt_plic_priority_restore (const u8 *priority); +/** +* Restore the PLIC priority state +* @param priority pointer to the memory region for the saved priority +* @param num size of the memory region including interrupt source 0 +*/ +void fdt_plic_priority_restore (const u8 *priority,u32 num);void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold);include/sbi_utils/irqchip/plic.h +3
-2@@ -18,9 +18,10 @@ struct plic_data { };/* So far, priorities on all consumers of these functions fit in 8 bits. */ -void plic_priority_save (const struct plic_data *plic,u8 *priority); +void plic_priority_save (const struct plic_data *plic,u8 *priority,u32 num); -void plic_priority_restore (const struct plic_data *plic,const u8 *priority); +void plic_priority_restore (const struct plic_data *plic,const u8 *priority, +u32 num);void plic_context_save (const struct plic_data *plic,int context_id,u32 *enable,u32 *threshold);lib/utils/irqchip/fdt_irqchip_plic.c +4
-4@@ -24,18 +24,18 @@ static struct plic_data plic[PLIC_MAX_NR];static struct plic_data *plic_hartid2data[SBI_HARTMASK_MAX_BITS ];static int plic_hartid2context[SBI_HARTMASK_MAX_BITS ][2 ]; -void fdt_plic_priority_save (u8 *priority) +void fdt_plic_priority_save (u8 *priority,u32 num) {struct plic_data *plic = plic_hartid2data[current_hartid ()]; -plic_priority_save (plic, priority); +plic_priority_save (plic, priority, num); } -void fdt_plic_priority_restore (const u8 *priority) +void fdt_plic_priority_restore (const u8 *priority,u32 num) {struct plic_data *plic = plic_hartid2data[current_hartid ()]; -plic_priority_restore (plic, priority); +plic_priority_restore (plic, priority, num); }void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold)lib/utils/irqchip/plic.c +11
-4@@ -36,15 +36,22 @@ static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val)writel (val, plic_priority); } -void plic_priority_save (const struct plic_data *plic,u8 *priority) +void plic_priority_save (const struct plic_data *plic,u8 *priority,u32 num) { -for (u32 i =1 ; i <= plic->num_src; i++) +if (num > plic->num_src) + num = plic->num_src; + +for (u32 i =1 ; i <= num; i++) priority[i] =plic_get_priority (plic, i); } -void plic_priority_restore (const struct plic_data *plic,const u8 *priority) +void plic_priority_restore (const struct plic_data *plic,const u8 *priority, +u32 num) { -for (u32 i =1 ; i <= plic->num_src; i++) +if (num > plic->num_src) + num = plic->num_src; + +for (u32 i =1 ; i <= num; i++)plic_set_priority (plic, i, priority[i]); }platform/generic/allwinner/sun20i-d1.c +2
-3@@ -79,13 +79,13 @@ static u32 plic_threshold;static void sun20i_d1_plic_save (void ) {fdt_plic_context_save (true, plic_sie, &plic_threshold); -fdt_plic_priority_save (plic_priority); +fdt_plic_priority_save (plic_priority,PLIC_SOURCES ); }static void sun20i_d1_plic_restore (void ) {thead_plic_restore (); -fdt_plic_priority_restore (plic_priority); +fdt_plic_priority_restore (plic_priority,PLIC_SOURCES );fdt_plic_context_restore (true, plic_sie, plic_threshold); } --2.34.1
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v3 3/5 lib: utils/irqchip: plic: Fix the off-by-one error in plic_context_init()
In reply to Bin MengThe number of interrupt enable register in words was once correct, but was wrongly changed to have an off-by-one error since
commit 8c362e7d065e ("lib: irqchip/plic: Factor out a context init function").
---
(no changes since v2)
Changes in v2:
- new patch: lib: utils/irqchip: plic: Fix the off-by-one error in plic_context_init()
lib/utils/irqchip/plic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/utils/irqchip/plic.c +1 -1
@@ -127,7 +127,7 @@ int plic_context_init(const struct plic_data *plic, int context_id,if (!plic || context_id <0 )return SBI_EINVAL ; - ie_words = (plic->num_src +31 ) /32 ; + ie_words = plic->num_src /32 +1 ; ie_value = enable ?0xffffffffU :0U ;for (u32 i =0 ; i < ie_words; i++)
Signature · 1 line
2.34.1
Samuel Holland
maintainerPATCH v3 3/5 lib: utils/irqchip: plic: Fix the off-by-one error in plic_context_init()
Quoted 3 lines · Bin Meng
The number of interrupt enable register in words was once correct, but was wrongly changed to have an off-by-one error since
commit8c362e7d065e("lib: irqchip/plic: Factor out a context init function").
Indeed, as far as I can tell from my recent reading, I misinterpreted the binding. I don't like that it excludes the reserved source 0, but I don't get to make that decision.
Quoted 3 lines
Fixes 8c362e7d065e ("lib: irqchip/plic: Factor out a context init function")Signed-off-by Bin Meng <[email protected]>Reviewed-by Anup Patel <[email protected]>
Quoted 23 lines
---
(no changes since v2)
Changes in v2:
- new patch: lib: utils/irqchip: plic: Fix the off-by-one error inplic_context_init()lib/utils/irqchip/plic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/utils/irqchip/plic.c +1
-1@@ -127,7 +127,7 @@ int plic_context_init(const struct plic_data *plic, int context_id,if (!plic || context_id <0 )return SBI_EINVAL ; - ie_words = (plic->num_src +31 ) /32 ; + ie_words = plic->num_src /32 +1 ; ie_value = enable ?0xffffffffU :0U ;for (u32 i =0 ; i < ie_words; i++)
Anup Patel
AppliedPATCH v3 3/5 lib: utils/irqchip: plic: Fix the off-by-one error in plic_context_init()
In reply to Bin MengQuoted 8 lines · Bin Meng
The number of interrupt enable register in words was once correct, but was wrongly changed to have an off-by-one error since
commit8c362e7d065e("lib: irqchip/plic: Factor out a context init function").Fixes 8c362e7d065e ("lib: irqchip/plic: Factor out a context init function")Signed-off-by Bin Meng <[email protected]>Reviewed-by Anup Patel <[email protected]>
Applied this patch to the riscv/opensbi repo.
Thanks,
Anup
Quoted 27 lines
---
(no changes since v2)
Changes in v2:
- new patch: lib: utils/irqchip: plic: Fix the off-by-one error inplic_context_init()lib/utils/irqchip/plic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/utils/irqchip/plic.c +1
-2@@ -127,7 +127,7 @@ int plic_context_init(const struct plic_data *plic, int context_id,if (!plic || context_id <0 )return SBI_EINVAL ; - ie_words = (plic->num_src +31 ) /32 ; + ie_words = plic->num_src /32 +1 ; ie_value = enable ?0xffffffffU :0U ;for (u32 i =0 ; i < ie_words; i++) --2.34.1
PATCH v3 4/5 lib: utils/irqchip: plic: Fix the off-by-one error in context save/restore helpers
In reply to Bin Mengplic->num_src holds the number of interrupt sources without interrupt source 0 but the interrupt enable register includes a bit for the interrupt source 0 in the first word.
---
(no changes since v2)
Changes in v2:
- new patch: lib: utils/irqchip: plic: Fix the off-by-one error in context save/restore helpers
lib/utils/irqchip/plic.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/utils/irqchip/plic.c +2 -2
@@ -100,7 +100,7 @@ static void plic_set_ie(const struct plic_data *plic, u32 cntxid,void plic_context_save (const struct plic_data *plic,int context_id,u32 *enable,u32 *threshold) { -u32 ie_words = (plic->num_src +31 ) /32 ; +u32 ie_words = plic->num_src /32 +1 ;for (u32 i =0 ; i < ie_words; i++) enable[i] =plic_get_ie (plic, context_id, i);
@@ -111,7 +111,7 @@ void plic_context_save(const struct plic_data *plic, int context_id,void plic_context_restore (const struct plic_data *plic,int context_id,const u32 *enable,u32 threshold) { -u32 ie_words = (plic->num_src +31 ) /32 ; +u32 ie_words = plic->num_src /32 +1 ;for (u32 i =0 ; i < ie_words; i++)plic_set_ie (plic, context_id, i, enable[i]);
Signature · 1 line
2.34.1
Samuel Holland
maintainerPATCH v3 4/5 lib: utils/irqchip: plic: Fix the off-by-one error in context save/restore helpers
Quoted 3 lines · Bin Meng
plic->num_srcholds the number of interrupt sources without interrupt source 0 but the interrupt enable register includes a bit for the interrupt source 0 in the first word.
On the other hand, it is not really feasible to exclude the first bit here, so maybe it makes sense to leave patch 1 as it is for consistency.
Quoted 6 lines
Fixes 415ecf28f7ad ("lib: irqchip/plic: Add context save/restore helpers")Signed-off-by Bin Meng <[email protected]>Reviewed-by Anup Patel <[email protected]>---
Quoted 7 lines
(no changes since v2)
Changes in v2:
- new patch: lib: utils/irqchip: plic: Fix the off-by-one error in context save/restore helperslib/utils/irqchip/plic.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Anup Patel
AppliedPATCH v3 4/5 lib: utils/irqchip: plic: Fix the off-by-one error in context save/restore helpers
In reply to Bin MengQuoted 8 lines · Bin Meng
plic->num_srcholds the number of interrupt sources without interrupt source 0 but the interrupt enable register includes a bit for the interrupt source 0 in the first word.Fixes 415ecf28f7ad ("lib: irqchip/plic: Add context save/restore helpers")Signed-off-by Bin Meng <[email protected]>Reviewed-by Anup Patel <[email protected]>
Applied this patch to the riscv/opensbi repo.
Thanks,
Anup
Quoted 36 lines
---
(no changes since v2)
Changes in v2:
- new patch: lib: utils/irqchip: plic: Fix the off-by-one error in context save/restore helperslib/utils/irqchip/plic.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)lib/utils/irqchip/plic.c +2
-3@@ -100,7 +100,7 @@ static void plic_set_ie(const struct plic_data *plic, u32 cntxid,void plic_context_save (const struct plic_data *plic,int context_id,u32 *enable,u32 *threshold) { -u32 ie_words = (plic->num_src +31 ) /32 ; +u32 ie_words = plic->num_src /32 +1 ;for (u32 i =0 ; i < ie_words; i++) enable[i] =plic_get_ie (plic, context_id, i);@@ -111,7 +111,7 @@ void plic_context_save(const struct plic_data *plic, int context_id,void plic_context_restore (const struct plic_data *plic,int context_id,const u32 *enable,u32 threshold) { -u32 ie_words = (plic->num_src +31 ) /32 ; +u32 ie_words = plic->num_src /32 +1 ;for (u32 i =0 ; i < ie_words; i++)plic_set_ie (plic, context_id, i, enable[i]); --2.34.1
PATCH v3 5/5 lib: utils/irqchip: plic: Ensure no out-of-bound access in context save/restore helpers
In reply to Bin MengCurrently the context save/restore helpers writes/reads the provided array using an index whose maximum value is determined by PLIC, which potentially may disagree with the caller to these helpers.
Add a parameter to ask the caller to provide the size limit of the array to ensure no out-of-bound access happens.
---
Changes in v3:
- move the size limit check to plic_context_save/restore
Changes in v2:
- new patch: lib: utils/irqchip: plic: Ensure no out-of-bound access in context save/restore helpers
include/sbi_utils/irqchip/fdt_irqchip_plic.h | 5 +++-- include/sbi_utils/irqchip/plic.h | 4 ++-- lib/utils/irqchip/fdt_irqchip_plic.c | 9 +++++---- lib/utils/irqchip/plic.c | 14 ++++++++++---- platform/generic/allwinner/sun20i-d1.c | 5 +++-- 5 files changed, 23 insertions(+), 14 deletions(-)
include/sbi_utils/irqchip/fdt_irqchip_plic.h +3 -2
@@ -23,9 +23,10 @@ void fdt_plic_priority_save(u8 *priority, u32 num);*/ void fdt_plic_priority_restore (const u8 *priority,u32 num); -void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold); +void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold,u32 num); -void fdt_plic_context_restore (bool smode,const u32 *enable,u32 threshold); +void fdt_plic_context_restore (bool smode,const u32 *enable,u32 threshold, +u32 num);void thead_plic_restore (void );
include/sbi_utils/irqchip/plic.h +2 -2
@@ -24,10 +24,10 @@ void plic_priority_restore(const struct plic_data *plic, const u8 *priority, u32 num);void plic_context_save (const struct plic_data *plic,int context_id, -u32 *enable,u32 *threshold); +u32 *enable,u32 *threshold,u32 num);void plic_context_restore (const struct plic_data *plic,int context_id, -const u32 *enable,u32 threshold); +const u32 *enable,u32 threshold,u32 num);int plic_context_init (const struct plic_data *plic,int context_id,bool enable,u32 threshold);
lib/utils/irqchip/fdt_irqchip_plic.c +5 -4
@@ -38,22 +38,23 @@ void fdt_plic_priority_restore(const u8 *priority, u32 num)plic_priority_restore (plic, priority, num); } -void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold) +void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold,u32 num) {u32 hartid =current_hartid ();plic_context_save (plic_hartid2data[hartid], plic_hartid2context[hartid][smode], - enable, threshold); + enable, threshold, num); } -void fdt_plic_context_restore (bool smode,const u32 *enable,u32 threshold) +void fdt_plic_context_restore (bool smode,const u32 *enable,u32 threshold, +u32 num) {u32 hartid =current_hartid ();plic_context_restore (plic_hartid2data[hartid], plic_hartid2context[hartid][smode], - enable, threshold); + enable, threshold, num); }static int irqchip_plic_warm_init (void )
lib/utils/irqchip/plic.c +10 -4
@@ -98,22 +98,28 @@ static void plic_set_ie(const struct plic_data *plic, u32 cntxid, }void plic_context_save (const struct plic_data *plic,int context_id, -u32 *enable,u32 *threshold) +u32 *enable,u32 *threshold,u32 num) {u32 ie_words = plic->num_src /32 +1 ; -for (u32 i =0 ; i < ie_words; i++) +if (num > ie_words) + num = ie_words; + +for (u32 i =0 ; i < num; i++) enable[i] =plic_get_ie (plic, context_id, i); *threshold =plic_get_thresh (plic, context_id); }void plic_context_restore (const struct plic_data *plic,int context_id, -const u32 *enable,u32 threshold) +const u32 *enable,u32 threshold,u32 num) {u32 ie_words = plic->num_src /32 +1 ; -for (u32 i =0 ; i < ie_words; i++) +if (num > ie_words) + num = ie_words; + +for (u32 i =0 ; i < num; i++)plic_set_ie (plic, context_id, i, enable[i]);plic_set_thresh (plic, context_id, threshold);
platform/generic/allwinner/sun20i-d1.c +3 -2
@@ -78,7 +78,7 @@ static u32 plic_threshold;static void sun20i_d1_plic_save (void ) { -fdt_plic_context_save (true, plic_sie, &plic_threshold); +fdt_plic_context_save (true, plic_sie, &plic_threshold,PLIC_IE_WORDS );fdt_plic_priority_save (plic_priority,PLIC_SOURCES ); }
@@ -86,7 +86,8 @@ static void sun20i_d1_plic_restore(void) {thead_plic_restore ();fdt_plic_priority_restore (plic_priority,PLIC_SOURCES ); -fdt_plic_context_restore (true, plic_sie, plic_threshold); +fdt_plic_context_restore (true, plic_sie, plic_threshold, +PLIC_IE_WORDS ); }/*
Signature · 1 line
2.34.1
PATCH v3 5/5 lib: utils/irqchip: plic: Ensure no out-of-bound access in context save/restore helpers
Quoted 9 lines · Bin Meng
Currently the context save/restore helpers writes/reads the provided array using an index whose maximum value is determined by PLIC, which potentially may disagree with the caller to these helpers.
Add a parameter to ask the caller to provide the size limit of the array to ensure no out-of-bound access happens.
Signed-off-by Bin Meng <[email protected]>
Looks good to me.
Regards,
Anup
Quoted 150 lines
---
Changes in v3:
- move the size limit check to plic_context_save/restoreChanges in v2:
- new patch: lib: utils/irqchip: plic: Ensure no out-of-bound access in context save/restore helpersinclude/sbi_utils/irqchip/fdt_irqchip_plic.h | 5 +++-- include/sbi_utils/irqchip/plic.h | 4 ++-- lib/utils/irqchip/fdt_irqchip_plic.c | 9 +++++---- lib/utils/irqchip/plic.c | 14 ++++++++++---- platform/generic/allwinner/sun20i-d1.c | 5 +++-- 5 files changed, 23 insertions(+), 14 deletions(-)include/sbi_utils/irqchip/fdt_irqchip_plic.h +3
-2@@ -23,9 +23,10 @@ void fdt_plic_priority_save(u8 *priority, u32 num);*/ void fdt_plic_priority_restore (const u8 *priority,u32 num); -void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold); +void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold,u32 num); -void fdt_plic_context_restore (bool smode,const u32 *enable,u32 threshold); +void fdt_plic_context_restore (bool smode,const u32 *enable,u32 threshold, +u32 num);void thead_plic_restore (void );include/sbi_utils/irqchip/plic.h +2
-2@@ -24,10 +24,10 @@ void plic_priority_restore(const struct plic_data *plic, const u8 *priority, u32 num);void plic_context_save (const struct plic_data *plic,int context_id, -u32 *enable,u32 *threshold); +u32 *enable,u32 *threshold,u32 num);void plic_context_restore (const struct plic_data *plic,int context_id, -const u32 *enable,u32 threshold); +const u32 *enable,u32 threshold,u32 num);int plic_context_init (const struct plic_data *plic,int context_id,bool enable,u32 threshold);lib/utils/irqchip/fdt_irqchip_plic.c +5
-4@@ -38,22 +38,23 @@ void fdt_plic_priority_restore(const u8 *priority, u32 num)plic_priority_restore (plic, priority, num); } -void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold) +void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold,u32 num) {u32 hartid =current_hartid ();plic_context_save (plic_hartid2data[hartid], plic_hartid2context[hartid][smode], - enable, threshold); + enable, threshold, num); } -void fdt_plic_context_restore (bool smode,const u32 *enable,u32 threshold) +void fdt_plic_context_restore (bool smode,const u32 *enable,u32 threshold, +u32 num) {u32 hartid =current_hartid ();plic_context_restore (plic_hartid2data[hartid], plic_hartid2context[hartid][smode], - enable, threshold); + enable, threshold, num); }static int irqchip_plic_warm_init (void )lib/utils/irqchip/plic.c +10
-4@@ -98,22 +98,28 @@ static void plic_set_ie(const struct plic_data *plic, u32 cntxid, }void plic_context_save (const struct plic_data *plic,int context_id, -u32 *enable,u32 *threshold) +u32 *enable,u32 *threshold,u32 num) {u32 ie_words = plic->num_src /32 +1 ; -for (u32 i =0 ; i < ie_words; i++) +if (num > ie_words) + num = ie_words; + +for (u32 i =0 ; i < num; i++) enable[i] =plic_get_ie (plic, context_id, i); *threshold =plic_get_thresh (plic, context_id); }void plic_context_restore (const struct plic_data *plic,int context_id, -const u32 *enable,u32 threshold) +const u32 *enable,u32 threshold,u32 num) {u32 ie_words = plic->num_src /32 +1 ; -for (u32 i =0 ; i < ie_words; i++) +if (num > ie_words) + num = ie_words; + +for (u32 i =0 ; i < num; i++)plic_set_ie (plic, context_id, i, enable[i]);plic_set_thresh (plic, context_id, threshold);platform/generic/allwinner/sun20i-d1.c +3
-3@@ -78,7 +78,7 @@ static u32 plic_threshold;static void sun20i_d1_plic_save (void ) { -fdt_plic_context_save (true, plic_sie, &plic_threshold); +fdt_plic_context_save (true, plic_sie, &plic_threshold,PLIC_IE_WORDS );fdt_plic_priority_save (plic_priority,PLIC_SOURCES ); }@@ -86,7 +86,8 @@ static void sun20i_d1_plic_restore(void) {thead_plic_restore ();fdt_plic_priority_restore (plic_priority,PLIC_SOURCES ); -fdt_plic_context_restore (true, plic_sie, plic_threshold); +fdt_plic_context_restore (true, plic_sie, plic_threshold, +PLIC_IE_WORDS ); }/* -- 2.34.1
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
Samuel Holland
maintainerPATCH v3 5/5 lib: utils/irqchip: plic: Ensure no out-of-bound access in context save/restore helpers
In reply to Bin MengQuoted 24 lines · Bin Meng
Currently the context save/restore helpers writes/reads the provided array using an index whose maximum value is determined by PLIC, which potentially may disagree with the caller to these helpers.
Add a parameter to ask the caller to provide the size limit of the array to ensure no out-of-bound access happens.
Signed-off-by Bin Meng <[email protected]>---
Changes in v3:
- move the size limit check to plic_context_save/restoreChanges in v2:
- new patch: lib: utils/irqchip: plic: Ensure no out-of-bound access in context save/restore helpersinclude/sbi_utils/irqchip/fdt_irqchip_plic.h | 5 +++-- include/sbi_utils/irqchip/plic.h | 4 ++-- lib/utils/irqchip/fdt_irqchip_plic.c | 9 +++++---- lib/utils/irqchip/plic.c | 14 ++++++++++---- platform/generic/allwinner/sun20i-d1.c | 5 +++-- 5 files changed, 23 insertions(+), 14 deletions(-)
[...]
platform/generic/allwinner/sun20i-d1.c static u32 plic_threshold; · line 81
Quoted 10 lines
platform/generic/allwinner/sun20i-d1.c +1
-1@@ -78,7 +78,7 @@ static u32 plic_threshold;static void sun20i_d1_plic_save (void ) { -fdt_plic_context_save (true, plic_sie, &plic_threshold); +fdt_plic_context_save (true, plic_sie, &plic_threshold,PLIC_IE_WORDS );
While the actual value ends up being the same, you should update the definition of PLIC_IE_WORDS to use the same formula used elsewhere.
Regards,
Samuel
Quoted 13 lines
platform/generic/allwinner/sun20i-d1.c +2
-1fdt_plic_priority_save (plic_priority,PLIC_SOURCES ); }@@ -86,7 +86,8 @@ static void sun20i_d1_plic_restore(void) {thead_plic_restore ();fdt_plic_priority_restore (plic_priority,PLIC_SOURCES ); -fdt_plic_context_restore (true, plic_sie, plic_threshold); +fdt_plic_context_restore (true, plic_sie, plic_threshold, +PLIC_IE_WORDS ); }/*
PATCH v3 5/5 lib: utils/irqchip: plic: Ensure no out-of-bound access in context save/restore helpers
Quoted 33 lines · Samuel Holland
Quoted 24 lines · Bin Meng
Currently the context save/restore helpers writes/reads the provided array using an index whose maximum value is determined by PLIC, which potentially may disagree with the caller to these helpers.
Add a parameter to ask the caller to provide the size limit of the array to ensure no out-of-bound access happens.
Signed-off-by Bin Meng <[email protected]>---
Changes in v3:
- move the size limit check to plic_context_save/restoreChanges in v2:
- new patch: lib: utils/irqchip: plic: Ensure no out-of-bound access in context save/restore helpersinclude/sbi_utils/irqchip/fdt_irqchip_plic.h | 5 +++-- include/sbi_utils/irqchip/plic.h | 4 ++-- lib/utils/irqchip/fdt_irqchip_plic.c | 9 +++++---- lib/utils/irqchip/plic.c | 14 ++++++++++---- platform/generic/allwinner/sun20i-d1.c | 5 +++-- 5 files changed, 23 insertions(+), 14 deletions(-)[...]
Quoted 6 lines
platform/generic/allwinner/sun20i-d1.c +0
-0@@ -78,7 +78,7 @@ static u32 plic_threshold;
Quoted 7 lines
platform/generic/allwinner/sun20i-d1.c static u32 plic_threshold; · line 81
Quoted 4 lines
platform/generic/allwinner/sun20i-d1.c +1
-1static void sun20i_d1_plic_save (void ) { -fdt_plic_context_save (true, plic_sie, &plic_threshold); +fdt_plic_context_save (true, plic_sie, &plic_threshold,PLIC_IE_WORDS );While the actual value ends up being the same, you should update the definition of
PLIC_IE_WORDSto use the same formula used elsewhere.
Let's have a separate patch for this.
Regards,
Anup
Quoted 23 lines
Regards,
Samuelplatform/generic/allwinner/sun20i-d1.c static void sun20i_d1_plic_restore(void) · line 93
Quoted 13 lines
platform/generic/allwinner/sun20i-d1.c +2
-1fdt_plic_priority_save (plic_priority,PLIC_SOURCES ); }@@ -86,7 +86,8 @@ static void sun20i_d1_plic_restore(void) {thead_plic_restore ();fdt_plic_priority_restore (plic_priority,PLIC_SOURCES ); -fdt_plic_context_restore (true, plic_sie, plic_threshold); +fdt_plic_context_restore (true, plic_sie, plic_threshold, +PLIC_IE_WORDS ); }/* --
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
Anup Patel
AppliedPATCH v3 5/5 lib: utils/irqchip: plic: Ensure no out-of-bound access in context save/restore helpers
In reply to Bin MengQuoted 9 lines · Bin Meng
Currently the context save/restore helpers writes/reads the provided array using an index whose maximum value is determined by PLIC, which potentially may disagree with the caller to these helpers.
Add a parameter to ask the caller to provide the size limit of the array to ensure no out-of-bound access happens.
Signed-off-by Bin Meng <[email protected]>
Applied this patch to the riscv/opensbi repo.
Thanks,
Anup
Quoted 150 lines
---
Changes in v3:
- move the size limit check to plic_context_save/restoreChanges in v2:
- new patch: lib: utils/irqchip: plic: Ensure no out-of-bound access in context save/restore helpersinclude/sbi_utils/irqchip/fdt_irqchip_plic.h | 5 +++-- include/sbi_utils/irqchip/plic.h | 4 ++-- lib/utils/irqchip/fdt_irqchip_plic.c | 9 +++++---- lib/utils/irqchip/plic.c | 14 ++++++++++---- platform/generic/allwinner/sun20i-d1.c | 5 +++-- 5 files changed, 23 insertions(+), 14 deletions(-)include/sbi_utils/irqchip/fdt_irqchip_plic.h +3
-2@@ -23,9 +23,10 @@ void fdt_plic_priority_save(u8 *priority, u32 num);*/ void fdt_plic_priority_restore (const u8 *priority,u32 num); -void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold); +void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold,u32 num); -void fdt_plic_context_restore (bool smode,const u32 *enable,u32 threshold); +void fdt_plic_context_restore (bool smode,const u32 *enable,u32 threshold, +u32 num);void thead_plic_restore (void );include/sbi_utils/irqchip/plic.h +2
-2@@ -24,10 +24,10 @@ void plic_priority_restore(const struct plic_data *plic, const u8 *priority, u32 num);void plic_context_save (const struct plic_data *plic,int context_id, -u32 *enable,u32 *threshold); +u32 *enable,u32 *threshold,u32 num);void plic_context_restore (const struct plic_data *plic,int context_id, -const u32 *enable,u32 threshold); +const u32 *enable,u32 threshold,u32 num);int plic_context_init (const struct plic_data *plic,int context_id,bool enable,u32 threshold);lib/utils/irqchip/fdt_irqchip_plic.c +5
-4@@ -38,22 +38,23 @@ void fdt_plic_priority_restore(const u8 *priority, u32 num)plic_priority_restore (plic, priority, num); } -void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold) +void fdt_plic_context_save (bool smode,u32 *enable,u32 *threshold,u32 num) {u32 hartid =current_hartid ();plic_context_save (plic_hartid2data[hartid], plic_hartid2context[hartid][smode], - enable, threshold); + enable, threshold, num); } -void fdt_plic_context_restore (bool smode,const u32 *enable,u32 threshold) +void fdt_plic_context_restore (bool smode,const u32 *enable,u32 threshold, +u32 num) {u32 hartid =current_hartid ();plic_context_restore (plic_hartid2data[hartid], plic_hartid2context[hartid][smode], - enable, threshold); + enable, threshold, num); }static int irqchip_plic_warm_init (void )lib/utils/irqchip/plic.c +10
-4@@ -98,22 +98,28 @@ static void plic_set_ie(const struct plic_data *plic, u32 cntxid, }void plic_context_save (const struct plic_data *plic,int context_id, -u32 *enable,u32 *threshold) +u32 *enable,u32 *threshold,u32 num) {u32 ie_words = plic->num_src /32 +1 ; -for (u32 i =0 ; i < ie_words; i++) +if (num > ie_words) + num = ie_words; + +for (u32 i =0 ; i < num; i++) enable[i] =plic_get_ie (plic, context_id, i); *threshold =plic_get_thresh (plic, context_id); }void plic_context_restore (const struct plic_data *plic,int context_id, -const u32 *enable,u32 threshold) +const u32 *enable,u32 threshold,u32 num) {u32 ie_words = plic->num_src /32 +1 ; -for (u32 i =0 ; i < ie_words; i++) +if (num > ie_words) + num = ie_words; + +for (u32 i =0 ; i < num; i++)plic_set_ie (plic, context_id, i, enable[i]);plic_set_thresh (plic, context_id, threshold);platform/generic/allwinner/sun20i-d1.c +3
-3@@ -78,7 +78,7 @@ static u32 plic_threshold;static void sun20i_d1_plic_save (void ) { -fdt_plic_context_save (true, plic_sie, &plic_threshold); +fdt_plic_context_save (true, plic_sie, &plic_threshold,PLIC_IE_WORDS );fdt_plic_priority_save (plic_priority,PLIC_SOURCES ); }@@ -86,7 +86,8 @@ static void sun20i_d1_plic_restore(void) {thead_plic_restore ();fdt_plic_priority_restore (plic_priority,PLIC_SOURCES ); -fdt_plic_context_restore (true, plic_sie, plic_threshold); +fdt_plic_context_restore (true, plic_sie, plic_threshold, +PLIC_IE_WORDS ); }/* -- 2.34.1
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v3 1/5 lib: utils/irqchip: plic: Fix the off-by-one error in priority save/restore helpers
In reply to Bin Menglib/utils/irqchip/plic.c static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val) · line 42
Quoted 11 lines · Bin Meng
lib/utils/irqchip/plic.c +1
-1@@ -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 needs to adjust the index into the priority array.
lib/utils/irqchip/plic.c static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val) · line 48
Quoted 5 lines
lib/utils/irqchip/plic.c +1
-1void 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]);
Likewise.
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."
PATCH v3 1/5 lib: utils/irqchip: plic: Fix the off-by-one error in priority save/restore helpers
Quoted 9 lines · Andreas Schwab
Quoted 6 lines · Bin Meng
lib/utils/irqchip/plic.c +0
-0@@ -38,13 +38,13 @@ static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val)
Quoted 7 lines
lib/utils/irqchip/plic.c static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val) · line 42
Quoted 5 lines
lib/utils/irqchip/plic.c +1
-1void 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 needs to adjust the index into the priority array.
Does that help anything? It just confuses people more.
I added function parameter descriptions in patch 2 to make it crystal clear, that the priority array needs to include interrupt source 0.
Quoted 9 lines
lib/utils/irqchip/plic.c static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val) · line 48
Quoted 6 lines
lib/utils/irqchip/plic.c +1
-1void 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]);Likewise.
Regards,
Bin
PATCH v3 1/5 lib: utils/irqchip: plic: Fix the off-by-one error in priority save/restore helpers
Quoted 18 lines · Bin Meng
Quoted 9 lines · Andreas Schwab
Quoted 6 lines · Bin Meng
lib/utils/irqchip/plic.c +0
-0@@ -38,13 +38,13 @@ static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val)Quoted 7 lines
lib/utils/irqchip/plic.c static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val) · line 42
Quoted 5 lines
lib/utils/irqchip/plic.c +1
-1void 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 needs to adjust the index into the priority array.
Quoted 1 line
Does that help anything?
Yes, it fixes an off-by-one error.
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."
PATCH v3 1/5 lib: utils/irqchip: plic: Fix the off-by-one error in priority save/restore helpers
In reply to Bin MengQuoted 2 lines · Bin Meng
I added function parameter descriptions in patch 2 to make it crystal clear, that the priority array needs to include interrupt source 0.
It should say that the array size must be one more than the maximum number of sources.
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."
Samuel Holland
maintainerPATCH v3 1/5 lib: utils/irqchip: plic: Fix the off-by-one error in priority save/restore helpers
In reply to Bin MengQuoted 18 lines · Bin Meng
Quoted 9 lines · Andreas Schwab
Quoted 6 lines · Bin Meng
lib/utils/irqchip/plic.c +0
-0@@ -38,13 +38,13 @@ static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val)Quoted 7 lines
lib/utils/irqchip/plic.c static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val) · line 42
Quoted 5 lines
lib/utils/irqchip/plic.c +1
-1void 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 needs to adjust the index into the priority array.
Quoted 4 lines
Does that help anything? It just confuses people more.
I added function parameter descriptions in patch 2 to make it crystal clear, that the priority array needs to include interrupt source 0.
To me, it is more confusing that when I ask to save the priority for N sources, I need to allocate an array with >N elements. And leaving array element 0 unused wastes memory.
Regards,
Samuel
PATCH v3 1/5 lib: utils/irqchip: plic: Fix the off-by-one error in priority save/restore helpers
Quoted 20 lines · Samuel Holland
Quoted 18 lines · Bin Meng
Quoted 16 lines · Andreas Schwab
lib/utils/irqchip/plic.c static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val) · line 42
Quoted 11 lines · Bin Meng
lib/utils/irqchip/plic.c +1
-1@@ -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 needs to adjust the index into the priority array.
Quoted 8 lines
Quoted 4 lines
Does that help anything? It just confuses people more.
I added function parameter descriptions in patch 2 to make it crystal clear, that the priority array needs to include interrupt source 0.
To me, it is more confusing that when I ask to save the priority for N sources, I need to allocate an array with >N elements. And leaving array element 0 unused wastes memory.
This can be a separate improvement patch.
Regards,
Anup
Quoted 9 lines
Regards,
Samuel--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v3 1/5 lib: utils/irqchip: plic: Fix the off-by-one error in priority save/restore helpers
Quoted 23 lines · Anup Patel
Quoted 22 lines · Samuel Holland
Quoted 20 lines · Bin Meng
Quoted 9 lines · Andreas Schwab
Quoted 6 lines · Bin Meng
lib/utils/irqchip/plic.c +0
-0@@ -38,13 +38,13 @@ static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val)Quoted 7 lines
lib/utils/irqchip/plic.c static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val) · line 42
Quoted 5 lines
lib/utils/irqchip/plic.c +1
-1void 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 needs to adjust the index into the priority array.
Does that help anything? It just confuses people more.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
I added function parameter descriptions in patch 2 to make it crystal clear, that the priority array needs to include interrupt source 0.
To me, it is more confusing that when I ask to save the priority for N sources, I need to allocate an array with >N elements. And leaving array element 0 unused wastes memory.
This can be a separate improvement patch.
It is a bug fix, not just an improvement.
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."
Anup Patel
AppliedPATCH v3 1/5 lib: utils/irqchip: plic: Fix the off-by-one error in priority save/restore helpers
In reply to Bin MengQuoted 6 lines · Bin Meng
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 <[email protected]>Reviewed-by Anup Patel <[email protected]>
Applied this patch to the riscv/opensbi repo.
Thanks,
Anup
Quoted 30 lines
---
(no changes since v1)
lib/utils/irqchip/plic.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)lib/utils/irqchip/plic.c +2
-3@@ -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]); } --2.34.1