When a reuseport group contains connected sockets, UDP lookup rescored the socket selected by the reuseport program. If that socket did not match the packet, lookup retained the stale selection while continuing the scan. This could deliver a packet to a socket connected to a different peer. Restore the candidate that established the current score when the rescored socket is not a better match. Keep the selected socket only when its score is higher, and apply the same logic to IPv4 and IPv6 lookups. Fixes: efc6b6f6c311 ("udp: Improve load balancing for SO_REUSEPORT.") Cc: stable@vger.kernel.org Reported-by: Vega Link: https://bugtracker.nebusec.ai/f/net/ipv6/udp.c||udp6_lib_lookup2||socket-hijack||m7q Signed-off-by: Zihan Xi --- net/ipv4/udp.c | 11 ++++++++--- net/ipv6/udp.c | 11 ++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index 2e5562f353f8..23a8c951559c 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -475,12 +475,17 @@ static struct sock *udp4_lib_lookup2(const struct net *net, rescore: score = compute_score(need_rescore ? result : sk, net, saddr, sport, daddr, hnum, dif, sdif); + if (need_rescore) { + if (score > badness) + badness = score; + else + result = sk; + continue; + } + if (score > badness) { badness = score; - if (need_rescore) - continue; - if (sk->sk_state == TCP_ESTABLISHED) { result = sk; continue; diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c index 9dcefa162522..c5adeaaf5b42 100644 --- a/net/ipv6/udp.c +++ b/net/ipv6/udp.c @@ -234,12 +234,17 @@ static struct sock *udp6_lib_lookup2(const struct net *net, rescore: score = compute_score(need_rescore ? result : sk, net, saddr, sport, daddr, hnum, dif, sdif); + if (need_rescore) { + if (score > badness) + badness = score; + else + result = sk; + continue; + } + if (score > badness) { badness = score; - if (need_rescore) - continue; - if (sk->sk_state == TCP_ESTABLISHED) { result = sk; continue; -- 2.43.0