Commit 7b70973d authored by Lorenz Bauer's avatar Lorenz Bauer Committed by Daniel Borkmann

bpf: sockmap: Only check ULP for TCP sockets

The sock map code checks that a socket does not have an active upper
layer protocol before inserting it into the map. This requires casting
via inet_csk, which isn't valid for UDP sockets.

Guard checks for ULP by checking inet_sk(sk)->is_icsk first.
Signed-off-by: default avatarLorenz Bauer <lmb@cloudflare.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Reviewed-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20200309111243.6982-2-lmb@cloudflare.com
parent 3e7c67d9
...@@ -360,7 +360,13 @@ static inline void sk_psock_restore_proto(struct sock *sk, ...@@ -360,7 +360,13 @@ static inline void sk_psock_restore_proto(struct sock *sk,
struct sk_psock *psock) struct sk_psock *psock)
{ {
sk->sk_prot->unhash = psock->saved_unhash; sk->sk_prot->unhash = psock->saved_unhash;
tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space); if (inet_csk_has_ulp(sk)) {
tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space);
} else {
sk->sk_write_space = psock->saved_write_space;
/* Pairs with lockless read in sk_clone_lock() */
WRITE_ONCE(sk->sk_prot, psock->sk_proto);
}
} }
static inline void sk_psock_set_state(struct sk_psock *psock, static inline void sk_psock_set_state(struct sk_psock *psock,
......
...@@ -335,4 +335,10 @@ static inline void inet_csk_inc_pingpong_cnt(struct sock *sk) ...@@ -335,4 +335,10 @@ static inline void inet_csk_inc_pingpong_cnt(struct sock *sk)
if (icsk->icsk_ack.pingpong < U8_MAX) if (icsk->icsk_ack.pingpong < U8_MAX)
icsk->icsk_ack.pingpong++; icsk->icsk_ack.pingpong++;
} }
static inline bool inet_csk_has_ulp(struct sock *sk)
{
return inet_sk(sk)->is_icsk && !!inet_csk(sk)->icsk_ulp_ops;
}
#endif /* _INET_CONNECTION_SOCK_H */ #endif /* _INET_CONNECTION_SOCK_H */
...@@ -384,7 +384,6 @@ static int sock_map_update_common(struct bpf_map *map, u32 idx, ...@@ -384,7 +384,6 @@ static int sock_map_update_common(struct bpf_map *map, u32 idx,
struct sock *sk, u64 flags) struct sock *sk, u64 flags)
{ {
struct bpf_stab *stab = container_of(map, struct bpf_stab, map); struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
struct inet_connection_sock *icsk = inet_csk(sk);
struct sk_psock_link *link; struct sk_psock_link *link;
struct sk_psock *psock; struct sk_psock *psock;
struct sock *osk; struct sock *osk;
...@@ -395,7 +394,7 @@ static int sock_map_update_common(struct bpf_map *map, u32 idx, ...@@ -395,7 +394,7 @@ static int sock_map_update_common(struct bpf_map *map, u32 idx,
return -EINVAL; return -EINVAL;
if (unlikely(idx >= map->max_entries)) if (unlikely(idx >= map->max_entries))
return -E2BIG; return -E2BIG;
if (unlikely(rcu_access_pointer(icsk->icsk_ulp_data))) if (inet_csk_has_ulp(sk))
return -EINVAL; return -EINVAL;
link = sk_psock_init_link(); link = sk_psock_init_link();
...@@ -738,7 +737,6 @@ static int sock_hash_update_common(struct bpf_map *map, void *key, ...@@ -738,7 +737,6 @@ static int sock_hash_update_common(struct bpf_map *map, void *key,
struct sock *sk, u64 flags) struct sock *sk, u64 flags)
{ {
struct bpf_htab *htab = container_of(map, struct bpf_htab, map); struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
struct inet_connection_sock *icsk = inet_csk(sk);
u32 key_size = map->key_size, hash; u32 key_size = map->key_size, hash;
struct bpf_htab_elem *elem, *elem_new; struct bpf_htab_elem *elem, *elem_new;
struct bpf_htab_bucket *bucket; struct bpf_htab_bucket *bucket;
...@@ -749,7 +747,7 @@ static int sock_hash_update_common(struct bpf_map *map, void *key, ...@@ -749,7 +747,7 @@ static int sock_hash_update_common(struct bpf_map *map, void *key,
WARN_ON_ONCE(!rcu_read_lock_held()); WARN_ON_ONCE(!rcu_read_lock_held());
if (unlikely(flags > BPF_EXIST)) if (unlikely(flags > BPF_EXIST))
return -EINVAL; return -EINVAL;
if (unlikely(icsk->icsk_ulp_data)) if (inet_csk_has_ulp(sk))
return -EINVAL; return -EINVAL;
link = sk_psock_init_link(); link = sk_psock_init_link();
......
...@@ -105,13 +105,6 @@ void tcp_update_ulp(struct sock *sk, struct proto *proto, ...@@ -105,13 +105,6 @@ void tcp_update_ulp(struct sock *sk, struct proto *proto,
{ {
struct inet_connection_sock *icsk = inet_csk(sk); struct inet_connection_sock *icsk = inet_csk(sk);
if (!icsk->icsk_ulp_ops) {
sk->sk_write_space = write_space;
/* Pairs with lockless read in sk_clone_lock() */
WRITE_ONCE(sk->sk_prot, proto);
return;
}
if (icsk->icsk_ulp_ops->update) if (icsk->icsk_ulp_ops->update)
icsk->icsk_ulp_ops->update(sk, proto, write_space); icsk->icsk_ulp_ops->update(sk, proto, write_space);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment