]> git.baikalelectronics.ru Git - kernel.git/commitdiff
vfs: check fd has read access in kernel_read_file_from_fd()
authorMatthew Wilcox (Oracle) <willy@infradead.org>
Mon, 18 Oct 2021 22:16:12 +0000 (15:16 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 27 Oct 2021 07:54:27 +0000 (09:54 +0200)
commit 032146cda85566abcd1c4884d9d23e4e30a07e9a upstream.

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/20211007220110.600005-1-willy@infradead.org
Fixes: a6060bf2e60a ("vfs: define kernel_copy_file_from_fd()")
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Reported-by: Hao Sun <sunhao.th@gmail.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Mimi Zohar <zohar@linux.ibm.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/exec.c

index 1b4d2206d53a108ad89ba4265d1b5263d9a0fb48..098de820abcc97406b6062391f5beaad51d30731 100644 (file)
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -988,7 +988,7 @@ int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size,
        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, buf, size, max_size, id);