Skip to content

Commit

Permalink
bpf, sockmap: SK_DROP on attempted redirects of unsupported af_vsock
Browse files Browse the repository at this point in the history
Don't mislead the callers of bpf_{sk,msg}_redirect_{map,hash}(): make sure
to immediately and visibly fail the forwarding of unsupported af_vsock
packets.

Fixes: 634f1a7 ("vsock: support sockmap")
Signed-off-by: Michal Luczaj <[email protected]>
Signed-off-by: Daniel Borkmann <[email protected]>
Acked-by: John Fastabend <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
mmhal authored and borkmann committed Oct 17, 2024
1 parent 2aa587f commit 9c5bd93
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/net/sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -2715,6 +2715,11 @@ static inline bool sk_is_stream_unix(const struct sock *sk)
return sk->sk_family == AF_UNIX && sk->sk_type == SOCK_STREAM;
}

static inline bool sk_is_vsock(const struct sock *sk)
{
return sk->sk_family == AF_VSOCK;
}

/**
* sk_eat_skb - Release a skb if it is no longer needed
* @sk: socket to eat this skb from
Expand Down
8 changes: 8 additions & 0 deletions net/core/sock_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,8 @@ BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
sk = __sock_map_lookup_elem(map, key);
if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
return SK_DROP;
if ((flags & BPF_F_INGRESS) && sk_is_vsock(sk))
return SK_DROP;

skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS);
return SK_PASS;
Expand Down Expand Up @@ -675,6 +677,8 @@ BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
return SK_DROP;
if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
return SK_DROP;
if (sk_is_vsock(sk))
return SK_DROP;

msg->flags = flags;
msg->sk_redir = sk;
Expand Down Expand Up @@ -1249,6 +1253,8 @@ BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
sk = __sock_hash_lookup_elem(map, key);
if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
return SK_DROP;
if ((flags & BPF_F_INGRESS) && sk_is_vsock(sk))
return SK_DROP;

skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS);
return SK_PASS;
Expand Down Expand Up @@ -1277,6 +1283,8 @@ BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
return SK_DROP;
if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
return SK_DROP;
if (sk_is_vsock(sk))
return SK_DROP;

msg->flags = flags;
msg->sk_redir = sk;
Expand Down

0 comments on commit 9c5bd93

Please sign in to comment.