Skip to content

Commit 9f7d416

Browse files
dvyukovIngo Molnar
authored and
Ingo Molnar
committed
kprobes: Unpoison stack in jprobe_return() for KASAN
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: [ ] [<ffffffff82d2b849>] dump_stack+0x12e/0x185 [ ] [<ffffffff817d3169>] kasan_report+0x489/0x4b0 [ ] [<ffffffff817d31a9>] __asan_report_load1_noabort+0x19/0x20 [ ] [<ffffffff82d49529>] memcmp+0xe9/0x150 [ ] [<ffffffff82df7486>] depot_save_stack+0x176/0x5c0 [ ] [<ffffffff817d2031>] save_stack+0xb1/0xd0 [ ] [<ffffffff817d27f2>] kasan_slab_free+0x72/0xc0 [ ] [<ffffffff817d05b8>] kfree+0xc8/0x2a0 [ ] [<ffffffff85b03f19>] skb_free_head+0x79/0xb0 [ ] [<ffffffff85b0900a>] skb_release_data+0x37a/0x420 [ ] [<ffffffff85b090ff>] skb_release_all+0x4f/0x60 [ ] [<ffffffff85b11348>] consume_skb+0x138/0x370 [ ] [<ffffffff8676ad7b>] sctp_chunk_put+0xcb/0x180 [ ] [<ffffffff8676ae88>] sctp_chunk_free+0x58/0x70 [ ] [<ffffffff8677fa5f>] sctp_inq_pop+0x68f/0xef0 [ ] [<ffffffff8675ee36>] sctp_assoc_bh_rcv+0xd6/0x4b0 [ ] [<ffffffff8677f2c1>] sctp_inq_push+0x131/0x190 [ ] [<ffffffff867bad69>] 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 <[email protected]> Acked-by: Masami Hiramatsu <[email protected]> Reviewed-by: Mark Rutland <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: Andrey Ryabinin <[email protected]> Cc: Lorenzo Pieralisi <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Will Deacon <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Ananth N Mavinakayanahalli <[email protected]> Cc: Anil S Keshavamurthy <[email protected]> Cc: "David S. Miller" <[email protected]> Cc: Masami Hiramatsu <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent 9254139 commit 9f7d416

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

arch/arm64/kernel/sleep.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ ENTRY(_cpu_resume)
135135

136136
#ifdef CONFIG_KASAN
137137
mov x0, sp
138-
bl kasan_unpoison_remaining_stack
138+
bl kasan_unpoison_task_stack_below
139139
#endif
140140

141141
ldp x19, x20, [x29, #16]

arch/x86/kernel/kprobes/core.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include <linux/kallsyms.h>
5151
#include <linux/ftrace.h>
5252
#include <linux/frame.h>
53+
#include <linux/kasan.h>
5354

5455
#include <asm/text-patching.h>
5556
#include <asm/cacheflush.h>
@@ -1081,6 +1082,9 @@ void jprobe_return(void)
10811082
{
10821083
struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
10831084

1085+
/* Unpoison stack redzones in the frames we are going to jump over. */
1086+
kasan_unpoison_stack_above_sp_to(kcb->jprobe_saved_sp);
1087+
10841088
asm volatile (
10851089
#ifdef CONFIG_X86_64
10861090
" xchg %%rbx,%%rsp \n"

include/linux/kasan.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static inline void kasan_disable_current(void)
4444
void kasan_unpoison_shadow(const void *address, size_t size);
4545

4646
void kasan_unpoison_task_stack(struct task_struct *task);
47+
void kasan_unpoison_stack_above_sp_to(const void *watermark);
4748

4849
void kasan_alloc_pages(struct page *page, unsigned int order);
4950
void kasan_free_pages(struct page *page, unsigned int order);
@@ -85,6 +86,7 @@ size_t kasan_metadata_size(struct kmem_cache *cache);
8586
static inline void kasan_unpoison_shadow(const void *address, size_t size) {}
8687

8788
static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
89+
static inline void kasan_unpoison_stack_above_sp_to(const void *watermark) {}
8890

8991
static inline void kasan_enable_current(void) {}
9092
static inline void kasan_disable_current(void) {}

mm/kasan/kasan.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <linux/string.h>
3535
#include <linux/types.h>
3636
#include <linux/vmalloc.h>
37+
#include <linux/bug.h>
3738

3839
#include "kasan.h"
3940
#include "../slab.h"
@@ -62,7 +63,7 @@ void kasan_unpoison_shadow(const void *address, size_t size)
6263
}
6364
}
6465

65-
static void __kasan_unpoison_stack(struct task_struct *task, void *sp)
66+
static void __kasan_unpoison_stack(struct task_struct *task, const void *sp)
6667
{
6768
void *base = task_stack_page(task);
6869
size_t size = sp - base;
@@ -77,9 +78,24 @@ void kasan_unpoison_task_stack(struct task_struct *task)
7778
}
7879

7980
/* Unpoison the stack for the current task beyond a watermark sp value. */
80-
asmlinkage void kasan_unpoison_remaining_stack(void *sp)
81+
asmlinkage void kasan_unpoison_task_stack_below(const void *watermark)
8182
{
82-
__kasan_unpoison_stack(current, sp);
83+
__kasan_unpoison_stack(current, watermark);
84+
}
85+
86+
/*
87+
* Clear all poison for the region between the current SP and a provided
88+
* watermark value, as is sometimes required prior to hand-crafted asm function
89+
* returns in the middle of functions.
90+
*/
91+
void kasan_unpoison_stack_above_sp_to(const void *watermark)
92+
{
93+
const void *sp = __builtin_frame_address(0);
94+
size_t size = watermark - sp;
95+
96+
if (WARN_ON(sp > watermark))
97+
return;
98+
kasan_unpoison_shadow(sp, size);
8399
}
84100

85101
/*

0 commit comments

Comments
 (0)