13 Feb, 2019

1 commit

  • [ Upstream commit 162bc7f5afd75b72acbe3c5f3488ef7e64a3fe36 ]

    If you have a CPU that fails to round up and then run 'btc' you'll end
    up crashing in kdb becaue we dereferenced NULL. Let's add a check.
    It's wise to also set the task to NULL when leaving the debugger so
    that if we fail to round up on a later entry into the debugger we
    won't backtrace a stale task.

    Signed-off-by: Douglas Anderson
    Acked-by: Daniel Thompson
    Signed-off-by: Daniel Thompson
    Signed-off-by: Sasha Levin

    Douglas Anderson
     

01 Dec, 2018

1 commit

  • [ Upstream commit c2b94c72d93d0929f48157eef128c4f9d2e603ce ]

    gcc 8.1.0 warns with:

    kernel/debug/kdb/kdb_support.c: In function ‘kallsyms_symbol_next’:
    kernel/debug/kdb/kdb_support.c:239:4: warning: ‘strncpy’ specified bound depends on the length of the source argument [-Wstringop-overflow=]
    strncpy(prefix_name, name, strlen(name)+1);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    kernel/debug/kdb/kdb_support.c:239:31: note: length computed here

    Use strscpy() with the destination buffer size, and use ellipses when
    displaying truncated symbols.

    v2: Use strscpy()

    Signed-off-by: Prarit Bhargava
    Cc: Jonathan Toppins
    Cc: Jason Wessel
    Cc: Daniel Thompson
    Cc: kgdb-bugreport@lists.sourceforge.net
    Reviewed-by: Daniel Thompson
    Signed-off-by: Daniel Thompson
    Signed-off-by: Sasha Levin

    Prarit Bhargava
     

21 Nov, 2018

2 commits

  • commit 568fb6f42ac6851320adaea25f8f1b94de14e40a upstream.

    Since commit ad67b74d2469 ("printk: hash addresses printed with %p"),
    all pointers printed with %p are printed with hashed addresses
    instead of real addresses in order to avoid leaking addresses in
    dmesg and syslog. But this applies to kdb too, with is unfortunate:

    Entering kdb (current=0x(ptrval), pid 329) due to Keyboard Entry
    kdb> ps
    15 sleeping system daemon (state M) processes suppressed,
    use 'ps A' to see all.
    Task Addr Pid Parent [*] cpu State Thread Command
    0x(ptrval) 329 328 1 0 R 0x(ptrval) *sh

    0x(ptrval) 1 0 0 0 S 0x(ptrval) init
    0x(ptrval) 3 2 0 0 D 0x(ptrval) rcu_gp
    0x(ptrval) 4 2 0 0 D 0x(ptrval) rcu_par_gp
    0x(ptrval) 5 2 0 0 D 0x(ptrval) kworker/0:0
    0x(ptrval) 6 2 0 0 D 0x(ptrval) kworker/0:0H
    0x(ptrval) 7 2 0 0 D 0x(ptrval) kworker/u2:0
    0x(ptrval) 8 2 0 0 D 0x(ptrval) mm_percpu_wq
    0x(ptrval) 10 2 0 0 D 0x(ptrval) rcu_preempt

    The whole purpose of kdb is to debug, and for debugging real addresses
    need to be known. In addition, data displayed by kdb doesn't go into
    dmesg.

    This patch replaces all %p by %px in kdb in order to display real
    addresses.

    Fixes: ad67b74d2469 ("printk: hash addresses printed with %p")
    Cc:
    Signed-off-by: Christophe Leroy
    Signed-off-by: Daniel Thompson
    Signed-off-by: Greg Kroah-Hartman

    Christophe Leroy
     
  • commit dded2e159208a9edc21dd5c5f583afa28d378d39 upstream.

    On a powerpc 8xx, 'btc' fails as follows:

    Entering kdb (current=0x(ptrval), pid 282) due to Keyboard Entry
    kdb> btc
    btc: cpu status: Currently on cpu 0
    Available cpus: 0
    kdb_getarea: Bad address 0x0

    when booting the kernel with 'debug_boot_weak_hash', it fails as well

    Entering kdb (current=0xba99ad80, pid 284) due to Keyboard Entry
    kdb> btc
    btc: cpu status: Currently on cpu 0
    Available cpus: 0
    kdb_getarea: Bad address 0xba99ad80

    On other platforms, Oopses have been observed too, see
    https://github.com/linuxppc/linux/issues/139

    This is due to btc calling 'btt' with %p pointer as an argument.

    This patch replaces %p by %px to get the real pointer value as
    expected by 'btt'

    Fixes: ad67b74d2469 ("printk: hash addresses printed with %p")
    Cc:
    Signed-off-by: Christophe Leroy
    Reviewed-by: Daniel Thompson
    Signed-off-by: Daniel Thompson
    Signed-off-by: Greg Kroah-Hartman

    Christophe Leroy
     

13 Jun, 2018

2 commits

  • The kzalloc() function has a 2-factor argument form, kcalloc(). This
    patch replaces cases of:

    kzalloc(a * b, gfp)

    with:
    kcalloc(a * b, gfp)

    as well as handling cases of:

    kzalloc(a * b * c, gfp)

    with:

    kzalloc(array3_size(a, b, c), gfp)

    as it's slightly less ugly than:

    kzalloc_array(array_size(a, b), c, gfp)

    This does, however, attempt to ignore constant size factors like:

    kzalloc(4 * 1024, gfp)

    though any constants defined via macros get caught up in the conversion.

    Any factors with a sizeof() of "unsigned char", "char", and "u8" were
    dropped, since they're redundant.

    The Coccinelle script used for this was:

    // Fix redundant parens around sizeof().
    @@
    type TYPE;
    expression THING, E;
    @@

    (
    kzalloc(
    - (sizeof(TYPE)) * E
    + sizeof(TYPE) * E
    , ...)
    |
    kzalloc(
    - (sizeof(THING)) * E
    + sizeof(THING) * E
    , ...)
    )

    // Drop single-byte sizes and redundant parens.
    @@
    expression COUNT;
    typedef u8;
    typedef __u8;
    @@

    (
    kzalloc(
    - sizeof(u8) * (COUNT)
    + COUNT
    , ...)
    |
    kzalloc(
    - sizeof(__u8) * (COUNT)
    + COUNT
    , ...)
    |
    kzalloc(
    - sizeof(char) * (COUNT)
    + COUNT
    , ...)
    |
    kzalloc(
    - sizeof(unsigned char) * (COUNT)
    + COUNT
    , ...)
    |
    kzalloc(
    - sizeof(u8) * COUNT
    + COUNT
    , ...)
    |
    kzalloc(
    - sizeof(__u8) * COUNT
    + COUNT
    , ...)
    |
    kzalloc(
    - sizeof(char) * COUNT
    + COUNT
    , ...)
    |
    kzalloc(
    - sizeof(unsigned char) * COUNT
    + COUNT
    , ...)
    )

    // 2-factor product with sizeof(type/expression) and identifier or constant.
    @@
    type TYPE;
    expression THING;
    identifier COUNT_ID;
    constant COUNT_CONST;
    @@

    (
    - kzalloc
    + kcalloc
    (
    - sizeof(TYPE) * (COUNT_ID)
    + COUNT_ID, sizeof(TYPE)
    , ...)
    |
    - kzalloc
    + kcalloc
    (
    - sizeof(TYPE) * COUNT_ID
    + COUNT_ID, sizeof(TYPE)
    , ...)
    |
    - kzalloc
    + kcalloc
    (
    - sizeof(TYPE) * (COUNT_CONST)
    + COUNT_CONST, sizeof(TYPE)
    , ...)
    |
    - kzalloc
    + kcalloc
    (
    - sizeof(TYPE) * COUNT_CONST
    + COUNT_CONST, sizeof(TYPE)
    , ...)
    |
    - kzalloc
    + kcalloc
    (
    - sizeof(THING) * (COUNT_ID)
    + COUNT_ID, sizeof(THING)
    , ...)
    |
    - kzalloc
    + kcalloc
    (
    - sizeof(THING) * COUNT_ID
    + COUNT_ID, sizeof(THING)
    , ...)
    |
    - kzalloc
    + kcalloc
    (
    - sizeof(THING) * (COUNT_CONST)
    + COUNT_CONST, sizeof(THING)
    , ...)
    |
    - kzalloc
    + kcalloc
    (
    - sizeof(THING) * COUNT_CONST
    + COUNT_CONST, sizeof(THING)
    , ...)
    )

    // 2-factor product, only identifiers.
    @@
    identifier SIZE, COUNT;
    @@

    - kzalloc
    + kcalloc
    (
    - SIZE * COUNT
    + COUNT, SIZE
    , ...)

    // 3-factor product with 1 sizeof(type) or sizeof(expression), with
    // redundant parens removed.
    @@
    expression THING;
    identifier STRIDE, COUNT;
    type TYPE;
    @@

    (
    kzalloc(
    - sizeof(TYPE) * (COUNT) * (STRIDE)
    + array3_size(COUNT, STRIDE, sizeof(TYPE))
    , ...)
    |
    kzalloc(
    - sizeof(TYPE) * (COUNT) * STRIDE
    + array3_size(COUNT, STRIDE, sizeof(TYPE))
    , ...)
    |
    kzalloc(
    - sizeof(TYPE) * COUNT * (STRIDE)
    + array3_size(COUNT, STRIDE, sizeof(TYPE))
    , ...)
    |
    kzalloc(
    - sizeof(TYPE) * COUNT * STRIDE
    + array3_size(COUNT, STRIDE, sizeof(TYPE))
    , ...)
    |
    kzalloc(
    - sizeof(THING) * (COUNT) * (STRIDE)
    + array3_size(COUNT, STRIDE, sizeof(THING))
    , ...)
    |
    kzalloc(
    - sizeof(THING) * (COUNT) * STRIDE
    + array3_size(COUNT, STRIDE, sizeof(THING))
    , ...)
    |
    kzalloc(
    - sizeof(THING) * COUNT * (STRIDE)
    + array3_size(COUNT, STRIDE, sizeof(THING))
    , ...)
    |
    kzalloc(
    - sizeof(THING) * COUNT * STRIDE
    + array3_size(COUNT, STRIDE, sizeof(THING))
    , ...)
    )

    // 3-factor product with 2 sizeof(variable), with redundant parens removed.
    @@
    expression THING1, THING2;
    identifier COUNT;
    type TYPE1, TYPE2;
    @@

    (
    kzalloc(
    - sizeof(TYPE1) * sizeof(TYPE2) * COUNT
    + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
    , ...)
    |
    kzalloc(
    - sizeof(TYPE1) * sizeof(THING2) * (COUNT)
    + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
    , ...)
    |
    kzalloc(
    - sizeof(THING1) * sizeof(THING2) * COUNT
    + array3_size(COUNT, sizeof(THING1), sizeof(THING2))
    , ...)
    |
    kzalloc(
    - sizeof(THING1) * sizeof(THING2) * (COUNT)
    + array3_size(COUNT, sizeof(THING1), sizeof(THING2))
    , ...)
    |
    kzalloc(
    - sizeof(TYPE1) * sizeof(THING2) * COUNT
    + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
    , ...)
    |
    kzalloc(
    - sizeof(TYPE1) * sizeof(THING2) * (COUNT)
    + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
    , ...)
    )

    // 3-factor product, only identifiers, with redundant parens removed.
    @@
    identifier STRIDE, SIZE, COUNT;
    @@

    (
    kzalloc(
    - (COUNT) * STRIDE * SIZE
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    kzalloc(
    - COUNT * (STRIDE) * SIZE
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    kzalloc(
    - COUNT * STRIDE * (SIZE)
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    kzalloc(
    - (COUNT) * (STRIDE) * SIZE
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    kzalloc(
    - COUNT * (STRIDE) * (SIZE)
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    kzalloc(
    - (COUNT) * STRIDE * (SIZE)
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    kzalloc(
    - (COUNT) * (STRIDE) * (SIZE)
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    kzalloc(
    - COUNT * STRIDE * SIZE
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    )

    // Any remaining multi-factor products, first at least 3-factor products,
    // when they're not all constants...
    @@
    expression E1, E2, E3;
    constant C1, C2, C3;
    @@

    (
    kzalloc(C1 * C2 * C3, ...)
    |
    kzalloc(
    - (E1) * E2 * E3
    + array3_size(E1, E2, E3)
    , ...)
    |
    kzalloc(
    - (E1) * (E2) * E3
    + array3_size(E1, E2, E3)
    , ...)
    |
    kzalloc(
    - (E1) * (E2) * (E3)
    + array3_size(E1, E2, E3)
    , ...)
    |
    kzalloc(
    - E1 * E2 * E3
    + array3_size(E1, E2, E3)
    , ...)
    )

    // And then all remaining 2 factors products when they're not all constants,
    // keeping sizeof() as the second factor argument.
    @@
    expression THING, E1, E2;
    type TYPE;
    constant C1, C2, C3;
    @@

    (
    kzalloc(sizeof(THING) * C2, ...)
    |
    kzalloc(sizeof(TYPE) * C2, ...)
    |
    kzalloc(C1 * C2 * C3, ...)
    |
    kzalloc(C1 * C2, ...)
    |
    - kzalloc
    + kcalloc
    (
    - sizeof(TYPE) * (E2)
    + E2, sizeof(TYPE)
    , ...)
    |
    - kzalloc
    + kcalloc
    (
    - sizeof(TYPE) * E2
    + E2, sizeof(TYPE)
    , ...)
    |
    - kzalloc
    + kcalloc
    (
    - sizeof(THING) * (E2)
    + E2, sizeof(THING)
    , ...)
    |
    - kzalloc
    + kcalloc
    (
    - sizeof(THING) * E2
    + E2, sizeof(THING)
    , ...)
    |
    - kzalloc
    + kcalloc
    (
    - (E1) * E2
    + E1, E2
    , ...)
    |
    - kzalloc
    + kcalloc
    (
    - (E1) * (E2)
    + E1, E2
    , ...)
    |
    - kzalloc
    + kcalloc
    (
    - E1 * E2
    + E1, E2
    , ...)
    )

    Signed-off-by: Kees Cook

    Kees Cook
     
  • The kmalloc() function has a 2-factor argument form, kmalloc_array(). This
    patch replaces cases of:

    kmalloc(a * b, gfp)

    with:
    kmalloc_array(a * b, gfp)

    as well as handling cases of:

    kmalloc(a * b * c, gfp)

    with:

    kmalloc(array3_size(a, b, c), gfp)

    as it's slightly less ugly than:

    kmalloc_array(array_size(a, b), c, gfp)

    This does, however, attempt to ignore constant size factors like:

    kmalloc(4 * 1024, gfp)

    though any constants defined via macros get caught up in the conversion.

    Any factors with a sizeof() of "unsigned char", "char", and "u8" were
    dropped, since they're redundant.

    The tools/ directory was manually excluded, since it has its own
    implementation of kmalloc().

    The Coccinelle script used for this was:

    // Fix redundant parens around sizeof().
    @@
    type TYPE;
    expression THING, E;
    @@

    (
    kmalloc(
    - (sizeof(TYPE)) * E
    + sizeof(TYPE) * E
    , ...)
    |
    kmalloc(
    - (sizeof(THING)) * E
    + sizeof(THING) * E
    , ...)
    )

    // Drop single-byte sizes and redundant parens.
    @@
    expression COUNT;
    typedef u8;
    typedef __u8;
    @@

    (
    kmalloc(
    - sizeof(u8) * (COUNT)
    + COUNT
    , ...)
    |
    kmalloc(
    - sizeof(__u8) * (COUNT)
    + COUNT
    , ...)
    |
    kmalloc(
    - sizeof(char) * (COUNT)
    + COUNT
    , ...)
    |
    kmalloc(
    - sizeof(unsigned char) * (COUNT)
    + COUNT
    , ...)
    |
    kmalloc(
    - sizeof(u8) * COUNT
    + COUNT
    , ...)
    |
    kmalloc(
    - sizeof(__u8) * COUNT
    + COUNT
    , ...)
    |
    kmalloc(
    - sizeof(char) * COUNT
    + COUNT
    , ...)
    |
    kmalloc(
    - sizeof(unsigned char) * COUNT
    + COUNT
    , ...)
    )

    // 2-factor product with sizeof(type/expression) and identifier or constant.
    @@
    type TYPE;
    expression THING;
    identifier COUNT_ID;
    constant COUNT_CONST;
    @@

    (
    - kmalloc
    + kmalloc_array
    (
    - sizeof(TYPE) * (COUNT_ID)
    + COUNT_ID, sizeof(TYPE)
    , ...)
    |
    - kmalloc
    + kmalloc_array
    (
    - sizeof(TYPE) * COUNT_ID
    + COUNT_ID, sizeof(TYPE)
    , ...)
    |
    - kmalloc
    + kmalloc_array
    (
    - sizeof(TYPE) * (COUNT_CONST)
    + COUNT_CONST, sizeof(TYPE)
    , ...)
    |
    - kmalloc
    + kmalloc_array
    (
    - sizeof(TYPE) * COUNT_CONST
    + COUNT_CONST, sizeof(TYPE)
    , ...)
    |
    - kmalloc
    + kmalloc_array
    (
    - sizeof(THING) * (COUNT_ID)
    + COUNT_ID, sizeof(THING)
    , ...)
    |
    - kmalloc
    + kmalloc_array
    (
    - sizeof(THING) * COUNT_ID
    + COUNT_ID, sizeof(THING)
    , ...)
    |
    - kmalloc
    + kmalloc_array
    (
    - sizeof(THING) * (COUNT_CONST)
    + COUNT_CONST, sizeof(THING)
    , ...)
    |
    - kmalloc
    + kmalloc_array
    (
    - sizeof(THING) * COUNT_CONST
    + COUNT_CONST, sizeof(THING)
    , ...)
    )

    // 2-factor product, only identifiers.
    @@
    identifier SIZE, COUNT;
    @@

    - kmalloc
    + kmalloc_array
    (
    - SIZE * COUNT
    + COUNT, SIZE
    , ...)

    // 3-factor product with 1 sizeof(type) or sizeof(expression), with
    // redundant parens removed.
    @@
    expression THING;
    identifier STRIDE, COUNT;
    type TYPE;
    @@

    (
    kmalloc(
    - sizeof(TYPE) * (COUNT) * (STRIDE)
    + array3_size(COUNT, STRIDE, sizeof(TYPE))
    , ...)
    |
    kmalloc(
    - sizeof(TYPE) * (COUNT) * STRIDE
    + array3_size(COUNT, STRIDE, sizeof(TYPE))
    , ...)
    |
    kmalloc(
    - sizeof(TYPE) * COUNT * (STRIDE)
    + array3_size(COUNT, STRIDE, sizeof(TYPE))
    , ...)
    |
    kmalloc(
    - sizeof(TYPE) * COUNT * STRIDE
    + array3_size(COUNT, STRIDE, sizeof(TYPE))
    , ...)
    |
    kmalloc(
    - sizeof(THING) * (COUNT) * (STRIDE)
    + array3_size(COUNT, STRIDE, sizeof(THING))
    , ...)
    |
    kmalloc(
    - sizeof(THING) * (COUNT) * STRIDE
    + array3_size(COUNT, STRIDE, sizeof(THING))
    , ...)
    |
    kmalloc(
    - sizeof(THING) * COUNT * (STRIDE)
    + array3_size(COUNT, STRIDE, sizeof(THING))
    , ...)
    |
    kmalloc(
    - sizeof(THING) * COUNT * STRIDE
    + array3_size(COUNT, STRIDE, sizeof(THING))
    , ...)
    )

    // 3-factor product with 2 sizeof(variable), with redundant parens removed.
    @@
    expression THING1, THING2;
    identifier COUNT;
    type TYPE1, TYPE2;
    @@

    (
    kmalloc(
    - sizeof(TYPE1) * sizeof(TYPE2) * COUNT
    + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
    , ...)
    |
    kmalloc(
    - sizeof(TYPE1) * sizeof(THING2) * (COUNT)
    + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
    , ...)
    |
    kmalloc(
    - sizeof(THING1) * sizeof(THING2) * COUNT
    + array3_size(COUNT, sizeof(THING1), sizeof(THING2))
    , ...)
    |
    kmalloc(
    - sizeof(THING1) * sizeof(THING2) * (COUNT)
    + array3_size(COUNT, sizeof(THING1), sizeof(THING2))
    , ...)
    |
    kmalloc(
    - sizeof(TYPE1) * sizeof(THING2) * COUNT
    + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
    , ...)
    |
    kmalloc(
    - sizeof(TYPE1) * sizeof(THING2) * (COUNT)
    + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
    , ...)
    )

    // 3-factor product, only identifiers, with redundant parens removed.
    @@
    identifier STRIDE, SIZE, COUNT;
    @@

    (
    kmalloc(
    - (COUNT) * STRIDE * SIZE
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    kmalloc(
    - COUNT * (STRIDE) * SIZE
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    kmalloc(
    - COUNT * STRIDE * (SIZE)
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    kmalloc(
    - (COUNT) * (STRIDE) * SIZE
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    kmalloc(
    - COUNT * (STRIDE) * (SIZE)
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    kmalloc(
    - (COUNT) * STRIDE * (SIZE)
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    kmalloc(
    - (COUNT) * (STRIDE) * (SIZE)
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    |
    kmalloc(
    - COUNT * STRIDE * SIZE
    + array3_size(COUNT, STRIDE, SIZE)
    , ...)
    )

    // Any remaining multi-factor products, first at least 3-factor products,
    // when they're not all constants...
    @@
    expression E1, E2, E3;
    constant C1, C2, C3;
    @@

    (
    kmalloc(C1 * C2 * C3, ...)
    |
    kmalloc(
    - (E1) * E2 * E3
    + array3_size(E1, E2, E3)
    , ...)
    |
    kmalloc(
    - (E1) * (E2) * E3
    + array3_size(E1, E2, E3)
    , ...)
    |
    kmalloc(
    - (E1) * (E2) * (E3)
    + array3_size(E1, E2, E3)
    , ...)
    |
    kmalloc(
    - E1 * E2 * E3
    + array3_size(E1, E2, E3)
    , ...)
    )

    // And then all remaining 2 factors products when they're not all constants,
    // keeping sizeof() as the second factor argument.
    @@
    expression THING, E1, E2;
    type TYPE;
    constant C1, C2, C3;
    @@

    (
    kmalloc(sizeof(THING) * C2, ...)
    |
    kmalloc(sizeof(TYPE) * C2, ...)
    |
    kmalloc(C1 * C2 * C3, ...)
    |
    kmalloc(C1 * C2, ...)
    |
    - kmalloc
    + kmalloc_array
    (
    - sizeof(TYPE) * (E2)
    + E2, sizeof(TYPE)
    , ...)
    |
    - kmalloc
    + kmalloc_array
    (
    - sizeof(TYPE) * E2
    + E2, sizeof(TYPE)
    , ...)
    |
    - kmalloc
    + kmalloc_array
    (
    - sizeof(THING) * (E2)
    + E2, sizeof(THING)
    , ...)
    |
    - kmalloc
    + kmalloc_array
    (
    - sizeof(THING) * E2
    + E2, sizeof(THING)
    , ...)
    |
    - kmalloc
    + kmalloc_array
    (
    - (E1) * E2
    + E1, E2
    , ...)
    |
    - kmalloc
    + kmalloc_array
    (
    - (E1) * (E2)
    + E1, E2
    , ...)
    |
    - kmalloc
    + kmalloc_array
    (
    - E1 * E2
    + E1, E2
    , ...)
    )

    Signed-off-by: Kees Cook

    Kees Cook
     

13 Apr, 2018

1 commit

  • Pull kdb updates from Jason Wessel:

    - fix 2032 time access issues and new compiler warnings

    - minor regression test cleanup

    - formatting fixes for end user use of kdb

    * tag 'for_linus-4.16' of git://git.kernel.org/pub/scm/linux/kernel/git/jwessel/kgdb:
    kdb: use memmove instead of overlapping memcpy
    kdb: use ktime_get_mono_fast_ns() instead of ktime_get_ts()
    kdb: bl: don't use tab character in output
    kdb: drop newline in unknown command output
    kdb: make "mdr" command repeat
    kdb: use __ktime_get_real_seconds instead of __current_kernel_time
    misc: kgdbts: Display progress of asynchronous tests

    Linus Torvalds
     

05 Feb, 2018

1 commit

  • gcc discovered that the memcpy() arguments in kdbnearsym() overlap, so
    we should really use memmove(), which is defined to handle that correctly:

    In function 'memcpy',
    inlined from 'kdbnearsym' at /git/arm-soc/kernel/debug/kdb/kdb_support.c:132:4:
    /git/arm-soc/include/linux/string.h:353:9: error: '__builtin_memcpy' accessing 792 bytes at offsets 0 and 8 overlaps 784 bytes at offset 8 [-Werror=restrict]
    return __builtin_memcpy(p, q, size);

    Signed-off-by: Arnd Bergmann
    Signed-off-by: Jason Wessel

    Arnd Bergmann
     

01 Feb, 2018

1 commit

  • The kdb code will print the monotonic time by ktime_get_ts(), but
    the ktime_get_ts() will be protected by a sequence lock, that will
    introduce one deadlock risk if the lock was already held in the
    context from which we entered the debugger.

    Thus we can use the ktime_get_mono_fast_ns() to get the monotonic
    time, which is NMI safe access to clock monotonic. Moreover we can
    remove the 'struct timespec', which is not y2038 safe.

    Signed-off-by: Baolin Wang
    Reviewed-by: Daniel Thompson
    Reviewed-by: Arnd Bergmann
    Signed-off-by: Jason Wessel

    Baolin Wang
     

25 Jan, 2018

4 commits

  • The "bl" (list breakpoints) command prints a '\t' (tab) character
    in its output, but on a console (video device), that just prints
    some odd graphics character. Instead of printing a tab character,
    just align the output with spaces.

    Signed-off-by: Randy Dunlap
    Cc: Daniel Thompson
    Cc: Jason Wessel
    Cc: kgdb-bugreport@lists.sourceforge.net
    Signed-off-by: Jason Wessel

    Randy Dunlap
     
  • When an unknown command is entered, kdb prints "Unknown kdb command:"
    and then the unknown text, including the newline character. This
    causes the ending single-quote mark to be printed on the next line
    by itself, so just change the ending newline character to a null
    character (end of string) so that it won't be "printed."

    Signed-off-by: Randy Dunlap
    Cc: Daniel Thompson
    Cc: Jason Wessel
    Cc: kgdb-bugreport@lists.sourceforge.net
    Signed-off-by: Jason Wessel

    Randy Dunlap
     
  • The "mdr" command should repeat (continue) when only Enter/Return
    is pressed, so make it do so.

    Signed-off-by: Randy Dunlap
    Cc: Daniel Thompson
    Cc: Jason Wessel
    Cc: kgdb-bugreport@lists.sourceforge.net
    Signed-off-by: Jason Wessel

    Randy Dunlap
     
  • kdb is the only user of the __current_kernel_time() interface, which is
    not y2038 safe and should be removed at some point.

    The kdb code also goes to great lengths to print the time in a
    human-readable format from 'struct timespec', again using a non-y2038-safe
    re-implementation of the generic time_to_tm() code.

    Using __current_kernel_time() here is necessary since the regular
    accessors that require a sequence lock might hang when called during the
    xtime update. However, this is safe in the particular case since kdb is
    only interested in the tv_sec field that is updated atomically.

    In order to make this y2038-safe, I'm converting the code to the generic
    time64_to_tm helper, but that introduces the problem that we have no
    interface like __current_kernel_time() that provides a 64-bit timestamp
    in a lockless, safe and architecture-independent way. I have multiple
    ideas for how to solve that:

    - __ktime_get_real_seconds() is lockless, but can return
    incorrect results on 32-bit architectures in the special case that
    we are in the process of changing the time across the epoch, either
    during the timer tick that overflows the seconds in 2038, or while
    calling settimeofday.

    - ktime_get_real_fast_ns() would work in this context, but does
    require a call into the clocksource driver to return a high-resolution
    timestamp. This may have undesired side-effects in the debugger,
    since we want to limit the interactions with the rest of the kernel.

    - Adding a ktime_get_real_fast_seconds() based on tk_fast_mono
    plus tkr->base_real without the tk_clock_read() delta. Not sure about
    the value of adding yet another interface here.

    - Changing the existing ktime_get_real_seconds() to use
    tk_fast_mono on 32-bit architectures rather than xtime_sec. I think
    this could work, but am not entirely sure if this is an improvement.

    I picked the first of those for simplicity here. It's technically
    not correct but probably good enough as the time is only used for the
    debugging output and the race will likely never be hit in practice.
    Another downside is having to move the declaration into a public header
    file.

    Let me know if anyone has a different preference.

    Cc: Andy Shevchenko
    Link: https://patchwork.kernel.org/patch/9775309/
    Signed-off-by: Arnd Bergmann
    Signed-off-by: Jason Wessel

    Arnd Bergmann
     

04 Jan, 2018

1 commit

  • - Rename from kdb_send_sig_info to kdb_send_sig
    As there is no meaningful siginfo sent

    - Use SEND_SIG_PRIV instead of generating a siginfo for a kdb
    signal. The generated siginfo had a bogus rationale and was
    not correct in the face of pid namespaces. SEND_SIG_PRIV
    is simpler and actually correct.

    - As the code grabs siglock just send the signal with siglock
    held instead of dropping siglock and attempting to grab it again.

    - Move the sig_valid test into kdb_kill where it can generate
    a good error message.

    Signed-off-by: Eric W. Biederman

    Eric W. Biederman
     

07 Dec, 2017

1 commit

  • kallsyms_symbol_next() returns a boolean (true on success). Currently
    kdb_read() tests the return value with an inequality that
    unconditionally evaluates to true.

    This is fixed in the obvious way and, since the conditional branch is
    supposed to be unreachable, we also add a WARN_ON().

    Reported-by: Dan Carpenter
    Signed-off-by: Daniel Thompson
    Cc: linux-stable
    Signed-off-by: Jason Wessel

    Daniel Thompson
     

02 Mar, 2017

6 commits


15 Dec, 2016

4 commits

  • kdb_trap_printk allows to pass normal printk() messages to kdb via
    vkdb_printk(). For example, it is used to get backtrace using the
    classic show_stack(), see kdb_show_stack().

    vkdb_printf() tries to avoid a potential infinite loop by disabling the
    trap. But this approach is racy, for example:

    CPU1 CPU2

    vkdb_printf()
    // assume that kdb_trap_printk == 0
    saved_trap_printk = kdb_trap_printk;
    kdb_trap_printk = 0;

    kdb_show_stack()
    kdb_trap_printk++;

    Problem1: Now, a nested printk() on CPU0 calls vkdb_printf()
    even when it should have been disabled. It will not
    cause a deadlock but...

    // using the outdated saved value: 0
    kdb_trap_printk = saved_trap_printk;

    kdb_trap_printk--;

    Problem2: Now, kdb_trap_printk == -1 and will stay like this.
    It means that all messages will get passed to kdb from
    now on.

    This patch removes the racy saved_trap_printk handling. Instead, the
    recursion is prevented by a check for the locked CPU.

    The solution is still kind of racy. A non-related printk(), from
    another process, might get trapped by vkdb_printf(). And the wanted
    printk() might not get trapped because kdb_printf_cpu is assigned. But
    this problem existed even with the original code.

    A proper solution would be to get_cpu() before setting kdb_trap_printk
    and trap messages only from this CPU. I am not sure if it is worth the
    effort, though.

    In fact, the race is very theoretical. When kdb is running any of the
    commands that use kdb_trap_printk there is a single active CPU and the
    other CPUs should be in a holding pen inside kgdb_cpu_enter().

    The only time this is violated is when there is a timeout waiting for
    the other CPUs to report to the holding pen.

    Finally, note that the situation is a bit schizophrenic. vkdb_printf()
    explicitly allows recursion but only from KDB code that calls
    kdb_printf() directly. On the other hand, the generic printk()
    recursion is not allowed because it might cause an infinite loop. This
    is why we could not hide the decision inside vkdb_printf() easily.

    Link: http://lkml.kernel.org/r/1480412276-16690-4-git-send-email-pmladek@suse.com
    Signed-off-by: Petr Mladek
    Cc: Daniel Thompson
    Cc: Jason Wessel
    Cc: Peter Zijlstra
    Cc: Sergey Senozhatsky
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Petr Mladek
     
  • kdb_printf_lock does not prevent other CPUs from entering the critical
    section because it is ignored when KDB_STATE_PRINTF_LOCK is set.

    The problematic situation might look like:

    CPU0 CPU1

    vkdb_printf()
    if (!KDB_STATE(PRINTF_LOCK))
    KDB_STATE_SET(PRINTF_LOCK);
    spin_lock_irqsave(&kdb_printf_lock, flags);

    vkdb_printf()
    if (!KDB_STATE(PRINTF_LOCK))

    BANG: The PRINTF_LOCK state is set and CPU1 is entering the critical
    section without spinning on the lock.

    The problem is that the code tries to implement locking using two state
    variables that are not handled atomically. Well, we need a custom
    locking because we want to allow reentering the critical section on the
    very same CPU.

    Let's use solution from Petr Zijlstra that was proposed for a similar
    scenario, see
    https://lkml.kernel.org/r/20161018171513.734367391@infradead.org

    This patch uses the same trick with cmpxchg(). The only difference is
    that we want to handle only recursion from the same context and
    therefore we disable interrupts.

    In addition, KDB_STATE_PRINTF_LOCK is removed. In fact, we are not able
    to set it a non-racy way.

    Link: http://lkml.kernel.org/r/1480412276-16690-3-git-send-email-pmladek@suse.com
    Signed-off-by: Petr Mladek
    Reviewed-by: Daniel Thompson
    Cc: Jason Wessel
    Cc: Peter Zijlstra
    Cc: Sergey Senozhatsky
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Petr Mladek
     
  • kdb_event state variable is only set but never checked in the kernel
    code.

    http://www.spinics.net/lists/kdb/msg01733.html suggests that this
    variable affected WARN_CONSOLE_UNLOCKED() in the original
    implementation. But this check never went upstream.

    The semantic is unclear and racy. The value is updated after the
    kdb_printf_lock is acquired and after it is released. It should be
    symmetric at minimum. The value should be manipulated either inside or
    outside the locked area.

    Fortunately, it seems that the original function is gone and we could
    simply remove the state variable.

    Link: http://lkml.kernel.org/r/1480412276-16690-2-git-send-email-pmladek@suse.com
    Signed-off-by: Petr Mladek
    Suggested-by: Daniel Thompson
    Cc: Jason Wessel
    Cc: Peter Zijlstra
    Cc: Sergey Senozhatsky
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Petr Mladek
     
  • We've got a delay loop waiting for secondary CPUs. That loop uses
    loops_per_jiffy. However, loops_per_jiffy doesn't actually mean how
    many tight loops make up a jiffy on all architectures. It is quite
    common to see things like this in the boot log:

    Calibrating delay loop (skipped), value calculated using timer
    frequency.. 48.00 BogoMIPS (lpj=24000)

    In my case I was seeing lots of cases where other CPUs timed out
    entering the debugger only to print their stack crawls shortly after the
    kdb> prompt was written.

    Elsewhere in kgdb we already use udelay(), so that should be safe enough
    to use to implement our timeout. We'll delay 1 ms for 1000 times, which
    should give us a full second of delay (just like the old code wanted)
    but allow us to notice that we're done every 1 ms.

    [akpm@linux-foundation.org: simplifications, per Daniel]
    Link: http://lkml.kernel.org/r/1477091361-2039-1-git-send-email-dianders@chromium.org
    Signed-off-by: Douglas Anderson
    Reviewed-by: Daniel Thompson
    Cc: Jason Wessel
    Cc: Brian Norris
    Cc: [4.0+]
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Douglas Anderson
     

13 Dec, 2016

1 commit

  • Commit 4bcc595ccd80 ("printk: reinstate KERN_CONT for printing
    continuation lines") allows to define more message headers for a single
    message. The motivation is that continuous lines might get mixed.
    Therefore it make sense to define the right log level for every piece of
    a cont line.

    This patch introduces printk_skip_headers() that will skip all headers
    and uses it in the kdb code instead of printk_skip_level().

    This approach helps to fix other printk_skip_level() users
    independently.

    Link: http://lkml.kernel.org/r/1478695291-12169-3-git-send-email-pmladek@suse.com
    Signed-off-by: Petr Mladek
    Cc: Joe Perches
    Cc: Sergey Senozhatsky
    Cc: Steven Rostedt
    Cc: Jason Wessel
    Cc: Jaroslav Kysela
    Cc: Takashi Iwai
    Cc: Chris Mason
    Cc: Josef Bacik
    Cc: David Sterba
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Petr Mladek
     

22 Feb, 2016

1 commit

  • It may be useful to debug writes to the readonly sections of memory,
    so provide a cmdline "rodata=off" to allow for this. This can be
    expanded in the future to support "log" and "write" modes, but that
    will need to be architecture-specific.

    This also makes KDB software breakpoints more usable, as read-only
    mappings can now be disabled on any kernel.

    Suggested-by: H. Peter Anvin
    Signed-off-by: Kees Cook
    Cc: Andy Lutomirski
    Cc: Arnd Bergmann
    Cc: Borislav Petkov
    Cc: Brian Gerst
    Cc: David Brown
    Cc: Denys Vlasenko
    Cc: Emese Revfy
    Cc: Linus Torvalds
    Cc: Mathias Krause
    Cc: Michael Ellerman
    Cc: PaX Team
    Cc: Peter Zijlstra
    Cc: Thomas Gleixner
    Cc: kernel-hardening@lists.openwall.com
    Cc: linux-arch
    Link: http://lkml.kernel.org/r/1455748879-21872-3-git-send-email-keescook@chromium.org
    Signed-off-by: Ingo Molnar

    Kees Cook
     

05 Dec, 2015

1 commit

  • Makes it easier to handle init vs core cleanly, though the change is
    fairly invasive across random architectures.

    It simplifies the rbtree code immediately, however, while keeping the
    core data together in the same cachline (now iff the rbtree code is
    enabled).

    Acked-by: Peter Zijlstra
    Reviewed-by: Josh Poimboeuf
    Signed-off-by: Rusty Russell
    Signed-off-by: Jiri Kosina

    Rusty Russell
     

20 Feb, 2015

8 commits

  • On non-developer devices, kgdb prevents the device from rebooting
    after a panic.

    Incase of panics and exceptions, to allow the device to reboot, prevent
    entering debug mode to avoid getting stuck waiting for the user to
    interact with debugger.

    To avoid entering the debugger on panic/exception without any extra
    configuration, panic_timeout is being used which can be set via
    /proc/sys/kernel/panic at run time and CONFIG_PANIC_TIMEOUT sets the
    default value.

    Setting panic_timeout indicates that the user requested machine to
    perform unattended reboot after panic. We dont want to get stuck waiting
    for the user input incase of panic.

    Cc: Andrew Morton
    Cc: kgdb-bugreport@lists.sourceforge.net
    Cc: linux-kernel@vger.kernel.org
    Cc: Android Kernel Team
    Cc: John Stultz
    Cc: Sumit Semwal
    Signed-off-by: Colin Cross
    [Kiran: Added context to commit message.
    panic_timeout is used instead of break_on_panic and
    break_on_exception to honor CONFIG_PANIC_TIMEOUT
    Modified the commit as per community feedback]
    Signed-off-by: Kiran Raparthy
    Signed-off-by: Daniel Thompson
    Signed-off-by: Jason Wessel

    Colin Cross
     
  • All current callers of kdb_getstr() can pass constant pointers via the
    prompt argument. This patch adds a const qualification to make explicit
    the fact that this is safe.

    Signed-off-by: Daniel Thompson
    Signed-off-by: Jason Wessel

    Daniel Thompson
     
  • Currently kdb allows the output of comamnds to be filtered using the
    | grep feature. This is useful but does not permit the output emitted
    shortly after a string match to be examined without wading through the
    entire unfiltered output of the command. Such a feature is particularly
    useful to navigate function traces because these traces often have a
    useful trigger string *before* the point of interest.

    This patch reuses the existing filtering logic to introduce a simple
    forward search to kdb that can be triggered from the more prompt.

    Signed-off-by: Daniel Thompson
    Signed-off-by: Jason Wessel

    Daniel Thompson
     
  • Currently when the "| grep" feature is used to filter the output of a
    command then the prompt is not displayed for the subsequent command.
    Likewise any characters typed by the user are also not echoed to the
    display. This rather disconcerting problem eventually corrects itself
    when the user presses Enter and the kdb_grepping_flag is cleared as
    kdb_parse() tries to make sense of whatever they typed.

    This patch resolves the problem by moving the clearing of this flag
    from the middle of command processing to the beginning.

    Signed-off-by: Daniel Thompson
    Signed-off-by: Jason Wessel

    Daniel Thompson
     
  • Issuing a stack dump feels ergonomically wrong when entering due to NMI.

    Entering due to NMI is normally a reaction to a user request, either the
    NMI button on a server or a "magic knock" on a UART. Therefore the
    backtrace behaviour on entry due to NMI should be like SysRq-g (no stack
    dump) rather than like oops.

    Note also that the stack dump does not offer any information that
    cannot be trivial retrieved using the 'bt' command.

    Signed-off-by: Daniel Thompson
    Signed-off-by: Jason Wessel

    Daniel Thompson
     
  • Currently when kdb traps printk messages then the raw log level prefix
    (consisting of '\001' followed by a numeral) does not get stripped off
    before the message is issued to the various I/O handlers supported by
    kdb. This causes annoying visual noise as well as causing problems
    grepping for ^. It is also a change of behaviour compared to normal usage
    of printk() usage. For example -h ends up with different output to
    that of kdb's "sr h".

    This patch addresses the problem by stripping log levels from messages
    before they are issued to the I/O handlers. printk() which can also
    act as an i/o handler in some cases is special cased; if the caller
    provided a log level then the prefix will be preserved when sent to
    printk().

    The addition of non-printable characters to the output of kdb commands is a
    regression, albeit and extremely elderly one, introduced by commit
    04d2c8c83d0e ("printk: convert the format for KERN_ to a 2 byte
    pattern"). Note also that this patch does *not* restore the original
    behaviour from v3.5. Instead it makes printk() from within a kdb command
    display the message without any prefix (i.e. like printk() normally does).

    Signed-off-by: Daniel Thompson
    Cc: Joe Perches
    Cc: stable@vger.kernel.org
    Signed-off-by: Jason Wessel

    Daniel Thompson
     
  • There was a follow on replacement patch against the prior
    "kgdb: Timeout if secondary CPUs ignore the roundup".

    See: https://lkml.org/lkml/2015/1/7/442

    This patch is the delta vs the patch that was committed upstream:
    * Fix an off-by-one error in kdb_cpu().
    * Replace NR_CPUS with CONFIG_NR_CPUS to tell checkpatch that we
    really want a static limit.
    * Removed the "KGDB: " prefix from the pr_crit() in debug_core.c
    (kgdb-next contains a patch which introduced pr_fmt() to this file
    to the tag will now be applied automatically).

    Cc: Daniel Thompson
    Cc:
    Signed-off-by: Jason Wessel

    Jason Wessel
     
  • The output of KDB 'summary' command should report MemTotal, MemFree
    and Buffers output in kB. Current codes report in unit of pages.

    A define of K(x) as
    is defined in the code, but not used.

    This patch would apply the define to convert the values to kB.
    Please include me on Cc on replies. I do not subscribe to linux-kernel.

    Signed-off-by: Jay Lan
    Cc:
    Signed-off-by: Jason Wessel

    Jay Lan
     

23 Jan, 2015

1 commit

  • Pull module and param fixes from Rusty Russell:
    "Surprising number of fixes this merge window :(

    The first two are minor fallout from the param rework which went in
    this merge window.

    The next three are a series which fixes a longstanding (but never
    previously reported and unlikely , so no CC stable) race between
    kallsyms and freeing the init section.

    Finally, a minor cleanup as our module refcount will now be -1 during
    unload"

    * tag 'fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux:
    module: make module_refcount() a signed integer.
    module: fix race in kallsyms resolution during module load success.
    module: remove mod arg from module_free, rename module_memfree().
    module_arch_freeing_init(): new hook for archs before module->module_init freed.
    param: fix uninitialized read with CONFIG_DEBUG_LOCK_ALLOC
    param: initialize store function to NULL if not available.

    Linus Torvalds
     

22 Jan, 2015

1 commit

  • James Bottomley points out that it will be -1 during unload. It's
    only used for diagnostics, so let's not hide that as it could be a
    clue as to what's gone wrong.

    Cc: Jason Wessel
    Acked-and-documention-added-by: James Bottomley
    Reviewed-by: Masami Hiramatsu
    Signed-off-by: Rusty Russell

    Rusty Russell
     

11 Nov, 2014

2 commits

  • -Convert printk( to pr_foo()
    -Add pr_fmt
    -Coalesce formats

    Cc: Jason Wessel
    Cc: Andrew Morton
    Cc: Joe Perches
    Signed-off-by: Fabian Frederick
    Signed-off-by: Jason Wessel

    Fabian Frederick
     
  • Currently if an active CPU fails to respond to a roundup request the CPU
    that requested the roundup will become stuck. This needlessly reduces the
    robustness of the debugger.

    This patch introduces a timeout allowing the system state to be examined
    even when the system contains unresponsive processors. It also modifies
    kdb's cpu command to make it censor attempts to switch to unresponsive
    processors and to report their state as (D)ead.

    Signed-off-by: Daniel Thompson
    Cc: Jason Wessel
    Signed-off-by: Andrew Morton
    Signed-off-by: Jason Wessel

    Daniel Thompson