06 Jan, 2017

3 commits

  • commit 64b875f7ac8a5d60a4e191479299e931ee949b67 upstream.

    When the flag PT_PTRACE_CAP was added the PTRACE_TRACEME path was
    overlooked. This can result in incorrect behavior when an application
    like strace traces an exec of a setuid executable.

    Further PT_PTRACE_CAP does not have enough information for making good
    security decisions as it does not report which user namespace the
    capability is in. This has already allowed one mistake through
    insufficient granulariy.

    I found this issue when I was testing another corner case of exec and
    discovered that I could not get strace to set PT_PTRACE_CAP even when
    running strace as root with a full set of caps.

    This change fixes the above issue with strace allowing stracing as
    root a setuid executable without disabling setuid. More fundamentaly
    this change allows what is allowable at all times, by using the correct
    information in it's decision.

    Fixes: 4214e42f96d4 ("v2.4.9.11 -> v2.4.9.12")
    Signed-off-by: "Eric W. Biederman"
    Signed-off-by: Greg Kroah-Hartman

    Eric W. Biederman
     
  • commit 613cc2b6f272c1a8ad33aefa21cad77af23139f7 upstream.

    If you have a process that has set itself to be non-dumpable, and it
    then undergoes exec(2), any CLOEXEC file descriptors it has open are
    "exposed" during a race window between the dumpable flags of the process
    being reset for exec(2) and CLOEXEC being applied to the file
    descriptors. This can be exploited by a process by attempting to access
    /proc//fd/... during this window, without requiring CAP_SYS_PTRACE.

    The race in question is after set_dumpable has been (for get_link,
    though the trace is basically the same for readlink):

    [vfs]
    -> proc_pid_link_inode_operations.get_link
    -> proc_pid_get_link
    -> proc_fd_access_allowed
    -> ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);

    Which will return 0, during the race window and CLOEXEC file descriptors
    will still be open during this window because do_close_on_exec has not
    been called yet. As a result, the ordering of these calls should be
    reversed to avoid this race window.

    This is of particular concern to container runtimes, where joining a
    PID namespace with file descriptors referring to the host filesystem
    can result in security issues (since PRCTL_SET_DUMPABLE doesn't protect
    against access of CLOEXEC file descriptors -- file descriptors which may
    reference filesystem objects the container shouldn't have access to).

    Cc: dev@opencontainers.org
    Reported-by: Michael Crosby
    Signed-off-by: Aleksa Sarai
    Signed-off-by: Al Viro
    Signed-off-by: Greg Kroah-Hartman

    Aleksa Sarai
     
  • commit f84df2a6f268de584a201e8911384a2d244876e3 upstream.

    When the user namespace support was merged the need to prevent
    ptrace from revealing the contents of an unreadable executable
    was overlooked.

    Correct this oversight by ensuring that the executed file
    or files are in mm->user_ns, by adjusting mm->user_ns.

    Use the new function privileged_wrt_inode_uidgid to see if
    the executable is a member of the user namespace, and as such
    if having CAP_SYS_PTRACE in the user namespace should allow
    tracing the executable. If not update mm->user_ns to
    the parent user namespace until an appropriate parent is found.

    Reported-by: Jann Horn
    Fixes: 9e4a36ece652 ("userns: Fail exec for suid and sgid binaries with ids outside our user namespace.")
    Signed-off-by: "Eric W. Biederman"
    Signed-off-by: Greg Kroah-Hartman

    Eric W. Biederman
     

19 Oct, 2016

1 commit


05 Aug, 2016

1 commit

  • Pull m68knommu updates from Greg Ungerer:
    "This series is all about Nicolas flat format support for MMU systems.

    Traditional m68k no-MMU flat format binaries can now be run on m68k
    MMU enabled systems too. The series includes some nice cleanups of
    the binfmt_flat code and converts it to using proper user space
    accessor functions.

    With all this in place you can boot and run a complete no-MMU flat
    format based user space on an MMU enabled system"

    * 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/gerg/m68knommu:
    m68k: enable binfmt_flat on systems with an MMU
    binfmt_flat: allow compressed flat binary format to work on MMU systems
    binfmt_flat: add MMU-specific support
    binfmt_flat: update libraries' data segment pointer with userspace accessors
    binfmt_flat: use clear_user() rather than memset() to clear .bss
    binfmt_flat: use proper user space accessors with old relocs code
    binfmt_flat: use proper user space accessors with relocs processing code
    binfmt_flat: clean up create_flat_tables() and stack accesses
    binfmt_flat: use generic transfer_args_to_stack()
    elf_fdpic_transfer_args_to_stack(): make it generic
    binfmt_flat: prevent kernel dammage from corrupted executable headers
    binfmt_flat: convert printk invocations to their modern form
    binfmt_flat: assorted cleanups
    m68k: use same start_thread() on MMU and no-MMU
    m68k: fix file path comment
    m68k: fix bFLT executable running on MMU enabled systems

    Linus Torvalds
     

03 Aug, 2016

1 commit

  • Some systems are memory constrained but they need to load very large
    firmwares. The firmware subsystem allows drivers to request this
    firmware be loaded from the filesystem, but this requires that the
    entire firmware be loaded into kernel memory first before it's provided
    to the driver. This can lead to a situation where we map the firmware
    twice, once to load the firmware into kernel memory and once to copy the
    firmware into the final resting place.

    This creates needless memory pressure and delays loading because we have
    to copy from kernel memory to somewhere else. Let's add a
    request_firmware_into_buf() API that allows drivers to request firmware
    be loaded directly into a pre-allocated buffer. This skips the
    intermediate step of allocating a buffer in kernel memory to hold the
    firmware image while it's read from the filesystem. It also requires
    that drivers know how much memory they'll require before requesting the
    firmware and negates any benefits of firmware caching because the
    firmware layer doesn't manage the buffer lifetime.

    For a 16MB buffer, about half the time is spent performing a memcpy from
    the buffer to the final resting place. I see loading times go from
    0.081171 seconds to 0.047696 seconds after applying this patch. Plus
    the vmalloc pressure is reduced.

    This is based on a patch from Vikram Mulukutla on codeaurora.org:
    https://www.codeaurora.org/cgit/quic/la/kernel/msm-3.18/commit/drivers/base/firmware_class.c?h=rel/msm-3.18&id=0a328c5f6cd999f5c591f172216835636f39bcb5

    Link: http://lkml.kernel.org/r/20160607164741.31849-4-stephen.boyd@linaro.org
    Signed-off-by: Stephen Boyd
    Cc: Mimi Zohar
    Cc: Vikram Mulukutla
    Cc: Mark Brown
    Cc: Ming Lei
    Cc: Greg Kroah-Hartman
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Stephen Boyd
     

25 Jul, 2016

1 commit

  • This copying of arguments and environment is common to both NOMMU
    binary formats we support. Let's make the elf_fdpic version available
    to the flat format as well.

    While at it, improve the code a bit not to copy below the actual
    data area.

    Signed-off-by: Nicolas Pitre
    Reviewed-by: Greg Ungerer
    Signed-off-by: Greg Ungerer

    Nicolas Pitre
     

24 Jun, 2016

1 commit

  • If a process gets access to a mount from a different user
    namespace, that process should not be able to take advantage of
    setuid files or selinux entrypoints from that filesystem. Prevent
    this by treating mounts from other mount namespaces and those not
    owned by current_user_ns() or an ancestor as nosuid.

    This will make it safer to allow more complex filesystems to be
    mounted in non-root user namespaces.

    This does not remove the need for MNT_LOCK_NOSUID. The setuid,
    setgid, and file capability bits can no longer be abused if code in
    a user namespace were to clear nosuid on an untrusted filesystem,
    but this patch, by itself, is insufficient to protect the system
    from abuse of files that, when execed, would increase MAC privilege.

    As a more concrete explanation, any task that can manipulate a
    vfsmount associated with a given user namespace already has
    capabilities in that namespace and all of its descendents. If they
    can cause a malicious setuid, setgid, or file-caps executable to
    appear in that mount, then that executable will only allow them to
    elevate privileges in exactly the set of namespaces in which they
    are already privileges.

    On the other hand, if they can cause a malicious executable to
    appear with a dangerous MAC label, running it could change the
    caller's security context in a way that should not have been
    possible, even inside the namespace in which the task is confined.

    As a hardening measure, this would have made CVE-2014-5207 much
    more difficult to exploit.

    Signed-off-by: Andy Lutomirski
    Signed-off-by: Seth Forshee
    Acked-by: James Morris
    Acked-by: Serge Hallyn
    Signed-off-by: Eric W. Biederman

    Andy Lutomirski
     

24 May, 2016

2 commits

  • setup_arg_pages requires mmap_sem for write. If the waiting task gets
    killed by the oom killer it would block oom_reaper from asynchronous
    address space reclaim and reduce the chances of timely OOM resolving.
    Wait for the lock in the killable mode and return with EINTR if the task
    got killed while waiting. All the callers are already handling error
    path and the fatal signal doesn't need any additional treatment.

    The same applies to __bprm_mm_init.

    Signed-off-by: Michal Hocko
    Acked-by: Oleg Nesterov
    Acked-by: Vlastimil Babka
    Cc: Alexander Viro
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Michal Hocko
     
  • remove_arg_zero() does free_arg_page() for no reason. This was needed
    before and only if CONFIG_MMU=y: see commit 4fc75ff4816c ("exec: fix
    remove_arg_zero"), install_arg_page() was called for every page != NULL
    in bprm->page[] array. Today install_arg_page() has already gone and
    free_arg_page() is nop after another commit b6a2fea39318 ("mm: variable
    length argument support").

    CONFIG_MMU=n does free_arg_pages() in free_bprm() and thus it doesn't
    need remove_arg_zero()->free_arg_page() too; apart from get_arg_page()
    it never checks if the page in bprm->page[] was allocated or not, so the
    "extra" non-freed page is fine. OTOH, this free_arg_page() can add the
    minor pessimization, the caller is going to do copy_strings_kernel()
    right after remove_arg_zero() which will likely need to re-allocate the
    same page again.

    And as Hujunjie pointed out, the "offset == PAGE_SIZE" check is wrong
    because we are going to increment bprm->p once again before return, so
    CONFIG_MMU=n "leaks" the page anyway if '0' is the final byte in this
    page.

    NOTE: remove_arg_zero() assumes that argv[0] is null-terminated but this
    is not necessarily true. copy_strings() does "len = strnlen_user(...)",
    then copy_from_user(len) but another thread or debuger can overwrite the
    trailing '0' in between. Afaics nothing really bad can happen because
    we must always have the null-terminated bprm->filename copied by the 1st
    copy_strings_kernel(), but perhaps we should change this code to check
    "bprm->p < bprm->exec" anyway, and/or change copy_strings() to ensure
    that the last byte in string is always zero.

    Link: http://lkml.kernel.org/r/20160517155335.GA31435@redhat.com
    Signed-off-by: Oleg Nesterov
    Reported by: hujunjie
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Oleg Nesterov
     

20 May, 2016

1 commit

  • Pull security subsystem updates from James Morris:
    "Highlights:

    - A new LSM, "LoadPin", from Kees Cook is added, which allows forcing
    of modules and firmware to be loaded from a specific device (this
    is from ChromeOS, where the device as a whole is verified
    cryptographically via dm-verity).

    This is disabled by default but can be configured to be enabled by
    default (don't do this if you don't know what you're doing).

    - Keys: allow authentication data to be stored in an asymmetric key.
    Lots of general fixes and updates.

    - SELinux: add restrictions for loading of kernel modules via
    finit_module(). Distinguish non-init user namespace capability
    checks. Apply execstack check on thread stacks"

    * 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security: (48 commits)
    LSM: LoadPin: provide enablement CONFIG
    Yama: use atomic allocations when reporting
    seccomp: Fix comment typo
    ima: add support for creating files using the mknodat syscall
    ima: fix ima_inode_post_setattr
    vfs: forbid write access when reading a file into memory
    fs: fix over-zealous use of "const"
    selinux: apply execstack check on thread stacks
    selinux: distinguish non-init user namespace capability checks
    LSM: LoadPin for kernel file loading restrictions
    fs: define a string representation of the kernel_read_file_id enumeration
    Yama: consolidate error reporting
    string_helpers: add kstrdup_quotable_file
    string_helpers: add kstrdup_quotable_cmdline
    string_helpers: add kstrdup_quotable
    selinux: check ss_initialized before revalidating an inode label
    selinux: delay inode label lookup as long as possible
    selinux: don't revalidate an inode's label when explicitly setting it
    selinux: Change bool variable name to index.
    KEYS: Add KEYCTL_DH_COMPUTE command
    ...

    Linus Torvalds
     

18 May, 2016

1 commit


01 May, 2016

1 commit

  • This patch is based on top of the "vfs: support for a common kernel file
    loader" patch set. In general when the kernel is reading a file into
    memory it does not want anything else writing to it.

    The kernel currently only forbids write access to a file being executed.
    This patch extends this locking to files being read by the kernel.

    Changelog:
    - moved function to kernel_read_file() - Mimi
    - updated patch description - Mimi

    Signed-off-by: Dmitry Kasatkin
    Cc: Al Viro
    Signed-off-by: Mimi Zohar
    Reviewed-by: Luis R. Rodriguez
    Acked-by: Kees Cook

    Dmitry Kasatkin
     

21 Mar, 2016

1 commit

  • Pull x86 protection key support from Ingo Molnar:
    "This tree adds support for a new memory protection hardware feature
    that is available in upcoming Intel CPUs: 'protection keys' (pkeys).

    There's a background article at LWN.net:

    https://lwn.net/Articles/643797/

    The gist is that protection keys allow the encoding of
    user-controllable permission masks in the pte. So instead of having a
    fixed protection mask in the pte (which needs a system call to change
    and works on a per page basis), the user can map a (handful of)
    protection mask variants and can change the masks runtime relatively
    cheaply, without having to change every single page in the affected
    virtual memory range.

    This allows the dynamic switching of the protection bits of large
    amounts of virtual memory, via user-space instructions. It also
    allows more precise control of MMU permission bits: for example the
    executable bit is separate from the read bit (see more about that
    below).

    This tree adds the MM infrastructure and low level x86 glue needed for
    that, plus it adds a high level API to make use of protection keys -
    if a user-space application calls:

    mmap(..., PROT_EXEC);

    or

    mprotect(ptr, sz, PROT_EXEC);

    (note PROT_EXEC-only, without PROT_READ/WRITE), the kernel will notice
    this special case, and will set a special protection key on this
    memory range. It also sets the appropriate bits in the Protection
    Keys User Rights (PKRU) register so that the memory becomes unreadable
    and unwritable.

    So using protection keys the kernel is able to implement 'true'
    PROT_EXEC on x86 CPUs: without protection keys PROT_EXEC implies
    PROT_READ as well. Unreadable executable mappings have security
    advantages: they cannot be read via information leaks to figure out
    ASLR details, nor can they be scanned for ROP gadgets - and they
    cannot be used by exploits for data purposes either.

    We know about no user-space code that relies on pure PROT_EXEC
    mappings today, but binary loaders could start making use of this new
    feature to map binaries and libraries in a more secure fashion.

    There is other pending pkeys work that offers more high level system
    call APIs to manage protection keys - but those are not part of this
    pull request.

    Right now there's a Kconfig that controls this feature
    (CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS) that is default enabled
    (like most x86 CPU feature enablement code that has no runtime
    overhead), but it's not user-configurable at the moment. If there's
    any serious problem with this then we can make it configurable and/or
    flip the default"

    * 'mm-pkeys-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (38 commits)
    x86/mm/pkeys: Fix mismerge of protection keys CPUID bits
    mm/pkeys: Fix siginfo ABI breakage caused by new u64 field
    x86/mm/pkeys: Fix access_error() denial of writes to write-only VMA
    mm/core, x86/mm/pkeys: Add execute-only protection keys support
    x86/mm/pkeys: Create an x86 arch_calc_vm_prot_bits() for VMA flags
    x86/mm/pkeys: Allow kernel to modify user pkey rights register
    x86/fpu: Allow setting of XSAVE state
    x86/mm: Factor out LDT init from context init
    mm/core, x86/mm/pkeys: Add arch_validate_pkey()
    mm/core, arch, powerpc: Pass a protection key in to calc_vm_flag_bits()
    x86/mm/pkeys: Actually enable Memory Protection Keys in the CPU
    x86/mm/pkeys: Add Kconfig prompt to existing config option
    x86/mm/pkeys: Dump pkey from VMA in /proc/pid/smaps
    x86/mm/pkeys: Dump PKRU with other kernel registers
    mm/core, x86/mm/pkeys: Differentiate instruction fetches
    x86/mm/pkeys: Optimize fault handling in access_error()
    mm/core: Do not enforce PKEY permissions on remote mm access
    um, pkeys: Add UML arch_*_access_permitted() methods
    mm/gup, x86/mm/pkeys: Check VMAs and PTEs for protection keys
    x86/mm/gup: Simplify get_user_pages() PTE bit handling
    ...

    Linus Torvalds
     

21 Feb, 2016

3 commits

  • This patch defines kernel_read_file_from_fd(), a wrapper for the VFS
    common kernel_read_file().

    Changelog:
    - Separated from the kernel modules patch
    Acked-by: Kees Cook
    Acked-by: Luis R. Rodriguez
    Cc: Al Viro

    Signed-off-by: Mimi Zohar

    Mimi Zohar
     
  • The kernel_read_file security hook is called prior to reading the file
    into memory.

    Changelog v4+:
    - export security_kernel_read_file()

    Signed-off-by: Mimi Zohar
    Acked-by: Kees Cook
    Acked-by: Luis R. Rodriguez
    Acked-by: Casey Schaufler

    Mimi Zohar
     
  • This patch defines kernel_read_file_from_path(), a wrapper for the VFS
    common kernel_read_file().

    Changelog:
    - revert error msg regression - reported by Sergey Senozhatsky
    - Separated from the IMA patch

    Signed-off-by: Mimi Zohar
    Acked-by: Kees Cook
    Acked-by: Luis R. Rodriguez
    Cc: Al Viro

    Mimi Zohar
     

19 Feb, 2016

2 commits

  • To differentiate between the kernel_read_file() callers, this patch
    defines a new enumeration named kernel_read_file_id and includes the
    caller identifier as an argument.

    Subsequent patches define READING_KEXEC_IMAGE, READING_KEXEC_INITRAMFS,
    READING_FIRMWARE, READING_MODULE, and READING_POLICY.

    Changelog v3:
    - Replace the IMA specific enumeration with a generic one.

    Signed-off-by: Mimi Zohar
    Acked-by: Kees Cook
    Acked-by: Luis R. Rodriguez
    Cc: Al Viro

    Mimi Zohar
     
  • For a while it was looked down upon to directly read files from Linux.
    These days there exists a few mechanisms in the kernel that do just
    this though to load a file into a local buffer. There are minor but
    important checks differences on each. This patch set is the first
    attempt at resolving some of these differences.

    This patch introduces a common function for reading files from the kernel
    with the corresponding security post-read hook and function.

    Changelog v4+:
    - export security_kernel_post_read_file() - Fengguang Wu
    v3:
    - additional bounds checking - Luis
    v2:
    - To simplify patch review, re-ordered patches

    Signed-off-by: Mimi Zohar
    Reviewed-by: Luis R. Rodriguez
    Acked-by: Kees Cook
    Cc: Al Viro

    Mimi Zohar
     

16 Feb, 2016

1 commit

  • For protection keys, we need to understand whether protections
    should be enforced in software or not. In general, we enforce
    protections when working on our own task, but not when on others.
    We call these "current" and "remote" operations.

    This patch introduces a new get_user_pages() variant:

    get_user_pages_remote()

    Which is a replacement for when get_user_pages() is called on
    non-current tsk/mm.

    We also introduce a new gup flag: FOLL_REMOTE which can be used
    for the "__" gup variants to get this new behavior.

    The uprobes is_trap_at_addr() location holds mmap_sem and
    calls get_user_pages(current->mm) on an instruction address. This
    makes it a pretty unique gup caller. Being an instruction access
    and also really originating from the kernel (vs. the app), I opted
    to consider this a 'remote' access where protection keys will not
    be enforced.

    Without protection keys, this patch should not change any behavior.

    Signed-off-by: Dave Hansen
    Reviewed-by: Thomas Gleixner
    Cc: Andrea Arcangeli
    Cc: Andrew Morton
    Cc: Andy Lutomirski
    Cc: Borislav Petkov
    Cc: Brian Gerst
    Cc: Dave Hansen
    Cc: Denys Vlasenko
    Cc: H. Peter Anvin
    Cc: Kirill A. Shutemov
    Cc: Linus Torvalds
    Cc: Naoya Horiguchi
    Cc: Peter Zijlstra
    Cc: Rik van Riel
    Cc: Srikar Dronamraju
    Cc: Vlastimil Babka
    Cc: jack@suse.cz
    Cc: linux-mm@kvack.org
    Link: http://lkml.kernel.org/r/20160212210154.3F0E51EA@viggo.jf.intel.com
    Signed-off-by: Ingo Molnar

    Dave Hansen
     

23 Jan, 2016

1 commit

  • parallel to mutex_{lock,unlock,trylock,is_locked,lock_nested},
    inode_foo(inode) being mutex_foo(&inode->i_mutex).

    Please, use those for access to ->i_mutex; over the coming cycle
    ->i_mutex will become rwsem, with ->lookup() done with it held
    only shared.

    Signed-off-by: Al Viro

    Al Viro
     

04 Jan, 2016

1 commit


10 Jul, 2015

1 commit

  • Today proc and sysfs do not contain any executable files. Several
    applications today mount proc or sysfs without noexec and nosuid and
    then depend on there being no exectuables files on proc or sysfs.
    Having any executable files show on proc or sysfs would cause
    a user space visible regression, and most likely security problems.

    Therefore commit to never allowing executables on proc and sysfs by
    adding a new flag to mark them as filesystems without executables and
    enforce that flag.

    Test the flag where MNT_NOEXEC is tested today, so that the only user
    visible effect will be that exectuables will be treated as if the
    execute bit is cleared.

    The filesystems proc and sysfs do not currently incoporate any
    executable files so this does not result in any user visible effects.

    This makes it unnecessary to vet changes to proc and sysfs tightly for
    adding exectuable files or changes to chattr that would modify
    existing files, as no matter what the individual file say they will
    not be treated as exectuable files by the vfs.

    Not having to vet changes to closely is important as without this we
    are only one proc_create call (or another goof up in the
    implementation of notify_change) from having problematic executables
    on proc. Those mistakes are all too easy to make and would create
    a situation where there are security issues or the assumptions of
    some program having to be broken (and cause userspace regressions).

    Signed-off-by: "Eric W. Biederman"

    Eric W. Biederman
     

13 May, 2015

1 commit

  • On architectures where the stack grows upwards (CONFIG_STACK_GROWSUP=y,
    currently parisc and metag only) stack randomization sometimes leads to crashes
    when the stack ulimit is set to lower values than STACK_RND_MASK (which is 8 MB
    by default if not defined in arch-specific headers).

    The problem is, that when the stack vm_area_struct is set up in fs/exec.c, the
    additional space needed for the stack randomization (as defined by the value of
    STACK_RND_MASK) was not taken into account yet and as such, when the stack
    randomization code added a random offset to the stack start, the stack
    effectively got smaller than what the user defined via rlimit_max(RLIMIT_STACK)
    which then sometimes leads to out-of-stack situations and crashes.

    This patch fixes it by adding the maximum possible amount of memory (based on
    STACK_RND_MASK) which theoretically could be added by the stack randomization
    code to the initial stack size. That way, the user-defined stack size is always
    guaranteed to be at minimum what is defined via rlimit_max(RLIMIT_STACK).

    This bug is currently not visible on the metag architecture, because on metag
    STACK_RND_MASK is defined to 0 which effectively disables stack randomization.

    The changes to fs/exec.c are inside an "#ifdef CONFIG_STACK_GROWSUP"
    section, so it does not affect other platformws beside those where the
    stack grows upwards (parisc and metag).

    Signed-off-by: Helge Deller
    Cc: linux-parisc@vger.kernel.org
    Cc: James Hogan
    Cc: linux-metag@vger.kernel.org
    Cc: stable@vger.kernel.org # v3.16+

    Helge Deller
     

20 Apr, 2015

1 commit


17 Apr, 2015

2 commits

  • We set sig->notify_count = -1 between RELEASE and ACQUIRE operations:

    spin_unlock_irq(lock);
    ...
    if (!thread_group_leader(tsk)) {
    ...
    for (;;) {
    sig->notify_count = -1;
    write_lock_irq(&tasklist_lock);

    There are no restriction on it so other processors may see this STORE
    mixed with other STOREs in both areas limited by the spinlocks.

    Probably, it may be reordered with the above

    sig->group_exit_task = tsk;
    sig->notify_count = zap_other_threads(tsk);

    in some way.

    Set it under tasklist_lock locked to be sure nothing will be reordered.

    Signed-off-by: Kirill Tkhai
    Acked-by: Oleg Nesterov
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Kirill Tkhai
     
  • Oleg cleverly suggested using xchg() to set the new mm->exe_file instead
    of calling set_mm_exe_file() which requires some form of serialization --
    mmap_sem in this case. For archs that do not have atomic rmw instructions
    we still fallback to a spinlock alternative, so this should always be
    safe. As such, we only need the mmap_sem for looking up the backing
    vm_file, which can be done sharing the lock. Naturally, this means we
    need to manually deal with both the new and old file reference counting,
    and we need not worry about the MMF_EXE_FILE_CHANGED bits, which can
    probably be deleted in the future anyway.

    Signed-off-by: Davidlohr Bueso
    Suggested-by: Oleg Nesterov
    Acked-by: Oleg Nesterov
    Reviewed-by: Konstantin Khlebnikov
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Davidlohr Bueso
     

23 Jan, 2015

1 commit

  • There are several areas in the kernel that create temporary filename
    objects using the following pattern:

    int func(const char *name)
    {
    struct filename *file = { .name = name };
    ...
    return 0;
    }

    ... which for the most part works okay, but it causes havoc within the
    audit subsystem as the filename object does not persist beyond the
    lifetime of the function. This patch converts all of these temporary
    filename objects into proper filename objects using getname_kernel()
    and putname() which ensure that the filename object persists until the
    audit subsystem is finished with it.

    Also, a special thanks to Al Viro, Guenter Roeck, and Sabrina Dubroca
    for helping resolve a difficult kernel panic on boot related to a
    use-after-free problem in kern_path_create(); the thread can be seen
    at the link below:

    * https://lkml.org/lkml/2015/1/20/710

    This patch includes code that was either based on, or directly written
    by Al in the above thread.

    CC: viro@zeniv.linux.org.uk
    CC: linux@roeck-us.net
    CC: sd@queasysnail.net
    CC: linux-fsdevel@vger.kernel.org
    Signed-off-by: Paul Moore
    Signed-off-by: Al Viro

    Paul Moore
     

14 Dec, 2014

1 commit

  • This patchset adds execveat(2) for x86, and is derived from Meredydd
    Luff's patch from Sept 2012 (https://lkml.org/lkml/2012/9/11/528).

    The primary aim of adding an execveat syscall is to allow an
    implementation of fexecve(3) that does not rely on the /proc filesystem,
    at least for executables (rather than scripts). The current glibc version
    of fexecve(3) is implemented via /proc, which causes problems in sandboxed
    or otherwise restricted environments.

    Given the desire for a /proc-free fexecve() implementation, HPA suggested
    (https://lkml.org/lkml/2006/7/11/556) that an execveat(2) syscall would be
    an appropriate generalization.

    Also, having a new syscall means that it can take a flags argument without
    back-compatibility concerns. The current implementation just defines the
    AT_EMPTY_PATH and AT_SYMLINK_NOFOLLOW flags, but other flags could be
    added in future -- for example, flags for new namespaces (as suggested at
    https://lkml.org/lkml/2006/7/11/474).

    Related history:
    - https://lkml.org/lkml/2006/12/27/123 is an example of someone
    realizing that fexecve() is likely to fail in a chroot environment.
    - http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=514043 covered
    documenting the /proc requirement of fexecve(3) in its manpage, to
    "prevent other people from wasting their time".
    - https://bugzilla.redhat.com/show_bug.cgi?id=241609 described a
    problem where a process that did setuid() could not fexecve()
    because it no longer had access to /proc/self/fd; this has since
    been fixed.

    This patch (of 4):

    Add a new execveat(2) system call. execveat() is to execve() as openat()
    is to open(): it takes a file descriptor that refers to a directory, and
    resolves the filename relative to that.

    In addition, if the filename is empty and AT_EMPTY_PATH is specified,
    execveat() executes the file to which the file descriptor refers. This
    replicates the functionality of fexecve(), which is a system call in other
    UNIXen, but in Linux glibc it depends on opening "/proc/self/fd/" (and
    so relies on /proc being mounted).

    The filename fed to the executed program as argv[0] (or the name of the
    script fed to a script interpreter) will be of the form "/dev/fd/"
    (for an empty filename) or "/dev/fd//", effectively
    reflecting how the executable was found. This does however mean that
    execution of a script in a /proc-less environment won't work; also, script
    execution via an O_CLOEXEC file descriptor fails (as the file will not be
    accessible after exec).

    Based on patches by Meredydd Luff.

    Signed-off-by: David Drysdale
    Cc: Meredydd Luff
    Cc: Shuah Khan
    Cc: "Eric W. Biederman"
    Cc: Andy Lutomirski
    Cc: Alexander Viro
    Cc: Thomas Gleixner
    Cc: Ingo Molnar
    Cc: "H. Peter Anvin"
    Cc: Kees Cook
    Cc: Arnd Bergmann
    Cc: Rich Felker
    Cc: Christoph Hellwig
    Cc: Michael Kerrisk
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    David Drysdale
     

18 Nov, 2014

2 commits

  • We no longer need mpx.h in exec.c. This will obviously also
    break the build for non-x86 builds. We get the MPX includes that
    we need from mmu_context.h now.

    Signed-off-by: Dave Hansen
    Cc: Dave Hansen
    Link: http://lkml.kernel.org/r/20141118003608.837015B3@viggo.jf.intel.com
    Signed-off-by: Thomas Gleixner

    Dave Hansen
     
  • This is really the meat of the MPX patch set. If there is one patch to
    review in the entire series, this is the one. There is a new ABI here
    and this kernel code also interacts with userspace memory in a
    relatively unusual manner. (small FAQ below).

    Long Description:

    This patch adds two prctl() commands to provide enable or disable the
    management of bounds tables in kernel, including on-demand kernel
    allocation (See the patch "on-demand kernel allocation of bounds tables")
    and cleanup (See the patch "cleanup unused bound tables"). Applications
    do not strictly need the kernel to manage bounds tables and we expect
    some applications to use MPX without taking advantage of this kernel
    support. This means the kernel can not simply infer whether an application
    needs bounds table management from the MPX registers. The prctl() is an
    explicit signal from userspace.

    PR_MPX_ENABLE_MANAGEMENT is meant to be a signal from userspace to
    require kernel's help in managing bounds tables.

    PR_MPX_DISABLE_MANAGEMENT is the opposite, meaning that userspace don't
    want kernel's help any more. With PR_MPX_DISABLE_MANAGEMENT, the kernel
    won't allocate and free bounds tables even if the CPU supports MPX.

    PR_MPX_ENABLE_MANAGEMENT will fetch the base address of the bounds
    directory out of a userspace register (bndcfgu) and then cache it into
    a new field (->bd_addr) in the 'mm_struct'. PR_MPX_DISABLE_MANAGEMENT
    will set "bd_addr" to an invalid address. Using this scheme, we can
    use "bd_addr" to determine whether the management of bounds tables in
    kernel is enabled.

    Also, the only way to access that bndcfgu register is via an xsaves,
    which can be expensive. Caching "bd_addr" like this also helps reduce
    the cost of those xsaves when doing table cleanup at munmap() time.
    Unfortunately, we can not apply this optimization to #BR fault time
    because we need an xsave to get the value of BNDSTATUS.

    ==== Why does the hardware even have these Bounds Tables? ====

    MPX only has 4 hardware registers for storing bounds information.
    If MPX-enabled code needs more than these 4 registers, it needs to
    spill them somewhere. It has two special instructions for this
    which allow the bounds to be moved between the bounds registers
    and some new "bounds tables".

    They are similar conceptually to a page fault and will be raised by
    the MPX hardware during both bounds violations or when the tables
    are not present. This patch handles those #BR exceptions for
    not-present tables by carving the space out of the normal processes
    address space (essentially calling the new mmap() interface indroduced
    earlier in this patch set.) and then pointing the bounds-directory
    over to it.

    The tables *need* to be accessed and controlled by userspace because
    the instructions for moving bounds in and out of them are extremely
    frequent. They potentially happen every time a register pointing to
    memory is dereferenced. Any direct kernel involvement (like a syscall)
    to access the tables would obviously destroy performance.

    ==== Why not do this in userspace? ====

    This patch is obviously doing this allocation in the kernel.
    However, MPX does not strictly *require* anything in the kernel.
    It can theoretically be done completely from userspace. Here are
    a few ways this *could* be done. I don't think any of them are
    practical in the real-world, but here they are.

    Q: Can virtual space simply be reserved for the bounds tables so
    that we never have to allocate them?
    A: As noted earlier, these tables are *HUGE*. An X-GB virtual
    area needs 4*X GB of virtual space, plus 2GB for the bounds
    directory. If we were to preallocate them for the 128TB of
    user virtual address space, we would need to reserve 512TB+2GB,
    which is larger than the entire virtual address space today.
    This means they can not be reserved ahead of time. Also, a
    single process's pre-popualated bounds directory consumes 2GB
    of virtual *AND* physical memory. IOW, it's completely
    infeasible to prepopulate bounds directories.

    Q: Can we preallocate bounds table space at the same time memory
    is allocated which might contain pointers that might eventually
    need bounds tables?
    A: This would work if we could hook the site of each and every
    memory allocation syscall. This can be done for small,
    constrained applications. But, it isn't practical at a larger
    scale since a given app has no way of controlling how all the
    parts of the app might allocate memory (think libraries). The
    kernel is really the only place to intercept these calls.

    Q: Could a bounds fault be handed to userspace and the tables
    allocated there in a signal handler instead of in the kernel?
    A: (thanks to tglx) mmap() is not on the list of safe async
    handler functions and even if mmap() would work it still
    requires locking or nasty tricks to keep track of the
    allocation state there.

    Having ruled out all of the userspace-only approaches for managing
    bounds tables that we could think of, we create them on demand in
    the kernel.

    Based-on-patch-by: Qiaowei Ren
    Signed-off-by: Dave Hansen
    Cc: linux-mm@kvack.org
    Cc: linux-mips@linux-mips.org
    Cc: Dave Hansen
    Link: http://lkml.kernel.org/r/20141114151829.AD4310DE@viggo.jf.intel.com
    Signed-off-by: Thomas Gleixner

    Dave Hansen
     

09 Oct, 2014

1 commit


09 Aug, 2014

1 commit

  • mm initialization on fork/exec is spread all over the place, which makes
    the code look inconsistent.

    We have mm_init(), which is supposed to init/nullify mm's internals, but
    it doesn't init all the fields it should:

    - on fork ->mmap,mm_rb,vmacache_seqnum,map_count,mm_cpumask,locked_vm
    are zeroed in dup_mmap();

    - on fork ->pmd_huge_pte is zeroed in dup_mm(), immediately before
    calling mm_init();

    - ->cpu_vm_mask_var ptr is initialized by mm_init_cpumask(), which is
    called before mm_init() on both fork and exec;

    - ->context is initialized by init_new_context(), which is called after
    mm_init() on both fork and exec;

    Let's consolidate all the initializations in mm_init() to make the code
    look cleaner.

    Signed-off-by: Vladimir Davydov
    Cc: Oleg Nesterov
    Cc: David Rientjes
    Cc: Christoph Lameter
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Vladimir Davydov
     

19 Jul, 2014

2 commits

  • Applying restrictive seccomp filter programs to large or diverse
    codebases often requires handling threads which may be started early in
    the process lifetime (e.g., by code that is linked in). While it is
    possible to apply permissive programs prior to process start up, it is
    difficult to further restrict the kernel ABI to those threads after that
    point.

    This change adds a new seccomp syscall flag to SECCOMP_SET_MODE_FILTER for
    synchronizing thread group seccomp filters at filter installation time.

    When calling seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC,
    filter) an attempt will be made to synchronize all threads in current's
    threadgroup to its new seccomp filter program. This is possible iff all
    threads are using a filter that is an ancestor to the filter current is
    attempting to synchronize to. NULL filters (where the task is running as
    SECCOMP_MODE_NONE) are also treated as ancestors allowing threads to be
    transitioned into SECCOMP_MODE_FILTER. If prctrl(PR_SET_NO_NEW_PRIVS,
    ...) has been set on the calling thread, no_new_privs will be set for
    all synchronized threads too. On success, 0 is returned. On failure,
    the pid of one of the failing threads will be returned and no filters
    will have been applied.

    The race conditions against another thread are:
    - requesting TSYNC (already handled by sighand lock)
    - performing a clone (already handled by sighand lock)
    - changing its filter (already handled by sighand lock)
    - calling exec (handled by cred_guard_mutex)
    The clone case is assisted by the fact that new threads will have their
    seccomp state duplicated from their parent before appearing on the tasklist.

    Holding cred_guard_mutex means that seccomp filters cannot be assigned
    while in the middle of another thread's exec (potentially bypassing
    no_new_privs or similar). The call to de_thread() may kill threads waiting
    for the mutex.

    Changes across threads to the filter pointer includes a barrier.

    Based on patches by Will Drewry.

    Suggested-by: Julien Tinnes
    Signed-off-by: Kees Cook
    Reviewed-by: Oleg Nesterov
    Reviewed-by: Andy Lutomirski

    Kees Cook
     
  • Since seccomp transitions between threads requires updates to the
    no_new_privs flag to be atomic, the flag must be part of an atomic flag
    set. This moves the nnp flag into a separate task field, and introduces
    accessors.

    Signed-off-by: Kees Cook
    Reviewed-by: Oleg Nesterov
    Reviewed-by: Andy Lutomirski

    Kees Cook
     

06 Jun, 2014

2 commits

  • perf tools like 'perf report' can aggregate samples by comm strings,
    which generally works. However, there are other potential use-cases.
    For example, to pair up 'calls' with 'returns' accurately (from branch
    events like Intel BTS) it is necessary to identify whether the process
    has exec'd. Although a comm event is generated when an 'exec' happens
    it is also generated whenever the comm string is changed on a whim
    (e.g. by prctl PR_SET_NAME). This patch adds a flag to the comm event
    to differentiate one case from the other.

    In order to determine whether the kernel supports the new flag, a
    selection bit named 'exec' is added to struct perf_event_attr. The
    bit does nothing but will cause perf_event_open() to fail if the bit
    is set on kernels that do not have it defined.

    Signed-off-by: Adrian Hunter
    Signed-off-by: Peter Zijlstra
    Link: http://lkml.kernel.org/r/537D9EBE.7030806@intel.com
    Cc: Paul Mackerras
    Cc: Dave Jones
    Cc: Arnaldo Carvalho de Melo
    Cc: David Ahern
    Cc: Jiri Olsa
    Cc: Alexander Viro
    Cc: Linus Torvalds
    Cc: linux-fsdevel@vger.kernel.org
    Cc: linux-kernel@vger.kernel.org
    Signed-off-by: Ingo Molnar

    Adrian Hunter
     
  • perf_event_comm() assumes that set_task_comm() is only called on
    exec(), and in particular that its only called on current.

    Neither are true, as Dave reported a WARN triggered by set_task_comm()
    being called on !current.

    Separate the exec() hook from the comm hook.

    Reported-by: Dave Jones
    Signed-off-by: Peter Zijlstra
    Cc: Alexander Viro
    Cc: Arnaldo Carvalho de Melo
    Cc: Linus Torvalds
    Cc: linux-fsdevel@vger.kernel.org
    Cc: linux-kernel@vger.kernel.org
    Link: http://lkml.kernel.org/r/20140521153219.GH5226@laptop.programming.kicks-ass.net
    [ Build fix. ]
    Signed-off-by: Ingo Molnar

    Peter Zijlstra
     

15 May, 2014

1 commit

  • Specify the maximum stack size for arches where the stack grows upward
    (parisc and metag) in asm/processor.h rather than hard coding in
    fs/exec.c so that metag can specify a smaller value of 256MB rather than
    1GB.

    This fixes a BUG on metag if the RLIMIT_STACK hard limit is increased
    beyond a safe value by root. E.g. when starting a process after running
    "ulimit -H -s unlimited" it will then attempt to use a stack size of the
    maximum 1GB which is far too big for metag's limited user virtual
    address space (stack_top is usually 0x3ffff000):

    BUG: failure at fs/exec.c:589/shift_arg_pages()!

    Signed-off-by: James Hogan
    Cc: Helge Deller
    Cc: "James E.J. Bottomley"
    Cc: linux-parisc@vger.kernel.org
    Cc: linux-metag@vger.kernel.org
    Cc: John David Anglin
    Cc: stable@vger.kernel.org # only needed for >= v3.9 (arch/metag)

    James Hogan
     

13 Apr, 2014

1 commit

  • Pull vfs updates from Al Viro:
    "The first vfs pile, with deep apologies for being very late in this
    window.

    Assorted cleanups and fixes, plus a large preparatory part of iov_iter
    work. There's a lot more of that, but it'll probably go into the next
    merge window - it *does* shape up nicely, removes a lot of
    boilerplate, gets rid of locking inconsistencie between aio_write and
    splice_write and I hope to get Kent's direct-io rewrite merged into
    the same queue, but some of the stuff after this point is having
    (mostly trivial) conflicts with the things already merged into
    mainline and with some I want more testing.

    This one passes LTP and xfstests without regressions, in addition to
    usual beating. BTW, readahead02 in ltp syscalls testsuite has started
    giving failures since "mm/readahead.c: fix readahead failure for
    memoryless NUMA nodes and limit readahead pages" - might be a false
    positive, might be a real regression..."

    * 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs: (63 commits)
    missing bits of "splice: fix racy pipe->buffers uses"
    cifs: fix the race in cifs_writev()
    ceph_sync_{,direct_}write: fix an oops on ceph_osdc_new_request() failure
    kill generic_file_buffered_write()
    ocfs2_file_aio_write(): switch to generic_perform_write()
    ceph_aio_write(): switch to generic_perform_write()
    xfs_file_buffered_aio_write(): switch to generic_perform_write()
    export generic_perform_write(), start getting rid of generic_file_buffer_write()
    generic_file_direct_write(): get rid of ppos argument
    btrfs_file_aio_write(): get rid of ppos
    kill the 5th argument of generic_file_buffered_write()
    kill the 4th argument of __generic_file_aio_write()
    lustre: don't open-code kernel_recvmsg()
    ocfs2: don't open-code kernel_recvmsg()
    drbd: don't open-code kernel_recvmsg()
    constify blk_rq_map_user_iov() and friends
    lustre: switch to kernel_sendmsg()
    ocfs2: don't open-code kernel_sendmsg()
    take iov_iter stuff to mm/iov_iter.c
    process_vm_access: tidy up a bit
    ...

    Linus Torvalds
     

08 Apr, 2014

1 commit

  • Starting from commit c4ad8f98bef7 ("execve: use 'struct filename *' for
    executable name passing") bprm->filename can not go away after
    flush_old_exec(), so we do not need to save the binary name in
    bprm->tcomm[] added by 96e02d158678 ("exec: fix use-after-free bug in
    setup_new_exec()").

    And there was never need for filename_to_taskname-like code, we can
    simply do set_task_comm(kbasename(filename).

    This patch has to change set_task_comm() and trace_task_rename() to
    accept "const char *", but I think this change is also good.

    Signed-off-by: Oleg Nesterov
    Cc: Heiko Carstens
    Cc: Steven Rostedt
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Oleg Nesterov