[PATCH] Loongarch: env: fix missing NULL checks for kstrdup

Li Jun posted 1 patch 2 weeks, 5 days ago
There is a newer version of this series
arch/loongarch/kernel/env.c | 2 ++
1 file changed, 2 insertions(+)
[PATCH] Loongarch: env: fix missing NULL checks for kstrdup
Posted by Li Jun 2 weeks, 5 days ago
Fixes potential kernel oops during early boot when memory
allocation fails while parsing CPU model from device tree.

Signed-off-by: Li Jun <lijun01@kylinos.cn>
---
 arch/loongarch/kernel/env.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/arch/loongarch/kernel/env.c b/arch/loongarch/kernel/env.c
index 841206fde3ab..8d331e4f1c9a 100644
--- a/arch/loongarch/kernel/env.c
+++ b/arch/loongarch/kernel/env.c
@@ -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.25.1
Re: [PATCH] Loongarch: env: fix missing NULL checks for kstrdup
Posted by Huacai Chen 2 weeks, 5 days ago
Hi, Jun,

On Wed, Mar 18, 2026 at 7:09 PM Li Jun <lijun01@kylinos.cn> wrote:
>
> Fixes potential kernel oops during early boot when memory
> allocation fails while parsing CPU model from device tree.
>
> Signed-off-by: Li Jun <lijun01@kylinos.cn>
> ---
>  arch/loongarch/kernel/env.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/arch/loongarch/kernel/env.c b/arch/loongarch/kernel/env.c
> index 841206fde3ab..8d331e4f1c9a 100644
> --- a/arch/loongarch/kernel/env.c
> +++ b/arch/loongarch/kernel/env.c
> @@ -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;
Missing a "of_node_put(root)" in the error path.

Huacai

>                 loongson_sysconf.cpuname = strsep(&cpuname, " ");
>         }
>         of_node_put(root);
> --
> 2.25.1
>
>