01 Dec, 2016

2 commits

  • Gcc revision 241896 implements use-after-scope detection. Will be
    available in gcc 7. Support it in KASAN.

    Gcc emits 2 new callbacks to poison/unpoison large stack objects when
    they go in/out of scope. Implement the callbacks and add a test.

    [dvyukov@google.com: v3]
    Link: http://lkml.kernel.org/r/1479998292-144502-1-git-send-email-dvyukov@google.com
    Link: http://lkml.kernel.org/r/1479226045-145148-1-git-send-email-dvyukov@google.com
    Signed-off-by: Dmitry Vyukov
    Acked-by: Andrey Ryabinin
    Cc: Alexander Potapenko
    Cc: [4.0+]
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Dmitry Vyukov
     
  • kasan_global struct is part of compiler/runtime ABI. gcc revision
    241983 has added a new field to kasan_global struct. Update kernel
    definition of kasan_global struct to include the new field.

    Without this patch KASAN is broken with gcc 7.

    Link: http://lkml.kernel.org/r/1479219743-28682-1-git-send-email-dvyukov@google.com
    Signed-off-by: Dmitry Vyukov
    Acked-by: Andrey Ryabinin
    Cc: Alexander Potapenko
    Cc: [4.0+]
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Dmitry Vyukov
     

16 Oct, 2016

1 commit

  • I observed false KSAN positives in the sctp code, when
    sctp uses jprobe_return() in jsctp_sf_eat_sack().

    The stray 0xf4 in shadow memory are stack redzones:

    [ ] ==================================================================
    [ ] BUG: KASAN: stack-out-of-bounds in memcmp+0xe9/0x150 at addr ffff88005e48f480
    [ ] Read of size 1 by task syz-executor/18535
    [ ] page:ffffea00017923c0 count:0 mapcount:0 mapping: (null) index:0x0
    [ ] flags: 0x1fffc0000000000()
    [ ] page dumped because: kasan: bad access detected
    [ ] CPU: 1 PID: 18535 Comm: syz-executor Not tainted 4.8.0+ #28
    [ ] Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
    [ ] ffff88005e48f2d0 ffffffff82d2b849 ffffffff0bc91e90 fffffbfff10971e8
    [ ] ffffed000bc91e90 ffffed000bc91e90 0000000000000001 0000000000000000
    [ ] ffff88005e48f480 ffff88005e48f350 ffffffff817d3169 ffff88005e48f370
    [ ] Call Trace:
    [ ] [] dump_stack+0x12e/0x185
    [ ] [] kasan_report+0x489/0x4b0
    [ ] [] __asan_report_load1_noabort+0x19/0x20
    [ ] [] memcmp+0xe9/0x150
    [ ] [] depot_save_stack+0x176/0x5c0
    [ ] [] save_stack+0xb1/0xd0
    [ ] [] kasan_slab_free+0x72/0xc0
    [ ] [] kfree+0xc8/0x2a0
    [ ] [] skb_free_head+0x79/0xb0
    [ ] [] skb_release_data+0x37a/0x420
    [ ] [] skb_release_all+0x4f/0x60
    [ ] [] consume_skb+0x138/0x370
    [ ] [] sctp_chunk_put+0xcb/0x180
    [ ] [] sctp_chunk_free+0x58/0x70
    [ ] [] sctp_inq_pop+0x68f/0xef0
    [ ] [] sctp_assoc_bh_rcv+0xd6/0x4b0
    [ ] [] sctp_inq_push+0x131/0x190
    [ ] [] sctp_backlog_rcv+0xe9/0xa20
    [ ... ]
    [ ] Memory state around the buggy address:
    [ ] ffff88005e48f380: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    [ ] ffff88005e48f400: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    [ ] >ffff88005e48f480: f4 f4 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    [ ] ^
    [ ] ffff88005e48f500: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    [ ] ffff88005e48f580: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    [ ] ==================================================================

    KASAN stack instrumentation poisons stack redzones on function entry
    and unpoisons them on function exit. If a function exits abnormally
    (e.g. with a longjmp like jprobe_return()), stack redzones are left
    poisoned. Later this leads to random KASAN false reports.

    Unpoison stack redzones in the frames we are going to jump over
    before doing actual longjmp in jprobe_return().

    Signed-off-by: Dmitry Vyukov
    Acked-by: Masami Hiramatsu
    Reviewed-by: Mark Rutland
    Cc: Mark Rutland
    Cc: Catalin Marinas
    Cc: Andrey Ryabinin
    Cc: Lorenzo Pieralisi
    Cc: Alexander Potapenko
    Cc: Will Deacon
    Cc: Andrew Morton
    Cc: Ananth N Mavinakayanahalli
    Cc: Anil S Keshavamurthy
    Cc: "David S. Miller"
    Cc: Masami Hiramatsu
    Cc: kasan-dev@googlegroups.com
    Cc: surovegin@google.com
    Cc: rostedt@goodmis.org
    Link: http://lkml.kernel.org/r/1476454043-101898-1-git-send-email-dvyukov@google.com
    Signed-off-by: Ingo Molnar

    Dmitry Vyukov
     

12 Aug, 2016

1 commit

  • It's quite unlikely that the user will so little memory that the per-CPU
    quarantines won't fit into the given fraction of the available memory.
    Even in that case he won't be able to do anything with the information
    given in the warning.

    Link: http://lkml.kernel.org/r/1470929182-101413-1-git-send-email-glider@google.com
    Signed-off-by: Alexander Potapenko
    Acked-by: Andrey Ryabinin
    Cc: Dmitry Vyukov
    Cc: Andrey Konovalov
    Cc: Christoph Lameter
    Cc: Joonsoo Kim
    Cc: Kuthonuzo Luruo
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Alexander Potapenko
     

03 Aug, 2016

7 commits

  • If the total amount of memory assigned to quarantine is less than the
    amount of memory assigned to per-cpu quarantines, |new_quarantine_size|
    may overflow. Instead, set it to zero.

    [akpm@linux-foundation.org: cleanup: use WARN_ONCE return value]
    Link: http://lkml.kernel.org/r/1470063563-96266-1-git-send-email-glider@google.com
    Fixes: 55834c59098d ("mm: kasan: initial memory quarantine implementation")
    Signed-off-by: Alexander Potapenko
    Reported-by: Dmitry Vyukov
    Cc: Andrey Ryabinin
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Alexander Potapenko
     
  • Currently we just dump stack in case of double free bug.
    Let's dump all info about the object that we have.

    [aryabinin@virtuozzo.com: change double free message per Alexander]
    Link: http://lkml.kernel.org/r/1470153654-30160-1-git-send-email-aryabinin@virtuozzo.com
    Link: http://lkml.kernel.org/r/1470062715-14077-6-git-send-email-aryabinin@virtuozzo.com
    Signed-off-by: Andrey Ryabinin
    Cc: Alexander Potapenko
    Cc: Dmitry Vyukov
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrey Ryabinin
     
  • The state of object currently tracked in two places - shadow memory, and
    the ->state field in struct kasan_alloc_meta. We can get rid of the
    latter. The will save us a little bit of memory. Also, this allow us
    to move free stack into struct kasan_alloc_meta, without increasing
    memory consumption. So now we should always know when the last time the
    object was freed. This may be useful for long delayed use-after-free
    bugs.

    As a side effect this fixes following UBSAN warning:
    UBSAN: Undefined behaviour in mm/kasan/quarantine.c:102:13
    member access within misaligned address ffff88000d1efebc for type 'struct qlist_node'
    which requires 8 byte alignment

    Link: http://lkml.kernel.org/r/1470062715-14077-5-git-send-email-aryabinin@virtuozzo.com
    Reported-by: kernel test robot
    Signed-off-by: Andrey Ryabinin
    Cc: Alexander Potapenko
    Cc: Dmitry Vyukov
    Cc: Christoph Lameter
    Cc: Pekka Enberg
    Cc: David Rientjes
    Cc: Joonsoo Kim
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrey Ryabinin
     
  • Size of slab object already stored in cache->object_size.

    Note, that kmalloc() internally rounds up size of allocation, so
    object_size may be not equal to alloc_size, but, usually we don't need
    to know the exact size of allocated object. In case if we need that
    information, we still can figure it out from the report. The dump of
    shadow memory allows to identify the end of allocated memory, and
    thereby the exact allocation size.

    Link: http://lkml.kernel.org/r/1470062715-14077-4-git-send-email-aryabinin@virtuozzo.com
    Signed-off-by: Andrey Ryabinin
    Cc: Alexander Potapenko
    Cc: Dmitry Vyukov
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrey Ryabinin
     
  • SLUB doesn't require disabled interrupts to call ___cache_free().

    Link: http://lkml.kernel.org/r/1470062715-14077-3-git-send-email-aryabinin@virtuozzo.com
    Signed-off-by: Andrey Ryabinin
    Acked-by: Alexander Potapenko
    Cc: Dmitry Vyukov
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrey Ryabinin
     
  • Currently we call quarantine_reduce() for ___GFP_KSWAPD_RECLAIM (implied
    by __GFP_RECLAIM) allocation. So, basically we call it on almost every
    allocation. quarantine_reduce() sometimes is heavy operation, and
    calling it with disabled interrupts may trigger hard LOCKUP:

    NMI watchdog: Watchdog detected hard LOCKUP on cpu 2irq event stamp: 1411258
    Call Trace:
    dump_stack+0x68/0x96
    watchdog_overflow_callback+0x15b/0x190
    __perf_event_overflow+0x1b1/0x540
    perf_event_overflow+0x14/0x20
    intel_pmu_handle_irq+0x36a/0xad0
    perf_event_nmi_handler+0x2c/0x50
    nmi_handle+0x128/0x480
    default_do_nmi+0xb2/0x210
    do_nmi+0x1aa/0x220
    end_repeat_nmi+0x1a/0x1e
    <> __kernel_text_address+0x86/0xb0
    print_context_stack+0x7b/0x100
    dump_trace+0x12b/0x350
    save_stack_trace+0x2b/0x50
    set_track+0x83/0x140
    free_debug_processing+0x1aa/0x420
    __slab_free+0x1d6/0x2e0
    ___cache_free+0xb6/0xd0
    qlist_free_all+0x83/0x100
    quarantine_reduce+0x177/0x1b0
    kasan_kmalloc+0xf3/0x100

    Reduce the quarantine_reduce iff direct reclaim is allowed.

    Fixes: 55834c59098d("mm: kasan: initial memory quarantine implementation")
    Link: http://lkml.kernel.org/r/1470062715-14077-2-git-send-email-aryabinin@virtuozzo.com
    Signed-off-by: Andrey Ryabinin
    Reported-by: Dave Jones
    Acked-by: Alexander Potapenko
    Cc: Dmitry Vyukov
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrey Ryabinin
     
  • Once an object is put into quarantine, we no longer own it, i.e. object
    could leave the quarantine and be reallocated. So having set_track()
    call after the quarantine_put() may corrupt slab objects.

    BUG kmalloc-4096 (Not tainted): Poison overwritten
    -----------------------------------------------------------------------------
    Disabling lock debugging due to kernel taint
    INFO: 0xffff8804540de850-0xffff8804540de857. First byte 0xb5 instead of 0x6b
    ...
    INFO: Freed in qlist_free_all+0x42/0x100 age=75 cpu=3 pid=24492
    __slab_free+0x1d6/0x2e0
    ___cache_free+0xb6/0xd0
    qlist_free_all+0x83/0x100
    quarantine_reduce+0x177/0x1b0
    kasan_kmalloc+0xf3/0x100
    kasan_slab_alloc+0x12/0x20
    kmem_cache_alloc+0x109/0x3e0
    mmap_region+0x53e/0xe40
    do_mmap+0x70f/0xa50
    vm_mmap_pgoff+0x147/0x1b0
    SyS_mmap_pgoff+0x2c7/0x5b0
    SyS_mmap+0x1b/0x30
    do_syscall_64+0x1a0/0x4e0
    return_from_SYSCALL_64+0x0/0x7a
    INFO: Slab 0xffffea0011503600 objects=7 used=7 fp=0x (null) flags=0x8000000000004080
    INFO: Object 0xffff8804540de848 @offset=26696 fp=0xffff8804540dc588
    Redzone ffff8804540de840: bb bb bb bb bb bb bb bb ........
    Object ffff8804540de848: 6b 6b 6b 6b 6b 6b 6b 6b b5 52 00 00 f2 01 60 cc kkkkkkkk.R....`.

    Similarly, poisoning after the quarantine_put() leads to false positive
    use-after-free reports:

    BUG: KASAN: use-after-free in anon_vma_interval_tree_insert+0x304/0x430 at addr ffff880405c540a0
    Read of size 8 by task trinity-c0/3036
    CPU: 0 PID: 3036 Comm: trinity-c0 Not tainted 4.7.0-think+ #9
    Call Trace:
    dump_stack+0x68/0x96
    kasan_report_error+0x222/0x600
    __asan_report_load8_noabort+0x61/0x70
    anon_vma_interval_tree_insert+0x304/0x430
    anon_vma_chain_link+0x91/0xd0
    anon_vma_clone+0x136/0x3f0
    anon_vma_fork+0x81/0x4c0
    copy_process.part.47+0x2c43/0x5b20
    _do_fork+0x16d/0xbd0
    SyS_clone+0x19/0x20
    do_syscall_64+0x1a0/0x4e0
    entry_SYSCALL64_slow_path+0x25/0x25

    Fix this by putting an object in the quarantine after all other
    operations.

    Fixes: 80a9201a5965 ("mm, kasan: switch SLUB to stackdepot, enable memory quarantine for SLUB")
    Link: http://lkml.kernel.org/r/1470062715-14077-1-git-send-email-aryabinin@virtuozzo.com
    Signed-off-by: Andrey Ryabinin
    Reported-by: Dave Jones
    Reported-by: Vegard Nossum
    Reported-by: Sasha Levin
    Acked-by: Alexander Potapenko
    Cc: Dmitry Vyukov
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrey Ryabinin
     

29 Jul, 2016

1 commit

  • For KASAN builds:
    - switch SLUB allocator to using stackdepot instead of storing the
    allocation/deallocation stacks in the objects;
    - change the freelist hook so that parts of the freelist can be put
    into the quarantine.

    [aryabinin@virtuozzo.com: fixes]
    Link: http://lkml.kernel.org/r/1468601423-28676-1-git-send-email-aryabinin@virtuozzo.com
    Link: http://lkml.kernel.org/r/1468347165-41906-3-git-send-email-glider@google.com
    Signed-off-by: Alexander Potapenko
    Cc: Andrey Konovalov
    Cc: Christoph Lameter
    Cc: Dmitry Vyukov
    Cc: Steven Rostedt (Red Hat)
    Cc: Joonsoo Kim
    Cc: Kostya Serebryany
    Cc: Andrey Ryabinin
    Cc: Kuthonuzo Luruo
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Alexander Potapenko
     

15 Jul, 2016

1 commit

  • There are two bugs on qlist_move_cache(). One is that qlist's tail
    isn't set properly. curr->next can be NULL since it is singly linked
    list and NULL value on tail is invalid if there is one item on qlist.
    Another one is that if cache is matched, qlist_put() is called and it
    will set curr->next to NULL. It would cause to stop the loop
    prematurely.

    These problems come from complicated implementation so I'd like to
    re-implement it completely. Implementation in this patch is really
    simple. Iterate all qlist_nodes and put them to appropriate list.

    Unfortunately, I got this bug sometime ago and lose oops message. But,
    the bug looks trivial and no need to attach oops.

    Fixes: 55834c59098d ("mm: kasan: initial memory quarantine implementation")
    Link: http://lkml.kernel.org/r/1467766348-22419-1-git-send-email-iamjoonsoo.kim@lge.com
    Signed-off-by: Joonsoo Kim
    Reviewed-by: Dmitry Vyukov
    Acked-by: Andrey Ryabinin
    Acked-by: Alexander Potapenko
    Cc: Kuthonuzo Luruo
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Joonsoo Kim
     

25 Jun, 2016

1 commit

  • Currently we may put reserved by mempool elements into quarantine via
    kasan_kfree(). This is totally wrong since quarantine may really free
    these objects. So when mempool will try to use such element,
    use-after-free will happen. Or mempool may decide that it no longer
    need that element and double-free it.

    So don't put object into quarantine in kasan_kfree(), just poison it.
    Rename kasan_kfree() to kasan_poison_kfree() to respect that.

    Also, we shouldn't use kasan_slab_alloc()/kasan_krealloc() in
    kasan_unpoison_element() because those functions may update allocation
    stacktrace. This would be wrong for the most of the remove_element call
    sites.

    (The only call site where we may want to update alloc stacktrace is
    in mempool_alloc(). Kmemleak solves this by calling
    kmemleak_update_trace(), so we could make something like that too.
    But this is out of scope of this patch).

    Fixes: 55834c59098d ("mm: kasan: initial memory quarantine implementation")
    Link: http://lkml.kernel.org/r/575977C3.1010905@virtuozzo.com
    Signed-off-by: Andrey Ryabinin
    Reported-by: Kuthonuzo Luruo
    Acked-by: Alexander Potapenko
    Cc: Dmitriy Vyukov
    Cc: Kostya Serebryany
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrey Ryabinin
     

10 Jun, 2016

1 commit

  • Change the following memory hot-add error messages to info messages.
    There is no need for these to be errors.

    kasan: WARNING: KASAN doesn't support memory hot-add
    kasan: Memory hot-add will be disabled

    Link: http://lkml.kernel.org/r/1464794430-5486-1-git-send-email-shuahkh@osg.samsung.com
    Signed-off-by: Shuah Khan
    Acked-by: Andrey Ryabinin
    Cc: Alexander Potapenko
    Cc: Dmitry Vyukov
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Shuah Khan
     

27 May, 2016

1 commit


21 May, 2016

3 commits

  • Memory access coded in an assembly won't be seen by KASAN as a compiler
    can instrument only C code. Add kasan_check_[read,write]() API which is
    going to be used to check a certain memory range.

    Link: http://lkml.kernel.org/r/1462538722-1574-3-git-send-email-aryabinin@virtuozzo.com
    Signed-off-by: Andrey Ryabinin
    Acked-by: Alexander Potapenko
    Cc: Dmitry Vyukov
    Cc: Ingo Molnar
    Cc: "H. Peter Anvin"
    Cc: Thomas Gleixner
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrey Ryabinin
     
  • When bogus memory access happens in mem[set,cpy,move]() it's usually
    caller's fault. So don't blame mem[set,cpy,move]() in bug report, blame
    the caller instead.

    Before:
    BUG: KASAN: out-of-bounds access in memset+0x23/0x40 at


    After:
    BUG: KASAN: out-of-bounds access in at

    Link: http://lkml.kernel.org/r/1462538722-1574-2-git-send-email-aryabinin@virtuozzo.com
    Signed-off-by: Andrey Ryabinin
    Acked-by: Alexander Potapenko
    Cc: Dmitry Vyukov
    Cc: Ingo Molnar
    Cc: "H. Peter Anvin"
    Cc: Thomas Gleixner
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrey Ryabinin
     
  • Quarantine isolates freed objects in a separate queue. The objects are
    returned to the allocator later, which helps to detect use-after-free
    errors.

    When the object is freed, its state changes from KASAN_STATE_ALLOC to
    KASAN_STATE_QUARANTINE. The object is poisoned and put into quarantine
    instead of being returned to the allocator, therefore every subsequent
    access to that object triggers a KASAN error, and the error handler is
    able to say where the object has been allocated and deallocated.

    When it's time for the object to leave quarantine, its state becomes
    KASAN_STATE_FREE and it's returned to the allocator. From now on the
    allocator may reuse it for another allocation. Before that happens,
    it's still possible to detect a use-after free on that object (it
    retains the allocation/deallocation stacks).

    When the allocator reuses this object, the shadow is unpoisoned and old
    allocation/deallocation stacks are wiped. Therefore a use of this
    object, even an incorrect one, won't trigger ASan warning.

    Without the quarantine, it's not guaranteed that the objects aren't
    reused immediately, that's why the probability of catching a
    use-after-free is lower than with quarantine in place.

    Quarantine isolates freed objects in a separate queue. The objects are
    returned to the allocator later, which helps to detect use-after-free
    errors.

    Freed objects are first added to per-cpu quarantine queues. When a
    cache is destroyed or memory shrinking is requested, the objects are
    moved into the global quarantine queue. Whenever a kmalloc call allows
    memory reclaiming, the oldest objects are popped out of the global queue
    until the total size of objects in quarantine is less than 3/4 of the
    maximum quarantine size (which is a fraction of installed physical
    memory).

    As long as an object remains in the quarantine, KASAN is able to report
    accesses to it, so the chance of reporting a use-after-free is
    increased. Once the object leaves quarantine, the allocator may reuse
    it, in which case the object is unpoisoned and KASAN can't detect
    incorrect accesses to it.

    Right now quarantine support is only enabled in SLAB allocator.
    Unification of KASAN features in SLAB and SLUB will be done later.

    This patch is based on the "mm: kasan: quarantine" patch originally
    prepared by Dmitry Chernenkov. A number of improvements have been
    suggested by Andrey Ryabinin.

    [glider@google.com: v9]
    Link: http://lkml.kernel.org/r/1462987130-144092-1-git-send-email-glider@google.com
    Signed-off-by: Alexander Potapenko
    Cc: Christoph Lameter
    Cc: Pekka Enberg
    Cc: David Rientjes
    Cc: Joonsoo Kim
    Cc: Andrey Konovalov
    Cc: Dmitry Vyukov
    Cc: Andrey Ryabinin
    Cc: Steven Rostedt
    Cc: Konstantin Serebryany
    Cc: Dmitry Chernenkov
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Alexander Potapenko
     

02 Apr, 2016

1 commit

  • Add the missing argument to set_track().

    Fixes: cd11016e5f52 ("mm, kasan: stackdepot implementation. Enable stackdepot for SLAB")
    Signed-off-by: Alexander Potapenko
    Cc: Andrey Konovalov
    Cc: Christoph Lameter
    Cc: Dmitry Vyukov
    Cc: Andrey Ryabinin
    Cc: Steven Rostedt
    Cc: Joonsoo Kim
    Cc: Konstantin Serebryany
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Alexander Potapenko
     

26 Mar, 2016

3 commits

  • Implement the stack depot and provide CONFIG_STACKDEPOT. Stack depot
    will allow KASAN store allocation/deallocation stack traces for memory
    chunks. The stack traces are stored in a hash table and referenced by
    handles which reside in the kasan_alloc_meta and kasan_free_meta
    structures in the allocated memory chunks.

    IRQ stack traces are cut below the IRQ entry point to avoid unnecessary
    duplication.

    Right now stackdepot support is only enabled in SLAB allocator. Once
    KASAN features in SLAB are on par with those in SLUB we can switch SLUB
    to stackdepot as well, thus removing the dependency on SLUB stack
    bookkeeping, which wastes a lot of memory.

    This patch is based on the "mm: kasan: stack depots" patch originally
    prepared by Dmitry Chernenkov.

    Joonsoo has said that he plans to reuse the stackdepot code for the
    mm/page_owner.c debugging facility.

    [akpm@linux-foundation.org: s/depot_stack_handle/depot_stack_handle_t]
    [aryabinin@virtuozzo.com: comment style fixes]
    Signed-off-by: Alexander Potapenko
    Signed-off-by: Andrey Ryabinin
    Cc: Christoph Lameter
    Cc: Pekka Enberg
    Cc: David Rientjes
    Cc: Joonsoo Kim
    Cc: Andrey Konovalov
    Cc: Dmitry Vyukov
    Cc: Steven Rostedt
    Cc: Konstantin Serebryany
    Cc: Dmitry Chernenkov
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Alexander Potapenko
     
  • Add GFP flags to KASAN hooks for future patches to use.

    This patch is based on the "mm: kasan: unified support for SLUB and SLAB
    allocators" patch originally prepared by Dmitry Chernenkov.

    Signed-off-by: Alexander Potapenko
    Cc: Christoph Lameter
    Cc: Pekka Enberg
    Cc: David Rientjes
    Cc: Joonsoo Kim
    Cc: Andrey Konovalov
    Cc: Dmitry Vyukov
    Cc: Andrey Ryabinin
    Cc: Steven Rostedt
    Cc: Konstantin Serebryany
    Cc: Dmitry Chernenkov
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Alexander Potapenko
     
  • Add KASAN hooks to SLAB allocator.

    This patch is based on the "mm: kasan: unified support for SLUB and SLAB
    allocators" patch originally prepared by Dmitry Chernenkov.

    Signed-off-by: Alexander Potapenko
    Cc: Christoph Lameter
    Cc: Pekka Enberg
    Cc: David Rientjes
    Cc: Joonsoo Kim
    Cc: Andrey Konovalov
    Cc: Dmitry Vyukov
    Cc: Andrey Ryabinin
    Cc: Steven Rostedt
    Cc: Konstantin Serebryany
    Cc: Dmitry Chernenkov
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Alexander Potapenko
     

23 Mar, 2016

1 commit

  • kcov provides code coverage collection for coverage-guided fuzzing
    (randomized testing). Coverage-guided fuzzing is a testing technique
    that uses coverage feedback to determine new interesting inputs to a
    system. A notable user-space example is AFL
    (http://lcamtuf.coredump.cx/afl/). However, this technique is not
    widely used for kernel testing due to missing compiler and kernel
    support.

    kcov does not aim to collect as much coverage as possible. It aims to
    collect more or less stable coverage that is function of syscall inputs.
    To achieve this goal it does not collect coverage in soft/hard
    interrupts and instrumentation of some inherently non-deterministic or
    non-interesting parts of kernel is disbled (e.g. scheduler, locking).

    Currently there is a single coverage collection mode (tracing), but the
    API anticipates additional collection modes. Initially I also
    implemented a second mode which exposes coverage in a fixed-size hash
    table of counters (what Quentin used in his original patch). I've
    dropped the second mode for simplicity.

    This patch adds the necessary support on kernel side. The complimentary
    compiler support was added in gcc revision 231296.

    We've used this support to build syzkaller system call fuzzer, which has
    found 90 kernel bugs in just 2 months:

    https://github.com/google/syzkaller/wiki/Found-Bugs

    We've also found 30+ bugs in our internal systems with syzkaller.
    Another (yet unexplored) direction where kcov coverage would greatly
    help is more traditional "blob mutation". For example, mounting a
    random blob as a filesystem, or receiving a random blob over wire.

    Why not gcov. Typical fuzzing loop looks as follows: (1) reset
    coverage, (2) execute a bit of code, (3) collect coverage, repeat. A
    typical coverage can be just a dozen of basic blocks (e.g. an invalid
    input). In such context gcov becomes prohibitively expensive as
    reset/collect coverage steps depend on total number of basic
    blocks/edges in program (in case of kernel it is about 2M). Cost of
    kcov depends only on number of executed basic blocks/edges. On top of
    that, kernel requires per-thread coverage because there are always
    background threads and unrelated processes that also produce coverage.
    With inlined gcov instrumentation per-thread coverage is not possible.

    kcov exposes kernel PCs and control flow to user-space which is
    insecure. But debugfs should not be mapped as user accessible.

    Based on a patch by Quentin Casasnovas.

    [akpm@linux-foundation.org: make task_struct.kcov_mode have type `enum kcov_mode']
    [akpm@linux-foundation.org: unbreak allmodconfig]
    [akpm@linux-foundation.org: follow x86 Makefile layout standards]
    Signed-off-by: Dmitry Vyukov
    Reviewed-by: Kees Cook
    Cc: syzkaller
    Cc: Vegard Nossum
    Cc: Catalin Marinas
    Cc: Tavis Ormandy
    Cc: Will Deacon
    Cc: Quentin Casasnovas
    Cc: Kostya Serebryany
    Cc: Eric Dumazet
    Cc: Alexander Potapenko
    Cc: Kees Cook
    Cc: Bjorn Helgaas
    Cc: Sasha Levin
    Cc: David Drysdale
    Cc: Ard Biesheuvel
    Cc: Andrey Ryabinin
    Cc: Kirill A. Shutemov
    Cc: Jiri Slaby
    Cc: Ingo Molnar
    Cc: Thomas Gleixner
    Cc: "H. Peter Anvin"
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Dmitry Vyukov
     

18 Mar, 2016

1 commit

  • Kernel style prefers a single string over split strings when the string is
    'user-visible'.

    Miscellanea:

    - Add a missing newline
    - Realign arguments

    Signed-off-by: Joe Perches
    Acked-by: Tejun Heo [percpu]
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Joe Perches
     

10 Mar, 2016

1 commit

  • Functions which the compiler has instrumented for ASAN place poison on
    the stack shadow upon entry and remove this poison prior to returning.

    In some cases (e.g. hotplug and idle), CPUs may exit the kernel a
    number of levels deep in C code. If there are any instrumented
    functions on this critical path, these will leave portions of the idle
    thread stack shadow poisoned.

    If a CPU returns to the kernel via a different path (e.g. a cold
    entry), then depending on stack frame layout subsequent calls to
    instrumented functions may use regions of the stack with stale poison,
    resulting in (spurious) KASAN splats to the console.

    Contemporary GCCs always add stack shadow poisoning when ASAN is
    enabled, even when asked to not instrument a function [1], so we can't
    simply annotate functions on the critical path to avoid poisoning.

    Instead, this series explicitly removes any stale poison before it can
    be hit. In the common hotplug case we clear the entire stack shadow in
    common code, before a CPU is brought online.

    On architectures which perform a cold return as part of cpu idle may
    retain an architecture-specific amount of stack contents. To retain the
    poison for this retained context, the arch code must call the core KASAN
    code, passing a "watermark" stack pointer value beyond which shadow will
    be cleared. Architectures which don't perform a cold return as part of
    idle do not need any additional code.

    This patch (of 3):

    Functions which the compiler has instrumented for KASAN place poison on
    the stack shadow upon entry and remove this poision prior to returning.

    In some cases (e.g. hotplug and idle), CPUs may exit the kernel a number
    of levels deep in C code. If there are any instrumented functions on this
    critical path, these will leave portions of the stack shadow poisoned.

    If a CPU returns to the kernel via a different path (e.g. a cold entry),
    then depending on stack frame layout subsequent calls to instrumented
    functions may use regions of the stack with stale poison, resulting in
    (spurious) KASAN splats to the console.

    To avoid this, we must clear stale poison from the stack prior to
    instrumented functions being called. This patch adds functions to the
    KASAN core for removing poison from (portions of) a task's stack. These
    will be used by subsequent patches to avoid problems with hotplug and
    idle.

    Signed-off-by: Mark Rutland
    Acked-by: Catalin Marinas
    Reviewed-by: Andrey Ryabinin
    Cc: Alexander Potapenko
    Cc: Lorenzo Pieralisi
    Cc: Will Deacon
    Cc: Ingo Molnar
    Cc: Peter Zijlstra
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Mark Rutland
     

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
     

21 Nov, 2015

1 commit

  • Kmemleak reports the following leak:

    unreferenced object 0xfffffbfff41ea000 (size 20480):
    comm "modprobe", pid 65199, jiffies 4298875551 (age 542.568s)
    hex dump (first 32 bytes):
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
    backtrace:
    [] kmemleak_alloc+0x4e/0xc0
    [] __vmalloc_node_range+0x4b8/0x740
    [] kasan_module_alloc+0x72/0xc0
    [] module_alloc+0x78/0xb0
    [] module_alloc_update_bounds+0x14/0x70
    [] layout_and_allocate+0x16f4/0x3c90
    [] load_module+0x2ff/0x6690
    [] SyS_finit_module+0x136/0x170
    [] system_call_fastpath+0x16/0x1b
    [] 0xffffffffffffffff

    kasan_module_alloc() allocates shadow memory for module and frees it on
    module unloading. It doesn't store the pointer to allocated shadow memory
    because it could be calculated from the shadowed address, i.e.
    kasan_mem_to_shadow(addr).

    Since kmemleak cannot find pointer to allocated shadow, it thinks that
    memory leaked.

    Use kmemleak_ignore() to tell kmemleak that this is not a leak and shadow
    memory doesn't contain any pointers.

    Signed-off-by: Andrey Ryabinin
    Acked-by: Catalin Marinas
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrey Ryabinin
     

06 Nov, 2015

12 commits

  • Currently we already taint the kernel in some cases. E.g. if we hit some
    bug in slub memory we call object_err() which will taint the kernel with
    TAINT_BAD_PAGE flag. But for other kind of bugs kernel left untainted.

    Always taint with TAINT_BAD_PAGE if kasan found some bug. This is useful
    for automated testing.

    Signed-off-by: Andrey Ryabinin
    Cc: Alexander Potapenko
    Reviewed-by: Dmitry Vyukov
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrey Ryabinin
     
  • Use IS_ALIGNED() to determine whether the shadow span two bytes. It
    generates less code and more readable. Also add some comments in shadow
    check functions.

    Signed-off-by: Xishi Qiu
    Acked-by: Andrey Ryabinin
    Cc: Andrey Konovalov
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Xishi Qiu
     
  • The current KASAN code can not find the following out-of-bounds bugs:

    char *ptr;
    ptr = kmalloc(8, GFP_KERNEL);
    memset(ptr+7, 0, 2);

    the cause of the problem is the type conversion error in
    *memory_is_poisoned_n* function. So this patch fix that.

    Signed-off-by: Wang Long
    Acked-by: Andrey Ryabinin
    Cc: Vladimir Murzin
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Wang Long
     
  • Update the reference to the kasan prototype repository on github, since it
    was renamed.

    Signed-off-by: Andrey Konovalov
    Cc: Andrey Ryabinin
    Cc: Dmitry Vyukov
    Cc: Alexander Potapenko
    Cc: Konstantin Serebryany
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrey Konovalov
     
  • We decided to use KASAN as the short name of the tool and
    KernelAddressSanitizer as the full one. Update log messages according to
    that.

    Signed-off-by: Andrey Konovalov
    Cc: Andrey Ryabinin
    Cc: Dmitry Vyukov
    Cc: Alexander Potapenko
    Cc: Konstantin Serebryany
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrey Konovalov
     
  • Makes KASAN accurately determine the type of the bad access. If the shadow
    byte value is in the [0, KASAN_SHADOW_SCALE_SIZE) range we can look at
    the next shadow byte to determine the type of the access.

    Signed-off-by: Andrey Konovalov
    Cc: Andrey Ryabinin
    Cc: Dmitry Vyukov
    Cc: Alexander Potapenko
    Cc: Konstantin Serebryany
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrey Konovalov
     
  • Update the names of the bad access types to better reflect the type of
    the access that happended and make these error types "literals" that can
    be used for classification and deduplication in scripts.

    Signed-off-by: Andrey Konovalov
    Cc: Andrey Ryabinin
    Cc: Dmitry Vyukov
    Cc: Alexander Potapenko
    Cc: Konstantin Serebryany
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrey Konovalov
     
  • Each access with address lower than
    kasan_shadow_to_mem(KASAN_SHADOW_START) is reported as user-memory-access.
    This is not always true, the accessed address might not be in user space.
    Fix this by reporting such accesses as null-ptr-derefs or
    wild-memory-accesses.

    There's another reason for this change. For userspace ASan we have a
    bunch of systems that analyze error types for the purpose of
    classification and deduplication. Sooner of later we will write them to
    KASAN as well. Then clearly and explicitly stated error types will bring
    value.

    Signed-off-by: Andrey Konovalov
    Cc: Andrey Ryabinin
    Cc: Dmitry Vyukov
    Cc: Alexander Potapenko
    Cc: Konstantin Serebryany
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Andrey Konovalov
     
  • When we end up calling kasan_report in real mode, our shadow mapping for
    the spinlock variable will show poisoned. This will result in us calling
    kasan_report_error with lock_report spin lock held. To prevent this
    disable kasan reporting when we are priting error w.r.t kasan.

    Signed-off-by: Aneesh Kumar K.V
    Reviewed-by: Andrey Ryabinin
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Aneesh Kumar K.V
     
  • We can't use generic functions like print_hex_dump to access kasan shadow
    region. This require us to setup another kasan shadow region for the
    address passed (kasan shadow address). Some architectures won't be able
    to do that. Hence make a copy of the shadow region row and pass that to
    generic functions.

    Signed-off-by: Aneesh Kumar K.V
    Reviewed-by: Andrey Ryabinin
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Aneesh Kumar K.V
     
  • Use is_module_address instead

    Signed-off-by: Aneesh Kumar K.V
    Reviewed-by: Andrey Ryabinin
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Aneesh Kumar K.V
     
  • The function only disable/enable reporting. In the later patch we will be
    adding a kasan early enable/disable. Rename kasan_enabled to properly
    reflect its function.

    Signed-off-by: Aneesh Kumar K.V
    Reviewed-by: Andrey Ryabinin
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Aneesh Kumar K.V