BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Cen Zhang (Microsoft)" <blbllhy@gmail.com>
Cc: mptcp@lists.linux.dev, bpf@vger.kernel.org
Subject: Re: [PATCH net v3 1/2] tcp: fix use-after-free in do_tcp_getsockopt(TCP_CONGESTION)
Date: Fri, 28 Aug 2026 23:55:18 +0000	[thread overview]
Message-ID: <20260828235519.3FC1B1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <d3f97f1acbf0010898148be6e6406e4b8b4a5c84.1787870710.git.blbllhy@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] BPF getsockopt helper can race with user-space getsockopt(TCP_SAVED_SYN) causing a use-after-free on tp->saved_syn.
--

commit aaf314b305f9bf7c88d6539be45ea64300819450
Author: Cen Zhang (Microsoft Security FORGE Labs) <blbllhy@gmail.com>

tcp: fix use-after-free in do_tcp_getsockopt(TCP_CONGESTION)

This commit fixes a use-after-free in do_tcp_getsockopt() for TCP_CONGESTION
by holding rcu_read_lock() while reading the congestion control name. It also
annotates icsk_ca_ops updates with WRITE_ONCE() to address KCSAN data-races.

Link: https://lore.kernel.org/all/20260821182449.79785-2-blbllhy@gmail.com/

> diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
> index 455441f1b6949..55ca74f6029fa 100644
> --- a/net/ipv4/tcp.c
> +++ b/net/ipv4/tcp.c
> @@ -4576,16 +4576,24 @@ int do_tcp_getsockopt(struct sock *sk, int level,
>  		val = !inet_csk_in_pingpong_mode(sk);
>  		break;
>  
> -	case TCP_CONGESTION:
> +	case TCP_CONGESTION: {
> +		char ca_name[TCP_CA_NAME_MAX] = {};
> +
>  		if (copy_from_sockptr(&len, optlen, sizeof(int)))
>  			return -EFAULT;
>  		len = min_t(unsigned int, len, TCP_CA_NAME_MAX);
>  		if (copy_to_sockptr(optlen, &len, sizeof(int)))
>  			return -EFAULT;
> -		if (copy_to_sockptr(optval, icsk->icsk_ca_ops->name, len))
> +
> +		rcu_read_lock();
> +		memcpy(ca_name, READ_ONCE(icsk->icsk_ca_ops)->name,
> +		       sizeof(ca_name));
> +		rcu_read_unlock();
> +
> +		if (copy_to_sockptr(optval, ca_name, len))
>  			return -EFAULT;
>  		return 0;
> -
> +	}
>  	case TCP_ULP:
>  		if (copy_from_sockptr(&len, optlen, sizeof(int)))
>  			return -EFAULT;

[Severity: High]
This is a pre-existing issue, but while reviewing this TCP getsockopt
fix, I noticed another potential use-after-free regression in the BPF
getsockopt helper.

In net/core/filter.c:sol_tcp_sockopt():

	if (getopt) {
		if (optname == TCP_SAVED_SYN) {
			struct tcp_sock *tp = tcp_sk(sk);

			if (!tp->saved_syn ||
			    *optlen > tcp_saved_syn_len(tp->saved_syn))
				return -EINVAL;
			memcpy(optval, tp->saved_syn->data, *optlen);

Since BPF iterators run without holding the socket lock, can this
locklessly read tp->saved_syn and call memcpy() on its data?

If a concurrent user-space thread calls getsockopt(TCP_SAVED_SYN) on the
same socket, it acquires sockopt_lock_sock(sk) and frees the memory via
kfree(tp->saved_syn) inside tcp_saved_syn_free().

Will the BPF program's memcpy() then dereference the freed memory?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1787870710.git.blbllhy@gmail.com?part=1

  parent reply	other threads:[~2026-08-28 23:55 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-27 23:55 [PATCH net v3 0/2] tcp: fix use-after-free in do_tcp_getsockopt() Cen Zhang (Microsoft)
2026-08-27 23:55 ` [PATCH net v3 1/2] tcp: fix use-after-free in do_tcp_getsockopt(TCP_CONGESTION) Cen Zhang (Microsoft)
2026-08-28  3:26   ` Jiayuan Chen
2026-08-28  9:41   ` Matthieu Baerts
2026-08-28 12:38   ` Breno Leitao
2026-08-28 13:02     ` Cen Zhang (Microsoft)
2026-08-28 14:17       ` Breno Leitao
2026-08-28 23:55   ` sashiko-bot [this message]
2026-08-27 23:55 ` [PATCH net v3 2/2] tcp: fix use-after-free in do_tcp_getsockopt(TCP_CC_INFO) Cen Zhang (Microsoft)
2026-08-28  3:14   ` Jiayuan Chen
2026-08-28  9:42   ` Matthieu Baerts

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=20260828235519.3FC1B1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=blbllhy@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=mptcp@lists.linux.dev \
    --cc=sashiko-reviews@lists.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