RE: [PATCH bpf-next 04/10] bpf: Make errors of bpf_tcp_check_syncookie distinguishable
flat view
From: John Fastabend <john.fastabend@gmail.com>
Date: 2021-10-20 03:28:28
Also in:
bpf
Maxim Mikityanskiy wrote:
bpf_tcp_check_syncookie returns errors when SYN cookie generation is disabled (EINVAL) or when no cookies were recently generated (ENOENT). The same error codes are used for other kinds of errors: invalid parameters (EINVAL), invalid packet (EINVAL, ENOENT), bad cookie (ENOENT). Such an overlap makes it impossible for a BPF program to distinguish different cases that may require different handling.
I'm not sure we can change these errors now. They are embedded in the helper API. I think a BPF program could uncover the meaning of the error anyways with some error path handling? Anyways even if we do change these most of us who run programs on multiple kernel versions would not be able to rely on them being one way or the other easily.
For a BPF program that accelerates generating and checking SYN cookies,
typical logic looks like this (with current error codes annotated):
1. Drop invalid packets (EINVAL, ENOENT).
2. Drop packets with bad cookies (ENOENT).
3. Pass packets with good cookies (0).
4. Pass all packets when cookies are not in use (EINVAL, ENOENT).
The last point also matches the behavior of cookie_v4_check and
cookie_v6_check that skip all checks if cookie generation is disabled or
no cookies were recently generated. Overlapping error codes, however,
make it impossible to distinguish case 4 from cases 1 and 2.
The original commit message of commit 399040847084 ("bpf: add helper to
check for a valid SYN cookie") mentions another use case, though:
traffic classification, where it's important to distinguish new
connections from existing ones, and case 4 should be distinguishable
from case 3.
To match the requirements of both use cases, this patch reassigns error
codes of bpf_tcp_check_syncookie and adds missing documentation:
1. EINVAL: Invalid packets.
2. EACCES: Packets with bad cookies.
3. 0: Packets with good cookies.
4. ENOENT: Cookies are not in use.
This way all four cases are easily distinguishable.
Signed-off-by: Maxim Mikityanskiy <redacted>
Reviewed-by: Tariq Toukan <tariqt@nvidia.com>At very leasst this would need a fixes tag and should be backported as a bug. Then we at least have a chance stable and LTS kernels report the same thing. [...]
quoted hunk ↗ jump to hunk
--- a/net/core/filter.c +++ b/net/core/filter.c
I'll take a stab at how a program can learn the error cause today.
BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
struct tcphdr *, th, u32, th_len)
{
#ifdef CONFIG_SYN_COOKIES
u32 cookie;
int ret;
// BPF program should know it pass bad values and can check
if (unlikely(!sk || th_len < sizeof(*th)))
return -EINVAL;
// sk_protocol and sk_state are exposed in sk and can be read directly
/* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */
if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
return -EINVAL;
// This is a user space knob right? I think this is a misconfig user can
// check before loading a program with check_syncookie?
if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies)
return -EINVAL;
// We have th pointer can't we just check?
if (!th->ack || th->rst || th->syn)
return -ENOENT;
if (tcp_synq_no_recent_overflow(sk))
return -ENOENT;
cookie = ntohl(th->ack_seq) - 1;
switch (sk->sk_family) {
case AF_INET:
// misconfiguration but can be checked.
if (unlikely(iph_len < sizeof(struct iphdr)))
return -EINVAL;
ret = __cookie_v4_check((struct iphdr *)iph, th, cookie);
break;
#if IS_BUILTIN(CONFIG_IPV6)
case AF_INET6:
// misconfiguration can check as well
if (unlikely(iph_len < sizeof(struct ipv6hdr)))
return -EINVAL;
ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie);
break;
#endif /* CONFIG_IPV6 */
default:
return -EPROTONOSUPPORT;
}
if (ret > 0)
return 0;
return -ENOENT;
#else
return -ENOTSUPP;
#endif
}
So I guess my point is we have all the fields we could write a bit
of BPF to find the error cause if necessary. Might be better than
dealing with changing the error code and having to deal with the
differences in kernels. I do see how it would have been better
to get errors correct on the first patch though :/
By the way I haven't got to the next set of patches with the
actual features, but why not push everything above this patch
as fixes in its own series. Then the fixes can get going why
we review the feature.
Thanks,
John