tcp: Remove unnecessary null check in tcp_inbound_md5_hash()
Subsystems (3)
PATCH 1/1 tcp: Remove unnecessary null check in tcp_inbound_md5_hash()
The 'if (!key && hash_location)' check in tcp_inbound_md5_hash() implies that hash_location might be null. However, later code in the function dereferences hash_location anyway, without checking for null first. Fortunately, there is no real bug, since tcp_inbound_md5_hash() is called only with non-null values of hash_location.
Therefore, remove the unnecessary and misleading null check of hash_location. This silences a Smatch static checker warning
(https://lore.kernel.org/netdev/[email protected]/)
Also fix the related comment at the beginning of the function.
--- net/ipv4/tcp.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
net/ipv4/tcp.c +4 -6
@@ -4884,22 +4884,20 @@ static enum skb_drop_reason tcp_inbound_md5_hash(const struct sock *sk, const struct sk_buff *skb, const void *saddr, const void *daddr, int family, int l3index, const __u8 *hash_location) { /* This gets called for each TCP segment that has TCP-MD5 option. - * We have 3 drop cases: - * o No MD5 hash and one expected. - * o MD5 hash and we're not expecting one. - * o MD5 hash and its wrong. + * We have 2 drop cases: + * o An MD5 signature is present, but we're not expecting one. + * o The MD5 signature is wrong. */ const struct tcp_sock *tp = tcp_sk(sk);struct tcp_md5sig_key *key;u8 newhash[16 ]; key =tcp_md5_do_lookup (sk, l3index, saddr, family); - -if (!key && hash_location) { +if (!key) {NET_INC_STATS (sock_net (sk),LINUX_MIB_TCPMD5UNEXPECTED );trace_tcp_hash_md5_unexpected (sk, skb);return SKB_DROP_REASON_TCP_MD5UNEXPECTED ; }
base-commit: 962ac5ca99a5c3e7469215bf47572440402dfd59
Signature · 1 line
2.51.0
Quoted 7 lines · Eric Biggers
The 'if (!key &&
hash_location)' check intcp_inbound_md5_hash()implies thathash_locationmight be null. However, later code in the function dereferenceshash_locationanyway, without checking for null first. Fortunately, there is no real bug, sincetcp_inbound_md5_hash()is called only with non-null values ofhash_location.
Quoted 7 lines
Therefore, remove the unnecessary and misleading null check of
hash_location. This silences a Smatch static checker warning
(https://lore.kernel.org/netdev/[email protected]/)Also fix the related comment at the beginning of the function.
Signed-off-by Eric Biggers <[email protected]>
Quoted 7 lines · Eric Biggers
The 'if (!key &&
hash_location)' check intcp_inbound_md5_hash()implies thathash_locationmight be null. However, later code in the function dereferenceshash_locationanyway, without checking for null first. Fortunately, there is no real bug, sincetcp_inbound_md5_hash()is called only with non-null values ofhash_location.
Quoted 7 lines
Therefore, remove the unnecessary and misleading null check of
hash_location. This silences a Smatch static checker warning
(https://lore.kernel.org/netdev/[email protected]/)Also fix the related comment at the beginning of the function.
Signed-off-by Eric Biggers <[email protected]>
Yeah, I think it's a left-over from the time when there was only TCP-MD5 support.
Thanks for cleaning this up,
Dmitry
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 11 lines · On Wed, 22 Oct 2025 15:12:09 -0700 you wrote:
The 'if (!key &&
hash_location)' check intcp_inbound_md5_hash()implies thathash_locationmight be null. However, later code in the function dereferenceshash_locationanyway, without checking for null first. Fortunately, there is no real bug, sincetcp_inbound_md5_hash()is called only with non-null values ofhash_location.Therefore, remove the unnecessary and misleading null check of
hash_location. This silences a Smatch static checker warning
(https://lore.kernel.org/netdev/[email protected]/)[...]
Here is the summary with links:
- [net-next] tcp: Remove unnecessary null check in tcp_inbound_md5_hash()
https://git.kernel.org/netdev/net-next/c/05774d7e4201
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html