🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
2 messages
2 total messages Started by MahnurA Mon, 30 Nov 2026 15:12
[BUG] KASAN: slab-use-after-free in bpf_skc_to_* via fexit on sk_common_release
#24660
Author: MahnurA
Date: Mon, 30 Nov 2026 15:12
115 lines
16876 bytes
From: Syeda Mahnur Asif <s.mahnur.a@gmail.com>

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] ==================================================================
Re: [BUG] KASAN: slab-use-after-free in bpf_skc_to_* via fexit on sk_common_release
#24943
Author: "Alexei Starovoitov"
Date: Tue, 01 Dec 2026 03:20
83 lines
13865 bytes
Not a new issue. I guess it's time to close it.
How about something like this:

diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 50b3f8ab9720..5562e5d746e7 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -1739,6 +1739,7 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
 			    const struct bpf_prog *tgt_prog,
 			    u32 btf_id,
 			    struct bpf_attach_target_info *tgt_info);
+bool bpf_prog_args_freed(const struct bpf_prog *prog);
 int bpf_check_ext_prologue(struct bpf_verifier_log *log,
 			   const struct bpf_prog *tgt_prog, u32 btf_id);
 void bpf_free_kfunc_btf_tab(struct bpf_kfunc_btf_tab *tab);
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index f2d2f4fdeca5..0e400d63a994 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -7285,6 +7285,40 @@ static bool prog_args_trusted(const struct bpf_prog *prog)
 	}
 }
 
+/* Functions that can free the socket that they take as an argument */
+static const char * const sk_free_funcs[] = {
+	"__sk_free",
+	"__vsock_release",
+	"dgram_close",
+	"l2tp_ip6_close",
+	"l2tp_ip_close",
+	"mctp_sk_close",
+	"pep_sock_close",
+	"ping_close",
+	"pn_sock_close",
+	"raw_close",
+	"rawv6_close",
+	"sctp_close",
+	"sk_common_release",
+	"sk_destruct",
+	"sk_free",
+	"sk_prot_free",
+	"sock_gen_put",
+	"udp_lib_close",
+	"unix_release_sock",
+};
+
+bool bpf_prog_args_freed(const struct bpf_prog *prog)
+{
+	enum bpf_attach_type atype = prog->expected_attach_type;
+
+	if (prog->type != BPF_PROG_TYPE_TRACING || prog->aux->saved_dst_prog_type ||
+	    (atype != BPF_TRACE_FEXIT && atype != BPF_TRACE_FSESSION))
+		return false;
+	return match_string(sk_free_funcs, ARRAY_SIZE(sk_free_funcs),
+			    prog->aux->attach_func_name) >= 0;
+}
+
 int btf_ctx_arg_offset(const struct btf *btf, const struct btf_type *func_proto,
 		       u32 arg_no)
 {
@@ -7680,6 +7714,8 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
 	info->reg_type = PTR_TO_BTF_ID;
 	if (prog_args_trusted(prog))
 		info->reg_type |= PTR_TRUSTED;
+	else if (bpf_prog_args_freed(prog))
+		info->reg_type |= PTR_UNTRUSTED;
 
 	if (btf_param_match_suffix(btf, &args[arg], "__nullable"))
 		info->reg_type |= PTR_MAYBE_NULL;
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index facb0d6d2e8d..ec4dd50d2f1a 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -2881,6 +2881,8 @@ static u64 bpf_prog_hook_key(const struct bpf_prog *fp)
 {
 	if (IS_ENABLED(CONFIG_BPF_SYSCALL) && fp->aux->attach_btf_trace)
 		return btf_raw_tp_null_mask(fp);
+	if (IS_ENABLED(CONFIG_BPF_SYSCALL) && bpf_prog_args_freed(fp))
+		return U64_MAX;
 	if (fp->type == BPF_PROG_TYPE_TRACING && fp->expected_attach_type != BPF_TRACE_ITER)
 		return fp->aux->saved_dst_ctx_type;
 	if (fp->type == BPF_PROG_TYPE_TRACING || fp->type == BPF_PROG_TYPE_LSM)
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads