loongarch/vdso: add missing NULL check after kcalloc()
PATCH 1/2 loongarch/vdso: add missing NULL check after kcalloc()
From: Jackie Liu <[email protected]>
The kcalloc() call in init_vdso() allocates memory for code_mapping.pages, but its return value was not validated before use. This could cause NULL pointer dereference if allocation fails.
Add a check for the kcalloc() result and return -ENOMEM on failure.
--- arch/loongarch/kernel/vdso.c | 2 ++ 1 file changed, 2 insertions(+)
arch/loongarch/kernel/vdso.c +2 -0
@@ -53,6 +53,8 @@ static int __init init_vdso(void) vdso_info.size =PAGE_ALIGN (vdso_end - vdso_start); vdso_info.code_mapping.pages =kcalloc (vdso_info.size /PAGE_SIZE ,sizeof (struct page *),GFP_KERNEL ); +if (!vdso_info.code_mapping.pages) +return -ENOMEM ; pfn =__phys_to_pfn (__pa_symbol (vdso_info.vdso));for (i =0 ; i < vdso_info.size /PAGE_SIZE ; i++)
Signature · 1 line
2.51.0
PATCH 2/2 loongarch/env: fix missing NULL check after kstrdup()
From: Jackie Liu <[email protected]>
The result of kstrdup() may be NULL when memory allocation fails. In init_cpu_fullname(), the return value was used directly by strsep() without validation, which could lead to NULL pointer dereference.
Add a proper check for the kstrdup() result and return -ENOMEM when allocation fails.
--- arch/loongarch/kernel/env.c | 2 ++ 1 file changed, 2 insertions(+)
arch/loongarch/kernel/env.c +2 -0
@@ -49,6 +49,8 @@ static int __init init_cpu_fullname(void) ret =of_property_read_string (root,"model" , &model);if (ret ==0 ) { cpuname =kstrdup (model,GFP_KERNEL ); +if (!cpuname) +return -ENOMEM ; loongson_sysconf.cpuname =strsep (&cpuname," " ); }of_node_put (root);
Signature · 1 line
2.51.0
Huacai Chen
maintainerRe: [PATCH 2/2] loongarch/env: fix missing NULL check after kstrdup()
Hi, Jackie,
Your partner has already sent a patch and be NAKed, don't you know?
https://lore.kernel.org/loongarch/[email protected]/T/#t
Huacai
Quoted 31 lines · Jackie Liu
From: Jackie Liu <[email protected]>
The result of
kstrdup()may be NULL when memory allocation fails. Ininit_cpu_fullname(), the return value was used directly bystrsep()without validation, which could lead to NULL pointer dereference.Add a proper check for the
kstrdup()result and return-ENOMEMwhen allocation fails.Signed-off-by Jackie Liu <[email protected]>--- arch/loongarch/kernel/env.c | 2 ++ 1 file changed, 2 insertions(+)arch/loongarch/kernel/env.c +2
-1@@ -49,6 +49,8 @@ static int __init init_cpu_fullname(void) ret =of_property_read_string (root,"model" , &model);if (ret ==0 ) { cpuname =kstrdup (model,GFP_KERNEL ); +if (!cpuname) +return -ENOMEM ; loongson_sysconf.cpuname =strsep (&cpuname," " ); }of_node_put (root); --2.51.0
Huacai Chen
maintainerHi, Jackie,
Someone has already sent a patch and be applied, don't you know?
Huacai
Quoted 30 lines · Jackie Liu
From: Jackie Liu <[email protected]>
The
kcalloc()call ininit_vdso()allocates memory for code_mapping.pages, but its return value was not validated before use. This could cause NULL pointer dereference if allocation fails.Add a check for the
kcalloc()result and return-ENOMEMon failure.Signed-off-by Jackie Liu <[email protected]>--- arch/loongarch/kernel/vdso.c | 2 ++ 1 file changed, 2 insertions(+)arch/loongarch/kernel/vdso.c +2
-1@@ -53,6 +53,8 @@ static int __init init_vdso(void) vdso_info.size =PAGE_ALIGN (vdso_end - vdso_start); vdso_info.code_mapping.pages =kcalloc (vdso_info.size /PAGE_SIZE ,sizeof (struct page *),GFP_KERNEL ); +if (!vdso_info.code_mapping.pages) +return -ENOMEM ; pfn =__phys_to_pfn (__pa_symbol (vdso_info.vdso));for (i =0 ; i < vdso_info.size /PAGE_SIZE ; i++) --2.51.0
2025年9月18日 15:17, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Hi Huacai
Quoted 8 lines
Hi, Jackie,
Someone has already sent a patch and be applied, don't you know?
Huacai
Ah, I just checked Linus's tree, While looking into other
memory allocation failures, I took the chance to try fixing this as well. Thanks for pointing it out.
---
Jackie Liu
Quoted 34 lines · Jackie Liu
Quoted 31 lines
From: Jackie Liu <[email protected]>
The
kcalloc()call ininit_vdso()allocates memory for code_mapping.pages, but its return value was not validated before use. This could cause NULL pointer dereference if allocation fails.Add a check for the
kcalloc()result and return-ENOMEMon failure.Signed-off-by Jackie Liu <[email protected]>---
arch/loongarch/kernel/vdso.c | 2 ++ 1 file changed, 2 insertions(+)diff --git
a/arch/loongarch/kernel/vdso.cb/arch/loongarch/kernel/vdso.cindex7b888d9085a0..088f8095210b100644
---a/arch/loongarch/kernel/vdso.c
+++b/arch/loongarch/kernel/vdso.c
@@ -53,6 +53,8 @@ static int__initinit_vdso(void)vdso_info.size =PAGE_ALIGN(vdso_end - vdso_start); vdso_info.code_mapping.pages =
kcalloc(vdso_info.size / PAGE_SIZE, sizeof(struct page *), GFP_KERNEL);
+ if (!vdso_info.code_mapping.pages)
+ return-ENOMEM;pfn = __phys_to_pfn(__pa_symbol(vdso_info.vdso)); for (i = 0; i < vdso_info.size /
PAGE_SIZE; i++) --
2.51.0
Re: [PATCH 2/2] loongarch/env: fix missing NULL check after kstrdup()
In reply to Huacai ChenHi Huacai.
2025年9月18日 15:15, "Huacai Chen" <[email protected] mailto:[email protected]?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:
Quoted 8 lines
Hi, Jackie,
Your partner has already sent a patch and be NAKed, don't you know?
https://lore.kernel.org/loongarch/[email protected]/T/#t
Huacai
Yes, I was just reviewing the code and happened to check this while investigating other crash issues. It looks like passing NULL to strsep won’t cause a crash, so this can safely be ignored. Thanks for pointing it out.
---
Jackie Liu
Quoted 35 lines · Jackie Liu
Quoted 32 lines
From: Jackie Liu <[email protected]>
The result of
kstrdup()may be NULL when memory allocation fails. Ininit_cpu_fullname(), the return value was used directly bystrsep()without validation, which could lead to NULL pointer dereference.Add a proper check for the
kstrdup()result and return-ENOMEMwhen allocation fails.Signed-off-by Jackie Liu <[email protected]>---
arch/loongarch/kernel/env.c | 2 ++ 1 file changed, 2 insertions(+)diff --git
a/arch/loongarch/kernel/env.cb/arch/loongarch/kernel/env.cindexc0a5dc9aeae2..b41a65ec45c3100644
---a/arch/loongarch/kernel/env.c
+++b/arch/loongarch/kernel/env.c
@@ -49,6 +49,8 @@ static int__initinit_cpu_fullname(void)ret =of_property_read_string(root, "model", &model);
if (ret == 0) {
cpuname =kstrdup(model, GFP_KERNEL);
+ if (!cpuname)
+ return-ENOMEM;
loongson_sysconf.cpuname =strsep(&cpuname, " ");
}
of_node_put(root);
--
2.51.0