Skip to content

Commit 2e8702c

Browse files
nvmmaxAlexei Starovoitov
authored andcommitted
bpf: Support dual-stack sockets in bpf_tcp_check_syncookie
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: 3990408 ("bpf: add helper to check for a valid SYN cookie") Signed-off-by: Maxim Mikityanskiy <maximmi@nvidia.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Reviewed-by: Tariq Toukan <tariqt@nvidia.com> Acked-by: Arthur Fabre <afabre@cloudflare.com> Link: https://lore.kernel.org/bpf/20220406124113.2795730-1-maximmi@nvidia.com
1 parent 0a210af commit 2e8702c

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

net/core/filter.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7016,24 +7016,33 @@ BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len
70167016
if (!th->ack || th->rst || th->syn)
70177017
return -ENOENT;
70187018

7019+
if (unlikely(iph_len < sizeof(struct iphdr)))
7020+
return -EINVAL;
7021+
70197022
if (tcp_synq_no_recent_overflow(sk))
70207023
return -ENOENT;
70217024

70227025
cookie = ntohl(th->ack_seq) - 1;
70237026

7024-
switch (sk->sk_family) {
7025-
case AF_INET:
7026-
if (unlikely(iph_len < sizeof(struct iphdr)))
7027+
/* Both struct iphdr and struct ipv6hdr have the version field at the
7028+
* same offset so we can cast to the shorter header (struct iphdr).
7029+
*/
7030+
switch (((struct iphdr *)iph)->version) {
7031+
case 4:
7032+
if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk))
70277033
return -EINVAL;
70287034

70297035
ret = __cookie_v4_check((struct iphdr *)iph, th, cookie);
70307036
break;
70317037

70327038
#if IS_BUILTIN(CONFIG_IPV6)
7033-
case AF_INET6:
7039+
case 6:
70347040
if (unlikely(iph_len < sizeof(struct ipv6hdr)))
70357041
return -EINVAL;
70367042

7043+
if (sk->sk_family != AF_INET6)
7044+
return -EINVAL;
7045+
70377046
ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie);
70387047
break;
70397048
#endif /* CONFIG_IPV6 */

0 commit comments

Comments
 (0)