From: MahnurA <s.mahnur.a@gmail.com>
To: alexei.starovoitov@gmail.com, daniel@iogearbox.net,
andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com,
bpf@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
emil@etsalapatis.com, ihor.solodrai@linux.dev,
john.fastabend@gmail.com, jolsa@kernel.org, martin.lau@linux.dev,
s.mahnur.a@gmail.com, sdf@fomichev.me, song@kernel.org,
yonghong.song@linux.dev
Subject: Re: [BUG] KASAN: slab-use-after-free in bpf_skc_to_* via fexit on sk_common_release
Date: Fri, 4 Dec 2026 15:19:02 +0100 [thread overview]
Message-ID: <20261204141902.605584-1-s.mahnur.a@gmail.com> (raw)
In-Reply-To: <DN92M6I4LTVS.G22ON77JZ6KQ@gmail.com>
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.
next prev parent reply other threads:[~2026-12-04 14:19 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 15:12 [BUG] KASAN: slab-use-after-free in bpf_skc_to_* via fexit on sk_common_release MahnurA
2026-12-01 3:20 ` Alexei Starovoitov
2026-12-04 14:19 ` MahnurA [this message]
2026-12-04 18:39 ` Alexei Starovoitov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261204141902.605584-1-s.mahnur.a@gmail.com \
--to=s.mahnur.a@gmail.com \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=ihor.solodrai@linux.dev \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=sdf@fomichev.me \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox