bpf: fix filed access without lock
Subsystems (5)
PATCH 1/1 bpf: fix filed access without lock
The tcp_bpf_recvmsg_parser() function, running in user context, retrieves seq_copied from tcp_sk without holding the socket lock, and stores it in a local variable seq. However, the softirq context can modify tcp_sk->seq_copied concurrently, for example, n tcp_read_sock().
As a result, the seq value is stale when it is assigned back to tcp_sk->copied_seq at the end of tcp_bpf_recvmsg_parser(), leading to incorrect behavior.
--- net/ipv4/tcp_bpf.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
net/ipv4/tcp_bpf.c +4 -3
@@ -221,9 +221,9 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk,int flags, int *addr_len) { - struct tcp_sock *tcp = tcp_sk(sk); + struct tcp_sock *tcp; +u32 seq;int peek = flags &MSG_PEEK ; -u32 seq = tcp->copied_seq;struct sk_psock *psock;int copied =0 ;
@@ -238,7 +238,8 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk,return tcp_recvmsg (sk, msg, len, flags, addr_len);lock_sock (sk); - + tcp =tcp_sk (sk); + seq = tcp->copied_seq;/* We may have received data on the sk_receive_queue pre-accept and * then we can not use read_skb in this context because we haven't * assigned a sk_socket yet so have no link to the ops. The work-around
Signature · 1 line
2.43.5
Quoted 8 lines · mrpre
The
tcp_bpf_recvmsg_parser()function, running in user context, retrievesseq_copiedfromtcp_skwithout holding the socket lock, and stores it in a local variable seq. However, the softirq context can modifytcp_sk->seq_copiedconcurrently, for example, ntcp_read_sock().As a result, the seq value is stale when it is assigned back to
tcp_sk->copied_seqat the end oftcp_bpf_recvmsg_parser(), leading to incorrect behavior.
Good catch! This makes sense to me. Mind to be more specific on the "incorrect behavior" here? What error or misbehavior did you see?
Quoted 2 lines
Signed-off-by mrpre <[email protected]>
Please use your real name for SoB, see https://docs.kernel.org/process/submitting-patches.html
Thanks.
PATCH v2 1/1 bpf: fix filed access without lock
In reply to mrpreThe tcp_bpf_recvmsg_parser() function, running in user context, retrieves seq_copied from tcp_sk without holding the socket lock, and stores it in a local variable seq. However, the softirq context can modify tcp_sk->seq_copied concurrently, for example, n tcp_read_sock().
As a result, the seq value is stale when it is assigned back to tcp_sk->copied_seq at the end of tcp_bpf_recvmsg_parser(), leading to incorrect behavior.
Due to concurrency, the copied_seq field in tcp_bpf_recvmsg_parser() might be set to an incorrect value (less than the actual copied_seq) at the end of function: 'WRITE_ONCE(tcp->copied_seq, seq)'. This causes the 'offset' to be negative in tcp_read_sock()->tcp_recv_skb() when processing new incoming packets (sk->copied_seq - skb->seq becomes less than 0), and all subsequent packets will be dropped.
---
V1 -> V2: add more commit message to describle the issue
--- net/ipv4/tcp_bpf.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
net/ipv4/tcp_bpf.c +4 -3
@@ -221,9 +221,9 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk,int flags, int *addr_len) { - struct tcp_sock *tcp = tcp_sk(sk); + struct tcp_sock *tcp; +u32 seq;int peek = flags &MSG_PEEK ; -u32 seq = tcp->copied_seq;struct sk_psock *psock;int copied =0 ;
@@ -238,7 +238,8 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk,return tcp_recvmsg (sk, msg, len, flags, addr_len);lock_sock (sk); - + tcp =tcp_sk (sk); + seq = tcp->copied_seq;/* We may have received data on the sk_receive_queue pre-accept and * then we can not use read_skb in this context because we haven't * assigned a sk_socket yet so have no link to the ops. The work-around
Signature · 1 line
2.43.5