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 @@ -320,6 +320,12 @@ break; } + /* we're not at the end, so parent must be a directory */ + if (parent->type != VDIR) { + error = ENOTDIR; + break; + } + /* locate the next separator */ for (sep = name, len = 0; *sep != '\0' && *sep != '/' && len < namelen; @@ -685,8 +691,12 @@ error = tarfs_lookup_path(tmp, name, namelen, &namep, &sep, &parent, &tnp, true); - if (error != 0) + if (error != 0) { + TARFS_DPF(ALLOC, "%s: failed to look up %.*s\n", __func__, + (int)namelen, name); + error = EINVAL; goto bad; + } if (tnp != NULL) { if (hdrp->typeflag[0] == TAR_TYPE_DIRECTORY) { /* XXX set attributes? */ 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 -p t/d/s + echo "world" >t/d/s/f + tar rf tarfs_notdir.tar t/d/s/f + 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 }