diff mbox series

[RFC,4/5] net/tcp-md5: Don't send RST if key (dis)appeared

Message ID 20230509221608.2569333-5-dima@arista.com (mailing list archive)
State RFC
Delegated to: Netdev Maintainers
Headers show
Series net/tcp-md5: Verify segments on TIME_WAIT sockets | expand

Checks

Context Check Description
netdev/series_format warning Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit fail Errors and warnings before: 31 this patch: 31
netdev/cc_maintainers success CCed 6 of 6 maintainers
netdev/build_clang fail Errors and warnings before: 12 this patch: 12
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn fail Errors and warnings before: 31 this patch: 31
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 30 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Dmitry Safonov May 9, 2023, 10:16 p.m. UTC
Seems cheap at this place as both key and hash_location were looked up
until now.

Signed-off-by: Dmitry Safonov <dima@arista.com>
---
 net/ipv4/tcp_ipv4.c | 10 ++++++++++
 net/ipv6/tcp_ipv6.c |  8 ++++++++
 2 files changed, 18 insertions(+)
diff mbox series

Patch

diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index f5b870943dcb..d94cd5e70d58 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -686,6 +686,16 @@  static bool tcp_v4_md5_sign_reset(struct net *net, const struct sock *sk,
 		l3index = tcp_v4_sdif(skb) ? inet_iif(skb) : 0;
 		addr = (union tcp_md5_addr *)&ip_hdr(skb)->saddr;
 		key = tcp_md5_do_lookup(sk, l3index, addr, AF_INET);
+		/* This segment should have been already verified by
+		 * tcp_inbound_md5_hash(). But that might raced with userspace
+		 * adding or deleting keys. So, follow the logic of
+		 * tcp_inbound_md5_hash() and avoid replying with TCP-MD5 sign
+		 * on non-signed segments and vice-versa.
+		 */
+		if (unlikely(!!key != !!hash_location)) {
+			rcu_read_unlock();
+			return true;
+		}
 	} else if (hash_location) {
 		const union tcp_md5_addr *addr;
 		int sdif = tcp_v4_sdif(skb);
diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
index 3756a43367a3..498dfa194b8b 100644
--- a/net/ipv6/tcp_ipv6.c
+++ b/net/ipv6/tcp_ipv6.c
@@ -993,6 +993,14 @@  static int tcp_v6_md5_lookup_reset_key(struct net *net, const struct sock *sk,
 		 */
 		l3index = tcp_v6_sdif(skb) ? tcp_v6_iif_l3_slave(skb) : 0;
 		*key = tcp_v6_md5_do_lookup(sk, &ipv6h->saddr, l3index);
+		/* This segment should have been already verified by
+		 * tcp_inbound_md5_hash(). But that might raced with userspace
+		 * adding or deleting keys. So, follow the logic of
+		 * tcp_inbound_md5_hash() and avoid replying with TCP-MD5 sign
+		 * on non-signed segments and vice-versa.
+		 */
+		if (unlikely(!!*key != !!hash_location))
+			return -ENOKEY;
 	} else if (hash_location) {
 		int dif = tcp_v6_iif_l3_slave(skb);
 		int sdif = tcp_v6_sdif(skb);