Re: [PATCH] perf/core: fix use-after-free in uprobe_perf_close

From: Peter Zijlstra
Date: Mon Apr 09 2018 - 07:48:54 EST


On Mon, Apr 09, 2018 at 07:03:46PM +0900, Prashant Bhole wrote:
> A use-after-free bug was caught by KASAN while running usdt related
> code. (BCC project. bcc/tests/python/test_usdt2.py)
>
<snip>
>
> Allocated by task 870:
> kasan_kmalloc+0xa0/0xd0
> kmem_cache_alloc_node+0x11a/0x430
> copy_process.part.19+0x11a0/0x41c0
> _do_fork+0x1be/0xa20
> do_syscall_64+0x198/0x490
> entry_SYSCALL_64_after_hwframe+0x3d/0xa2
>
> Freed by task 0:
> __kasan_slab_free+0x12e/0x180
> kmem_cache_free+0x102/0x4d0
> free_task+0xfe/0x160
> __put_task_struct+0x189/0x290
> delayed_put_task_struct+0x119/0x250
> rcu_process_callbacks+0xa6c/0x1b60
> __do_softirq+0x238/0x7ae
>
> The buggy address belongs to the object at ffff880384f9b480
> which belongs to the cache task_struct of size 12928
>
> It occurs because task_struct is freed before perf_event which refers
> to the task and task flags are checked while teardown of the event.
> perf_event_alloc() assigns task_struct to hw.target of perf_event,
> but there is no reference counting for it.
>
> As a fix we get_task_struct() in perf_event_alloc() at above mentioned
> assignment and put_task_struct() in _free_event().
>
> Signed-off-by: Prashant Bhole <bhole_prashant_q7@xxxxxxxxxxxxx>

Acked-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>

Ingo, can you make this happen, probably with a Cc-stable as well.