sockhash/sockmap fixes
Subsystems (6)
John Fastabend
reviewerv2 cover sockhash/sockmap fixes
First three patches resolve issues found while testing sockhash and reviewing code. Syzbot also found them about the same time as I was working on fixes. The main issue is in the sockhash path we reduced the scope of sk_callback lock but this meant we could get update and close running in parallel so fix that here.
Then testing sk_msg and sk_skb programs together found that skb->dev is not always assigned and some of the helpers were depending on this to lookup max mtu. Fix this by using SKB_MAX_ALLOC when no MTU is available.
Finally, Martin spotted that the sockmap code was still using the qdisc skb cb structure. But I was sure we had fixed this long ago. Looks like we missed it in a merge conflict resolution and then by chance data_end offset was the same in both structures so everything sort of continued to work even though it could break at any moment if the structs ever change. So redo the conversion and this time also convert the helpers.
v2: fix '0 files changed' issue in patches
---
John Fastabend (4):
bpf: fix sk_skb programs without skb->dev assigned
bpf: sockhash, disallow bpf_tcp_close and update in parallel
bpf: sockmap, consume_skb in close path
bpf: sockmap, convert bpf_compute_data_pointers to bpf_*_sk_skb
include/net/tcp.h | 4 ++ kernel/bpf/sockmap.c | 24 ++++++++++-- kernel/bpf/syscall.c | 4 +- net/core/filter.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 121 insertions(+), 12 deletions(-)
John Fastabend
reviewerPATCH v2 1/4 bpf: fix sk_skb programs without skb->dev assigned
Multiple BPF helpers in use by sk_skb programs calculate the max skb length using the __bpf_skb_max_len function. However, this calculates the max length using the skb->dev pointer which can be NULL when an sk_skb program is paired with an sk_msg program.
To force this a sk_msg program needs to redirect into the ingress path of a sock with an attach sk_skb program. Then the the sk_skb program would need to call one of the helpers that adjust the skb size.
To fix the null ptr dereference use SKB_MAX_ALLOC size if no dev is available.
--- net/core/filter.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
net/core/filter.c +2 -1
@@ -2779,7 +2779,8 @@ static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)static u32 __bpf_skb_max_len (const struct sk_buff *skb) { -return skb->dev->mtu + skb->dev->hard_header_len; +return skb->dev ? skb->dev->mtu + skb->dev->hard_header_len : +SKB_MAX_ALLOC ; }static int bpf_skb_adjust_net (struct sk_buff *skb,s32 len_diff)
John Fastabend
reviewerPATCH v2 2/4 bpf: sockhash, disallow bpf_tcp_close and update in parallel
In reply to John FastabendAfter latest lock updates there is no longer anything preventing a close and recvmsg call running in parallel. Additionally, we can race update with close if we close a socket and simultaneously update if via the BPF userspace API (note the cgroup ops are already run with sock_lock held).
To resolve this take sock_lock in close and update paths.
--- kernel/bpf/sockmap.c | 15 +++++++++++++++ kernel/bpf/syscall.c | 4 +++- 2 files changed, 18 insertions(+), 1 deletion(-)
kernel/bpf/sockmap.c +15 -0
@@ -312,10 +312,12 @@ static void bpf_tcp_close(struct sock *sk, long timeout)struct smap_psock *psock;struct sock *osk; +lock_sock (sk);rcu_read_lock (); psock =smap_psock_sk (sk);if (unlikely (!psock)) {rcu_read_unlock (); +release_sock (sk);return sk->sk_prot->close (sk, timeout); }
@@ -371,6 +373,7 @@ static void bpf_tcp_close(struct sock *sk, long timeout) e =psock_map_pop (sk, psock); }rcu_read_unlock (); +release_sock (sk);close_fun (sk, timeout); }
@@ -2069,7 +2072,13 @@ static int sock_map_update_elem(struct bpf_map *map,return -EOPNOTSUPP ; } +lock_sock (skops.sk); +preempt_disable (); +rcu_read_lock (); err =sock_map_ctx_update_elem (&skops, map, key, flags); +rcu_read_unlock (); +preempt_enable (); +release_sock (skops.sk);fput (socket->file);return err; }
@@ -2410,7 +2419,13 @@ static int sock_hash_update_elem(struct bpf_map *map,return -EINVAL ; } +lock_sock (skops.sk); +preempt_disable (); +rcu_read_lock (); err =sock_hash_ctx_update_elem (&skops, map, key, flags); +rcu_read_unlock (); +preempt_enable (); +release_sock (skops.sk);fput (socket->file);return err; }
kernel/bpf/syscall.c +3 -1
@@ -735,7 +735,9 @@ static int map_update_elem(union bpf_attr *attr)if (bpf_map_is_dev_bound (map)) { err =bpf_map_offload_update_elem (map, key, value, attr->flags); goto out; - }else if (map->map_type ==BPF_MAP_TYPE_CPUMAP ) { + }else if (map->map_type ==BPF_MAP_TYPE_CPUMAP || + map->map_type ==BPF_MAP_TYPE_SOCKHASH || + map->map_type ==BPF_MAP_TYPE_SOCKMAP ) { err = map->ops->map_update_elem (map, key, value, attr->flags); goto out; }
John Fastabend
reviewerPATCH v2 3/4 bpf: sockmap, consume_skb in close path
In reply to John FastabendCurrently, when a sock is closed and the bpf_tcp_close() callback is used we remove memory but do not free the skb. Call consume_skb() if the skb is attached to the buffer.
--- kernel/bpf/sockmap.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/bpf/sockmap.c +4 -1
@@ -571,7 +571,8 @@ static int free_sg(struct sock *sk, int start, struct sk_msg_buff *md)while (sg[i].length) { free += sg[i].length;sk_mem_uncharge (sk, sg[i].length); -put_page (sg_page (&sg[i])); +if (!md->skb) +put_page (sg_page (&sg[i])); sg[i].length =0 ; sg[i].page_link =0 ; sg[i].offset =0 ;
@@ -580,6 +581,8 @@ static int free_sg(struct sock *sk, int start, struct sk_msg_buff *md)if (i ==MAX_SKB_FRAGS ) i =0 ; } +if (md->skb) +consume_skb (md->skb);return free; }
John Fastabend
reviewerPATCH v2 4/4 bpf: sockmap, convert bpf_compute_data_pointers to bpf_*_sk_skb
In reply to John FastabendIn commit
'bpf: bpf_compute_data uses incorrect cb structure' (8108a7751512)
we added the routine bpf_compute_data_end_sk_skb() to compute the correct data_end values, but this has since been lost. In kernel v4.14 this was correct and the above patch was applied in it entirety. Then when v4.14 was merged into v4.15-rc1 net-next tree we lost the piece that renamed bpf_compute_data_pointers to the new function bpf_compute_data_end_sk_skb. This was done here,
e1ea2f9856b7 ("Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net")
When it conflicted with the following rename patch,
6aaae2b6c433 ("bpf: rename bpf_compute_data_end into bpf_compute_data_pointers")
Finally, after a refactor I thought even the function bpf_compute_data_end_sk_skb() was no longer needed and it was erroneously removed.
However, we never reverted the sk_skb_convert_ctx_access() usage of tcp_skb_cb which had been committed and survived the merge conflict. Here we fix this by adding back the helper and *_data_end_sk_skb() usage. Using the bpf_skc_data_end mapping is not correct because it expects a qdisc_skb_cb object but at the sock layer this is not the case. Even though it happens to work here because we don't overwrite any data in-use at the socket layer and the cb structure is cleared later this has potential to create some subtle issues. But, even more concretely the filter.c access check uses tcp_skb_cb.
And by some act of chance though,
struct bpf_skb_data_end {
struct qdisc_skb_cb qdisc_cb; /* 0 28 */
/* XXX 4 bytes hole, try to pack */
void * data_meta; /* 32 8 */
void * data_end; /* 40 8 */
/* size: 48, cachelines: 1, members: 3 */
/* sum members: 44, holes: 1, sum holes: 4 */
/* last cacheline: 48 bytes */
};and then tcp_skb_cb,
struct tcp_skb_cb {
[...]
struct {
__u32 flags; /* 24 4 */
struct sock * sk_redir; /* 32 8 */
void * data_end; /* 40 8 */
} bpf; /* 24 */
};So when we use offset_of() to track down the byte offset we get 40 in either case and everything continues to work. Fix this mess and use correct structures its unclear how long this might actually work for until someone moves the structs around.
--- include/net/tcp.h | 4 ++ kernel/bpf/sockmap.c | 4 +- net/core/filter.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 97 insertions(+), 9 deletions(-)
include/net/tcp.h +4 -0
@@ -828,6 +828,10 @@ struct tcp_skb_cb {#define TCP_SKB_CB (__skb) ((struct tcp_skb_cb *)&((__skb)->cb[0])) +static inline void bpf_compute_data_end_sk_skb (struct sk_buff *skb) +{ +TCP_SKB_CB (skb)->bpf.data_end = skb->data +skb_headlen (skb); +}#if IS_ENABLED (CONFIG_IPV6 )/* This is the variant of inet6_iif() that must be used by TCP,
kernel/bpf/sockmap.c +2 -2
@@ -1236,7 +1236,7 @@ static int smap_verdict_func(struct smap_psock *psock, struct sk_buff *skb)*/ TCP_SKB_CB (skb)->bpf.sk_redir =NULL ; skb->sk = psock->sock; -bpf_compute_data_pointers (skb); +bpf_compute_data_end_sk_skb (skb);preempt_disable (); rc = (*prog->bpf_func)(skb, prog->insnsi);preempt_enable ();
@@ -1491,7 +1491,7 @@ static int smap_parse_func_strparser(struct strparser *strp,* any socket yet. */ skb->sk = psock->sock; -bpf_compute_data_pointers (skb); +bpf_compute_data_end_sk_skb (skb); rc = (*prog->bpf_func)(skb, prog->insnsi); skb->sk =NULL ;rcu_read_unlock ();
net/core/filter.c +91 -7
154 lines not on this page · raw message
Alexei Starovoitov
AppliedQuoted 19 lines · John Fastabend
First three patches resolve issues found while testing sockhash and reviewing code. Syzbot also found them about the same time as I was working on fixes. The main issue is in the sockhash path we reduced the scope of
sk_callbacklock but this meant we could get update and close running in parallel so fix that here.Then testing
sk_msgandsk_skbprograms together found thatskb->devis not always assigned and some of the helpers were depending on this to lookup max mtu. Fix this by usingSKB_MAX_ALLOCwhen no MTU is available.Finally, Martin spotted that the sockmap code was still using the qdisc skb cb structure. But I was sure we had fixed this long ago. Looks like we missed it in a merge conflict resolution and then by chance
data_endoffset was the same in both structures so everything sort of continued to work even though it could break at any moment if the structs ever change. So redo the conversion and this time also convert the helpers.
Quoted 1 line
v2: fix '0 files changed' issue in patches
Applied, Thanks