virtio_net: add optional flow dissection in virtio_net_hdr_to_skb
Subsystems (9)
v3 cover virtio_net: add optional flow dissection in virtio_net_hdr_to_skb
From: Tanner Love <[email protected]>
First patch extends the flow dissector BPF program type to accept virtio-net header members.
Second patch uses this feature to add optional flow dissection in virtio_net_hdr_to_skb(). This allows admins to define permitted packets more strictly, for example dropping deprecated UDP_UFO packets.
Third patch extends kselftest to cover this feature.
Tanner Love (3):
net: flow_dissector: extend bpf flow dissector support with vnet hdr
virtio_net: add optional flow dissection in virtio_net_hdr_to_skb selftests/net: amend bpf flow dissector prog to do vnet hdr validation
drivers/net/bonding/bond_main.c | 2 +- include/linux/skbuff.h | 26 ++- include/linux/virtio_net.h | 25 ++- include/net/flow_dissector.h | 6 + include/uapi/linux/bpf.h | 6 + net/core/filter.c | 55 +++++ net/core/flow_dissector.c | 27 ++- net/core/sysctl_net_core.c | 9 + tools/include/uapi/linux/bpf.h | 6 + tools/testing/selftests/bpf/progs/bpf_flow.c | 188 +++++++++++++----- .../selftests/bpf/test_flow_dissector.c | 181 +++++++++++++++-- .../selftests/bpf/test_flow_dissector.sh | 19 ++ 12 files changed, 470 insertions(+), 80 deletions(-)
Signature · 1 line
2.32.0.rc0.204.g9fa02ecfa5-goog
PATCH v3 1/3 net: flow_dissector: extend bpf flow dissector support with vnet hdr
From: Tanner Love <[email protected]>
Amend the bpf flow dissector program type to accept virtio_net_hdr members. Do this to enable bpf flow dissector programs to perform virtio-net header validation. The next patch in this series will add a flow dissection hook in virtio_net_hdr_to_skb and make use of this extended functionality. That commit message has more background on the use case.
--- drivers/net/bonding/bond_main.c | 2 +- include/linux/skbuff.h | 26 ++++++++++++---- include/net/flow_dissector.h | 6 ++++ include/uapi/linux/bpf.h | 6 ++++ net/core/filter.c | 55 +++++++++++++++++++++++++++++++++ net/core/flow_dissector.c | 24 ++++++++++++-- tools/include/uapi/linux/bpf.h | 6 ++++ 7 files changed, 116 insertions(+), 9 deletions(-)
drivers/net/bonding/bond_main.c +1 -1
@@ -3554,7 +3554,7 @@ static bool bond_flow_dissect(struct bonding *bond, struct sk_buff *skb,case BOND_XMIT_POLICY_ENCAP34 :memset (fk,0 ,sizeof (*fk));return __skb_flow_dissect (NULL , skb, &flow_keys_bonding, - fk,NULL ,0 ,0 ,0 ,0 ); + fk,NULL ,0 ,0 ,0 ,0 ,NULL );default :break ; }
include/linux/skbuff.h +20 -6
@@ -1312,18 +1312,20 @@ struct bpf_flow_dissector;bool bpf_flow_dissect (struct bpf_prog *prog,struct bpf_flow_dissector *ctx,__be16 proto,int nhoff,int hlen,unsigned int flags); +struct virtio_net_hdr ;bool __skb_flow_dissect (const struct net *net,const struct sk_buff *skb,struct flow_dissector *flow_dissector,void *target_container,const void *data, -__be16 proto,int nhoff,int hlen,unsigned int flags); +__be16 proto,int nhoff,int hlen,unsigned int flags, +const struct virtio_net_hdr *vhdr);static inline bool skb_flow_dissect (const struct sk_buff *skb,struct flow_dissector *flow_dissector,void *target_container,unsigned int flags) {return __skb_flow_dissect (NULL , skb, flow_dissector, - target_container,NULL ,0 ,0 ,0 , flags); + target_container,NULL ,0 ,0 ,0 , flags,NULL ); } static inline bool skb_flow_dissect_flow_keys(const struct sk_buff *skb,
@@ -1332,7 +1334,20 @@ static inline bool skb_flow_dissect_flow_keys(const struct sk_buff *skb, {memset (flow,0 ,sizeof (*flow));return __skb_flow_dissect (NULL , skb, &flow_keys_dissector, - flow,NULL ,0 ,0 ,0 , flags); + flow,NULL ,0 ,0 ,0 , flags,NULL ); +} + +static inline bool +__skb_flow_dissect_flow_keys_basic (const struct net *net, +const struct sk_buff *skb, +struct flow_keys_basic *flow, +const void *data,__be16 proto, +int nhoff,int hlen,unsigned int flags, +const struct virtio_net_hdr *vhdr) +{ +memset (flow,0 ,sizeof (*flow)); +return __skb_flow_dissect (net, skb, &flow_keys_basic_dissector, flow, + data, proto, nhoff, hlen, flags, vhdr); } static inline bool
@@ -1342,9 +1357,8 @@ skb_flow_dissect_flow_keys_basic(const struct net *net, const void *data, __be16 proto, int nhoff, int hlen, unsigned int flags) { - memset(flow, 0, sizeof(*flow)); -return __skb_flow_dissect (net, skb, &flow_keys_basic_dissector, flow, - data, proto, nhoff, hlen, flags); + return __skb_flow_dissect_flow_keys_basic(net, skb, flow, data, proto, +nhoff ,hlen ,flags ,NULL ); } void skb_flow_dissect_meta(const struct sk_buff *skb,
include/net/flow_dissector.h +6 -0
@@ -370,6 +370,12 @@ struct bpf_flow_dissector {const struct sk_buff *skb;const void *data;const void *data_end; +__u8 vhdr_flags; +__u8 vhdr_gso_type; +__u16 vhdr_hdr_len; +__u16 vhdr_gso_size; +__u16 vhdr_csum_start; +__u16 vhdr_csum_offset; }; static inline void
include/uapi/linux/bpf.h +6 -0
@@ -5155,6 +5155,12 @@ struct __sk_buff {__u32 gso_segs; __bpf_md_ptr(struct bpf_sock *, sk);__u32 gso_size; +__u8 vhdr_flags; +__u8 vhdr_gso_type; +__u16 vhdr_hdr_len; +__u16 vhdr_gso_size; +__u16 vhdr_csum_start; +__u16 vhdr_csum_offset; };struct bpf_tunnel_key {
net/core/filter.c +55 -0
67 lines not on this page · raw message
net/core/flow_dissector.c +22 -2
@@ -28,6 +28,7 @@#include <scsi/fc/fc_fcoe.h> #include <uapi/linux/batadv_packet.h> #include <linux/bpf.h> +#include <linux/virtio_net.h> #if IS_ENABLED (CONFIG_NF_CONNTRACK )#include <net/netfilter/nf_conntrack_core.h> #include <net/netfilter/nf_conntrack_labels.h>
@@ -904,6 +905,7 @@ bool bpf_flow_dissect(struct bpf_prog *prog, struct bpf_flow_dissector *ctx,* @hlen: packet header length, if @data is NULL use skb_headlen(skb) * @flags: flags that control the dissection process, e.g. * FLOW_DISSECTOR_F_STOP_AT_ENCAP. +* @vhdr: virtio_net_header to include in kernel context for BPF flow dissector * * The function will try to retrieve individual keys into target specified * by flow_dissector from either the skbuff or a raw buffer specified by the
@@ -915,7 +917,8 @@ bool __skb_flow_dissect(const struct net *net,const struct sk_buff *skb, struct flow_dissector *flow_dissector, void *target_container, const void *data, - __be16 proto, int nhoff, int hlen, unsigned int flags) + __be16 proto, int nhoff, int hlen, unsigned int flags, + const struct virtio_net_hdr *vhdr) { struct flow_dissector_key_control *key_control;struct flow_dissector_key_basic *key_basic;
@@ -1001,6 +1004,23 @@ bool __skb_flow_dissect(const struct net *net,__be16 n_proto = proto;struct bpf_prog *prog; +if (vhdr) { + ctx.vhdr_flags = vhdr->flags; + ctx.vhdr_gso_type = vhdr->gso_type; + ctx.vhdr_hdr_len = +__virtio16_to_cpu (virtio_legacy_is_little_endian (), + vhdr->hdr_len); + ctx.vhdr_gso_size = +__virtio16_to_cpu (virtio_legacy_is_little_endian (), + vhdr->gso_size); + ctx.vhdr_csum_start = +__virtio16_to_cpu (virtio_legacy_is_little_endian (), + vhdr->csum_start); + ctx.vhdr_csum_offset = +__virtio16_to_cpu (virtio_legacy_is_little_endian (), + vhdr->csum_offset); + } +if (skb) { ctx.skb = skb;/* we can't use 'proto' in the skb case
@@ -1610,7 +1630,7 @@ u32 __skb_get_hash_symmetric(const struct sk_buff *skb)memset (&keys,0 ,sizeof (keys));__skb_flow_dissect (NULL , skb, &flow_keys_dissector_symmetric, &keys,NULL ,0 ,0 ,0 , -FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL ); +FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL ,NULL );return __flow_hash_from_keys (&keys, &hashrnd); }
tools/include/uapi/linux/bpf.h +6 -0
@@ -5155,6 +5155,12 @@ struct __sk_buff {__u32 gso_segs; __bpf_md_ptr(struct bpf_sock *, sk);__u32 gso_size; +__u8 vhdr_flags; +__u8 vhdr_gso_type; +__u16 vhdr_hdr_len; +__u16 vhdr_gso_size; +__u16 vhdr_csum_start; +__u16 vhdr_csum_offset; };struct bpf_tunnel_key {
Signature · 1 line
2.32.0.rc0.204.g9fa02ecfa5-goog
PATCH v3 2/3 virtio_net: add optional flow dissection in virtio_net_hdr_to_skb
In reply to Tanner LoveFrom: Tanner Love <[email protected]>
Syzkaller bugs have resulted from loose specification of virtio_net_hdr[1]. Enable execution of a BPF flow dissector program in virtio_net_hdr_to_skb to validate the vnet header and drop bad input.
The existing behavior of accepting these vnet headers is part of the ABI. But individual admins may want to enforce restrictions. For example, verifying that a GSO_TCPV4 gso_type matches packet contents:
unencapsulated TCP/IPV4 packet with payload exceeding gso_size and hdr_len at payload offset.
Introduce a new sysctl net.core.flow_dissect_vnet_hdr controlling a static key to decide whether to perform flow dissection. When the key is false, virtio_net_hdr_to_skb computes as before.
[1] https://syzkaller.appspot.com/bug?id=b419a5ca95062664fe1a60b764621eb4526e2cd0
--- include/linux/virtio_net.h | 25 +++++++++++++++++++++---- net/core/flow_dissector.c | 3 +++ net/core/sysctl_net_core.c | 9 +++++++++ 3 files changed, 33 insertions(+), 4 deletions(-)
include/linux/virtio_net.h +21 -4
@@ -25,10 +25,13 @@ static inline int virtio_net_hdr_set_proto(struct sk_buff *skb,return 0 ; } +DECLARE_STATIC_KEY_FALSE (sysctl_flow_dissect_vnet_hdr_key); +static inline int virtio_net_hdr_to_skb (struct sk_buff *skb,const struct virtio_net_hdr *hdr,bool little_endian) { +struct flow_keys_basic keys;unsigned int gso_type =0 ;unsigned int thlen =0 ;unsigned int p_off =0 ;
@@ -78,13 +81,24 @@ static inline int virtio_net_hdr_to_skb(struct sk_buff *skb, p_off =skb_transport_offset (skb) + thlen;if (!pskb_may_pull (skb, p_off))return -EINVAL ; - }else { + } + +/* BPF flow dissection for optional strict validation. +* +* Admins can define permitted packets more strictly, such as dropping +* deprecated UDP_UFO packets and requiring skb->protocol to be non-zero +* and matching packet headers. +*/ +if (static_branch_unlikely (&sysctl_flow_dissect_vnet_hdr_key) && + !__skb_flow_dissect_flow_keys_basic (NULL , skb, &keys,NULL ,0 ,0 ,0 , +0 , hdr)) +return -EINVAL ; + +if (!(hdr->flags &VIRTIO_NET_HDR_F_NEEDS_CSUM )) {/* gso packets without NEEDS_CSUM do not set transport_offset. * probe and drop if does not match one of the above types. */ if (gso_type && skb->network_header) { -struct flow_keys_basic keys; -if (!skb->protocol) {__be16 protocol =dev_parse_header_protocol (skb);
@@ -92,8 +106,11 @@ static inline int virtio_net_hdr_to_skb(struct sk_buff *skb,if (protocol && protocol != skb->protocol)return -EINVAL ; } + retry: -if (!skb_flow_dissect_flow_keys_basic (NULL , skb, &keys, +/* only if flow dissection not already done */ +if (!static_branch_unlikely (&sysctl_flow_dissect_vnet_hdr_key) && + !skb_flow_dissect_flow_keys_basic (NULL , skb, &keys,NULL ,0 ,0 ,0 ,0 )) {/* UFO does not specify ipv4 or 6: try both */
net/core/flow_dissector.c +3 -0
@@ -35,6 +35,9 @@#endif #include <linux/bpf-netns.h> +DEFINE_STATIC_KEY_FALSE (sysctl_flow_dissect_vnet_hdr_key); +EXPORT_SYMBOL (sysctl_flow_dissect_vnet_hdr_key); +static void dissector_set_key (struct flow_dissector *flow_dissector,enum flow_dissector_key_id key_id) {
net/core/sysctl_net_core.c +9 -0
@@ -36,6 +36,8 @@ static int net_msg_warn; /* Unused, but still a sysctl */int sysctl_fb_tunnels_only_for_init_net__read_mostly =0 ;EXPORT_SYMBOL (sysctl_fb_tunnels_only_for_init_net); +DECLARE_STATIC_KEY_FALSE (sysctl_flow_dissect_vnet_hdr_key); +/* 0 - Keep current behavior: * IPv4: inherit all current settings from init_net * IPv6: reset all settings to default
@@ -580,6 +582,13 @@ static struct ctl_table net_core_table[] = { .extra1 = SYSCTL_ONE, .extra2 = &int_3600, }, + { + .procname = "flow_dissect_vnet_hdr", + .data = &sysctl_flow_dissect_vnet_hdr_key.key, + .maxlen = sizeof(sysctl_flow_dissect_vnet_hdr_key), + .mode = 0644, + .proc_handler = proc_do_static_key, + }, { } };
Signature · 1 line
2.32.0.rc0.204.g9fa02ecfa5-goog
PATCH v3 3/3 selftests/net: amend bpf flow dissector prog to do vnet hdr validation
In reply to Tanner LoveFrom: Tanner Love <[email protected]>
Change the BPF flow dissector program to perform various checks on the virtio_net_hdr fields after doing flow dissection.
Amend test_flow_dissector.(c|sh) to add test cases that inject packets with reasonable or unreasonable virtio-net headers and assert that bad packets are dropped and good packets are not. Do this via packet socket; the kernel executes tpacket_snd, which enters virtio_net_hdr_to_skb, where flow dissection / vnet header validation occurs.
--- tools/testing/selftests/bpf/progs/bpf_flow.c | 188 +++++++++++++----- .../selftests/bpf/test_flow_dissector.c | 181 +++++++++++++++-- .../selftests/bpf/test_flow_dissector.sh | 19 ++ 3 files changed, 321 insertions(+), 67 deletions(-)
tools/testing/selftests/bpf/progs/bpf_flow.c +143 -45
@@ -13,6 +13,7 @@#include <linux/tcp.h> #include <linux/udp.h> #include <linux/if_packet.h> +#include <linux/virtio_net.h> #include <sys/socket.h> #include <linux/if_tunnel.h> #include <linux/mpls.h>
@@ -71,15 +72,119 @@ struct { __type(value, struct bpf_flow_keys); } last_dissection SEC(".maps"); -static __always_inline int export_flow_keys(struct bpf_flow_keys *keys, -int ret) +/* Drops invalid virtio-net headers */ +static __always_inline int validate_virtio_net_hdr(const struct __sk_buff *skb) { +const struct bpf_flow_keys *keys = skb->flow_keys; + +/* Check gso */ +if (skb->vhdr_gso_type !=VIRTIO_NET_HDR_GSO_NONE ) { +if (!(skb->vhdr_flags &VIRTIO_NET_HDR_F_NEEDS_CSUM )) +return BPF_DROP ; + +if (keys->is_encap) +return BPF_DROP ; + +switch (skb->vhdr_gso_type & ~VIRTIO_NET_HDR_GSO_ECN ) { +case VIRTIO_NET_HDR_GSO_TCPV4 : +if (keys->addr_proto !=ETH_P_IP || + keys->ip_proto !=IPPROTO_TCP ) +return BPF_DROP ; + +if (skb->vhdr_gso_size >= skb->len - keys->thoff - +sizeof (struct tcphdr )) +return BPF_DROP ; + +break ; +case VIRTIO_NET_HDR_GSO_TCPV6 : +if (keys->addr_proto !=ETH_P_IPV6 || + keys->ip_proto !=IPPROTO_TCP ) +return BPF_DROP ; + +if (skb->vhdr_gso_size >= skb->len - keys->thoff - +sizeof (struct tcphdr )) +return BPF_DROP ; + +break ; +case VIRTIO_NET_HDR_GSO_UDP : +if (keys->ip_proto !=IPPROTO_UDP ) +return BPF_DROP ; + +if (skb->vhdr_gso_size >= skb->len - keys->thoff - +sizeof (struct udphdr )) +return BPF_DROP ; + +break ; +default : +return BPF_DROP ; + } + } + +/* Check hdr_len */ +if (skb->vhdr_hdr_len) { +switch (keys->ip_proto) { +case IPPROTO_TCP : +if (skb->vhdr_hdr_len != skb->flow_keys->thoff + +sizeof (struct tcphdr )) +return BPF_DROP ; + +break ; +case IPPROTO_UDP : +if (skb->vhdr_hdr_len != keys->thoff + +sizeof (struct udphdr )) +return BPF_DROP ; + +break ; + } + } + +/* Check csum */ +if (skb->vhdr_flags &VIRTIO_NET_HDR_F_NEEDS_CSUM ) { +if (keys->addr_proto !=ETH_P_IP && + keys->addr_proto !=ETH_P_IPV6 ) +return BPF_DROP ; + +if (skb->vhdr_csum_start != keys->thoff) +return BPF_DROP ; + +switch (keys->ip_proto) { +case IPPROTO_TCP : +if (skb->vhdr_csum_offset != + offsetof(struct tcphdr , check)) +return BPF_DROP ; + +break ; +case IPPROTO_UDP : +if (skb->vhdr_csum_offset != + offsetof(struct udphdr , check)) +return BPF_DROP ; + +break ; +default : +return BPF_DROP ; + } + } + +return BPF_OK ; +} + +/* Common steps to perform regardless of where protocol parsing finishes: +* 1. store flow keys in map +* 2. if parse result is BPF_OK, parse the vnet hdr if present +* 3. return the parse result +*/ +static __always_inline int parse_epilogue(struct __sk_buff *skb, int ret) +{ +const struct bpf_flow_keys *keys = skb->flow_keys;__u32 key = (__u32)(keys->sport) <<16 | keys->dport;struct bpf_flow_keys val;memcpy (&val, keys,sizeof (val));bpf_map_update_elem (&last_dissection, &key, &val,BPF_ANY ); -return ret; + +if (ret !=BPF_OK ) +return ret; +return validate_virtio_net_hdr (skb); }#define IPV6_FLOWLABEL_MASK __bpf_constant_htonl(0x000FFFFF)
@@ -114,8 +219,6 @@ static __always_inline void *bpf_flow_dissect_get_header(struct __sk_buff *skb,/* Dispatches on ETHERTYPE */ static __always_inline int parse_eth_proto(struct __sk_buff *skb, __be16 proto) { -struct bpf_flow_keys *keys = skb->flow_keys; -switch (proto) {case bpf_htons (ETH_P_IP ):bpf_tail_call_static (skb, &jmp_table,IP );
@@ -131,12 +234,10 @@ static __always_inline int parse_eth_proto(struct __sk_buff *skb, __be16 proto)case bpf_htons (ETH_P_8021AD ):bpf_tail_call_static (skb, &jmp_table,VLAN );break ; -default : -/* Protocol not supported */ -return export_flow_keys (keys,BPF_DROP ); } -return export_flow_keys (keys,BPF_DROP ); +/* Protocol not supported */ +return parse_epilogue (skb,BPF_DROP ); }SEC ("flow_dissector" )
@@ -162,28 +263,28 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)case IPPROTO_ICMP : icmp =bpf_flow_dissect_get_header (skb,sizeof (*icmp), &_icmp);if (!icmp) -return export_flow_keys (keys,BPF_DROP ); -return export_flow_keys (keys,BPF_OK ); +return parse_epilogue (skb,BPF_DROP ); +return parse_epilogue (skb,BPF_OK );case IPPROTO_IPIP : keys->is_encap = true;if (keys->flags &BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP ) -return export_flow_keys (keys,BPF_OK ); +return parse_epilogue (skb,BPF_OK );return parse_eth_proto (skb,bpf_htons (ETH_P_IP ));case IPPROTO_IPV6 : keys->is_encap = true;if (keys->flags &BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP ) -return export_flow_keys (keys,BPF_OK ); +return parse_epilogue (skb,BPF_OK );return parse_eth_proto (skb,bpf_htons (ETH_P_IPV6 ));case IPPROTO_GRE : gre =bpf_flow_dissect_get_header (skb,sizeof (*gre), &_gre);if (!gre) -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP );if (bpf_htons (gre->flags &GRE_VERSION ))/* Only inspect standard GRE packets with version 0 */ -return export_flow_keys (keys,BPF_OK ); +return parse_epilogue (skb,BPF_OK ); keys->thoff +=sizeof (*gre);/* Step over GRE Flags and Proto */ if (GRE_IS_CSUM (gre->flags))
@@ -195,13 +296,13 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto) keys->is_encap = true;if (keys->flags &BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP ) -return export_flow_keys (keys,BPF_OK ); +return parse_epilogue (skb,BPF_OK );if (gre->proto ==bpf_htons (ETH_P_TEB )) { eth =bpf_flow_dissect_get_header (skb,sizeof (*eth), &_eth);if (!eth) -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP ); keys->thoff +=sizeof (*eth);
@@ -212,37 +313,35 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)case IPPROTO_TCP : tcp =bpf_flow_dissect_get_header (skb,sizeof (*tcp), &_tcp);if (!tcp) -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP );if (tcp->doff <5 ) -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP );if ((__u8 *)tcp + (tcp->doff <<2 ) > data_end) -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP ); keys->sport = tcp->source; keys->dport = tcp->dest; -return export_flow_keys (keys,BPF_OK ); +return parse_epilogue (skb,BPF_OK );case IPPROTO_UDP :case IPPROTO_UDPLITE : udp =bpf_flow_dissect_get_header (skb,sizeof (*udp), &_udp);if (!udp) -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP ); keys->sport = udp->source; keys->dport = udp->dest; -return export_flow_keys (keys,BPF_OK ); +return parse_epilogue (skb,BPF_OK );default : -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP ); } -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP ); } static __always_inline int parse_ipv6_proto(struct __sk_buff *skb, __u8 nexthdr) { -struct bpf_flow_keys *keys = skb->flow_keys; -switch (nexthdr) {case IPPROTO_HOPOPTS :case IPPROTO_DSTOPTS :
@@ -255,7 +354,7 @@ static __always_inline int parse_ipv6_proto(struct __sk_buff *skb, __u8 nexthdr)return parse_ip_proto (skb, nexthdr); } -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP ); }PROG (IP )(struct __sk_buff *skb)
@@ -268,11 +367,11 @@ PROG(IP)(struct __sk_buff *skb) iph =bpf_flow_dissect_get_header (skb,sizeof (*iph), &_iph);if (!iph) -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP );/* IP header cannot be smaller than 20 bytes */ if (iph->ihl <5 ) -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP ); keys->addr_proto =ETH_P_IP ; keys->ipv4_src = iph->saddr;
@@ -281,7 +380,7 @@ PROG(IP)(struct __sk_buff *skb) keys->thoff += iph->ihl <<2 ;if (data + keys->thoff > data_end) -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP );if (iph->frag_off &bpf_htons (IP_MF |IP_OFFSET )) { keys->is_frag = true;
@@ -302,7 +401,7 @@ PROG(IP)(struct __sk_buff *skb) }if (done) -return export_flow_keys (keys,BPF_OK ); +return parse_epilogue (skb,BPF_OK );return parse_ip_proto (skb, iph->protocol); }
@@ -314,7 +413,7 @@ PROG(IPV6)(struct __sk_buff *skb) ip6h =bpf_flow_dissect_get_header (skb,sizeof (*ip6h), &_ip6h);if (!ip6h) -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP ); keys->addr_proto =ETH_P_IPV6 ;memcpy (&keys->ipv6_src, &ip6h->saddr,2 *sizeof (ip6h->saddr));
@@ -324,7 +423,7 @@ PROG(IPV6)(struct __sk_buff *skb) keys->flow_label =ip6_flowlabel (ip6h);if (keys->flags &BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL ) -return export_flow_keys (keys,BPF_OK ); +return parse_epilogue (skb,BPF_OK );return parse_ipv6_proto (skb, ip6h->nexthdr); }
@@ -336,7 +435,7 @@ PROG(IPV6OP)(struct __sk_buff *skb) ip6h =bpf_flow_dissect_get_header (skb,sizeof (*ip6h), &_ip6h);if (!ip6h) -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP );/* hlen is in 8-octets and does not include the first 8 bytes * of the header
@@ -354,7 +453,7 @@ PROG(IPV6FR)(struct __sk_buff *skb) fragh =bpf_flow_dissect_get_header (skb,sizeof (*fragh), &_fragh);if (!fragh) -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP ); keys->thoff +=sizeof (*fragh); keys->is_frag = true;
@@ -367,9 +466,9 @@ PROG(IPV6FR)(struct __sk_buff *skb)* explicitly asked for. */ if (!(keys->flags &BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG )) -return export_flow_keys (keys,BPF_OK ); +return parse_epilogue (skb,BPF_OK ); }else { -return export_flow_keys (keys,BPF_OK ); +return parse_epilogue (skb,BPF_OK ); }return parse_ipv6_proto (skb, fragh->nexthdr);
@@ -377,14 +476,13 @@ PROG(IPV6FR)(struct __sk_buff *skb)PROG (MPLS )(struct __sk_buff *skb) { -struct bpf_flow_keys *keys = skb->flow_keys;struct mpls_label *mpls, _mpls; mpls =bpf_flow_dissect_get_header (skb,sizeof (*mpls), &_mpls);if (!mpls) -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP ); -return export_flow_keys (keys,BPF_OK ); +return parse_epilogue (skb,BPF_OK ); }PROG (VLAN )(struct __sk_buff *skb)
@@ -396,10 +494,10 @@ PROG(VLAN)(struct __sk_buff *skb)if (keys->n_proto ==bpf_htons (ETH_P_8021AD )) { vlan =bpf_flow_dissect_get_header (skb,sizeof (*vlan), &_vlan);if (!vlan) -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP );if (vlan->h_vlan_encapsulated_proto !=bpf_htons (ETH_P_8021Q )) -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP ); keys->nhoff +=sizeof (*vlan); keys->thoff +=sizeof (*vlan);
@@ -407,14 +505,14 @@ PROG(VLAN)(struct __sk_buff *skb) vlan =bpf_flow_dissect_get_header (skb,sizeof (*vlan), &_vlan);if (!vlan) -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP ); keys->nhoff +=sizeof (*vlan); keys->thoff +=sizeof (*vlan);/* Only allow 8021AD + 8021Q double tagging and no triple tagging.*/ if (vlan->h_vlan_encapsulated_proto ==bpf_htons (ETH_P_8021AD ) || vlan->h_vlan_encapsulated_proto ==bpf_htons (ETH_P_8021Q )) -return export_flow_keys (keys,BPF_DROP ); +return parse_epilogue (skb,BPF_DROP ); keys->n_proto = vlan->h_vlan_encapsulated_proto;return parse_eth_proto (skb, vlan->h_vlan_encapsulated_proto);
tools/testing/selftests/bpf/test_flow_dissector.c +159 -22
343 lines not on this page · raw message
tools/testing/selftests/bpf/test_flow_dissector.sh +19 -0
@@ -51,6 +51,9 @@ if [[ -z $(ip netns identify $$) ]]; then echo "Skipping root flow dissector test, bpftool not found" >&2 fi + orig_flow_dissect_sysctl=$(</proc/sys/net/core/flow_dissect_vnet_hdr) + sysctl net.core.flow_dissect_vnet_hdr=1 + # Run the rest of the tests in a net namespace. ../net/in_netns.sh "$0" "$@" err=$(( $err + $? ))
@@ -61,6 +64,7 @@ if [[ -z $(ip netns identify $$) ]]; then echo "selftests: $TESTNAME [FAILED]"; fi + sysctl net.core.flow_dissect_vnet_hdr=$orig_flow_dissect_sysctl exit $err fi
@@ -165,4 +169,19 @@ tc filter add dev lo parent ffff: protocol ipv6 pref 1337 flower ip_proto \ # Send 10 IPv6/UDP packets from port 10. Filter should not drop any. ./test_flow_dissector -i 6 -f 10 + +echo "Testing virtio-net header validation..." +echo "Testing valid vnet headers. Should *not* be dropped." +./with_addr.sh ./test_flow_dissector -i 4 -D 192.168.0.1 -S 1.1.1.1 -p -v +echo "Testing partial transport header. Should be dropped." +./with_addr.sh ./test_flow_dissector -i 4 -D 192.168.0.1 -S 1.1.1.1 -p -v -U -E +echo "Testing valid vnet gso spec. Should *not* be dropped." +./with_addr.sh ./test_flow_dissector -i 4 -D 192.168.0.1 -S 1.1.1.1 -p -v -g -c -l 8000 +echo "Testing invalid vnet gso size. Should be dropped." +./with_addr.sh ./test_flow_dissector -i 4 -D 192.168.0.1 -S 1.1.1.1 -p -v -g -c -l 100 -E +echo "Testing invalid vnet header len. Should be dropped." +./with_addr.sh ./test_flow_dissector -i 4 -D 192.168.0.1 -S 1.1.1.1 -p -v -L -E +echo "Testing vnet gso without csum. Should be dropped." +./with_addr.sh ./test_flow_dissector -i 4 -D 192.168.0.1 -S 1.1.1.1 -p -v -g -l 8000 -E + exit 0
Signature · 1 line
2.32.0.rc0.204.g9fa02ecfa5-goog
David Miller
maintainerFrom: Tanner Love <[email protected]>
Date: Tue, 1 Jun 2021 18:18:37 -0400
Quoted 11 lines
From: Tanner Love <[email protected]>
First patch extends the flow dissector BPF program type to accept virtio-net header members.
Second patch uses this feature to add optional flow dissection in
virtio_net_hdr_to_skb(). This allows admins to define permitted packets more strictly, for example dropping deprecatedUDP_UFOpackets.Third patch extends kselftest to cover this feature.
Definitely need some bpf review of these changes.
Alexei, Daniel?
Thanks.
Quoted 9 lines · David Miller
From: Tanner Love <[email protected]>
Date: Tue, 1 Jun 2021 18:18:37 -0400Quoted 5 lines
From: Tanner Love <[email protected]>
First patch extends the flow dissector BPF program type to accept virtio-net header members.
Quoted 8 lines
Quoted 6 lines
Second patch uses this feature to add optional flow dissection in
virtio_net_hdr_to_skb(). This allows admins to define permitted packets more strictly, for example dropping deprecatedUDP_UFOpackets.Third patch extends kselftest to cover this feature.
Definitely need some bpf review of these changes.
Yeah. imo it's more bpf material than net.
We'll process it.
Re: [PATCH net-next v3 1/3] net: flow_dissector: extend bpf flow dissector support with vnet hdr
In reply to Tanner LoveQuoted 125 lines · Tanner Love
From: Tanner Love <[email protected]>
Amend the bpf flow dissector program type to accept
virtio_net_hdrmembers. Do this to enable bpf flow dissector programs to perform virtio-net header validation. The next patch in this series will add a flow dissection hook invirtio_net_hdr_to_skband make use of this extended functionality. That commit message has more background on the use case.Signed-off-by Tanner Love <[email protected]>Reviewed-by Willem de Bruijn <[email protected]>Reviewed-by Petar Penkov <[email protected]>--- drivers/net/bonding/bond_main.c | 2 +- include/linux/skbuff.h | 26 ++++++++++++---- include/net/flow_dissector.h | 6 ++++ include/uapi/linux/bpf.h | 6 ++++ net/core/filter.c | 55 +++++++++++++++++++++++++++++++++ net/core/flow_dissector.c | 24 ++++++++++++-- tools/include/uapi/linux/bpf.h | 6 ++++ 7 files changed, 116 insertions(+), 9 deletions(-)drivers/net/bonding/bond_main.c +1
-1@@ -3554,7 +3554,7 @@ static bool bond_flow_dissect(struct bonding *bond,struct sk_buff *skb, case BOND_XMIT_POLICY_ENCAP34: memset(fk, 0, sizeof(*fk));return __skb_flow_dissect (NULL , skb, &flow_keys_bonding, - fk,NULL ,0 ,0 ,0 ,0 ); + fk,NULL ,0 ,0 ,0 ,0 ,NULL );default :break ;}include/linux/skbuff.h +4
-2@@ -1312,18 +1312,20 @@ struct bpf_flow_dissector;bool bpf_flow_dissect (struct bpf_prog *prog,struct bpf_flow_dissector *ctx,__be16 proto,int nhoff,int hlen,unsigned int flags); +struct virtio_net_hdr ;bool __skb_flow_dissect (const struct net *net,const struct sk_buff *skb,struct flow_dissector *flow_dissector,void *target_container,const void *data, -__be16 proto,int nhoff,int hlen,unsigned int flags); +__be16 proto,int nhoff,int hlen,unsigned int flags, +const struct virtio_net_hdr *vhdr);static inline bool skb_flow_dissect (const struct sk_buff *skb,struct flow_dissector *flow_dissector,void *target_container,unsigned int flags) {return __skb_flow_dissect (NULL , skb, flow_dissector, - target_container,NULL ,0 ,0 ,0 , flags); + target_container,NULL ,0 ,0 ,0 , flags,NULL ); }static inline bool skb_flow_dissect_flow_keys(const struct
sk_buff*skb,include/linux/skbuff.h +14
-1@@ -1332,7 +1334,20 @@ static inline bool skb_flow_dissect_flow_keys(const struct sk_buff *skb, { memset(flow, 0, sizeof(*flow));return __skb_flow_dissect (NULL , skb, &flow_keys_dissector, - flow,NULL ,0 ,0 ,0 , flags); + flow,NULL ,0 ,0 ,0 , flags,NULL ); +} + +static inline bool +__skb_flow_dissect_flow_keys_basic (const struct net *net, +const struct sk_buff *skb, +struct flow_keys_basic *flow, +const void *data,__be16 proto, +int nhoff,int hlen,unsigned int flags, +const struct virtio_net_hdr *vhdr) +{ +memset (flow,0 ,sizeof (*flow)); +return __skb_flow_dissect (net, skb, &flow_keys_basic_dissector, flow, + data, proto, nhoff, hlen, flags, vhdr); }static inline bool
include/linux/skbuff.h +2
-3@@ -1342,9 +1357,8 @@ skb_flow_dissect_flow_keys_basic(const struct net *net, const void *data, __be16 proto, int nhoff, int hlen, unsigned int flags) { -memset (flow,0 ,sizeof (*flow)); -return __skb_flow_dissect (net, skb, &flow_keys_basic_dissector, flow, - data, proto, nhoff, hlen, flags); +return __skb_flow_dissect_flow_keys_basic (net, skb, flow, data, proto, + nhoff, hlen, flags,NULL ); }void skb_flow_dissect_meta(const struct
sk_buff*skb,include/net/flow_dissector.h +6
-0@@ -370,6 +370,12 @@ struct bpf_flow_dissector {const struct sk_buff *skb;const void *data;const void *data_end; +__u8 vhdr_flags; +__u8 vhdr_gso_type; +__u16 vhdr_hdr_len; +__u16 vhdr_gso_size; +__u16 vhdr_csum_start; +__u16 vhdr_csum_offset; }; static inline voidinclude/uapi/linux/bpf.h +0
-0@@ -5155,6 +5155,12 @@ struct __sk_buff {__u32 gso_segs; __bpf_md_ptr(struct bpf_sock *, sk);__u32 gso_size;
[..]
include/uapi/linux/bpf.h
Quoted 6 lines
include/uapi/linux/bpf.h +6
-0+__u8 vhdr_flags; +__u8 vhdr_gso_type; +__u16 vhdr_hdr_len; +__u16 vhdr_gso_size; +__u16 vhdr_csum_start; +__u16 vhdr_csum_offset;
These are flow dissector specific, any reason not to add them to struct bpf_flow_keys instead?
Quoted 123 lines · Tanner Love
From: Tanner Love <[email protected]>
Syzkaller bugs have resulted from loose specification of virtio_net_hdr[1]. Enable execution of a BPF flow dissector program in
virtio_net_hdr_to_skbto validate the vnet header and drop bad input.The existing behavior of accepting these vnet headers is part of the ABI. But individual admins may want to enforce restrictions. For example, verifying that a
GSO_TCPV4gso_typematches packet contents:
unencapsulated TCP/IPV4 packet with payload exceedinggso_sizeandhdr_lenat payload offset.Introduce a new sysctl net.core.flow_dissect_vnet_hdr controlling a static key to decide whether to perform flow dissection. When the key is false,
virtio_net_hdr_to_skbcomputes as before.[1] https://syzkaller.appspot.com/bug?id=b419a5ca95062664fe1a60b764621eb4526e2cd0
[ um build error ]Reported-by kernel test robot <[email protected]>Signed-off-by Tanner Love <[email protected]>Suggested-by Willem de Bruijn <[email protected]>--- include/linux/virtio_net.h | 25 +++++++++++++++++++++---- net/core/flow_dissector.c | 3 +++ net/core/sysctl_net_core.c | 9 +++++++++ 3 files changed, 33 insertions(+), 4 deletions(-)include/linux/virtio_net.h +3
-0@@ -25,10 +25,13 @@ static inline int virtio_net_hdr_set_proto(structsk_buff *skb, return 0; } +DECLARE_STATIC_KEY_FALSE (sysctl_flow_dissect_vnet_hdr_key); +static inline int virtio_net_hdr_to_skb (struct sk_buff *skb,const struct virtio_net_hdr *hdr,bool little_endian) { +struct flow_keys_basic keys;unsigned int gso_type =0 ;unsigned int thlen =0 ;unsigned int p_off = 0;include/linux/virtio_net.h +18
-4@@ -78,13 +81,24 @@ static inline int virtio_net_hdr_to_skb(structsk_buff *skb, p_off =skb_transport_offset (skb) + thlen;if (!pskb_may_pull (skb, p_off))return -EINVAL ; - }else { + } + +/* BPF flow dissection for optional strict validation. +* +* Admins can define permitted packets more strictly, such as dropping +* deprecated UDP_UFO packets and requiring skb->protocol to be non-zero +* and matching packet headers. +*/ +if (static_branch_unlikely (&sysctl_flow_dissect_vnet_hdr_key) && + !__skb_flow_dissect_flow_keys_basic (NULL , skb, &keys,NULL ,0 ,0 ,0 , +0 , hdr)) +return -EINVAL ; + +if (!(hdr->flags &VIRTIO_NET_HDR_F_NEEDS_CSUM )) {/* gso packets without NEEDS_CSUM do not set transport_offset. * probe and drop if does not match one of the above types. */ if (gso_type && skb->network_header) { -struct flow_keys_basic keys; -if (!skb->protocol) {__be16 protocol =dev_parse_header_protocol (skb);@@ -92,8 +106,11 @@ static inline int virtio_net_hdr_to_skb(struct sk_buff *skb, if (protocol && protocol != skb->protocol) return -EINVAL; } + retry: -if (!skb_flow_dissect_flow_keys_basic (NULL , skb, &keys, +/* only if flow dissection not already done */ +if (!static_branch_unlikely (&sysctl_flow_dissect_vnet_hdr_key) && + !skb_flow_dissect_flow_keys_basic (NULL , skb, &keys,NULL ,0 ,0 ,0 ,0 )) {/* UFO does not specify ipv4 or 6: try both */
net/core/flow_dissector.c +3
-0@@ -35,6 +35,9 @@#endif #include <linux/bpf-netns.h> +DEFINE_STATIC_KEY_FALSE (sysctl_flow_dissect_vnet_hdr_key); +EXPORT_SYMBOL (sysctl_flow_dissect_vnet_hdr_key); +static void dissector_set_key (struct flow_dissector *flow_dissector,enum flow_dissector_key_id key_id) {net/core/sysctl_net_core.c +2
-0@@ -36,6 +36,8 @@ static int net_msg_warn; /* Unused, but still a sysctl*/ int sysctl_fb_tunnels_only_for_init_net__read_mostly =0 ;EXPORT_SYMBOL (sysctl_fb_tunnels_only_for_init_net); +DECLARE_STATIC_KEY_FALSE (sysctl_flow_dissect_vnet_hdr_key); +/* 0 - Keep current behavior: * IPv4: inherit all current settings from init_net * IPv6: reset all settings to default
net/core/sysctl_net_core.c +0
-0@@ -580,6 +582,13 @@ static struct ctl_table net_core_table[] = { .extra1 = SYSCTL_ONE, .extra2 = &int_3600, },
[..]
net/core/sysctl_net_core.c static struct ctl_table net_core_table[] = { · line 586
Quoted 2 lines
net/core/sysctl_net_core.c +2
-0+ { + .procname = "flow_dissect_vnet_hdr",
This sounds generic, but iiuc, it makes sense only for bpf flow dissection? Should it be bpf_flow_dissect_vnet_hdr instead?
Or should we drop this sysctl in favor of some per-program flag (either attach_flags or some signal via btf)? That way, individual bpf programs can signal their willingness to parse vnet hdr.
Quoted 10 lines
net/core/sysctl_net_core.c +5
-1+ .data = &sysctl_flow_dissect_vnet_hdr_key.key, + .maxlen = sizeof(sysctl_flow_dissect_vnet_hdr_key), + .mode = 0644, + .proc_handler = proc_do_static_key, + }, { } }; --2.32.0.rc0.204.g9fa02ecfa5-goog
Quoted 9 lines · Tanner Love
From: Tanner Love <[email protected]>
Syzkaller bugs have resulted from loose specification of virtio_net_hdr[1]. Enable execution of a BPF flow dissector program in
virtio_net_hdr_to_skbto validate the vnet header and drop bad input.The existing behavior of accepting these vnet headers is part of the ABI.
So ?
It's ok to fix ABI when it's broken.
The whole feature is a way to workaround broken ABI with additional BPF based validation.
It's certainly a novel idea.
I've never seen BPF being used to fix the kernel bugs.
But I think the better way forward is to admit that vnet ABI is broken and fix it in the kernel with proper validation.
BPF-based validation is a band-aid. The out of the box kernel will stay broken and syzbot will continue to crash it.
Quoted 12 lines · Alexei Starovoitov
Quoted 9 lines · Tanner Love
From: Tanner Love <[email protected]>
Syzkaller bugs have resulted from loose specification of virtio_net_hdr[1]. Enable execution of a BPF flow dissector program in
virtio_net_hdr_to_skbto validate the vnet header and drop bad input.The existing behavior of accepting these vnet headers is part of the ABI.
Quoted 10 lines
So ?
It's ok to fix ABI when it's broken.
The whole feature is a way to workaround broken ABI with additional BPF based validation.
It's certainly a novel idea.
I've never seen BPF being used to fix the kernel bugs.
But I think the better way forward is to admit that vnet ABI is broken and fix it in the kernel with proper validation.
BPF-based validation is a band-aid. The out of the box kernel will stay broken and syzbot will continue to crash it.
The intention is not to use this to avoid kernel fixes.
There are three parts:
1. is the ABI broken and can we simply drop certain weird packets?
2. will we accept an extra packet parsing stage in virtio_net_hdr_to_skb to find all the culprits?
3. do we want to add yet another parser in C when this is exactly what the BPF flow dissector does well?
The virtio_net_hdr interface is more permissive than I think it was intended. But weird edge cases like protocol 0 do occur. So I believe it is simply too late to drop everything that is not obviously correct.
More problematic is that some of the gray area cases are non-trivial and require protocol parsing. Do the packet headers match the gso type? Are subtle variants like IPv6/TCP with extension headers supported or not?
We have previously tried to add unconditional protocol parsing in virtio_net_hdr_to_skb, but received pushback because this will cause performance regressions, e.g., for vm-to-vm forwarding.
Combined, that's why I think BPF flow dissection is the right approach here. It is optional, can be as pedantic as the admin wants / workload allows (e.g., dropping UFO altogether) and ensures protocol parsing itself is robust. Even if not enabled continuously, offers a quick way to patch a vulnerability when it is discovered. This is the same reasoning of the existing BPF flow dissector.
It is not intended as an alternative to subsequently fixing a bug in the kernel path.
Quoted 56 lines · Willem de Bruijn
Quoted 12 lines · Alexei Starovoitov
Quoted 9 lines · Tanner Love
From: Tanner Love <[email protected]>
Syzkaller bugs have resulted from loose specification of virtio_net_hdr[1]. Enable execution of a BPF flow dissector program in
virtio_net_hdr_to_skbto validate the vnet header and drop bad input.The existing behavior of accepting these vnet headers is part of the ABI.
Quoted 10 lines
So ?
It's ok to fix ABI when it's broken.
The whole feature is a way to workaround broken ABI with additional BPF based validation.
It's certainly a novel idea.
I've never seen BPF being used to fix the kernel bugs.
But I think the better way forward is to admit that vnet ABI is broken and fix it in the kernel with proper validation.
BPF-based validation is a band-aid. The out of the box kernel will stay broken and syzbot will continue to crash it.The intention is not to use this to avoid kernel fixes.
There are three parts:
1. is the ABI broken and can we simply drop certain weird packets?
2. will we accept an extra packet parsing stage invirtio_net_hdr_to_skbto find all the culprits?
3. do we want to add yet another parser in C when this is exactly what the BPF flow dissector does well?The
virtio_net_hdrinterface is more permissive than I think it was intended. But weird edge cases like protocol 0 do occur. So I believe it is simply too late to drop everything that is not obviously correct.More problematic is that some of the gray area cases are non-trivial and require protocol parsing. Do the packet headers match the gso type? Are subtle variants like IPv6/TCP with extension headers supported or not?
We have previously tried to add unconditional protocol parsing in
virtio_net_hdr_to_skb, but received pushback because this will cause performance regressions, e.g., for vm-to-vm forwarding.Combined, that's why I think BPF flow dissection is the right approach here. It is optional, can be as pedantic as the admin wants / workload allows (e.g., dropping UFO altogether) and ensures protocol parsing itself is robust. Even if not enabled continuously, offers a quick way to patch a vulnerability when it is discovered. This is the same reasoning of the existing BPF flow dissector.
Quoted 2 lines
It is not intended as an alternative to subsequently fixing a bug in the kernel path.
Thanks for these details. They need to be part of commit log.
As far as patches the overhead of copying extra fields can be avoided. How about copying single pointer to struct virtio_net_hdr into bpf_flow_keys instead? See struct bpf_sockopt and _bpf_md_ptr(struct bpf_sock *, sk).
The overhead will much lower and bpf prog will access all virtio_net_hdr directly.
Quoted 11 lines · Tanner Love
From: Tanner Love <[email protected]>
First patch extends the flow dissector BPF program type to accept virtio-net header members.
Second patch uses this feature to add optional flow dissection in
virtio_net_hdr_to_skb(). This allows admins to define permitted packets more strictly, for example dropping deprecatedUDP_UFOpackets.Third patch extends kselftest to cover this feature.
I wonder why virtio maintainers is not copied in this series.
Several questions:
1) having bpf core to know about virito-net header seems like a layer violation, it doesn't scale as we may add new fields, actually there's already fields that is not implemented in the spec but not Linux right now.
2) virtio_net_hdr_to_skb() is not the single entry point, packet could go via XDP
3) I wonder whether we can simply use XDP to solve this issue (metadata probably but I don't have a deep thought)
4) If I understand the code correctly, it should deal with all dodgy packets instead of just for virtio
Thanks
Quoted 20 lines
Tanner Love (3):
net:flow_dissector: extend bpf flow dissector support with vnet hdr
virtio_net: add optional flow dissection invirtio_net_hdr_to_skbselftests/net: amend bpf flow dissector prog to do vnet hdr validationdrivers/net/bonding/bond_main.c | 2 +- include/linux/skbuff.h | 26 ++- include/linux/virtio_net.h | 25 ++- include/net/flow_dissector.h | 6 + include/uapi/linux/bpf.h | 6 + net/core/filter.c | 55 +++++ net/core/flow_dissector.c | 27 ++- net/core/sysctl_net_core.c | 9 + tools/include/uapi/linux/bpf.h | 6 + tools/testing/selftests/bpf/progs/bpf_flow.c | 188 +++++++++++++----- .../selftests/bpf/test_flow_dissector.c | 181 +++++++++++++++-- .../selftests/bpf/test_flow_dissector.sh | 19 ++ 12 files changed, 470 insertions(+), 80 deletions(-)
Quoted 13 lines · Jason Wang
Quoted 10 lines · Tanner Love
From: Tanner Love <[email protected]>
First patch extends the flow dissector BPF program type to accept virtio-net header members.
Second patch uses this feature to add optional flow dissection in
virtio_net_hdr_to_skb(). This allows admins to define permitted packets more strictly, for example dropping deprecatedUDP_UFOpackets.
Quoted 4 lines
Quoted 1 line
Third patch extends kselftest to cover this feature.
I wonder why virtio maintainers is not copied in this series.
Sorry, an oversight.
Quoted 5 lines
Several questions:
1) having bpf core to know about virito-net header seems like a layer violation, it doesn't scale as we may add new fields, actually there's already fields that is not implemented in the spec but not Linux right now.
struct virtio_net_hdr is used by multiple interfaces, not just virtio. The interface as is will remain, regardless of additional extensions.
If the interface is extended, the validation can be extended with it.
Just curious: can you share what extra fields may be in the pipeline? The struct has historically not seen (m)any changes.
Quoted 2 lines
2)
virtio_net_hdr_to_skb()is not the single entry point, packet could go via XDP
Do you mean AF_XDP? As far as I know, vnet_hdr is the only injection interface for complex packets that include offload instructions (GSO, csum) -- which are the ones mostly implicated in bug reports.
Quoted 4 lines
3) I wonder whether we can simply use XDP to solve this issue (metadata probably but I don't have a deep thought)
4) If I understand the code correctly, it should deal with all dodgy packets instead of just for virtio
Yes. Some callers of virtio_net_hdr_to_skb, such as tun_get_user and virtio receive_buf, pass all packets to it. Others, like tap_get_user and packet_snd, only call it if a virtio_net_hdr is passed. Once we have a validation hook, ideally all packets need to pass it. Modifying callers like tap_get_user can be a simple follow-on.
Quoted 13 lines · Willem de Bruijn
Quoted 12 lines · Jason Wang
Quoted 10 lines · Tanner Love
From: Tanner Love <[email protected]>
First patch extends the flow dissector BPF program type to accept virtio-net header members.
Second patch uses this feature to add optional flow dissection in
virtio_net_hdr_to_skb(). This allows admins to define permitted packets more strictly, for example dropping deprecatedUDP_UFOpackets.
Quoted 4 lines
Quoted 3 lines
Quoted 1 line
Third patch extends kselftest to cover this feature.
I wonder why virtio maintainers is not copied in this series.
Sorry, an oversight.
No problem.
Quoted 10 lines
Quoted 6 lines
Several questions:
1) having bpf core to know about virito-net header seems like a layer violation, it doesn't scale as we may add new fields, actually there's already fields that is not implemented in the spec but not Linux right now.
struct
virtio_net_hdris used by multiple interfaces, not just virtio. The interface as is will remain, regardless of additional extensions.If the interface is extended, the validation can be extended with it.
One possible problem is that there's no sufficient context.
The vnet header length is not a fixed value but depends on the feature negotiation. The num_buffers (not implemented in this series) is an example. The field doesn't not exist for legacy device if mergeable buffer is disabled. If we decide to go with this way, we probably need to fix this by introducing a vnet header length.
And I'm not sure it can work for all the future cases e.g the semantic of a field may vary depends on the feature negotiated, but maybe it's safe since it needs to set the flags.
Another note is that the spec doesn't exclude the possibility to have a complete new vnet header format in the future. And the bpf program is unaware of any virtio features.
Quoted 3 lines
Just curious: can you share what extra fields may be in the pipeline? The struct has historically not seen (m)any changes.
For extra fields, I vaguely remember we had some discussions on the possible method to extend that, but I forget the actual features.
But spec support RSC which may reuse csum_start/offset but it looks to me RSC is not something like Linux need.
Quoted 4 lines
Quoted 3 lines
2)
virtio_net_hdr_to_skb()is not the single entry point, packet could go via XDPDo you mean
AF_XDP?
Yes and kernel XDP as well. If the packet is redirected or transmitted, it won't even go to virtio_net_hdr_to_skb().
Since there's no GSO/csum support for XDP, it's probably ok, but needs to consider this for the future consider the multi-buffer XDP is being developed right now, we can release those restriction.
Quoted 3 lines
As far as I know,
vnet_hdris the only injection
interface for complex packets that include offload instructions (GSO, csum) -- which are the ones mostly implicated in bug reports.
Ideally, if GSO/csum is supported by XDP, it would be more simple to use XDP I think.
Quoted 10 lines
Quoted 5 lines
3) I wonder whether we can simply use XDP to solve this issue (metadata probably but I don't have a deep thought)
4) If I understand the code correctly, it should deal with all dodgy packets instead of just for virtioYes. Some callers of
virtio_net_hdr_to_skb, such astun_get_userand virtioreceive_buf, pass all packets to it. Others, liketap_get_userandpacket_snd, only call it if avirtio_net_hdris passed. Once we have a validation hook, ideally all packets need to pass it. Modifying callers liketap_get_usercan be a simple follow-on.
Ok.
Thanks
Quoted 1 line
Quoted 19 lines
Quoted 9 lines
Quoted 5 lines
Several questions:
1) having bpf core to know about virito-net header seems like a layer violation, it doesn't scale as we may add new fields, actually there's already fields that is not implemented in the spec but not Linux right now.
struct
virtio_net_hdris used by multiple interfaces, not just virtio. The interface as is will remain, regardless of additional extensions.If the interface is extended, the validation can be extended with it.
One possible problem is that there's no sufficient context.
The vnet header length is not a fixed value but depends on the feature negotiation. The
num_buffers(not implemented in this series) is an example. The field doesn't not exist for legacy device if mergeable buffer is disabled. If we decide to go with this way, we probably need to fix this by introducing a vnet header length.
Quoted 7 lines
And I'm not sure it can work for all the future cases e.g the semantic of a field may vary depends on the feature negotiated, but maybe it's safe since it needs to set the flags.
Another note is that the spec doesn't exclude the possibility to have a complete new vnet header format in the future. And the bpf program is unaware of any virtio features.
We can extend the program with a version or type field, if multiple variants appear. The callers can set this.
Thanks for the examples. As a matter of fact, I do know that kind of extension. I proposed new fields myself this winter, to for timestamps, pacing offload and hash info on tx:
https://lore.kernel.org/netdev/[email protected]/T/#mcbd4dff966a93d61a31844c9d968e7cd4ee7f0ab
Like num_buffers, those are new fields appended to the struct.
Agreed that if the semantics of the existing fields would change or a whole new v2 type would be defined (with much stricter semantics that time around, and validation from the start), then a type field in the flow dissector will be needed.
That is feasible and won't have to break the BPF interface.
Quoted 13 lines
Quoted 3 lines
Just curious: can you share what extra fields may be in the pipeline? The struct has historically not seen (m)any changes.
For extra fields, I vaguely remember we had some discussions on the possible method to extend that, but I forget the actual features.
But spec support RSC which may reuse csum_start/offset but it looks to me RSC is not something like Linux need.
Quoted 1 line
Quoted 7 lines
Quoted 3 lines
Quoted 2 lines
2)
virtio_net_hdr_to_skb()is not the single entry point, packet could go via XDPDo you mean
AF_XDP?Yes and kernel XDP as well. If the packet is redirected or transmitted, it won't even go to
virtio_net_hdr_to_skb().
Redirected packets are already in the kernel.
This is strictly a chokepoint for new packets injected from userspace.
Quoted 3 lines
Since there's no GSO/csum support for XDP, it's probably ok, but needs to consider this for the future consider the multi-buffer XDP is being developed right now, we can release those restriction.
Yes, we have to make sure not to introduce the same issues with any XDP GSO extensions, if it comes to that.
Quoted 7 lines
Quoted 3 lines
As far as I know,
vnet_hdris the only injection
interface for complex packets that include offload instructions (GSO, csum) -- which are the ones mostly implicated in bug reports.Ideally, if GSO/csum is supported by XDP, it would be more simple to use XDP I think.
That might actually reduce the odds of seeing new virtio_net_hdr extensions?
That legacy interface is here to stay, though, so we have to continue to be prepared to handle any input that comes that way.