Skip to contentSkip to conversation

arm64/smp: check !ipi_desc[i] in arch_show_interrupts

PATCH In review Chen Jun 2 participants 3 messages May 27, 2022 – Jun 15, 2022 arm64

lore
Share & tools
lore

Chen Jun

author
#0 lore raw

PATCH 1/1 arm64/smp: check !ipi_desc[i] in arch_show_interrupts

There is a potential dereferencing null pointer issue in arch_show_interrupts.

Problem 1:
int arch_show_interrupts(struct seq_file *p, int prec)

        for (i = 0; i < NR_IPI; i++) {
                seq_printf(p, "%10u ", irq_desc_kstat_cpu(ipi_desc[i],
			cpu));

Only ipi_desc[0..nr_ipi - 1] are initialized in set_smp_ipi_range. and ipi_desc[nr_ipi..NR_IPI] are NULL. irq_desc_kstat_cpu will dereference NULL pointer.
For now, the problem can not be triggered, because NR_IPI is always equal to nr_ipi.

Problem 2:
If request_percpu_irq failed in set_smp_ipi_range, ipi_desc[i] would be NULL.
irq_desc_kstat_cpu will dereference NULL pointer.

check !ipi_desc[i] (as arm does) to avoid the problem.

---
 arch/arm64/kernel/smp.c | 3 +++
 1 file changed, 3 insertions(+)
arch/arm64/kernel/smp.c +3 -0
index 62ed361a4376..3d54f464428b 100644
@@ -781,6 +781,9 @@ int arch_show_interrupts(struct seq_file *p, int prec)
 	unsigned int cpu, i;
 
 	for (i = 0; i < NR_IPI; i++) {
+		if (!ipi_desc[i])
+			continue;
+
 		seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i,
 			   prec >= 4 ? " " : "");
 		for_each_online_cpu(cpu)
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 32 lines · Chen Jun

There is a potential dereferencing null pointer issue in arch_show_interrupts.

Problem 1:
int arch_show_interrupts(struct seq_file *p, int prec)

        for (i = 0; i < NR_IPI; i++) {
                seq_printf(p, "%10u ", irq_desc_kstat_cpu(ipi_desc[i],
			cpu));

Only ipi_desc[0..nr_ipi - 1] are initialized in set_smp_ipi_range. and ipi_desc[nr_ipi..NR_IPI] are NULL. irq_desc_kstat_cpu will dereference NULL pointer.
For now, the problem can not be triggered, because NR_IPI is always equal to nr_ipi.

Problem 2:
If request_percpu_irq failed in set_smp_ipi_range, ipi_desc[i] would be NULL.
irq_desc_kstat_cpu will dereference NULL pointer.

check !ipi_desc[i] (as arm does) to avoid the problem.

---
 arch/arm64/kernel/smp.c | 3 +++
 1 file changed, 3 insertions(+)
arch/arm64/kernel/smp.c +0 -0
index 62ed361a4376..3d54f464428b 100644

arch/arm64/kernel/smp.c int arch_show_interrupts(struct seq_file *p, int prec) · line 785

Quoted 5 lines
arch/arm64/kernel/smp.c +2 -0
@@ -781,6 +781,9 @@ int arch_show_interrupts(struct seq_file *p, int prec)
 	unsigned int cpu, i;
 
 	for (i = 0; i < NR_IPI; i++) {
+		if (!ipi_desc[i])
+			continue;

Why not just use nr_ipi instead of NR_IPI?

Will

#2 lore raw
Quoted 35 lines · Will Deacon
Quoted 34 lines · Chen Jun

There is a potential dereferencing null pointer issue in arch_show_interrupts.

Problem 1:
int arch_show_interrupts(struct seq_file *p, int prec)

         for (i = 0; i < NR_IPI; i++) {
                 seq_printf(p, "%10u ", irq_desc_kstat_cpu(ipi_desc[i],
			cpu));

Only ipi_desc[0..nr_ipi - 1] are initialized in set_smp_ipi_range. and ipi_desc[nr_ipi..NR_IPI] are NULL. irq_desc_kstat_cpu will dereference NULL pointer.
For now, the problem can not be triggered, because NR_IPI is always equal to nr_ipi.

Problem 2:
If request_percpu_irq failed in set_smp_ipi_range, ipi_desc[i] would be NULL.
irq_desc_kstat_cpu will dereference NULL pointer.

check !ipi_desc[i] (as arm does) to avoid the problem.

---
  arch/arm64/kernel/smp.c | 3 +++
  1 file changed, 3 insertions(+)
arch/arm64/kernel/smp.c +0 -0
index 62ed361a4376..3d54f464428b 100644
@@ -781,6 +781,9 @@ int arch_show_interrupts(struct seq_file *p, int prec)
  	unsigned int cpu, i;
 
Quoted 5 lines

arch/arm64/kernel/smp.c int arch_show_interrupts(struct seq_file *p, int prec) · line 785

Quoted 3 lines
arch/arm64/kernel/smp.c +2 -0
  	for (i = 0; i < NR_IPI; i++) {
+		if (!ipi_desc[i])
+			continue;

Why not just use nr_ipi instead of NR_IPI?

Yee, that is what I do at first. But I noticed that:

void __init set_smp_ipi_range(int ipi_base, int n)

     for (i = 0; i < nr_ipi; i++) {
         err = request_percpu_irq(ipi_base + i, ipi_handler,
                                  "IPI", &cpu_number);
         WARN_ON(err);
         ipi_desc[i] = irq_to_desc(ipi_base + i);

If request_percpu_irq return a error, I not sure if ipi_desc[i] makes sense.

Quoted 3 lines

Will

Signature · 2 lines
Regards
Chen Jun