initramfs_test: kunit test for cpio.filesize > PATH_MAX
PATCH 1/1 initramfs_test: kunit test for cpio.filesize > PATH_MAX
initramfs unpack skips over cpio entries where namesize > PATH_MAX, instead of returning an error. Add coverage for this behaviour.
--- init/initramfs_test.c | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)
init/initramfs_test.c +47 -0
@@ -447,6 +447,52 @@ static void __init initramfs_test_fname_pad(struct kunit *test)kfree (tbufs); } +static void __init initramfs_test_fname_path_max (struct kunit *test) +{ +char *err; +size_t len; +struct kstat st0, st1; +char fdata[] ="this file data will not be unpacked" ; +struct test_fname_path_max { +char fname_oversize[PATH_MAX +1 ]; +char fname_ok[PATH_MAX ]; +char cpio_src[(CPIO_HDRLEN +PATH_MAX +3 +sizeof (fdata)) *2 ]; + } *tbufs =kzalloc (sizeof (struct test_fname_path_max ),GFP_KERNEL ); +struct initramfs_test_cpio c[] = { { + .magic ="070701" , + .ino =1 , + .mode =S_IFDIR |0777 , + .nlink =1 , + .namesize =sizeof (tbufs->fname_oversize), + .fname = tbufs->fname_oversize, + .filesize =sizeof (fdata), + .data = fdata, + }, { + .magic ="070701" , + .ino =2 , + .mode =S_IFDIR |0777 , + .nlink =1 , + .namesize =sizeof (tbufs->fname_ok), + .fname = tbufs->fname_ok, + } }; + +memset (tbufs->fname_oversize,'/' ,sizeof (tbufs->fname_oversize) -1 ); +memset (tbufs->fname_ok,'/' ,sizeof (tbufs->fname_ok) -1 ); +memcpy (tbufs->fname_oversize,"fname_oversize" , +sizeof ("fname_oversize" ) -1 ); +memcpy (tbufs->fname_ok,"fname_ok" ,sizeof ("fname_ok" ) -1 ); + len =fill_cpio (c,ARRAY_SIZE (c), tbufs->cpio_src); + +/* unpack skips over fname_oversize instead of returning an error */ + err =unpack_to_rootfs (tbufs->cpio_src, len); +KUNIT_EXPECT_NULL (test, err); + +KUNIT_EXPECT_EQ (test,init_stat ("fname_oversize" , &st0,0 ), -ENOENT ); +KUNIT_EXPECT_EQ (test,init_stat ("fname_ok" , &st1,0 ),0 ); + +kfree (tbufs); +} +/* * The kunit_case/_suite struct cannot be marked as __initdata as this will be * used in debugfs to retrieve results after test has run.
@@ -459,6 +505,7 @@ static struct kunit_case __refdata initramfs_test_cases[] = {KUNIT_CASE (initramfs_test_hardlink),KUNIT_CASE (initramfs_test_many),KUNIT_CASE (initramfs_test_fname_pad), + KUNIT_CASE(initramfs_test_fname_path_max), {}, };
Signature · 1 line
2.51.0
PATCH v2 1/1 initramfs_test: kunit test for cpio.filesize > PATH_MAX
initramfs unpack skips over cpio entries where namesize > PATH_MAX, instead of returning an error. Add coverage for this behaviour.
---
v2:
- clean up created directory
init/initramfs_test.c | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)
init/initramfs_test.c +48 -0
@@ -447,6 +447,53 @@ static void __init initramfs_test_fname_pad(struct kunit *test)kfree (tbufs); } +static void __init initramfs_test_fname_path_max (struct kunit *test) +{ +char *err; +size_t len; +struct kstat st0, st1; +char fdata[] ="this file data will not be unpacked" ; +struct test_fname_path_max { +char fname_oversize[PATH_MAX +1 ]; +char fname_ok[PATH_MAX ]; +char cpio_src[(CPIO_HDRLEN +PATH_MAX +3 +sizeof (fdata)) *2 ]; + } *tbufs =kzalloc (sizeof (struct test_fname_path_max ),GFP_KERNEL ); +struct initramfs_test_cpio c[] = { { + .magic ="070701" , + .ino =1 , + .mode =S_IFDIR |0777 , + .nlink =1 , + .namesize =sizeof (tbufs->fname_oversize), + .fname = tbufs->fname_oversize, + .filesize =sizeof (fdata), + .data = fdata, + }, { + .magic ="070701" , + .ino =2 , + .mode =S_IFDIR |0777 , + .nlink =1 , + .namesize =sizeof (tbufs->fname_ok), + .fname = tbufs->fname_ok, + } }; + +memset (tbufs->fname_oversize,'/' ,sizeof (tbufs->fname_oversize) -1 ); +memset (tbufs->fname_ok,'/' ,sizeof (tbufs->fname_ok) -1 ); +memcpy (tbufs->fname_oversize,"fname_oversize" , +sizeof ("fname_oversize" ) -1 ); +memcpy (tbufs->fname_ok,"fname_ok" ,sizeof ("fname_ok" ) -1 ); + len =fill_cpio (c,ARRAY_SIZE (c), tbufs->cpio_src); + +/* unpack skips over fname_oversize instead of returning an error */ + err =unpack_to_rootfs (tbufs->cpio_src, len); +KUNIT_EXPECT_NULL (test, err); + +KUNIT_EXPECT_EQ (test,init_stat ("fname_oversize" , &st0,0 ), -ENOENT ); +KUNIT_EXPECT_EQ (test,init_stat ("fname_ok" , &st1,0 ),0 ); +KUNIT_EXPECT_EQ (test,init_rmdir ("fname_ok" ),0 ); + +kfree (tbufs); +} +/* * The kunit_case/_suite struct cannot be marked as __initdata as this will be * used in debugfs to retrieve results after test has run.
@@ -459,6 +506,7 @@ static struct kunit_case __refdata initramfs_test_cases[] = {KUNIT_CASE (initramfs_test_hardlink),KUNIT_CASE (initramfs_test_many),KUNIT_CASE (initramfs_test_fname_pad), + KUNIT_CASE(initramfs_test_fname_path_max), {}, };
Signature · 1 line
2.51.0
Quoted 4 lines · David Disseldorp
initramfs unpack skips over cpio entries where namesize >
PATH_MAX, instead of returning an error. Add coverage for this behaviour.
Thanks, I like the test for cpio unpacking. I think that's very valuable.
---
Applied to the vfs-7.0.misc branch of the vfs/vfs.git tree. Patches in the vfs-7.0.misc branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs-7.0.misc
[1/1] initramfs_test: kunit test for cpio.filesize > PATH_MAX
https://git.kernel.org/vfs/vfs/c/5b6e22a5d937