From 46d4e325101c41f99bd880f7c9e59c9bbaba5bfe Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 15 Dec 2024 20:49:12 +0200 Subject: [PATCH] Kenrel: Validate inode open mode on read/write syscalls --- kernel/kernel/OpenFileDescriptorSet.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kernel/kernel/OpenFileDescriptorSet.cpp b/kernel/kernel/OpenFileDescriptorSet.cpp index 5b7c5fe6..1f3907c0 100644 --- a/kernel/kernel/OpenFileDescriptorSet.cpp +++ b/kernel/kernel/OpenFileDescriptorSet.cpp @@ -300,6 +300,8 @@ namespace Kernel { TRY(validate_fd(fd)); auto& open_file = m_open_files[fd]; + if (!(open_file->flags & O_RDONLY)) + return BAN::Error::from_errno(EBADF); if ((open_file->flags & O_NONBLOCK) && !open_file->inode()->can_read()) return 0; size_t nread = TRY(open_file->inode()->read(open_file->offset, buffer)); @@ -311,6 +313,8 @@ namespace Kernel { TRY(validate_fd(fd)); auto& open_file = m_open_files[fd]; + if (!(open_file->flags & O_WRONLY)) + return BAN::Error::from_errno(EBADF); if ((open_file->flags & O_NONBLOCK) && !open_file->inode()->can_write()) return 0; if (open_file->flags & O_APPEND)