From 913373053fe857a5c585e5546acbac78f21b153c Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Tue, 9 Nov 2021 19:24:42 -0500 Subject: [PATCH] ntfs: Prevent moving directories into themselves (even indirectly.) libntfs does not check for this. --- .../file_systems/ntfs/kernel_interface.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/add-ons/kernel/file_systems/ntfs/kernel_interface.cpp b/src/add-ons/kernel/file_systems/ntfs/kernel_interface.cpp index f6787fe238..24cffa8966 100644 --- a/src/add-ons/kernel/file_systems/ntfs/kernel_interface.cpp +++ b/src/add-ons/kernel/file_systems/ntfs/kernel_interface.cpp @@ -1001,6 +1001,30 @@ fs_rename(fs_volume* _volume, fs_vnode* _oldDir, const char* oldName, if (status != B_OK) return status; + // Prevent moving a directory into one of its own children. + if (old_directory != new_directory) { + u64 oldIno = ntfs_fuse_inode_lookup(&volume->lowntfs, old_directory->inode, oldName); + if (oldIno == (u64)-1) + return B_ENTRY_NOT_FOUND; + + ino_t parent = new_directory->inode; + const ino_t root = FILE_root; + + while (true) { + if (parent == oldIno) + return B_BAD_VALUE; + else if (parent == root || parent == old_directory->inode) + break; + + vnode* parentNode; + if (get_vnode(_volume, parent, (void**)&parentNode) != B_OK) + return B_ERROR; + + parent = parentNode->parent_inode; + put_vnode(_volume, parentNode->inode); + } + } + if (ntfs_fuse_rename(&volume->lowntfs, old_directory->inode, oldName, new_directory->inode, newName) != 0) return errno;