From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qv1-f54.google.com (mail-qv1-f54.google.com [209.85.219.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BB95B2750E6 for ; Fri, 31 Jul 2026 14:05:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785506717; cv=none; b=JPvze1QJNfphicl2DzVHkjVnCTmIJaTACMIWrcdZg8G+CLceMR28SioTU3o/gLuoXlNl9YO3tU7HTN0CqpCxIZ3MrFHMItXrEYDbrAb1HzDz2xo3Z1VKwv77k8MenCu43dwI/4RsHSiCkS0tZAWm0oqT+LDUU/gBKMFVfVCBRQI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785506717; c=relaxed/simple; bh=DJaYyC6I0eeyH0VOiP3H/RmtqxMs3E0alLK7QJv8A1Q=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=tiJXlIAzCkEHs9dHjxFf/F4FYjhU6AhKSIXfyZOl6qBnxYtI4sZu6jUnbDwWZobKUFnergPLzDv7FfYad6ukU5+zleCThudl1Zd+DYMyTHHRu7N47RFxyuaRbtjPX3roOdzuLHcnhUm0SXub+LamVDmK924qWxzSeM1NfRp4Cok= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=trailofbits.com; spf=pass smtp.mailfrom=trailofbits.com; dkim=pass (2048-bit key) header.d=trailofbits.com header.i=@trailofbits.com header.b=Jw6tNxcd; arc=none smtp.client-ip=209.85.219.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=trailofbits.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=trailofbits.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=trailofbits.com header.i=@trailofbits.com header.b="Jw6tNxcd" Received: by mail-qv1-f54.google.com with SMTP id 6a1803df08f44-9034b6b7674so7049126d6.0 for ; Fri, 31 Jul 2026 07:05:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=trailofbits.com; s=google; t=1785506714; x=1786111514; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=X8TedcA+moJqV+IYfTUJ+9XUuKwi4pJEzGmzELAhbYo=; b=Jw6tNxcdLw7YHz2NMVwDfSy3Y1CeuWtXdx2j8CdFNx/rn2tTrlwfRf9YcB4q/miofI WteDB06GeRAj+0I6lDCzBpGnrf+36PxwMnGW5wqI66zcMP+G2fRn+RCHTS5vtJEdseRy eOZpSrB3ky7IJVF1+SkLn/ZphhZEgZhujmIiqqNk5XZ7Aq8zCtCQLFq4MLYU//2pDAd/ UXwlofJ3Na7V4b6gC6ga/Cmw8JF3T0s/2c/RVvB6O0nn1bXRjEJsEXGoEqApPvDqIlgb zVuvvzpVQESiCIB/V5NtWS6vhAJRP5clIjJs/JEGfz5MUdjamBdLOtGdcF8Ol7QYIgaO eFLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785506714; x=1786111514; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=X8TedcA+moJqV+IYfTUJ+9XUuKwi4pJEzGmzELAhbYo=; b=lKz8pNFcx4dkhc+93lSJOT9jdG6q9NwZD3sEXwPNihi5nmQ8i0VgHFKo2WWqkZpsNJ l7lri4vB0vdzqjtx0hC7YiczxQI7ZqFzlrQNZIqAGopQNYnvbu0vCXGrUb8bAYHh1EJm 5x/abZSdSYODtg4KOgpyB1KUuiKmJXYNQpv5zDFlGbrNVKTQYHTzB14YLh69x/3pnYRH CEtuFJPW49T/8xJrCfE++NZyXIin3EP1Zbt4BVsdFAIq/JYwc1X8soXhVYhtzEaIGJWJ ucfbOwdehWvjPaFNffvQqf1UXYCFto6QRBpZToTowEdsrxo+sI4wyz2JlhQbxkQdNU/b hy0g== X-Forwarded-Encrypted: i=1; AHgh+Ro8ATkhPd8w96rocht1BZhyu1CSg96HVWbc1jMDLQU1w7a1cjuud9BEs/4rgNLAG9tAl54xr5xF0DHpCMQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxQQl0if4OtiTGjZuejyiD3A8WaiilT9JbLWrD4SowntY77HT56 U0zkJJxbG56hh0bvNyCiHiNjvdX+We8485ivtaEQEMLiToB/GseVLyLpWjrH3whcwcY= X-Gm-Gg: AR+sD10IG1hXhKHn7eehtA3Ef//FzFIYL24jumg8xY9lfdiIwoDqfiDGF46rDFLVHPN GdC2euo8sHcFDiEI/pvZJx15JBKo3LV0tjzx+he6Ccf56J1OoD64sszotSnmW1Yoz3P04Gl83aR PCdK2fdg1sOgNiX0vamrFL311EXnNoPQ2Kx0okU0nQhGjnA97oj2buMPUpgXEmA9gTp1ozSn+GS kAudQyJqyr6kGEPToualE6+13cMCZGHXs6SP4vEDvsfJyIrP1fVLSn6fcjCaTz40fM6InqWjThf p8WAG0ECJL8jiEk9isMkbU2PF/CKlQP2VgbAPG6AJgQglN5RxN6BTH/Jlu7Vz4XRHvPOjjLgxNf k1ACOpxXtKRQBL/jqJ2nGdvfJmHCHmVky0YyLN1Wi3M6bmysnKgS98gKpa5IeZPfVzZS8s9sML2 CT+ASzteHWmWJaEZGeTvhI6FhRjpp2kj1iFBLvee0v00EJICKiub3qasZ67BMktNapqI0Id4up3 72K X-Received: by 2002:a05:6214:3a02:b0:8e7:8d53:2417 with SMTP id 6a1803df08f44-90842242e52mr44508206d6.27.1785506714177; Fri, 31 Jul 2026 07:05:14 -0700 (PDT) Received: from localhost ([146.190.222.192]) by smtp.gmail.com with UTF8SMTPSA id 6a1803df08f44-908434bb4efsm11018806d6.21.2026.07.31.07.05.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 31 Jul 2026 07:05:13 -0700 (PDT) From: David Lee To: edumazet@google.com, ncardwell@google.com, davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com Cc: David Lee , Kyle Zeng , Dominik 'Disconnect3d' Czarnota , kuniyu@google.com, horms@kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] net: inet: prevent lhash2 lookups from escaping into ehash Date: Fri, 31 Jul 2026 14:05:12 +0000 Message-ID: <20260731140512.566464-1-david.lee@trailofbits.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The lhash2 and ehash tables share sk_nulls_node. When a listening socket is unhashed and rehashed as a connected socket while an RCU listener lookup is walking it, the reader can follow the node's new next pointer into ehash. inet_lhash2_lookup() and inet6_lhash2_lookup() do not validate the terminal nulls marker, so they can return an ehash entry under the listener lookup's unreferenced ownership contract. In particular, returning a TIME_WAIT socket makes the TCP receive path consume a reference that the lookup never acquired. Repeated races can free the object while it remains linked in the hash tables. Skip non-listening sockets before scoring them. Also restart the walk when its terminal nulls marker does not match the expected lhash2 slot, as the established lookup already does. Fixes: cae3873c5b3a ("net: inet: Retire port only listening_hash") Bug found and triaged by OpenAI Security Research and validated by Trail of Bits. Assisted-by: Codex:gpt-5.6-sol gpt-5.5-cyber Signed-off-by: Kyle Zeng --- Trail of Bits has a reproducer for this bug that triggers a KASAN use-after-free and can share if needed. net/ipv4/inet_hashtables.c | 20 ++++++++++++++++---- net/ipv6/inet6_hashtables.c | 20 ++++++++++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c index ba0faa9ae..563c8af44 100644 --- a/net/ipv4/inet_hashtables.c +++ b/net/ipv4/inet_hashtables.c @@ -415,16 +415,23 @@ EXPORT_SYMBOL_GPL(inet_lookup_reuseport); /* called with rcu_read_lock() : No refcount taken on the socket */ static struct sock *inet_lhash2_lookup(const struct net *net, struct inet_listen_hashbucket *ilb2, + unsigned int slot, struct sk_buff *skb, int doff, const __be32 saddr, __be16 sport, const __be32 daddr, const unsigned short hnum, const int dif, const int sdif) { - struct sock *sk, *result = NULL; + struct sock *sk, *result; struct hlist_nulls_node *node; - int score, hiscore = 0; + int score, hiscore; +begin: + result = NULL; + hiscore = 0; sk_nulls_for_each_rcu(sk, node, &ilb2->nulls_head) { + if (READ_ONCE(sk->sk_state) != TCP_LISTEN) + continue; + score = compute_score(sk, net, hnum, daddr, dif, sdif); if (score > hiscore) { result = inet_lookup_reuseport(net, sk, skb, doff, @@ -437,6 +444,9 @@ static struct sock *inet_lhash2_lookup(const struct net *net, } } + if (get_nulls_value(node) != slot + LISTENING_NULLS_BASE) + goto begin; + return result; } @@ -486,7 +496,8 @@ struct sock *__inet_lookup_listener(const struct net *net, hash2 = ipv4_portaddr_hash(net, daddr, hnum); ilb2 = inet_lhash2_bucket(hashinfo, hash2); - result = inet_lhash2_lookup(net, ilb2, skb, doff, + result = inet_lhash2_lookup(net, ilb2, + hash2 & hashinfo->lhash2_mask, skb, doff, saddr, sport, daddr, hnum, dif, sdif); if (result) @@ -496,7 +507,8 @@ struct sock *__inet_lookup_listener(const struct net *net, hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum); ilb2 = inet_lhash2_bucket(hashinfo, hash2); - result = inet_lhash2_lookup(net, ilb2, skb, doff, + result = inet_lhash2_lookup(net, ilb2, + hash2 & hashinfo->lhash2_mask, skb, doff, saddr, sport, htonl(INADDR_ANY), hnum, dif, sdif); done: diff --git a/net/ipv6/inet6_hashtables.c b/net/ipv6/inet6_hashtables.c index b111b51d6..a9dfd47dd 100644 --- a/net/ipv6/inet6_hashtables.c +++ b/net/ipv6/inet6_hashtables.c @@ -184,16 +184,23 @@ EXPORT_SYMBOL_GPL(inet6_lookup_reuseport); /* called with rcu_read_lock() */ static struct sock *inet6_lhash2_lookup(const struct net *net, struct inet_listen_hashbucket *ilb2, + unsigned int slot, struct sk_buff *skb, int doff, const struct in6_addr *saddr, const __be16 sport, const struct in6_addr *daddr, const unsigned short hnum, const int dif, const int sdif) { - struct sock *sk, *result = NULL; + struct sock *sk, *result; struct hlist_nulls_node *node; - int score, hiscore = 0; + int score, hiscore; +begin: + result = NULL; + hiscore = 0; sk_nulls_for_each_rcu(sk, node, &ilb2->nulls_head) { + if (READ_ONCE(sk->sk_state) != TCP_LISTEN) + continue; + score = compute_score(sk, net, hnum, daddr, dif, sdif); if (score > hiscore) { result = inet6_lookup_reuseport(net, sk, skb, doff, @@ -206,6 +213,9 @@ static struct sock *inet6_lhash2_lookup(const struct net *net, } } + if (get_nulls_value(node) != slot + LISTENING_NULLS_BASE) + goto begin; + return result; } @@ -260,7 +270,8 @@ struct sock *inet6_lookup_listener(const struct net *net, hash2 = ipv6_portaddr_hash(net, daddr, hnum); ilb2 = inet_lhash2_bucket(hashinfo, hash2); - result = inet6_lhash2_lookup(net, ilb2, skb, doff, + result = inet6_lhash2_lookup(net, ilb2, + hash2 & hashinfo->lhash2_mask, skb, doff, saddr, sport, daddr, hnum, dif, sdif); if (result) @@ -270,7 +281,8 @@ struct sock *inet6_lookup_listener(const struct net *net, hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum); ilb2 = inet_lhash2_bucket(hashinfo, hash2); - result = inet6_lhash2_lookup(net, ilb2, skb, doff, + result = inet6_lhash2_lookup(net, ilb2, + hash2 & hashinfo->lhash2_mask, skb, doff, saddr, sport, &in6addr_any, hnum, dif, sdif); done: