* [PATCH] initramfs_test: add NULL check for kmalloc in initramfs_test_fname_overrun
@ 2026-07-15 2:08 longlong yan
2026-07-16 13:47 ` David Disseldorp
0 siblings, 1 reply; 2+ messages in thread
From: longlong yan @ 2026-07-15 2:08 UTC (permalink / raw)
To: viro, brauner, jack, linux-fsdevel, linux-kernel; +Cc: longlong yan
Add missing NULL check for kmalloc return value in
initramfs_test_fname_overrun(). Without this check, if kmalloc fails,
the subsequent memset() will dereference a NULL pointer, causing a
kernel crash.
Fixes: 83c0b27266ec ("initramfs_test: kunit tests for initramfs unpacking")
Signed-off-by: longlong yan <yanlonglong@kylinos.cn>
---
init/initramfs_test.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/init/initramfs_test.c b/init/initramfs_test.c
index 1fc990a66563..c7aab7e5383e 100644
--- a/init/initramfs_test.c
+++ b/init/initramfs_test.c
@@ -189,6 +189,9 @@ static void __init initramfs_test_fname_overrun(struct kunit *test)
* are already available (e.g. no compression).
*/
cpio_srcbuf = kmalloc(CPIO_HDRLEN + PATH_MAX + 3, GFP_KERNEL);
+ if (!cpio_srcbuf)
+ return;
+
memset(cpio_srcbuf, 'B', CPIO_HDRLEN + PATH_MAX + 3);
/* limit overrun to avoid crashes / filp_open() ENAMETOOLONG */
cpio_srcbuf[CPIO_HDRLEN + strlen(c[0].fname) + 20] = '\0';
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] initramfs_test: add NULL check for kmalloc in initramfs_test_fname_overrun
2026-07-15 2:08 [PATCH] initramfs_test: add NULL check for kmalloc in initramfs_test_fname_overrun longlong yan
@ 2026-07-16 13:47 ` David Disseldorp
0 siblings, 0 replies; 2+ messages in thread
From: David Disseldorp @ 2026-07-16 13:47 UTC (permalink / raw)
To: longlong yan; +Cc: viro, brauner, jack, linux-fsdevel, linux-kernel
Hi,
On Wed, 15 Jul 2026 10:08:19 +0800, longlong yan wrote:
> Add missing NULL check for kmalloc return value in
> initramfs_test_fname_overrun(). Without this check, if kmalloc fails,
> the subsequent memset() will dereference a NULL pointer, causing a
> kernel crash.
>
> Fixes: 83c0b27266ec ("initramfs_test: kunit tests for initramfs unpacking")
> Signed-off-by: longlong yan <yanlonglong@kylinos.cn>
> ---
> init/initramfs_test.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/init/initramfs_test.c b/init/initramfs_test.c
> index 1fc990a66563..c7aab7e5383e 100644
> --- a/init/initramfs_test.c
> +++ b/init/initramfs_test.c
> @@ -189,6 +189,9 @@ static void __init initramfs_test_fname_overrun(struct kunit *test)
> * are already available (e.g. no compression).
> */
> cpio_srcbuf = kmalloc(CPIO_HDRLEN + PATH_MAX + 3, GFP_KERNEL);
> + if (!cpio_srcbuf)
> + return;
> +
> memset(cpio_srcbuf, 'B', CPIO_HDRLEN + PATH_MAX + 3);
Panicking on kunit test alloc failure is reasonable behaviour IMO;
the Fixes tag isn't appropriate here. Also, your patch is only
handling one of the many allocations. KUNIT_ASSERT_NOT_NULL() would be
a better option if we wanted to explictly catch these failures.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-07-16 13:47 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-15 2:08 [PATCH] initramfs_test: add NULL check for kmalloc in initramfs_test_fname_overrun longlong yan
2026-07-16 13:47 ` David Disseldorp
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox