initramfs_test: add NULL check for kmalloc in initramfs_test_fname_overrun
PATCH 1/1 initramfs_test: add NULL check for kmalloc in initramfs_test_fname_overrun
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.
--- init/initramfs_test.c | 3 +++ 1 file changed, 3 insertions(+)
init/initramfs_test.c +3 -0
@@ -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' ;
Signature · 1 line
2.43.0
Hi,
Quoted 16 lines · longlong yan
Add missing NULL check for kmalloc return value in
initramfs_test_fname_overrun(). Without this check, if kmalloc fails, the subsequentmemset()will dereference a NULL pointer, causing a kernel crash.Fixes 83c0b27266ec ("initramfs_test: kunit tests for initramfs unpacking")Signed-off-by longlong yan <[email protected]>--- init/initramfs_test.c | 3 +++ 1 file changed, 3 insertions(+)init/initramfs_test.c +0
-0
init/initramfs_test.c static void __init initramfs_test_fname_overrun(struct kunit *test) · line 195
Quoted 7 lines
init/initramfs_test.c +3
-0@@ -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.