Skip to content

Commit

Permalink
fs: check FMODE_WRITE in __kernel_write
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Christoph Hellwig committed Jul 8, 2020
1 parent 9db9775 commit a01ac27
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions fs/read_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit a01ac27

Please sign in to comment.