From: wangyining ext2fs_mkdir2() checks whether the destination name already exists only after writing a new inode and directory data. On EEXIST, cleanup undoes the allocation accounting but leaves the newly written inode on disk with a nonzero link count and references to blocks marked free. For example, on a freshly formatted ext4 image: img=$(mktemp) truncate -s 32M "$img" mke2fs -q -F -t ext4 "$img" debugfs -w -R 'mkdir /lost+found' "$img" e2fsck -fn "$img" The final check reports an unconnected directory, link-count errors and inode/block bitmap differences. This is also reproducible with ext2 and inline-data directories. Check the destination name before allocating or writing anything when the parent already exists. Retain the post-initialization lookup for a self-parented directory, since that form is also used to bootstrap the parent inode itself. Add a regression test covering an existing directory and an existing regular file on ext2, ext4 and inline-data filesystems. Check both that the complete image is unchanged and that a read-only e2fsck succeeds. Signed-off-by: wangyining --- lib/ext2fs/mkdir.c | 28 +++++++++++++------ tests/d_mkdir_existing/expect | 30 +++++++++++++++++++++ tests/d_mkdir_existing/name | 1 + tests/d_mkdir_existing/script | 51 +++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 tests/d_mkdir_existing/expect create mode 100644 tests/d_mkdir_existing/name create mode 100644 tests/d_mkdir_existing/script diff --git a/lib/ext2fs/mkdir.c b/lib/ext2fs/mkdir.c index 45f6e9e..4bf64d7 100644 --- a/lib/ext2fs/mkdir.c +++ b/lib/ext2fs/mkdir.c @@ -47,6 +47,16 @@ errcode_t ext2fs_mkdir2(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino, EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); + /* Reject existing names before writing an unlinked inode to disk. */ + if (name && parent != ino) { + retval = ext2fs_lookup(fs, parent, name, strlen(name), 0, + &scratch_ino); + if (!retval) + return EXT2_ET_DIR_EXISTS; + if (retval != EXT2_ET_FILE_NOT_FOUND) + return retval; + } + /* * Create a new dir with inline data iff this feature is enabled * and ino >= EXT2_FIRST_INO. @@ -159,15 +169,17 @@ errcode_t ext2fs_mkdir2(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino, * Link the directory into the filesystem hierarchy */ if (name) { - retval = ext2fs_lookup(fs, parent, name, strlen(name), 0, - &scratch_ino); - if (!retval) { - retval = EXT2_ET_DIR_EXISTS; - name = 0; - goto cleanup; + /* A self-parented directory could not be looked up until now. */ + if (parent == ino) { + retval = ext2fs_lookup(fs, parent, name, strlen(name), 0, + &scratch_ino); + if (!retval) { + retval = EXT2_ET_DIR_EXISTS; + goto cleanup; + } + if (retval != EXT2_ET_FILE_NOT_FOUND) + goto cleanup; } - if (retval != EXT2_ET_FILE_NOT_FOUND) - goto cleanup; retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_DIR | link_flags); if (retval) diff --git a/tests/d_mkdir_existing/expect b/tests/d_mkdir_existing/expect new file mode 100644 index 0000000..6927ee0 --- /dev/null +++ b/tests/d_mkdir_existing/expect @@ -0,0 +1,30 @@ +ext2: mkdir /existing +ext2fs_mkdir2: Ext2 directory already exists while creating directory "existing" +mkdir: Ext2 directory already exists +Image comparison exit status: 0 +Fsck exit status: 0 +ext2: mkdir /file +ext2fs_mkdir2: Ext2 directory already exists while creating directory "file" +mkdir: Ext2 directory already exists +Image comparison exit status: 0 +Fsck exit status: 0 +ext4: mkdir /existing +ext2fs_mkdir2: Ext2 directory already exists while creating directory "existing" +mkdir: Ext2 directory already exists +Image comparison exit status: 0 +Fsck exit status: 0 +ext4: mkdir /file +ext2fs_mkdir2: Ext2 directory already exists while creating directory "file" +mkdir: Ext2 directory already exists +Image comparison exit status: 0 +Fsck exit status: 0 +inline: mkdir /existing +ext2fs_mkdir2: Ext2 directory already exists while creating directory "existing" +mkdir: Ext2 directory already exists +Image comparison exit status: 0 +Fsck exit status: 0 +inline: mkdir /file +ext2fs_mkdir2: Ext2 directory already exists while creating directory "file" +mkdir: Ext2 directory already exists +Image comparison exit status: 0 +Fsck exit status: 0 diff --git a/tests/d_mkdir_existing/name b/tests/d_mkdir_existing/name new file mode 100644 index 0000000..c21b153 --- /dev/null +++ b/tests/d_mkdir_existing/name @@ -0,0 +1 @@ +mkdir on an existing name leaves the filesystem unchanged diff --git a/tests/d_mkdir_existing/script b/tests/d_mkdir_existing/script new file mode 100644 index 0000000..6b1fc75 --- /dev/null +++ b/tests/d_mkdir_existing/script @@ -0,0 +1,51 @@ +if ! test -x $DEBUGFS_EXE; then + echo "$test_name: $test_description: skipped (no debugfs)" + return 0 +fi + +OUT=$test_name.log +EXP=$test_dir/expect +> $OUT + +for fs_type in ext2 ext4 inline; do + rm -f $TMPFILE + if [ "$fs_type" = inline ]; then + $MKE2FS -q -F -t ext4 -O inline_data -b 1024 $TMPFILE 8192 \ + > /dev/null 2>&1 + else + $MKE2FS -q -F -t $fs_type -b 1024 $TMPFILE 8192 \ + > /dev/null 2>&1 + fi + $DEBUGFS -w $TMPFILE << EOF > /dev/null 2>&1 +mkdir /existing +write /dev/null /file +EOF + cp $TMPFILE $TMPFILE.before + for entry in existing file; do + cp $TMPFILE.before $TMPFILE + echo "$fs_type: mkdir /$entry" >> $OUT + $DEBUGFS -w -R "mkdir /$entry" $TMPFILE 2>&1 | \ + $CLEAN_OUTPUT | sed 's/ *$//' >> $OUT + cmp -s $TMPFILE.before $TMPFILE + echo "Image comparison exit status: $?" >> $OUT + $FSCK -fn $TMPFILE > $TMPFILE.fsck 2>&1 + status=$? + echo "Fsck exit status: $status" >> $OUT + if [ "$status" != 0 ]; then + cat $TMPFILE.fsck >> $OUT + fi + done +done + +cmp -s $OUT $EXP +status=$? +if [ "$status" = 0 ]; then + echo "$test_name: $test_description: ok" + touch $test_name.ok +else + echo "$test_name: $test_description: failed" + diff $DIFF_OPTS $EXP $OUT > $test_name.failed +fi + +rm -f $TMPFILE $TMPFILE.before $TMPFILE.fsck +unset OUT EXP fs_type entry base-commit: 75e95512b22c57db042a53e2fe1af7fcc0a3b2e3