mirror of
https://gitee.com/bianbu-linux/linux-6.6
synced 2025-07-08 00:23:23 -04:00
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:
parent
b7df9ada9a
commit
f71c6143c2
5 changed files with 63 additions and 44 deletions
|
@ -154,12 +154,12 @@ static unsigned long long (*bpf_skb_ancestor_cgroup_id)(void *ctx, int level) =
|
|||
(void *) BPF_FUNC_skb_ancestor_cgroup_id;
|
||||
static struct bpf_sock *(*bpf_sk_lookup_tcp)(void *ctx,
|
||||
struct bpf_sock_tuple *tuple,
|
||||
int size, unsigned int netns_id,
|
||||
int size, unsigned long long netns_id,
|
||||
unsigned long long flags) =
|
||||
(void *) BPF_FUNC_sk_lookup_tcp;
|
||||
static struct bpf_sock *(*bpf_sk_lookup_udp)(void *ctx,
|
||||
struct bpf_sock_tuple *tuple,
|
||||
int size, unsigned int netns_id,
|
||||
int size, unsigned long long netns_id,
|
||||
unsigned long long flags) =
|
||||
(void *) BPF_FUNC_sk_lookup_udp;
|
||||
static int (*bpf_sk_release)(struct bpf_sock *sk) =
|
||||
|
|
|
@ -72,7 +72,7 @@ int bpf_sk_lookup_test0(struct __sk_buff *skb)
|
|||
return TC_ACT_SHOT;
|
||||
|
||||
tuple_len = ipv4 ? sizeof(tuple->ipv4) : sizeof(tuple->ipv6);
|
||||
sk = bpf_sk_lookup_tcp(skb, tuple, tuple_len, 0, 0);
|
||||
sk = bpf_sk_lookup_tcp(skb, tuple, tuple_len, BPF_F_CURRENT_NETNS, 0);
|
||||
if (sk)
|
||||
bpf_sk_release(sk);
|
||||
return sk ? TC_ACT_OK : TC_ACT_UNSPEC;
|
||||
|
@ -84,7 +84,7 @@ int bpf_sk_lookup_test1(struct __sk_buff *skb)
|
|||
struct bpf_sock_tuple tuple = {};
|
||||
struct bpf_sock *sk;
|
||||
|
||||
sk = bpf_sk_lookup_tcp(skb, &tuple, sizeof(tuple), 0, 0);
|
||||
sk = bpf_sk_lookup_tcp(skb, &tuple, sizeof(tuple), BPF_F_CURRENT_NETNS, 0);
|
||||
if (sk)
|
||||
bpf_sk_release(sk);
|
||||
return 0;
|
||||
|
@ -97,7 +97,7 @@ int bpf_sk_lookup_uaf(struct __sk_buff *skb)
|
|||
struct bpf_sock *sk;
|
||||
__u32 family = 0;
|
||||
|
||||
sk = bpf_sk_lookup_tcp(skb, &tuple, sizeof(tuple), 0, 0);
|
||||
sk = bpf_sk_lookup_tcp(skb, &tuple, sizeof(tuple), BPF_F_CURRENT_NETNS, 0);
|
||||
if (sk) {
|
||||
bpf_sk_release(sk);
|
||||
family = sk->family;
|
||||
|
@ -112,7 +112,7 @@ int bpf_sk_lookup_modptr(struct __sk_buff *skb)
|
|||
struct bpf_sock *sk;
|
||||
__u32 family;
|
||||
|
||||
sk = bpf_sk_lookup_tcp(skb, &tuple, sizeof(tuple), 0, 0);
|
||||
sk = bpf_sk_lookup_tcp(skb, &tuple, sizeof(tuple), BPF_F_CURRENT_NETNS, 0);
|
||||
if (sk) {
|
||||
sk += 1;
|
||||
bpf_sk_release(sk);
|
||||
|
@ -127,7 +127,7 @@ int bpf_sk_lookup_modptr_or_null(struct __sk_buff *skb)
|
|||
struct bpf_sock *sk;
|
||||
__u32 family;
|
||||
|
||||
sk = bpf_sk_lookup_tcp(skb, &tuple, sizeof(tuple), 0, 0);
|
||||
sk = bpf_sk_lookup_tcp(skb, &tuple, sizeof(tuple), BPF_F_CURRENT_NETNS, 0);
|
||||
sk += 1;
|
||||
if (sk)
|
||||
bpf_sk_release(sk);
|
||||
|
@ -139,7 +139,7 @@ int bpf_sk_lookup_test2(struct __sk_buff *skb)
|
|||
{
|
||||
struct bpf_sock_tuple tuple = {};
|
||||
|
||||
bpf_sk_lookup_tcp(skb, &tuple, sizeof(tuple), 0, 0);
|
||||
bpf_sk_lookup_tcp(skb, &tuple, sizeof(tuple), BPF_F_CURRENT_NETNS, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -149,7 +149,7 @@ int bpf_sk_lookup_test3(struct __sk_buff *skb)
|
|||
struct bpf_sock_tuple tuple = {};
|
||||
struct bpf_sock *sk;
|
||||
|
||||
sk = bpf_sk_lookup_tcp(skb, &tuple, sizeof(tuple), 0, 0);
|
||||
sk = bpf_sk_lookup_tcp(skb, &tuple, sizeof(tuple), BPF_F_CURRENT_NETNS, 0);
|
||||
bpf_sk_release(sk);
|
||||
bpf_sk_release(sk);
|
||||
return 0;
|
||||
|
@ -161,7 +161,7 @@ int bpf_sk_lookup_test4(struct __sk_buff *skb)
|
|||
struct bpf_sock_tuple tuple = {};
|
||||
struct bpf_sock *sk;
|
||||
|
||||
sk = bpf_sk_lookup_tcp(skb, &tuple, sizeof(tuple), 0, 0);
|
||||
sk = bpf_sk_lookup_tcp(skb, &tuple, sizeof(tuple), BPF_F_CURRENT_NETNS, 0);
|
||||
bpf_sk_release(sk);
|
||||
return 0;
|
||||
}
|
||||
|
@ -169,7 +169,7 @@ int bpf_sk_lookup_test4(struct __sk_buff *skb)
|
|||
void lookup_no_release(struct __sk_buff *skb)
|
||||
{
|
||||
struct bpf_sock_tuple tuple = {};
|
||||
bpf_sk_lookup_tcp(skb, &tuple, sizeof(tuple), 0, 0);
|
||||
bpf_sk_lookup_tcp(skb, &tuple, sizeof(tuple), BPF_F_CURRENT_NETNS, 0);
|
||||
}
|
||||
|
||||
SEC("fail_no_release_subcall")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue