bpf: Support sk lookup in netns with id 0
David Ahern and Nicolas Dichtel report that the handling of the netns id 0 is incorrect for the BPF socket lookup helpers: rather than finding the netns with id 0, it is resolving to the current netns. This renders the netns_id 0 inaccessible. To fix this, adjust the API for the netns to treat all negative s32 values as a lookup in the current netns (including u64 values which when truncated to s32 become negative), while any values with a positive value in the signed 32-bit integer space would result in a lookup for a socket in the netns corresponding to that id. As before, if the netns with that ID does not exist, no socket will be found. Any netns outside of these ranges will fail to find a corresponding socket, as those values are reserved for future usage. Signed-off-by: Joe Stringer <joe@wand.net.nz> Acked-by: Nicolas Dichtel <nicolas.dichtel@6wind.com> Acked-by: Joey Pabalinas <joeypabalinas@gmail.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:

committed by
Alexei Starovoitov

parent
b7df9ada9a
commit
f71c6143c2
@@ -4890,22 +4890,23 @@ bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
|
||||
struct net *net;
|
||||
|
||||
family = len == sizeof(tuple->ipv4) ? AF_INET : AF_INET6;
|
||||
if (unlikely(family == AF_UNSPEC || netns_id > U32_MAX || flags))
|
||||
if (unlikely(family == AF_UNSPEC || flags ||
|
||||
!((s32)netns_id < 0 || netns_id <= S32_MAX)))
|
||||
goto out;
|
||||
|
||||
if (skb->dev)
|
||||
caller_net = dev_net(skb->dev);
|
||||
else
|
||||
caller_net = sock_net(skb->sk);
|
||||
if (netns_id) {
|
||||
if ((s32)netns_id < 0) {
|
||||
net = caller_net;
|
||||
sk = sk_lookup(net, tuple, skb, family, proto);
|
||||
} else {
|
||||
net = get_net_ns_by_id(caller_net, netns_id);
|
||||
if (unlikely(!net))
|
||||
goto out;
|
||||
sk = sk_lookup(net, tuple, skb, family, proto);
|
||||
put_net(net);
|
||||
} else {
|
||||
net = caller_net;
|
||||
sk = sk_lookup(net, tuple, skb, family, proto);
|
||||
}
|
||||
|
||||
if (sk)
|
||||
|
Reference in New Issue
Block a user