提交 edbaa822 编写于 作者: M Maxim Mikityanskiy 提交者: Zheng Zengkai

bpf: Support dual-stack sockets in bpf_tcp_check_syncookie

stable inclusion
from stable-v5.10.111
commit 970a6bb72912f2accae52e811a6618e3d4a5b0ff
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I5GL1Z

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=970a6bb72912f2accae52e811a6618e3d4a5b0ff

--------------------------------

[ Upstream commit 2e8702cc ]

bpf_tcp_gen_syncookie looks at the IP version in the IP header and
validates the address family of the socket. It supports IPv4 packets in
AF_INET6 dual-stack sockets.

On the other hand, bpf_tcp_check_syncookie looks only at the address
family of the socket, ignoring the real IP version in headers, and
validates only the packet size. This implementation has some drawbacks:

1. Packets are not validated properly, allowing a BPF program to trick
   bpf_tcp_check_syncookie into handling an IPv6 packet on an IPv4
   socket.

2. Dual-stack sockets fail the checks on IPv4 packets. IPv4 clients end
   up receiving a SYNACK with the cookie, but the following ACK gets
   dropped.

This patch fixes these issues by changing the checks in
bpf_tcp_check_syncookie to match the ones in bpf_tcp_gen_syncookie. IP
version from the header is taken into account, and it is validated
properly with address family.

Fixes: 39904084 ("bpf: add helper to check for a valid SYN cookie")
Signed-off-by: NMaxim Mikityanskiy <maximmi@nvidia.com>
Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
Reviewed-by: NTariq Toukan <tariqt@nvidia.com>
Acked-by: NArthur Fabre <afabre@cloudflare.com>
Link: https://lore.kernel.org/bpf/20220406124113.2795730-1-maximmi@nvidia.comSigned-off-by: NSasha Levin <sashal@kernel.org>
Signed-off-by: NZheng Zengkai <zhengzengkai@huawei.com>
Reviewed-by: NWei Li <liwei391@huawei.com>
上级 6ff12e06
...@@ -6492,24 +6492,33 @@ BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len ...@@ -6492,24 +6492,33 @@ BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len
if (!th->ack || th->rst || th->syn) if (!th->ack || th->rst || th->syn)
return -ENOENT; return -ENOENT;
if (unlikely(iph_len < sizeof(struct iphdr)))
return -EINVAL;
if (tcp_synq_no_recent_overflow(sk)) if (tcp_synq_no_recent_overflow(sk))
return -ENOENT; return -ENOENT;
cookie = ntohl(th->ack_seq) - 1; cookie = ntohl(th->ack_seq) - 1;
switch (sk->sk_family) { /* Both struct iphdr and struct ipv6hdr have the version field at the
case AF_INET: * same offset so we can cast to the shorter header (struct iphdr).
if (unlikely(iph_len < sizeof(struct iphdr))) */
switch (((struct iphdr *)iph)->version) {
case 4:
if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk))
return -EINVAL; return -EINVAL;
ret = __cookie_v4_check((struct iphdr *)iph, th, cookie); ret = __cookie_v4_check((struct iphdr *)iph, th, cookie);
break; break;
#if IS_BUILTIN(CONFIG_IPV6) #if IS_BUILTIN(CONFIG_IPV6)
case AF_INET6: case 6:
if (unlikely(iph_len < sizeof(struct ipv6hdr))) if (unlikely(iph_len < sizeof(struct ipv6hdr)))
return -EINVAL; return -EINVAL;
if (sk->sk_family != AF_INET6)
return -EINVAL;
ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie); ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie);
break; break;
#endif /* CONFIG_IPV6 */ #endif /* CONFIG_IPV6 */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册