Re: [BUG] KASAN: slab-use-after-free in bpf_skc_to_* via fexit on sk_common_release
From: MahnurA
Date: Fri Dec 04 2026 - 09:19:16 EST
On Fri Aug 21, 2026 at 7:30 PM CEST, Andrii Nakryiko wrote:
> +/* 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;
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) exit
Causal 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:
>>> 0 | (79) r1 = *(u64 *)(r1 +0)
1 | (85) call bpf_skc_to_tcp_sock#137
2 | (b7) r0 = 0
Suggestion:
Pass a value with one of the accepted pointer or scalar types for this call.