1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-15 01:44:58 +00:00

Kernel+LibC: Don't allow a directory to become a subdirectory of itself

If you try to do this (e.g "mv directory directory"), sys$rename() will
now fail with EDIRINTOSELF.

Dr. POSIX says we should return EINVAL for this, but a custom error
code allows us to print a much more helpful error message when this
problem occurs. :^)
This commit is contained in:
Andreas Kling 2020-11-01 17:17:23 +01:00
parent 13aa3d2d62
commit a28f29c82c
3 changed files with 8 additions and 1 deletions

View file

@ -522,6 +522,11 @@ KResult VFS::rename(StringView old_path, StringView new_path, Custody& base)
if (&old_parent_inode.fs() != &new_parent_inode.fs())
return KResult(-EXDEV);
for (auto* new_ancestor = new_parent_custody.ptr(); new_ancestor; new_ancestor = new_ancestor->parent()) {
if (&old_inode == &new_ancestor->inode())
return KResult(-EDIRINTOSELF);
}
auto current_process = Process::current();
if (!new_parent_inode.metadata().may_write(*current_process))
return KResult(-EACCES);