19 Oct, 2021

1 commit

  • 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: b844f0ecbc56 ("vfs: define kernel_copy_file_from_fd()")
    Signed-off-by: Matthew Wilcox (Oracle)
    Reported-by: Hao Sun
    Reviewed-by: Kees Cook
    Acked-by: Christian Brauner
    Cc: Al Viro
    Cc: Mimi Zohar
    Cc:
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Matthew Wilcox (Oracle)
     

08 Apr, 2021

1 commit


05 Oct, 2020

6 commits

  • To perform partial reads, callers of kernel_read_file*() must have a
    non-NULL file_size argument and a preallocated buffer. The new "offset"
    argument can then be used to seek to specific locations in the file to
    fill the buffer to, at most, "buf_size" per call.

    Where possible, the LSM hooks can report whether a full file has been
    read or not so that the contents can be reasoned about.

    Signed-off-by: Kees Cook
    Link: https://lore.kernel.org/r/20201002173828.2099543-14-keescook@chromium.org
    Signed-off-by: Greg Kroah-Hartman

    Kees Cook
     
  • As with the kernel_load_data LSM hook, add a "contents" flag to the
    kernel_read_file LSM hook that indicates whether the LSM can expect
    a matching call to the kernel_post_read_file LSM hook with the full
    contents of the file. With the coming addition of partial file read
    support for kernel_read_file*() API, the LSM will no longer be able
    to always see the entire contents of a file during the read calls.

    For cases where the LSM must read examine the complete file contents,
    it will need to do so on its own every time the kernel_read_file
    hook is called with contents=false (or reject such cases). Adjust all
    existing LSMs to retain existing behavior.

    Signed-off-by: Kees Cook
    Reviewed-by: Mimi Zohar
    Link: https://lore.kernel.org/r/20201002173828.2099543-12-keescook@chromium.org
    Signed-off-by: Greg Kroah-Hartman

    Kees Cook
     
  • In preparation for adding partial read support, add an optional output
    argument to kernel_read_file*() that reports the file size so callers
    can reason more easily about their reading progress.

    Signed-off-by: Kees Cook
    Reviewed-by: Mimi Zohar
    Reviewed-by: Luis Chamberlain
    Reviewed-by: James Morris
    Acked-by: Scott Branden
    Link: https://lore.kernel.org/r/20201002173828.2099543-8-keescook@chromium.org
    Signed-off-by: Greg Kroah-Hartman

    Kees Cook
     
  • In preparation for further refactoring of kernel_read_file*(), rename
    the "max_size" argument to the more accurate "buf_size", and correct
    its type to size_t. Add kerndoc to explain the specifics of how the
    arguments will be used. Note that with buf_size now size_t, it can no
    longer be negative (and was never called with a negative value). Adjust
    callers to use it as a "maximum size" when *buf is NULL.

    Signed-off-by: Kees Cook
    Reviewed-by: Mimi Zohar
    Reviewed-by: Luis Chamberlain
    Reviewed-by: James Morris
    Acked-by: Scott Branden
    Link: https://lore.kernel.org/r/20201002173828.2099543-7-keescook@chromium.org
    Signed-off-by: Greg Kroah-Hartman

    Kees Cook
     
  • In preparation for refactoring kernel_read_file*(), remove the redundant
    "size" argument which is not needed: it can be included in the return
    code, with callers adjusted. (VFS reads already cannot be larger than
    INT_MAX.)

    Signed-off-by: Kees Cook
    Reviewed-by: Mimi Zohar
    Reviewed-by: Luis Chamberlain
    Reviewed-by: James Morris
    Acked-by: Scott Branden
    Link: https://lore.kernel.org/r/20201002173828.2099543-6-keescook@chromium.org
    Signed-off-by: Greg Kroah-Hartman

    Kees Cook
     
  • These routines are used in places outside of exec(2), so in preparation
    for refactoring them, move them into a separate source file,
    fs/kernel_read_file.c.

    Signed-off-by: Kees Cook
    Reviewed-by: Mimi Zohar
    Reviewed-by: Luis Chamberlain
    Acked-by: Scott Branden
    Link: https://lore.kernel.org/r/20201002173828.2099543-5-keescook@chromium.org
    Signed-off-by: Greg Kroah-Hartman

    Kees Cook