Skip to contentSkip to conversation

x86/acpi: fix potential NULL deref in acpi_wakeup_cpu()

PATCH In review Chenyuan Yang 4 participants 5 messages Apr 11, 2025 – May 16, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/acpi: fix potential NULL deref in acpi_wakeup_cpu()

The result of memremap() may be NULL on failure, leading to a NULL dereference. Add explicit checks after memremap() call: if the MADT mailbox fails to map, return immediately.

This is similar to the commit 966d47e1f27c
("efi: fix potential NULL deref in efi_mem_reserve_persistent").

This is found by our static analysis tool KNighter.

Fixes 2b5e22afae07 ("x86/acpi: Extract ACPI MADT wakeup code into a separate file")
---
 arch/x86/kernel/acpi/madt_wakeup.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/x86/kernel/acpi/madt_wakeup.c +4 -0
index f36f28405dcc..b386ec4b87c2 100644
@@ -143,6 +143,10 @@ static int acpi_wakeup_cpu(u32 apicid, unsigned long start_ip)
 		acpi_mp_wake_mailbox = memremap(acpi_mp_wake_mailbox_paddr,
 						sizeof(*acpi_mp_wake_mailbox),
 						MEMREMAP_WB);
+		if (!acpi_mp_wake_mailbox) {
+			pr_err("Failed to remap MADT mailbox\n");
+			return -ENOMEM;
+		}
 	}
 
 	/*
Signature · 1 line
2.34.1
#1 lore raw
Quoted 5 lines · Chenyuan Yang

The result of memremap() may be NULL on failure, leading to a NULL dereference. Add explicit checks after memremap() call: if the MADT mailbox fails to map, return immediately.

Quoted 7 lines

This is similar to the commit 966d47e1f27c
("efi: fix potential NULL deref in efi_mem_reserve_persistent").

This is found by our static analysis tool KNighter.

Fixes 2b5e22afae07 ("x86/acpi: Extract ACPI MADT wakeup code into a separate file")

Well, it's good to add the author of the commit you're trying to fix to the CC list.

Kirill, what do you think about this?

Quoted 22 lines
---
 arch/x86/kernel/acpi/madt_wakeup.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/x86/kernel/acpi/madt_wakeup.c +4 -1
index f36f28405dcc..b386ec4b87c2 100644
@@ -143,6 +143,10 @@ static int acpi_wakeup_cpu(u32 apicid, unsigned long start_ip)
                acpi_mp_wake_mailbox = memremap(acpi_mp_wake_mailbox_paddr,
                                                sizeof(*acpi_mp_wake_mailbox),
                                                MEMREMAP_WB);
+               if (!acpi_mp_wake_mailbox) {
+                       pr_err("Failed to remap MADT mailbox\n");
+                       return -ENOMEM;
+               }
        }
 
        /*
--

2.34.1

#2 lore raw
Quoted 11 lines · Rafael J. Wysocki
Quoted 10 lines · Chenyuan Yang

The result of memremap() may be NULL on failure, leading to a NULL dereference. Add explicit checks after memremap() call: if the MADT mailbox fails to map, return immediately.

This is similar to the commit 966d47e1f27c
("efi: fix potential NULL deref in efi_mem_reserve_persistent").

This is found by our static analysis tool KNighter.

Quoted 7 lines
Quoted 2 lines
Fixes 2b5e22afae07 ("x86/acpi: Extract ACPI MADT wakeup code into a separate file")

Well, it's good to add the author of the commit you're trying to fix to the CC list.

Kirill, what do you think about this?

Looks reasonable to me.

We fail to remap a single page. It is likely to be fatally broken system if we get there. But okay, let's handle it.

Maybe use pr_err_once(). No need to spam an error for every CPU.

Quoted 22 lines
Quoted 22 lines
---
 arch/x86/kernel/acpi/madt_wakeup.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/x86/kernel/acpi/madt_wakeup.c +4 -1
index f36f28405dcc..b386ec4b87c2 100644
@@ -143,6 +143,10 @@ static int acpi_wakeup_cpu(u32 apicid, unsigned long start_ip)
                acpi_mp_wake_mailbox = memremap(acpi_mp_wake_mailbox_paddr,
                                                sizeof(*acpi_mp_wake_mailbox),
                                                MEMREMAP_WB);
+               if (!acpi_mp_wake_mailbox) {
+                       pr_err("Failed to remap MADT mailbox\n");
+                       return -ENOMEM;
+               }
        }
 
        /*
--

2.34.1

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
Reviewed-by on v1 1/1
#3 lore raw
In reply to Chenyuan Yang
Quoted 4 lines · Chenyuan Yang

The result of memremap() may be NULL on failure, leading to a NULL dereference. Add explicit checks after memremap() call: if the MADT mailbox fails to map, return immediately.

Quoted 8 lines

This is similar to the commit 966d47e1f27c
("efi: fix potential NULL deref in efi_mem_reserve_persistent").

This is found by our static analysis tool KNighter.

Fixes 2b5e22afae07 ("x86/acpi: Extract ACPI MADT wakeup code into a separate file")

---

Looks fine to me

Quoted 18 lines
  arch/x86/kernel/acpi/madt_wakeup.c | 4 ++++
  1 file changed, 4 insertions(+)
arch/x86/kernel/acpi/madt_wakeup.c +4 -0
index f36f28405dcc..b386ec4b87c2 100644
@@ -143,6 +143,10 @@ static int acpi_wakeup_cpu(u32 apicid, unsigned long start_ip)
  		acpi_mp_wake_mailbox = memremap(acpi_mp_wake_mailbox_paddr,
  						sizeof(*acpi_mp_wake_mailbox),
  						MEMREMAP_WB);
+		if (!acpi_mp_wake_mailbox) {
+			pr_err("Failed to remap MADT mailbox\n");
+			return -ENOMEM;
+		}
  	}
 
  	/*
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
#4 lore raw
In reply to Chenyuan Yang
Quoted 4 lines · Chenyuan Yang

The result of memremap() may be NULL on failure, leading to a NULL dereference. Add explicit checks after memremap() call: if the MADT mailbox fails to map, return immediately.

This mailbox is called Multiprocessor Wakeup Mailbox in the specification, so please follow the established terminology.

Quoted 16 lines

This is similar to the commit 966d47e1f27c
("efi: fix potential NULL deref in efi_mem_reserve_persistent").

This is found by our static analysis tool KNighter.

Fixes 2b5e22afae07 ("x86/acpi: Extract ACPI MADT wakeup code into a separate file")
---
 arch/x86/kernel/acpi/madt_wakeup.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/x86/kernel/acpi/madt_wakeup.c +0 -0
index f36f28405dcc..b386ec4b87c2 100644

arch/x86/kernel/acpi/madt_wakeup.c static int acpi_wakeup_cpu(u32 apicid, unsigned long start_ip) · line 147

Quoted 5 lines
arch/x86/kernel/acpi/madt_wakeup.c +2 -0
@@ -143,6 +143,10 @@ static int acpi_wakeup_cpu(u32 apicid, unsigned long start_ip)
                acpi_mp_wake_mailbox = memremap(acpi_mp_wake_mailbox_paddr,
                                                sizeof(*acpi_mp_wake_mailbox),
                                                MEMREMAP_WB);
+               if (!acpi_mp_wake_mailbox) {
+                       pr_err("Failed to remap MADT mailbox\n");

Please follow Kirill's advice on using pr_err_once() here and use the proper terminology in the message (also this is mapping, not remapping, even though memremap() is used).

arch/x86/kernel/acpi/madt_wakeup.c static int acpi_wakeup_cpu(u32 apicid, unsigned long start_ip)

Quoted 6 lines
arch/x86/kernel/acpi/madt_wakeup.c +2 -1
+                       return -ENOMEM;
+               }
        }
 
        /*
--

Thanks!