All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] loongarch/vdso: add missing NULL check after kcalloc()
@ 2025-09-18  6:44 Jackie Liu
  2025-09-18  6:44 ` [PATCH 2/2] loongarch/env: fix missing NULL check after kstrdup() Jackie Liu
  2025-09-18  7:17 ` [PATCH 1/2] loongarch/vdso: add missing NULL check after kcalloc() Huacai Chen
  0 siblings, 2 replies; 6+ messages in thread
From: Jackie Liu @ 2025-09-18  6:44 UTC (permalink / raw)
  To: chenhuacai; +Cc: kernel, loongarch, liu.yun

From: Jackie Liu <liuyun01@kylinos.cn>

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.

Signed-off-by: Jackie Liu <liuyun01@kylinos.cn>
---
 arch/loongarch/kernel/vdso.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/arch/loongarch/kernel/vdso.c b/arch/loongarch/kernel/vdso.c
index 7b888d9085a0..088f8095210b 100644
--- a/arch/loongarch/kernel/vdso.c
+++ b/arch/loongarch/kernel/vdso.c
@@ -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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/2] loongarch/env: fix missing NULL check after kstrdup()
  2025-09-18  6:44 [PATCH 1/2] loongarch/vdso: add missing NULL check after kcalloc() Jackie Liu
@ 2025-09-18  6:44 ` Jackie Liu
  2025-09-18  7:15   ` Huacai Chen
  2025-09-18  7:17 ` [PATCH 1/2] loongarch/vdso: add missing NULL check after kcalloc() Huacai Chen
  1 sibling, 1 reply; 6+ messages in thread
From: Jackie Liu @ 2025-09-18  6:44 UTC (permalink / raw)
  To: chenhuacai; +Cc: kernel, loongarch, liu.yun

From: Jackie Liu <liuyun01@kylinos.cn>

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.

Signed-off-by: Jackie Liu <liuyun01@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 c0a5dc9aeae2..b41a65ec45c3 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.51.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] loongarch/env: fix missing NULL check after kstrdup()
  2025-09-18  6:44 ` [PATCH 2/2] loongarch/env: fix missing NULL check after kstrdup() Jackie Liu
@ 2025-09-18  7:15   ` Huacai Chen
  2025-09-18  7:27     ` Jackie Liu
  0 siblings, 1 reply; 6+ messages in thread
From: Huacai Chen @ 2025-09-18  7:15 UTC (permalink / raw)
  To: Jackie Liu; +Cc: kernel, loongarch

Hi, Jackie,

Your partner has already sent a patch and be NAKed, don't you know?

https://lore.kernel.org/loongarch/5169d057-28c9-439c-a6b7-1138b4f79e65@xen0n.name/T/#t

Huacai

On Thu, Sep 18, 2025 at 2:45 PM Jackie Liu <liu.yun@linux.dev> wrote:
>
> From: Jackie Liu <liuyun01@kylinos.cn>
>
> 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.
>
> Signed-off-by: Jackie Liu <liuyun01@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 c0a5dc9aeae2..b41a65ec45c3 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.51.0
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] loongarch/vdso: add missing NULL check after kcalloc()
  2025-09-18  6:44 [PATCH 1/2] loongarch/vdso: add missing NULL check after kcalloc() Jackie Liu
  2025-09-18  6:44 ` [PATCH 2/2] loongarch/env: fix missing NULL check after kstrdup() Jackie Liu
@ 2025-09-18  7:17 ` Huacai Chen
  2025-09-18  7:24   ` Jackie Liu
  1 sibling, 1 reply; 6+ messages in thread
From: Huacai Chen @ 2025-09-18  7:17 UTC (permalink / raw)
  To: Jackie Liu; +Cc: kernel, loongarch

Hi, Jackie,

Someone has already sent a patch and be applied, don't you know?

https://lore.kernel.org/loongarch/CAAhV-H6hTFrUO5gTx6FYo89TAouEykTvKpLNkqFfhtsg0PVLiw@mail.gmail.com/T/#t

Huacai

On Thu, Sep 18, 2025 at 2:45 PM Jackie Liu <liu.yun@linux.dev> wrote:
>
> From: Jackie Liu <liuyun01@kylinos.cn>
>
> 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.
>
> Signed-off-by: Jackie Liu <liuyun01@kylinos.cn>
> ---
>  arch/loongarch/kernel/vdso.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/arch/loongarch/kernel/vdso.c b/arch/loongarch/kernel/vdso.c
> index 7b888d9085a0..088f8095210b 100644
> --- a/arch/loongarch/kernel/vdso.c
> +++ b/arch/loongarch/kernel/vdso.c
> @@ -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
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] loongarch/vdso: add missing NULL check after kcalloc()
  2025-09-18  7:17 ` [PATCH 1/2] loongarch/vdso: add missing NULL check after kcalloc() Huacai Chen
@ 2025-09-18  7:24   ` Jackie Liu
  0 siblings, 0 replies; 6+ messages in thread
From: Jackie Liu @ 2025-09-18  7:24 UTC (permalink / raw)
  To: Huacai Chen; +Cc: kernel, loongarch

2025年9月18日 15:17, "Huacai Chen" <chenhuacai@kernel.org mailto:chenhuacai@kernel.org?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:

Hi Huacai

> 
> Hi, Jackie,
> 
> Someone has already sent a patch and be applied, don't you know?
> 
> https://lore.kernel.org/loongarch/CAAhV-H6hTFrUO5gTx6FYo89TAouEykTvKpLNkqFfhtsg0PVLiw@mail.gmail.com/T/#t
> 
> 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

> 
> On Thu, Sep 18, 2025 at 2:45 PM Jackie Liu <liu.yun@linux.dev> wrote:
> 
> > 
> > From: Jackie Liu <liuyun01@kylinos.cn>
> > 
> >  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.
> > 
> >  Signed-off-by: Jackie Liu <liuyun01@kylinos.cn>
> >  ---
> >  arch/loongarch/kernel/vdso.c | 2 ++
> >  1 file changed, 2 insertions(+)
> > 
> >  diff --git a/arch/loongarch/kernel/vdso.c b/arch/loongarch/kernel/vdso.c
> >  index 7b888d9085a0..088f8095210b 100644
> >  --- a/arch/loongarch/kernel/vdso.c
> >  +++ b/arch/loongarch/kernel/vdso.c
> >  @@ -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
> >
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] loongarch/env: fix missing NULL check after kstrdup()
  2025-09-18  7:15   ` Huacai Chen
@ 2025-09-18  7:27     ` Jackie Liu
  0 siblings, 0 replies; 6+ messages in thread
From: Jackie Liu @ 2025-09-18  7:27 UTC (permalink / raw)
  To: Huacai Chen; +Cc: kernel, loongarch

Hi Huacai.


2025年9月18日 15:15, "Huacai Chen" <chenhuacai@kernel.org mailto:chenhuacai@kernel.org?to=%22Huacai%20Chen%22%20%3Cchenhuacai%40kernel.org%3E > 写到:


> 
> Hi, Jackie,
> 
> Your partner has already sent a patch and be NAKed, don't you know?
> 
> https://lore.kernel.org/loongarch/5169d057-28c9-439c-a6b7-1138b4f79e65@xen0n.name/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

> 
> On Thu, Sep 18, 2025 at 2:45 PM Jackie Liu <liu.yun@linux.dev> wrote:
> 
> > 
> > From: Jackie Liu <liuyun01@kylinos.cn>
> > 
> >  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.
> > 
> >  Signed-off-by: Jackie Liu <liuyun01@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 c0a5dc9aeae2..b41a65ec45c3 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.51.0
> >
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-09-18  7:28 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-18  6:44 [PATCH 1/2] loongarch/vdso: add missing NULL check after kcalloc() Jackie Liu
2025-09-18  6:44 ` [PATCH 2/2] loongarch/env: fix missing NULL check after kstrdup() Jackie Liu
2025-09-18  7:15   ` Huacai Chen
2025-09-18  7:27     ` Jackie Liu
2025-09-18  7:17 ` [PATCH 1/2] loongarch/vdso: add missing NULL check after kcalloc() Huacai Chen
2025-09-18  7:24   ` Jackie Liu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.