05 Jun, 2020

1 commit

  • The latest compiler expects slightly different function prototypes
    for the ubsan helpers:

    lib/ubsan.c:192:6: error: conflicting types for built-in function '__ubsan_handle_add_overflow'; expected 'void(void *, void *, void *)' [-Werror=builtin-declaration-mismatch]
    192 | void __ubsan_handle_add_overflow(struct overflow_data *data,
    | ^~~~~~~~~~~~~~~~~~~~~~~~~~~
    lib/ubsan.c:200:6: error: conflicting types for built-in function '__ubsan_handle_sub_overflow'; expected 'void(void *, void *, void *)' [-Werror=builtin-declaration-mismatch]
    200 | void __ubsan_handle_sub_overflow(struct overflow_data *data,
    | ^~~~~~~~~~~~~~~~~~~~~~~~~~~
    lib/ubsan.c:207:6: error: conflicting types for built-in function '__ubsan_handle_mul_overflow'; expected 'void(void *, void *, void *)' [-Werror=builtin-declaration-mismatch]
    207 | void __ubsan_handle_mul_overflow(struct overflow_data *data,
    | ^~~~~~~~~~~~~~~~~~~~~~~~~~~
    lib/ubsan.c:214:6: error: conflicting types for built-in function '__ubsan_handle_negate_overflow'; expected 'void(void *, void *)' [-Werror=builtin-declaration-mismatch]
    214 | void __ubsan_handle_negate_overflow(struct overflow_data *data,
    | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    lib/ubsan.c:234:6: error: conflicting types for built-in function '__ubsan_handle_divrem_overflow'; expected 'void(void *, void *, void *)' [-Werror=builtin-declaration-mismatch]
    234 | void __ubsan_handle_divrem_overflow(struct overflow_data *data,
    | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Change the Linux implementation to match these, using a local typed
    pointer.

    Signed-off-by: Arnd Bergmann
    Signed-off-by: Andrew Morton
    Reviewed-by: Kees Cook
    Cc: Andrey Ryabinin
    Cc: Herbert Xu
    Cc: Julien Grall
    Link: http://lkml.kernel.org/r/20200429185948.4189600-1-arnd@arndb.de
    Signed-off-by: Linus Torvalds

    Arnd Bergmann
     

08 Apr, 2020

2 commits

  • When syzbot tries to figure out how to deduplicate bug reports, it prefers
    seeing a hint about a specific bug type (we can do better than just
    "UBSAN"). This lifts the handler reason into the UBSAN report line that
    includes the file path that tripped a check. Unfortunately, UBSAN does
    not provide function names.

    Suggested-by: Dmitry Vyukov
    Signed-off-by: Kees Cook
    Signed-off-by: Andrew Morton
    Cc: Alexander Potapenko
    Cc: Andrey Konovalov
    Cc: Andrey Ryabinin
    Cc: Ard Biesheuvel
    Cc: Arnd Bergmann
    Cc: Dan Carpenter
    Cc: Elena Petrova
    Cc: "Gustavo A. R. Silva"
    Link: http://lkml.kernel.org/r/20200227193516.32566-7-keescook@chromium.org
    Link: https://lore.kernel.org/lkml/CACT4Y+bsLJ-wFx_TaXqax3JByUOWB3uk787LsyMVcfW6JzzGvg@mail.gmail.com
    Signed-off-by: Linus Torvalds

    Kees Cook
     
  • Syzkaller expects kernel warnings to panic when the panic_on_warn sysctl
    is set. More work is needed here to have UBSan reuse the WARN
    infrastructure, but for now, just check the flag manually.

    Signed-off-by: Kees Cook
    Signed-off-by: Andrew Morton
    Cc: Alexander Potapenko
    Cc: Andrey Konovalov
    Cc: Andrey Ryabinin
    Cc: Ard Biesheuvel
    Cc: Arnd Bergmann
    Cc: Dan Carpenter
    Cc: Dmitry Vyukov
    Cc: Elena Petrova
    Cc: "Gustavo A. R. Silva"
    Link: https://lore.kernel.org/lkml/CACT4Y+bsLJ-wFx_TaXqax3JByUOWB3uk787LsyMVcfW6JzzGvg@mail.gmail.com
    Link: http://lkml.kernel.org/r/20200227193516.32566-5-keescook@chromium.org
    Signed-off-by: Linus Torvalds

    Kees Cook
     

05 Dec, 2019

1 commit

  • At the moment, UBSAN report will be serialized using a spin_lock(). On
    RT-systems, spinlocks are turned to rt_spin_lock and may sleep. This
    will result to the following splat if the undefined behavior is in a
    context that can sleep:

    BUG: sleeping function called from invalid context at /src/linux/kernel/locking/rtmutex.c:968
    in_atomic(): 1, irqs_disabled(): 128, pid: 3447, name: make
    1 lock held by make/3447:
    #0: 000000009a966332 (&mm->mmap_sem){++++}, at: do_page_fault+0x140/0x4f8
    irq event stamp: 6284
    hardirqs last enabled at (6283): [] _raw_spin_unlock_irqrestore+0x90/0xa0
    hardirqs last disabled at (6284): [] _raw_spin_lock_irqsave+0x30/0x78
    softirqs last enabled at (2430): [] fpsimd_restore_current_state+0x60/0xe8
    softirqs last disabled at (2427): [] fpsimd_restore_current_state+0x28/0xe8
    Preemption disabled at:
    [] rt_mutex_futex_unlock+0x4c/0xb0
    CPU: 3 PID: 3447 Comm: make Tainted: G W 5.2.14-rt7-01890-ge6e057589653 #911
    Call trace:
    dump_backtrace+0x0/0x148
    show_stack+0x14/0x20
    dump_stack+0xbc/0x104
    ___might_sleep+0x154/0x210
    rt_spin_lock+0x68/0xa0
    ubsan_prologue+0x30/0x68
    handle_overflow+0x64/0xe0
    __ubsan_handle_add_overflow+0x10/0x18
    __lock_acquire+0x1c28/0x2a28
    lock_acquire+0xf0/0x370
    _raw_spin_lock_irqsave+0x58/0x78
    rt_mutex_futex_unlock+0x4c/0xb0
    rt_spin_unlock+0x28/0x70
    get_page_from_freelist+0x428/0x2b60
    __alloc_pages_nodemask+0x174/0x1708
    alloc_pages_vma+0x1ac/0x238
    __handle_mm_fault+0x4ac/0x10b0
    handle_mm_fault+0x1d8/0x3b0
    do_page_fault+0x1c8/0x4f8
    do_translation_fault+0xb8/0xe0
    do_mem_abort+0x3c/0x98
    el0_da+0x20/0x24

    The spin_lock() will protect against multiple CPUs to output a report
    together, I guess to prevent them from being interleaved. However, they
    can still interleave with other messages (and even splat from
    __might_sleep).

    So the lock usefulness seems pretty limited. Rather than trying to
    accomodate RT-system by switching to a raw_spin_lock(), the lock is now
    completely dropped.

    Link: http://lkml.kernel.org/r/20190920100835.14999-1-julien.grall@arm.com
    Signed-off-by: Julien Grall
    Reported-by: Andre Przywara
    Acked-by: Andrey Ryabinin
    Cc: Thomas Gleixner
    Cc: Sebastian Andrzej Siewior
    Cc: Steven Rostedt
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Julien Grall
     

27 Nov, 2019

1 commit

  • Pull x86 asm updates from Ingo Molnar:
    "The main changes in this cycle were:

    - Cross-arch changes to move the linker sections for NOTES and
    EXCEPTION_TABLE into the RO_DATA area, where they belong on most
    architectures. (Kees Cook)

    - Switch the x86 linker fill byte from x90 (NOP) to 0xcc (INT3), to
    trap jumps into the middle of those padding areas instead of
    sliding execution. (Kees Cook)

    - A thorough cleanup of symbol definitions within x86 assembler code.
    The rather randomly named macros got streamlined around a
    (hopefully) straightforward naming scheme:

    SYM_START(name, linkage, align...)
    SYM_END(name, sym_type)

    SYM_FUNC_START(name)
    SYM_FUNC_END(name)

    SYM_CODE_START(name)
    SYM_CODE_END(name)

    SYM_DATA_START(name)
    SYM_DATA_END(name)

    etc - with about three times of these basic primitives with some
    label, local symbol or attribute variant, expressed via postfixes.

    No change in functionality intended. (Jiri Slaby)

    - Misc other changes, cleanups and smaller fixes"

    * 'x86-asm-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (67 commits)
    x86/entry/64: Remove pointless jump in paranoid_exit
    x86/entry/32: Remove unused resume_userspace label
    x86/build/vdso: Remove meaningless CFLAGS_REMOVE_*.o
    m68k: Convert missed RODATA to RO_DATA
    x86/vmlinux: Use INT3 instead of NOP for linker fill bytes
    x86/mm: Report actual image regions in /proc/iomem
    x86/mm: Report which part of kernel image is freed
    x86/mm: Remove redundant address-of operators on addresses
    xtensa: Move EXCEPTION_TABLE to RO_DATA segment
    powerpc: Move EXCEPTION_TABLE to RO_DATA segment
    parisc: Move EXCEPTION_TABLE to RO_DATA segment
    microblaze: Move EXCEPTION_TABLE to RO_DATA segment
    ia64: Move EXCEPTION_TABLE to RO_DATA segment
    h8300: Move EXCEPTION_TABLE to RO_DATA segment
    c6x: Move EXCEPTION_TABLE to RO_DATA segment
    arm64: Move EXCEPTION_TABLE to RO_DATA segment
    alpha: Move EXCEPTION_TABLE to RO_DATA segment
    x86/vmlinux: Move EXCEPTION_TABLE to RO_DATA segment
    x86/vmlinux: Actually use _etext for the end of the text segment
    vmlinux.lds.h: Allow EXCEPTION_TABLE to live in RO_DATA
    ...

    Linus Torvalds
     

17 Nov, 2019

1 commit

  • In order to use 128-bit integer arithmetic in C code, the architecture
    needs to have declared support for it by setting ARCH_SUPPORTS_INT128,
    and it requires a version of the toolchain that supports this at build
    time. This is why all existing tests for ARCH_SUPPORTS_INT128 also test
    whether __SIZEOF_INT128__ is defined, since this is only the case for
    compilers that can support 128-bit integers.

    Let's fold this additional test into the Kconfig declaration of
    ARCH_SUPPORTS_INT128 so that we can also use the symbol in Makefiles,
    e.g., to decide whether a certain object needs to be included in the
    first place.

    Cc: Masahiro Yamada
    Signed-off-by: Ard Biesheuvel
    Signed-off-by: Herbert Xu

    Ard Biesheuvel
     

28 Oct, 2019

1 commit

  • The new check_zeroed_user() function uses variable shifts inside of a
    user_access_begin()/user_access_end() section and that results in GCC
    emitting __ubsan_handle_shift_out_of_bounds() calls, even though
    through value range analysis it would be able to see that the UB in
    question is impossible.

    Annotate and whitelist this UBSAN function; continued use of
    user_access_begin()/user_access_end() will undoubtedly result in
    further uses of function.

    Reported-by: Randy Dunlap
    Tested-by: Randy Dunlap
    Signed-off-by: Peter Zijlstra (Intel)
    Acked-by: Randy Dunlap
    Acked-by: Christian Brauner
    Cc: Josh Poimboeuf
    Cc: Linus Torvalds
    Cc: Peter Zijlstra
    Cc: Stephen Rothwell
    Cc: Thomas Gleixner
    Cc: cyphar@cyphar.com
    Cc: keescook@chromium.org
    Cc: linux@rasmusvillemoes.dk
    Fixes: f5a1a536fa14 ("lib: introduce copy_struct_from_user() helper")
    Link: https://lkml.kernel.org/r/20191021131149.GA19358@hirez.programming.kicks-ass.net
    Signed-off-by: Ingo Molnar

    Peter Zijlstra
     

19 Jun, 2019

1 commit

  • Based on 2 normalized pattern(s):

    this program is free software you can redistribute it and or modify
    it under the terms of the gnu general public license version 2 as
    published by the free software foundation

    this program is free software you can redistribute it and or modify
    it under the terms of the gnu general public license version 2 as
    published by the free software foundation #

    extracted by the scancode license scanner the SPDX license identifier

    GPL-2.0-only

    has been chosen to replace the boilerplate/reference in 4122 file(s).

    Signed-off-by: Thomas Gleixner
    Reviewed-by: Enrico Weigelt
    Reviewed-by: Kate Stewart
    Reviewed-by: Allison Randal
    Cc: linux-spdx@vger.kernel.org
    Link: https://lkml.kernel.org/r/20190604081206.933168790@linutronix.de
    Signed-off-by: Greg Kroah-Hartman

    Thomas Gleixner
     

07 May, 2019

3 commits

  • Pull objtool updates from Ingo Molnar:
    "This is a series from Peter Zijlstra that adds x86 build-time uaccess
    validation of SMAP to objtool, which will detect and warn about the
    following uaccess API usage bugs and weirdnesses:

    - call to %s() with UACCESS enabled
    - return with UACCESS enabled
    - return with UACCESS disabled from a UACCESS-safe function
    - recursive UACCESS enable
    - redundant UACCESS disable
    - UACCESS-safe disables UACCESS

    As it turns out not leaking uaccess permissions outside the intended
    uaccess functionality is hard when the interfaces are complex and when
    such bugs are mostly dormant.

    As a bonus we now also check the DF flag. We had at least one
    high-profile bug in that area in the early days of Linux, and the
    checking is fairly simple. The checks performed and warnings emitted
    are:

    - call to %s() with DF set
    - return with DF set
    - return with modified stack frame
    - recursive STD
    - redundant CLD

    It's all x86-only for now, but later on this can also be used for PAN
    on ARM and objtool is fairly cross-platform in principle.

    While all warnings emitted by this new checking facility that got
    reported to us were fixed, there might be GCC version dependent
    warnings that were not reported yet - which we'll address, should they
    trigger.

    The warnings are non-fatal build warnings"

    * 'core-objtool-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (27 commits)
    mm/uaccess: Use 'unsigned long' to placate UBSAN warnings on older GCC versions
    x86/uaccess: Dont leak the AC flag into __put_user() argument evaluation
    sched/x86_64: Don't save flags on context switch
    objtool: Add Direction Flag validation
    objtool: Add UACCESS validation
    objtool: Fix sibling call detection
    objtool: Rewrite alt->skip_orig
    objtool: Add --backtrace support
    objtool: Rewrite add_ignores()
    objtool: Handle function aliases
    objtool: Set insn->func for alternatives
    x86/uaccess, kcov: Disable stack protector
    x86/uaccess, ftrace: Fix ftrace_likely_update() vs. SMAP
    x86/uaccess, ubsan: Fix UBSAN vs. SMAP
    x86/uaccess, kasan: Fix KASAN vs SMAP
    x86/smap: Ditch __stringify()
    x86/uaccess: Introduce user_access_{save,restore}()
    x86/uaccess, signal: Fix AC=1 bloat
    x86/uaccess: Always inline user_access_begin()
    x86/uaccess, xen: Suppress SMAP warnings
    ...

    Linus Torvalds
     
  • The kernel the kernel is built with -Wvla for some time, so is not
    supposed to have any variable length arrays. Remove vla bounds checking
    from ubsan since it's useless now.

    Signed-off-by: Andrey Ryabinin
    Signed-off-by: Linus Torvalds

    Andrey Ryabinin
     
  • Building lib/ubsan.c with gcc-9 results in a ton of nasty warnings like
    this one:

    lib/ubsan.c warning: conflicting types for built-in function
    ‘__ubsan_handle_negate_overflow’; expected ‘void(void *, void *)’ [-Wbuiltin-declaration-mismatch]

    The kernel's declarations of __ubsan_handle_*() often uses 'unsigned
    long' types in parameters while GCC these parameters as 'void *' types,
    hence the mismatch.

    Fix this by using 'void *' to match GCC's declarations.

    Reported-by: Linus Torvalds
    Signed-off-by: Andrey Ryabinin
    Fixes: c6d308534aef ("UBSAN: run-time undefined behavior sanity checker")
    Cc:
    Signed-off-by: Linus Torvalds

    Andrey Ryabinin
     

03 Apr, 2019

1 commit

  • UBSAN can insert extra code in random locations; including AC=1
    sections. Typically this code is not safe and needs wrapping.

    So far, only __ubsan_handle_type_mismatch* have been observed in AC=1
    sections and therefore only those are annotated.

    Signed-off-by: Peter Zijlstra (Intel)
    Cc: Borislav Petkov
    Cc: Dmitry Vyukov
    Cc: Josh Poimboeuf
    Cc: Linus Torvalds
    Cc: Peter Zijlstra
    Cc: Thomas Gleixner
    Signed-off-by: Ingo Molnar

    Peter Zijlstra
     

19 Nov, 2018

1 commit

  • gcc-8 complains about the prototype for this function:

    lib/ubsan.c:432:1: error: ignoring attribute 'noreturn' in declaration of a built-in function '__ubsan_handle_builtin_unreachable' because it conflicts with attribute 'const' [-Werror=attributes]

    This is actually a GCC's bug. In GCC internals
    __ubsan_handle_builtin_unreachable() declared with both 'noreturn' and
    'const' attributes instead of only 'noreturn':

    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84210

    Workaround this by removing the noreturn attribute.

    [aryabinin: add information about GCC bug in changelog]
    Link: http://lkml.kernel.org/r/20181107144516.4587-1-aryabinin@virtuozzo.com
    Signed-off-by: Arnd Bergmann
    Signed-off-by: Andrey Ryabinin
    Acked-by: Olof Johansson
    Cc:
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Arnd Bergmann
     

07 Feb, 2018

3 commits

  • Similarly to type mismatch checks, new GCC 8.x and Clang also changed for
    ABI for returns_nonnull checks. While we can update our code to conform
    the new ABI it's more reasonable to just remove it. Because it's just
    dead code, we don't have any single user of returns_nonnull attribute in
    the whole kernel.

    And AFAIU the advantage that this attribute could bring would be mitigated
    by -fno-delete-null-pointer-checks cflag that we use to build the kernel.
    So it's unlikely we will have a lot of returns_nonnull attribute in
    future.

    So let's just remove the code, it has no use.

    [aryabinin@virtuozzo.com: fix warning]
    Link: http://lkml.kernel.org/r/20180122165711.11510-1-aryabinin@virtuozzo.com
    Link: http://lkml.kernel.org/r/20180119152853.16806-2-aryabinin@virtuozzo.com
    Signed-off-by: Andrey Ryabinin
    Cc: Sodagudi Prasad
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrey Ryabinin
     
  • UBSAN=y fails to build with new GCC/clang:

    arch/x86/kernel/head64.o: In function `sanitize_boot_params':
    arch/x86/include/asm/bootparam_utils.h:37: undefined reference to `__ubsan_handle_type_mismatch_v1'

    because Clang and GCC 8 slightly changed ABI for 'type mismatch' errors.
    Compiler now uses new __ubsan_handle_type_mismatch_v1() function with
    slightly modified 'struct type_mismatch_data'.

    Let's add new 'struct type_mismatch_data_common' which is independent from
    compiler's layout of 'struct type_mismatch_data'. And make
    __ubsan_handle_type_mismatch[_v1]() functions transform compiler-dependent
    type mismatch data to our internal representation. This way, we can
    support both old and new compilers with minimal amount of change.

    Link: http://lkml.kernel.org/r/20180119152853.16806-1-aryabinin@virtuozzo.com
    Signed-off-by: Andrey Ryabinin
    Reported-by: Sodagudi Prasad
    Cc: [4.5+]
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrey Ryabinin
     
  • A vist from the spelling fairy.

    Cc: David Laight
    Cc: Andrey Ryabinin
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrew Morton
     

03 Aug, 2016

1 commit

  • handle_object_size_mismatch() used %pk to format a kernel pointer with
    pr_err(). This seemed to be a misspelling for %pK, but using this to
    format a kernel pointer does not make much sence here.

    Therefore use %p instead, like in handle_missaligned_access().

    Link: http://lkml.kernel.org/r/20160730083010.11569-1-nicolas.iooss_linux@m4x.org
    Signed-off-by: Nicolas Iooss
    Acked-by: Andrey Ryabinin
    Cc: Joe Perches
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Nicolas Iooss
     

21 Jan, 2016

1 commit

  • UBSAN uses compile-time instrumentation to catch undefined behavior
    (UB). Compiler inserts code that perform certain kinds of checks before
    operations that could cause UB. If check fails (i.e. UB detected)
    __ubsan_handle_* function called to print error message.

    So the most of the work is done by compiler. This patch just implements
    ubsan handlers printing errors.

    GCC has this capability since 4.9.x [1] (see -fsanitize=undefined
    option and its suboptions).
    However GCC 5.x has more checkers implemented [2].
    Article [3] has a bit more details about UBSAN in the GCC.

    [1] - https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/Debugging-Options.html
    [2] - https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html
    [3] - http://developerblog.redhat.com/2014/10/16/gcc-undefined-behavior-sanitizer-ubsan/

    Issues which UBSAN has found thus far are:

    Found bugs:

    * out-of-bounds access - 97840cb67ff5 ("netfilter: nfnetlink: fix
    insufficient validation in nfnetlink_bind")

    undefined shifts:

    * d48458d4a768 ("jbd2: use a better hash function for the revoke
    table")

    * 10632008b9e1 ("clockevents: Prevent shift out of bounds")

    * 'x << -1' shift in ext4 -
    http://lkml.kernel.org/r/

    * undefined rol32(0) -
    http://lkml.kernel.org/r/

    * undefined dirty_ratelimit calculation -
    http://lkml.kernel.org/r/

    * undefined roundown_pow_of_two(0) -
    http://lkml.kernel.org/r/

    * [WONTFIX] undefined shift in __bpf_prog_run -
    http://lkml.kernel.org/r/

    WONTFIX here because it should be fixed in bpf program, not in kernel.

    signed overflows:

    * 32a8df4e0b33f ("sched: Fix odd values in effective_load()
    calculations")

    * mul overflow in ntp -
    http://lkml.kernel.org/r/

    * incorrect conversion into rtc_time in rtc_time64_to_tm() -
    http://lkml.kernel.org/r/

    * unvalidated timespec in io_getevents() -
    http://lkml.kernel.org/r/

    * [NOTABUG] signed overflow in ktime_add_safe() -
    http://lkml.kernel.org/r/

    [akpm@linux-foundation.org: fix unused local warning]
    [akpm@linux-foundation.org: fix __int128 build woes]
    Signed-off-by: Andrey Ryabinin
    Cc: Peter Zijlstra
    Cc: Sasha Levin
    Cc: Randy Dunlap
    Cc: Rasmus Villemoes
    Cc: Jonathan Corbet
    Cc: Michal Marek
    Cc: Thomas Gleixner
    Cc: Ingo Molnar
    Cc: "H. Peter Anvin"
    Cc: Yury Gribov
    Cc: Dmitry Vyukov
    Cc: Konstantin Khlebnikov
    Cc: Kostya Serebryany
    Cc: Johannes Berg
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrey Ryabinin