[PATCH RESEND] x86/pmem: Fix a null pointer dereference in register_e820_pmem()

Haoxiang Li posted 1 patch 5 months, 2 weeks ago
arch/x86/kernel/pmem.c | 2 ++
1 file changed, 2 insertions(+)
[PATCH RESEND] x86/pmem: Fix a null pointer dereference in register_e820_pmem()
Posted by Haoxiang Li 5 months, 2 weeks ago
Add check for the return value of platform_device_alloc()
to prevent null pointer dereference.

Fixes: 7a67832c7e44 ("libnvdimm, e820: make CONFIG_X86_PMEM_LEGACY a tristate option")
Cc: stable@vger.kernel.org
Signed-off-by: Haoxiang Li <haoxiang_li2024@163.com>
---
 arch/x86/kernel/pmem.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/arch/x86/kernel/pmem.c b/arch/x86/kernel/pmem.c
index 23154d24b117..04fb221716ff 100644
--- a/arch/x86/kernel/pmem.c
+++ b/arch/x86/kernel/pmem.c
@@ -27,6 +27,8 @@ static __init int register_e820_pmem(void)
 	 * simply here to trigger the module to load on demand.
 	 */
 	pdev = platform_device_alloc("e820_pmem", -1);
+	if (!pdev)
+		return -ENOMEM;
 
 	rc = platform_device_add(pdev);
 	if (rc)
-- 
2.25.1
Re: [PATCH RESEND] x86/pmem: Fix a null pointer dereference in register_e820_pmem()
Posted by Greg KH 5 months, 2 weeks ago
On Thu, Jul 03, 2025 at 06:08:09PM +0800, Haoxiang Li wrote:
> Add check for the return value of platform_device_alloc()
> to prevent null pointer dereference.
> 
> Fixes: 7a67832c7e44 ("libnvdimm, e820: make CONFIG_X86_PMEM_LEGACY a tristate option")
> Cc: stable@vger.kernel.org
> Signed-off-by: Haoxiang Li <haoxiang_li2024@163.com>
> ---
>  arch/x86/kernel/pmem.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/arch/x86/kernel/pmem.c b/arch/x86/kernel/pmem.c
> index 23154d24b117..04fb221716ff 100644
> --- a/arch/x86/kernel/pmem.c
> +++ b/arch/x86/kernel/pmem.c
> @@ -27,6 +27,8 @@ static __init int register_e820_pmem(void)
>  	 * simply here to trigger the module to load on demand.
>  	 */
>  	pdev = platform_device_alloc("e820_pmem", -1);
> +	if (!pdev)
> +		return -ENOMEM;

As this can only happen if you are out of memory, AND that really can't
happen at early boot time, how have you tested this code to verify that
this works?

thanks,

greg k-h