diff --git a/sys/fs/tarfs/tarfs_subr.c b/sys/fs/tarfs/tarfs_subr.c --- a/sys/fs/tarfs/tarfs_subr.c +++ b/sys/fs/tarfs/tarfs_subr.c @@ -171,6 +171,8 @@ TARFS_DPF(ALLOC, "%s(%.*s)\n", __func__, (int)namelen, name); + if (parent != NULL && parent->type != VDIR) + return (ENOTDIR); tnp = malloc(sizeof(struct tarfs_node), M_TARFSNODE, M_WAITOK | M_ZERO); mtx_init(&tnp->lock, "tarfs node lock", NULL, MTX_DEF); tnp->gen = arc4random(); @@ -233,7 +235,6 @@ panic("%s: type %d not allowed", __func__, type); } if (parent != NULL) { - MPASS(parent->type == VDIR); TARFS_NODE_LOCK(parent); TAILQ_INSERT_TAIL(&parent->dir.dirhead, tnp, dirents); parent->size += sizeof(struct tarfs_node); diff --git a/sys/fs/tarfs/tarfs_vfsops.c b/sys/fs/tarfs/tarfs_vfsops.c --- a/sys/fs/tarfs/tarfs_vfsops.c +++ b/sys/fs/tarfs/tarfs_vfsops.c @@ -697,6 +697,12 @@ error = EINVAL; goto bad; } + if (parent->type != VDIR) { + TARFS_DPF(ALLOC, "%s: parent of %.*s is not a directory\n", __func__, + (int)namelen, name); + error = EINVAL; + goto bad; + } switch (hdrp->typeflag[0]) { case TAR_TYPE_DIRECTORY: error = tarfs_alloc_node(tmp, namep, sep - namep, VDIR, diff --git a/tests/sys/fs/tarfs/tarfs_test.sh b/tests/sys/fs/tarfs/tarfs_test.sh --- a/tests/sys/fs/tarfs/tarfs_test.sh +++ b/tests/sys/fs/tarfs/tarfs_test.sh @@ -33,10 +33,11 @@ sum=4da2143234486307bb44eaa610375301781a577d1172f362b88bb4b1643dee62 atf_test_case tarfs_test -tarfs_test_head() { +tarfs_basic_head() { + atf_set "descr" "Basic function test" atf_set "require.user" "root" } -tarfs_test_body() { +tarfs_basic_body() { mkdir "${mnt}" "${mktar}" tarfs_test.tar.zst atf_check mount -rt tarfs tarfs_test.tar.zst "${mnt}" @@ -45,10 +46,32 @@ atf_check_equal "$(stat -f%d,%i "${mnt}"/sparse_file)" "$(stat -L -f%d,%i "${mnt}"/long_link)" atf_check_equal "$(sha256 -q "${mnt}"/sparse_file)" ${sum} } -tarfs_test_cleanup() { +tarfs_basic_cleanup() { + umount "${mnt}" +} + +atf_test_case tarfs_notdir +tarfs_notdir_head() { + atf_set "descr" "Regression test for PR 269519" + atf_set "require.user" "root" +} +tarfs_notdir_body() { + mkdir "${mnt}" + mkdir t + echo "hello" >t/d + tar cf tarfs_notdir.tar t + rm t/d + mkdir t/d + echo "world" >t/d/b + tar rf tarfs_notdir.tar t + atf_check -s not-exit:0 -e match:"Invalid" \ + mount -rt tarfs tarfs_notdir.tar "${mnt}" +} +tarfs_notdir_cleanup() { umount "${mnt}" } atf_init_test_cases() { - atf_add_test_case tarfs_test + atf_add_test_case tarfs_basic + atf_add_test_case tarfs_notdir }