From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7F8831D88D0; Mon, 4 Nov 2024 10:53:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730717637; cv=none; b=ebfuI1R3ClLpFNa1+xJO+X5LvzE6tUhsOaqVIMq39n5wr3/kowf0xdRNSj9z53SBqNdJaaRT4gq/lrFPyjK0wyh6nFAqLA8vJpb0Y7YTMcTh2mddjgaGwcKU0Y3DJcL5ZrLH2nBsA9VQLHkDksV/aUnJ/A+UtRC12iYMIJkEJGw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730717637; c=relaxed/simple; bh=M17UFKuQXLwyH4RUZSo9NLj/fpjaDLtaPCNoxViMqt8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hjqORzZLC/8jyVpT5Zj9knsaMBJSRiuW4w9DJGLgLWwZpzSBR+1Qmj8+KEapHNWxXC/C/uBt7kgwhcilcKmtI+6+wIWlnKzuZr7mO8ckIyc01qQZV8OuV0OmA3QLcZ38qIlkU2GNE4fFJEZdTss4oKsRQifaQ2XtggfxQIHFUBY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DVJKSEYY; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DVJKSEYY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6D870C4CED1; Mon, 4 Nov 2024 10:53:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730717637; bh=M17UFKuQXLwyH4RUZSo9NLj/fpjaDLtaPCNoxViMqt8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DVJKSEYYHg2I1QPNNUyqjrTuthWJil75WScS3jxhuHCdAP9k7NoJ666+5XNtyFP/X B3bR6CSgmdCZWnL3RnFHGGchdZGhNkeQaNnBrfVI+d93FkRwdUYvUCLSD3MN28zsud jepKK09AP96aLL6yVFbsL1r3uDilgyIkCbzHBbS/ZpdPKK1f9kEsDBV6BrxIym8p99 Q4sqqQEhQD7cXp0IpVeApN2UjIXI0+fCPpXFRAavedBPlw8H6uTR4uCEQeHfe2G52u Xv/XU7CLyIW4rtFfdhdCf6Ioedt2UspLml972ybRqf+rzjGIRBUnyyvWrRhoLvIiXU P+gXYBI3zJspQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Jiayuan Chen , Martin KaFai Lau , Sasha Levin , john.fastabend@gmail.com, jakub@cloudflare.com, edumazet@google.com, davem@davemloft.net, dsahern@kernel.org, kuba@kernel.org, pabeni@redhat.com, netdev@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH AUTOSEL 6.1 10/11] bpf: fix filed access without lock Date: Mon, 4 Nov 2024 05:53:08 -0500 Message-ID: <20241104105324.97393-10-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241104105324.97393-1-sashal@kernel.org> References: <20241104105324.97393-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.1.115 Content-Transfer-Encoding: 8bit From: Jiayuan Chen [ Upstream commit a32aee8f0d987a7cba7fcc28002553361a392048 ] 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. 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. Signed-off-by: Jiayuan Chen Link: https://lore.kernel.org/r/20241028065226.35568-1-mrpre@163.com Signed-off-by: Martin KaFai Lau Signed-off-by: Sasha Levin --- net/ipv4/tcp_bpf.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c index 07a896685d0d3..f67e4c9f8d40e 100644 --- a/net/ipv4/tcp_bpf.c +++ b/net/ipv4/tcp_bpf.c @@ -216,11 +216,11 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk, int flags, int *addr_len) { - struct tcp_sock *tcp = tcp_sk(sk); int peek = flags & MSG_PEEK; - u32 seq = tcp->copied_seq; struct sk_psock *psock; + struct tcp_sock *tcp; int copied = 0; + u32 seq; if (unlikely(flags & MSG_ERRQUEUE)) return inet_recv_error(sk, msg, len, addr_len); @@ -233,7 +233,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 -- 2.43.0