From nobody Tue Mar 3 03:35:58 2026 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 007B439150F; Mon, 2 Mar 2026 20:35:48 +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=1772483748; cv=none; b=kHjXV5IWF53vBLpEIkizlxs7yOhGH2eMDY8oXZyeyWdR4S+08oteEEHrHr3lGsG/d5nYURqJ0kDXP7wv2LObXAujX77Cx9MDJq5keEeZE3EzaU63o0HZSXZVhgLSx10rAmYCLO9mdpkP1XLWDG7XavWKaAjzniswVCaaocuNits= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772483748; c=relaxed/simple; bh=S5U+nwWUo/YnHQ0SZdVhUDJM12nU0GQ5riZpJ10XbZc=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=mFyzMOZijhrAQQ4S3R5L5ZzwltBU8U/An1nwZdp01454Wcvs6lt4LoYh/YK19bHqFBK+1Z/4kY+gteOvAAnLqmcX7DRItuZEqiY+ZPgH1hH2synk7w1W9VEHgcVsDrcshOM0AjcqaCW9lg50bnmTJnEFPO4Q9XmNcRFYOnAkbK0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=p7H+cHSV; 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="p7H+cHSV" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5390AC19423; Mon, 2 Mar 2026 20:35:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1772483747; bh=S5U+nwWUo/YnHQ0SZdVhUDJM12nU0GQ5riZpJ10XbZc=; h=From:To:Cc:Subject:Date:From; b=p7H+cHSVsjHeIxOS0kYtWolV9OZyHx4WpNP8CseckZ6OI1zW/CM6vJN12OuSxt42v cqbyNQayItg/RWluGatL/h7gkrQcbHua7vvMdFlhr+tqA7v/TDoWxTTNLYS3bqmB3C Mkmt5aVQCtZNeuI7LMY/AoGKni78sQe57NTF4I3QEYjUOgWCBwL7Jv9wRzz3mVyHE9 +8Fwi7JOeLbEtUj0IxR7stizRXol+axM6Idxw8mZIwYd9tV4BzgWO44HHeThLDJX/9 XxDYAsKd9wV4KXEOHnUFek7etut8gykh55Vgf0XfggjMq/vMQu5xreqHWQJSXyEjYp 1H/jYqbWwhcYg== From: Eric Biggers To: netdev@vger.kernel.org, "David S . Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: Simon Horman , Neal Cardwell , Kuniyuki Iwashima , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Biggers , stable@vger.kernel.org Subject: [PATCH net] net/tcp-md5: Fix MAC comparison to be constant-time Date: Mon, 2 Mar 2026 12:34:09 -0800 Message-ID: <20260302203409.13388-1-ebiggers@kernel.org> X-Mailer: git-send-email 2.53.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To prevent timing attacks, MACs need to be compared in constant time. Use the appropriate helper function for this. Fixes: cfb6eeb4c860 ("[TCP]: MD5 Signature Option (RFC2385) support.") Fixes: 658ddaaf6694 ("tcp: md5: RST: getting md5 key from listener") Cc: stable@vger.kernel.org Signed-off-by: Eric Biggers --- net/ipv4/Kconfig | 1 + net/ipv4/tcp.c | 3 ++- net/ipv4/tcp_ipv4.c | 3 ++- net/ipv6/tcp_ipv6.c | 3 ++- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/net/ipv4/Kconfig b/net/ipv4/Kconfig index b71c22475c515..45f5d401460c5 100644 --- a/net/ipv4/Kconfig +++ b/net/ipv4/Kconfig @@ -759,10 +759,11 @@ config TCP_AO If unsure, say N. =20 config TCP_MD5SIG bool "TCP: MD5 Signature Option support (RFC2385)" select CRYPTO_LIB_MD5 + select CRYPTO_LIB_UTILS help RFC2385 specifies a method of giving MD5 protection to TCP sessions. Its main (only?) use is to protect BGP sessions between core routers on the Internet. =20 diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 8cdc26e8ad689..202a4e57a2188 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -242,10 +242,11 @@ */ =20 #define pr_fmt(fmt) "TCP: " fmt =20 #include +#include #include #include #include #include #include @@ -4968,11 +4969,11 @@ tcp_inbound_md5_hash(const struct sock *sk, const s= truct sk_buff *skb, */ if (family =3D=3D AF_INET) tcp_v4_md5_hash_skb(newhash, key, NULL, skb); else tp->af_specific->calc_md5_hash(newhash, key, NULL, skb); - if (memcmp(hash_location, newhash, 16) !=3D 0) { + if (crypto_memneq(hash_location, newhash, 16)) { NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5FAILURE); trace_tcp_hash_md5_mismatch(sk, skb); return SKB_DROP_REASON_TCP_MD5FAILURE; } return SKB_NOT_DROPPED_YET; diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index d53d39be291a5..910c25cb24e10 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -86,10 +86,11 @@ #include #include #include =20 #include +#include =20 #include =20 #ifdef CONFIG_TCP_MD5SIG static void tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_ke= y *key, @@ -837,11 +838,11 @@ static void tcp_v4_send_reset(const struct sock *sk, = struct sk_buff *skb, key =3D tcp_md5_do_lookup(sk1, l3index, addr, AF_INET); if (!key) goto out; =20 tcp_v4_md5_hash_skb(newhash, key, NULL, skb); - if (memcmp(md5_hash_location, newhash, 16) !=3D 0) + if (crypto_memneq(md5_hash_location, newhash, 16)) goto out; } =20 if (key) { rep.opt[0] =3D htonl((TCPOPT_NOP << 24) | diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index e46a0efae0123..5195a46b951ea 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -66,10 +66,11 @@ =20 #include #include =20 #include +#include =20 #include =20 static void tcp_v6_send_reset(const struct sock *sk, struct sk_buff *skb, enum sk_rst_reason reason); @@ -1046,11 +1047,11 @@ static void tcp_v6_send_reset(const struct sock *sk= , struct sk_buff *skb, if (!key.md5_key) goto out; key.type =3D TCP_KEY_MD5; =20 tcp_v6_md5_hash_skb(newhash, key.md5_key, NULL, skb); - if (memcmp(md5_hash_location, newhash, 16) !=3D 0) + if (crypto_memneq(md5_hash_location, newhash, 16)) goto out; } #endif =20 if (th->ack) base-commit: 9439a661c2e80485406ce2c90b107ca17858382d --=20 2.53.0