KASAN: slab-use-after-free in bpf_skc_to_* via fexit on sk_common_release
Subsystems (4)
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] ==================================================================
Not a new issue. I guess it's time to close it.
How about something like this:
include/linux/bpf_verifier.h +1 -0
@@ -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);
kernel/bpf/btf.c +36 -0
@@ -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 ;
kernel/bpf/core.c +2 -0
@@ -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 )
Quoted 38 lines · Andrii Nakryiko
kernel/bpf/btf.c +34
-0+/* 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) {
kernel/bpf/btf.c bool btf_ctx_access(int off, int size, enum bpf_access_type type, · line 7721
Quoted 8 lines
kernel/bpf/btf.c +2
-0@@ -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 ;
This plugs the hole temporarily. Not sure if this is the right way or what could be a better way. The list of functions is probably not exhaustive(?), and will need to be updated in line with the kernel. I also think that a bunch of such untrusted pointers probably occur in some way with other helpers and normal kernel functioning so it is a bit of a whack-a-mole situation. The verifier is correctly rejecting the program, but now the error message is not clear enough I think to highlight that this is an attachment point issue specifically. An example below:
libbpf: prog 'bpf_prog0': -- BEGIN PROG LOAD LOG --
0: R1=ctx() R10=fp0
; int BPF_PROG(bpf_prog0, struct sock *sk) { @ bpf_prog0.c:7
0: (79) r1 = *(u64 *)(r1 +0)
func 'sk_common_release' arg0 has btf_id 145052 type STRUCT 'sock'
1: R1=untrusted_ptr_sock()
; bpf_skc_to_tcp_sock(sk); @ bpf_prog0.c:11
1: (85) call bpf_skc_to_tcp_sock#137
R1 type=untrusted_ptr_ expected=sock_common, sock, tcp_sock, xdp_sock, ptr_, trusted_ptr_
Verification failed: Call Type Safety: Invalid call argument
Reason:
The first argument (R1) to bpf_skc_to_tcp_sock does not satisfy the verifier contract: it has type untrusted_ptr_, but this argument accepts sock_common, sock, tcp_sock, xdp_sock, ptr_, trusted_ptr_.
At:
bpf_prog0 @ bpf_prog0.c:11:5
Source context:
9 | ...
10 | ...
>>> 11 | bpf_skc_to_tcp_sock(sk);
| ^-- error: invalid first argument (R1) for bpf_skc_to_tcp_sock
12 | ...
13 | ...
Instruction context:
0 | (79) r1 = *(u64 *)(r1 +0)
>>> 1 | (85) call bpf_skc_to_tcp_sock#137
2 | (b7) r0 = 0
3 | (95) exitCausal path:
bpf_prog0 @ bpf_prog0.c:7:5
Source context:
5 | ...
6 | ...
>>> 7 | int BPF_PROG(bpf_prog0, struct sock *sk) {
| ^-- update: R1 changed from context pointer at offset 0 to untrusted_ptr_ type=(struct
| sock) at offset 0
8 | ...
9 | ...Instruction context:
Quoted 1 line
Quoted 1 line
Quoted 1 line
0 | (79) r1 = *(u64 *)(r1 +0)
1 | (85) call bpf_skc_to_tcp_sock#137
2 | (b7) r0 = 0Suggestion:
Pass a value with one of the accepted pointer or scalar types for this call.
Quoted 2 lines · MahnurA
This plugs the hole temporarily. Not sure if this is the right way or what could be a better way.
Pls wrap your emails to ~80 char per line.
imo the list is the least bad way.
raw_tp_null_args[], untrusted_lsm_hooks and noreturn_deny are manual lists too.
Quoted 3 lines
The verifier is correctly rejecting the program, but now the error message is not clear enough I think to highlight that this is an attachment point issue specifically.
Fair enough. btf_ctx_access() can say why, right after the "has btf_id" line. This goes on top of the diff. I didn't compile it:
bpf_log(log, "func '%s' arg%d has btf_id %d type %s '%s'\n",
tname, arg, info->btf_id, btf_type_str(t), __btf_name_by_offset(btf, t->name_off));
+ if (bpf_prog_args_freed(prog))
+ bpf_log(log, "func '%s' can free arg%d, marking it as untrusted\n",
+ tname, arg);
Then the log has:
func 'sk_common_release' arg0 has btf_id 145052 type STRUCT 'sock' func 'sk_common_release' can free arg0, marking it as untrusted