Skip to contentSkip to conversation

xen: Fix null pointer dereference in xen_init_lock_cpu()

PATCH In review Ma Ke 3 participants 3 messages Jun 26, 2024

Subsystems (2)
lore
Share & tools
lore

Ma Ke

author
#0 lore raw

PATCH 1/1 xen: Fix null pointer dereference in xen_init_lock_cpu()

kasprintf() is used for formatting strings and dynamically allocating memory space. If memory allocation fails, kasprintf() will return NULL. We should add a check to ensure that failure does not occur.

Fixes d5de8841355a ("x86: split spinlock implementations out into their own files")

---
Found this error through static analysis.

---
 arch/x86/xen/spinlock.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/xen/spinlock.c +2 -0
index 5c6fc16e4b92..fe3cd95c1604 100644
@@ -75,6 +75,8 @@ void xen_init_lock_cpu(int cpu)
 	     cpu, per_cpu(lock_kicker_irq, cpu));
 
 	name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
+	if (!name)
+		return;
 	per_cpu(irq_name, cpu) = name;
 	irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
 				     cpu,
Signature · 1 line
2.25.1
#1 lore raw
Quoted 17 lines · Ma Ke

kasprintf() is used for formatting strings and dynamically allocating memory space. If memory allocation fails, kasprintf() will return NULL. We should add a check to ensure that failure does not occur.

Fixes d5de8841355a ("x86: split spinlock implementations out into their own files")

---
Found this error through static analysis.

---
 arch/x86/xen/spinlock.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/xen/spinlock.c +0 -0
index 5c6fc16e4b92..fe3cd95c1604 100644

arch/x86/xen/spinlock.c void xen_init_lock_cpu(int cpu) · line 82

Quoted 8 lines
arch/x86/xen/spinlock.c +2 -0
@@ -75,6 +75,8 @@ void xen_init_lock_cpu(int cpu)
 	     cpu, per_cpu(lock_kicker_irq, cpu));
 
 	name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
+	if (!name)
+		return;
 	per_cpu(irq_name, cpu) = name;
 	irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
 				     cpu,

While yes, error checking would better be added here, this isn't enough. You're treating an easy to diagnose issue (at the point where the NULL would be attempted to be de-referenced) for a possibly more difficult to diagnose issue: Any such failure will also need propagating back up the call stack.

Jan

#2 lore raw
In reply to Ma Ke
Quoted 3 lines · Ma Ke

kasprintf() is used for formatting strings and dynamically allocating memory space. If memory allocation fails, kasprintf() will return NULL. We should add a check to ensure that failure does not occur.

Did you also consider what happens to the machine if you omit the rest of this function at init?

As is, it is *extremely* unlikely the machine will fail the allocation at boot (it has all the memory unused after all) and if for some mysterious reason it does fail, we get a nice bug halting the boot and telling us where shit hit fan.

Now we silently continue with undefined state and will likely run into trouble later because we failed to setup things, like that irqhandler. At which point everybody will be needing to buy a new WTF'o'meter to figure out WTF happened to get in that insane position.

Quoted 4 lines
Fixes d5de8841355a ("x86: split spinlock implementations out into their own files")

---
Found this error through static analysis.

Just because your tool found something, doesn't mean you have to be a tool and also not think about things.

Please use brain don't be a tool.

Quoted 20 lines
---
 arch/x86/xen/spinlock.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/xen/spinlock.c +2 -0
index 5c6fc16e4b92..fe3cd95c1604 100644
@@ -75,6 +75,8 @@ void xen_init_lock_cpu(int cpu)
 	     cpu, per_cpu(lock_kicker_irq, cpu));
 
 	name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
+	if (!name)
+		return;
 	per_cpu(irq_name, cpu) = name;
 	irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
 				     cpu,
Signature · 1 line
2.25.1