fs: check FMODE_WRITE in __kernel_write
authorChristoph Hellwig <hch@lst.de>
Fri, 8 May 2020 06:55:03 +0000 (08:55 +0200)
committerChristoph Hellwig <hch@lst.de>
Wed, 8 Jul 2020 06:27:56 +0000 (08:27 +0200)
Add a WARN_ON_ONCE if the file isn't actually open for write.  This
matches the check done in vfs_write, but actually warn warns as a
kernel user calling write on a file not opened for writing is a pretty
obvious programming error.

Signed-off-by: Christoph Hellwig <hch@lst.de>
fs/read_write.c

index 2c601d853ff3d83fd8576213bf9d0da033649d19..8f9fc05990ae8b522ef415b24ef751c7e9deb4de 100644 (file)
@@ -505,6 +505,8 @@ ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t
        const char __user *p;
        ssize_t ret;
 
+       if (WARN_ON_ONCE(!(file->f_mode & FMODE_WRITE)))
+               return -EBADF;
        if (!(file->f_mode & FMODE_CAN_WRITE))
                return -EINVAL;