From: Dmitry Safonov <dima@arista.com>
To: linux-kernel@vger.kernel.org, David Ahern <dsahern@kernel.org>,
Eric Dumazet <edumazet@google.com>,
Paolo Abeni <pabeni@redhat.com>, Jakub Kicinski <kuba@kernel.org>,
"David S. Miller" <davem@davemloft.net>
Cc: Dmitry Safonov <dima@arista.com>,
Dmitry Safonov <0x7f454c46@gmail.com>,
Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>,
Leonard Crestez <cdleonard@gmail.com>,
Salam Noureddine <noureddine@arista.com>,
netdev@vger.kernel.org
Subject: [RFC 4/5] net/tcp-md5: Don't send RST if key (dis)appeared
Date: Tue, 9 May 2023 23:16:07 +0100 [thread overview]
Message-ID: <20230509221608.2569333-5-dima@arista.com> (raw)
In-Reply-To: <20230509221608.2569333-1-dima@arista.com>
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 --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);
--
2.40.0
next prev parent reply other threads:[~2023-05-09 22:16 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-09 22:16 [PATCH 0/5] net/tcp-md5: Verify segments on TIME_WAIT sockets Dmitry Safonov
2023-05-09 22:16 ` [PATCH 1/5] net/tcp: Separate TCP-MD5 signing from tcp_v{4,6}_send_reset() Dmitry Safonov
2023-05-09 22:16 ` [PATCH 2/5] net/tcp: Use tcp_v6_md5_hash_skb() instead of .calc_md5_hash() Dmitry Safonov
2023-05-09 22:16 ` [RFC 3/5] net/tcp-md5: Verify inbound segments on twsk Dmitry Safonov
2023-05-09 22:16 ` Dmitry Safonov [this message]
2023-05-09 22:16 ` [RFC 5/5] net/tcp-md5: Don't send ACK if key (dis)appears Dmitry Safonov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230509221608.2569333-5-dima@arista.com \
--to=dima@arista.com \
--cc=0x7f454c46@gmail.com \
--cc=cdleonard@gmail.com \
--cc=davem@davemloft.net \
--cc=dsahern@kernel.org \
--cc=edumazet@google.com \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=noureddine@arista.com \
--cc=pabeni@redhat.com \
--cc=yoshfuji@linux-ipv6.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).