KASAN: slab-use-after-free in bpf_skc_to_* via fexit on sk_common_release
Subsystems (3)
From: Syeda Mahnur Asif <[email protected]>
Hi,
While running an fexit BPF program on sk_common_release() that calls bpf_skc_to_tcp_sock(), my fuzzer hit a KASAN slab-use-after-free during network namespace teardown.
Reproduced on Kernel: Kernel: 7.4.0-rc4
(built from the mainline v7.4-rc4 tarball)
sk_common_release() drops the last reference to sk and frees it. The fexit program then runs on the freed pointer and bpf_skc_to_* helpers read sk->sk_state, tripping KASAN. The IGMP control socket created in igmp_net_init() has no other holders, so the free happens inside the traced function and netns teardown reproduces it reliably.
Reproducer:
BPF program: Any bpf_skc_to_* helper taking the sk from the context as the argument
Trigger: C program (any short-lived network namespace should trigger it)
#define _GNU_SOURCE
#include <sched.h>
#include <unistd.h>
int main(void)
{
unshare(CLONE_NEWNET);
return 0;
}KASAN splat below. I can test patches.
[ 1983.388616] BUG: KASAN: slab-use-after-free in bpf_skc_to_tcp_sock (./include/net/sock.h:2295 net/core/filter.c:12345 net/core/filter.c:12343) [ 1983.388631] Read of size 1 at addr ffff888102f48912 by task kworker/u4:0/13 [ 1983.388637] [ 1983.388643] CPU: 0 UID: 0 PID: 13 Comm: kworker/u4:0 Not tainted 7.4.0-rc4 #2 PREEMPT(full) [ 1983.388652] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014 [ 1983.388658] Workqueue: netns cleanup_net [ 1983.388671] Call Trace: [ 1983.388673] <TASK> [ 1983.388678] dump_stack_lvl (lib/dump_stack.c:94 lib/dump_stack.c:120) [ 1983.388687] print_report (mm/kasan/report.c:378 mm/kasan/report.c:482) [ 1983.388714] kasan_report (mm/kasan/report.c:595) [ 1983.388766] bpf_skc_to_tcp_sock (./include/net/sock.h:2295 net/core/filter.c:12345 net/core/filter.c:12343) [ 1983.388773] bpf_prog_76aaea67dcf4f15b_bpf_prog4321+0x3b/0x3f [ 1983.388786] bpf_trampoline_6442594000+0x64/0xd3 [ 1983.388792] ? ip_mc_drop_socket (net/ipv4/igmp.c:2898) [ 1983.388803] udp_lib_close (./include/net/udp.h:322) [ 1983.388814] inet_release (net/ipv4/af_inet.c:443) [ 1983.388824] sock_release (net/socket.c:735 net/socket.c:763) [ 1983.388843] igmp_net_exit (./include/net/inet_common.h:72 net/ipv4/igmp.c:3245) [ 1983.388851] ops_undo_list (net/core/net_namespace.c:200 net/core/net_namespace.c:253) [ 1983.388885] cleanup_net (net/core/net_namespace.c:713) [ 1983.388916] process_one_work (kernel/workqueue.c:3495) [ 1983.388955] worker_thread (kernel/workqueue.c:3578 kernel/workqueue.c:3659) [ 1983.388973] kthread (kernel/kthread.c:436) [ 1983.388992] ret_from_fork (arch/x86/kernel/process.c:158) [ 1983.389024] ret_from_fork_asm (arch/x86/entry/entry_64.S:245) [ 1983.389042] </TASK> [ 1983.389045] [ 1983.389048] Allocated by task 88: [ 1983.389052] kasan_save_stack (mm/kasan/common.c:57) [ 1983.389062] kasan_save_track (mm/kasan/common.c:79) [ 1983.389069] kasan_save_alloc_info (mm/kasan/generic.c:585) [ 1983.389076] __kasan_slab_alloc (mm/kasan/common.c:354 mm/kasan/common.c:380) [ 1983.389084] kmem_cache_alloc_noprof (./include/linux/kasan.h:257 mm/slub.c:4720 mm/slub.c:5033 mm/slub.c:5047) [ 1983.389092] sk_prot_alloc (net/core/sock.c:2275 (discriminator 2)) [ 1983.389101] sk_alloc (net/core/sock.c:2337) [ 1983.389107] inet_create (net/ipv4/af_inet.c:334 net/ipv4/af_inet.c:260) [ 1983.389113] __sock_create (net/socket.c:1679) [ 1983.389120] sock_create_kern (net/socket.c:1755) [ 1983.389128] inet_ctl_sock_create (net/ipv4/af_inet.c:1642) [ 1983.389135] igmp_net_init (net/ipv4/igmp.c:3223) [ 1983.389143] ops_init (net/core/net_namespace.c:137) [ 1983.389149] setup_net (net/core/net_namespace.c:448) [ 1983.389164] copy_net_ns (net/core/net_namespace.c:589) [ 1983.389171] create_new_namespaces (kernel/nsproxy.c:132) [ 1983.389182] unshare_nsproxy_namespaces (kernel/nsproxy.c:234 (discriminator 4)) [ 1983.389190] ksys_unshare (kernel/fork.c:3306) [ 1983.389190] __x64_sys_unshare (kernel/fork.c:3372 kernel/fork.c:3370 kernel/fork.c:3370) [ 1983.389190] x64_sys_call (./arch/x86/include/generated/asm/syscalls_64.h:273) [ 1983.389190] do_syscall_64 (arch/x86/entry/syscall_64.c:61 arch/x86/entry/syscall_64.c:89) [ 1983.389190] entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121) [ 1983.389190] [ 1983.389190] The buggy address belongs to the object at ffff888102f48900 [ 1983.389190] which belongs to the cache UDP of size 1984 [ 1983.389190] The buggy address is located 18 bytes inside of [ 1983.389190] freed 1984-byte region [ffff888102f48900, ffff888102f490c0) [ 1983.389190] [ 1983.389190] The buggy address belongs to the physical page: [ 1983.389190] page: refcount:0 mapcount:0 mapping:0000000000000000 index:0xffff888102f491c0 pfn:0x102f48 [ 1983.389190] head: order:3 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0 [ 1983.389190] flags: 0x200000000000240(workingset|head|node=0|zone=2) [ 1983.389190] page_type: f5(slab) [ 1983.389190] raw: 0200000000000240 ffff888102d29040 ffff888102d28308 ffff888102d28308 [ 1983.389190] raw: ffff888102f491c0 00000002000e0002 00000000f5000000 0000000000000000 [ 1983.389190] head: 0200000000000240 ffff888102d29040 ffff888102d28308 ffff888102d28308 [ 1983.389190] head: ffff888102f491c0 00000002000e0002 00000000f5000000 0000000000000000 [ 1983.389190] head: 0200000000000003 fffffffffffffe01 00000000ffffffff 00000000ffffffff [ 1983.389190] head: 0000000000000000 0000000000000000 00000000ffffffff 0000000000000008 [ 1983.389190] page dumped because: kasan: bad access detected [ 1983.389190] [ 1983.389190] Memory state around the buggy address: [ 1983.389190] ffff888102f48800: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc [ 1983.389190] ffff888102f48880: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc [ 1983.389190] >ffff888102f48900: fa fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb [ 1983.389190] ^ [ 1983.389190] ffff888102f48980: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb [ 1983.389190] ffff888102f48a00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb [ 1983.389190] ==================================================================