Skip to content

Commit

Permalink
vfs: check fd has read access in kernel_read_file_from_fd()
Browse files Browse the repository at this point in the history
If we open a file without read access and then pass the fd to a syscall
whose implementation calls kernel_read_file_from_fd(), we get a warning
from __kernel_read():

        if (WARN_ON_ONCE(!(file->f_mode & FMODE_READ)))

This currently affects both finit_module() and kexec_file_load(), but it
could affect other syscalls in the future.

Link: https://lkml.kernel.org/r/[email protected]
Fixes: b844f0e ("vfs: define kernel_copy_file_from_fd()")
Signed-off-by: Matthew Wilcox (Oracle) <[email protected]>
Reported-by: Hao Sun <[email protected]>
Reviewed-by: Kees Cook <[email protected]>
Acked-by: Christian Brauner <[email protected]>
Cc: Al Viro <[email protected]>
Cc: Mimi Zohar <[email protected]>
Cc: <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Matthew Wilcox (Oracle) authored and torvalds committed Oct 19, 2021
1 parent b0e9012 commit 032146c
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion fs/kernel_read_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ int kernel_read_file_from_fd(int fd, loff_t offset, void **buf,
struct fd f = fdget(fd);
int ret = -EBADF;

if (!f.file)
if (!f.file || !(f.file->f_mode & FMODE_READ))
goto out;

ret = kernel_read_file(f.file, offset, buf, buf_size, file_size, id);
Expand Down

0 comments on commit 032146c

Please sign in to comment.