Re: [RFC PATCH net] net: fix data-races around sk->sk_forward_alloc

From: Wang Liang
Date: Fri Nov 01 2024 - 02:25:12 EST



在 2024/10/31 22:08, Eric Dumazet 写道:
On Thu, Oct 31, 2024 at 1:06 PM Wang Liang <wangliang74@xxxxxxxxxx> wrote:
Syzkaller reported this warning:
Was this a public report ?
Yes,I find the report here (the C repo in the url is useful):

https://syzkaller.appspot.com/bug?id=3e9b62ff331dcc3a6c28c41207f3b9911828a46b
[ 65.568203][ C0] ------------[ cut here ]------------
[ 65.569339][ C0] WARNING: CPU: 0 PID: 16 at net/ipv4/af_inet.c:156 inet_sock_destruct+0x1c5/0x1e0
[ 65.575017][ C0] Modules linked in:
[ 65.575699][ C0] CPU: 0 UID: 0 PID: 16 Comm: ksoftirqd/0 Not tainted 6.12.0-rc5 #26
[ ...]
Oh the horror, this is completely wrong and unsafe anyway.

TCP listen path MUST be lockless, and stay lockless.

Ask yourself : Why would a listener even hold a pktoptions in the first place ?

Normally, each request socket can hold an ireq->pktopts (see in
tcp_v6_init_req())

The skb_clone_and_charge_r() happen later in tcp_v6_syn_recv_sock()

The correct fix is to _not_ call skb_clone_and_charge_r() for a
listener socket, of course, this never made _any_ sense.

The following patch should fix both TCP and DCCP, and as a bonus make
TCP SYN processing faster
for listeners requesting these IPV6_PKTOPTIONS things.
Thank you very much for your suggestion and patch!

However, the problem remains unsolved when I use the following patch to test.

Because skb_clone_and_charge_r() is still called when sk_state is TCP_LISTEN in discard tag.

So I modify the patch like this (it works after local test):

diff --git a/net/dccp/ipv6.c b/net/dccp/ipv6.c
index da5dba120bc9..2d07f7385783 100644
--- a/net/dccp/ipv6.c
+++ b/net/dccp/ipv6.c
@@ -618,7 +618,7 @@ static int dccp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
           by tcp. Feel free to propose better solution.
                                               --ANK (980728)
         */
-       if (np->rxopt.all)
+       if (np->rxopt.all && (sk->sk_state != DCCP_LISTEN))
                opt_skb = skb_clone_and_charge_r(skb, sk);

        if (sk->sk_state == DCCP_OPEN) { /* Fast path */
diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
index d71ab4e1efe1..0ab06ed78cac 100644
--- a/net/ipv6/tcp_ipv6.c
+++ b/net/ipv6/tcp_ipv6.c
@@ -1618,7 +1618,7 @@ int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
           by tcp. Feel free to propose better solution.
                                               --ANK (980728)
         */
-       if (np->rxopt.all)
+       if (np->rxopt.all && (sk->sk_state != TCP_LISTEN))
                opt_skb = skb_clone_and_charge_r(skb, sk);

        if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
@@ -1656,8 +1656,6 @@ int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
                                if (reason)
                                        goto reset;
                        }
-                       if (opt_skb)
-                               __kfree_skb(opt_skb);
                        return 0;
                }
        } else


diff --git a/net/dccp/ipv6.c b/net/dccp/ipv6.c
index da5dba120bc9a55c5fd9d6feda791b0ffc887423..d6649246188d72b3df6c74750779b7aa5910dcb7
100644
--- a/net/dccp/ipv6.c
+++ b/net/dccp/ipv6.c
@@ -618,7 +618,7 @@ static int dccp_v6_do_rcv(struct sock *sk, struct
sk_buff *skb)
by tcp. Feel free to propose better solution.
--ANK (980728)
*/
- if (np->rxopt.all)
+ if (np->rxopt.all && sk->sk_state != DCCP_LISTEN)
opt_skb = skb_clone_and_charge_r(skb, sk);

if (sk->sk_state == DCCP_OPEN) { /* Fast path */
diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
index d71ab4e1efe1c6598cf3d3e4334adf0881064ce9..e643dbaec9ccc92eb2d9103baf185c957ad1dd2e
100644
--- a/net/ipv6/tcp_ipv6.c
+++ b/net/ipv6/tcp_ipv6.c
@@ -1605,25 +1605,12 @@ int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
* is currently called with bh processing disabled.
*/

- /* Do Stevens' IPV6_PKTOPTIONS.
-
- Yes, guys, it is the only place in our code, where we
- may make it not affecting IPv4.
- The rest of code is protocol independent,
- and I do not like idea to uglify IPv4.
-
- Actually, all the idea behind IPV6_PKTOPTIONS
- looks not very well thought. For now we latch
- options, received in the last packet, enqueued
- by tcp. Feel free to propose better solution.
- --ANK (980728)
- */
- if (np->rxopt.all)
- opt_skb = skb_clone_and_charge_r(skb, sk);

if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
struct dst_entry *dst;

+ if (np->rxopt.all)
+ opt_skb = skb_clone_and_charge_r(skb, sk);
dst = rcu_dereference_protected(sk->sk_rx_dst,
lockdep_sock_is_held(sk));

@@ -1656,13 +1643,13 @@ int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
if (reason)
goto reset;
}
- if (opt_skb)
- __kfree_skb(opt_skb);
return 0;
}
} else
sock_rps_save_rxhash(sk, skb);

+ if (np->rxopt.all)
+ opt_skb = skb_clone_and_charge_r(skb, sk);
reason = tcp_rcv_state_process(sk, skb);
if (reason)
goto reset;