From 64d0f30bd92ccb0335422bffc9179702150bdb00 Mon Sep 17 00:00:00 2001 From: apio Date: Thu, 11 May 2023 21:29:49 +0200 Subject: [PATCH] kernel: Disallow write-related flags when opening a directory, not only when the directory flag is set Also return EISDIR instead of EINVAL as that is what Dr. POSIX mandates. --- kernel/src/sys/open.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/kernel/src/sys/open.cpp b/kernel/src/sys/open.cpp index 3a57f3ea..9453e574 100644 --- a/kernel/src/sys/open.cpp +++ b/kernel/src/sys/open.cpp @@ -23,11 +23,6 @@ Result sys_openat(Registers*, SyscallArgs args) // Caller did not pass either O_RDONLY, O_WRONLY or O_RDWR if ((flags & O_RDWR) == 0) { return err(EINVAL); } - if (flags & O_DIRECTORY) - { - if ((flags & O_WRONLY) || (flags & O_CREAT)) return err(EINVAL); - } - if (flags & O_TMPFILE) { if (!(flags & O_WRONLY)) return err(EINVAL); @@ -66,6 +61,11 @@ Result sys_openat(Registers*, SyscallArgs args) if (inode->type() != VFS::InodeType::Directory && (flags & O_DIRECTORY)) return err(ENOTDIR); + if (inode->type() == VFS::InodeType::Directory) + { + if ((flags & O_WRONLY) || (flags & O_CREAT)) return err(EISDIR); + } + if ((flags & O_WRONLY) && (flags & O_TRUNC)) inode->truncate(0); int fd = TRY(current->allocate_fd(0));