more virtio-net csum hardening
Subsystems (11)
Michael S. Tsirkin
maintainerv3 cover more virtio-net csum hardening
As reported first by Paulos Yibelo <[email protected]> (after which others, credited below, found more ways the bug can affect users):
allowing untrusted hardware to specify csum start/offset for CHECKSUM_PARTIAL was not a good idea, because, if it edits the packet outside the actual checksum, and depending on the setup, the networking stack could be looking at the packet both before and after the edit, and seeing different results.
Previously, commit 6785011f8b16 ("ipv4: validate checksum_start before completing checksum") validated all known instances where this causes the most grief, but the real fix is to make sure that either only actual checksum fields are ever edited,
or the checksum is completed before the net stack looks at it, so it is always consistent.
This basically goes back to
what we did before f43798c27684 ("tun: Allow GSO using virtio_net_hdr"), except in a narrow list of specified known good cases.
Summary of the patchset:
These are definitely net material (and seem to be too big for stable, or I would recommend backporting there, too):
net: account for ingress-resolved virtio net hdr state
- accounting, not the fix itself, dependency of the next one
net: resolve uncommon partial checksums at ingress
- fix itself
net: add a sysctl to opt out of virtio net hdr checksum validation
- reducing risk of the fix by adding a sysctl to disable it
These are cleanups, can be deferred to net-next, too:
net: add ip4_parse()
virtio_net: cleanup protocol calculation
- cleanup resulting code duplication
(kept separate to simplify backporting)
Changes since v1:
- reorder patches to fix bisectability, addressing a comment by Björn Töpel.
- code refactoring, different patch splitup,
a sysctl knob to disable as suggested by Willem de Bruijn.
- fix a bunch of minor issues spotted by LLMs
Changes since v2:
virtio_net: parse tunnel csum_start little-endian
- new patch. correctly parsing the header is a dependency of validating it, otherwise i do not know what to validate
net: account for ingress-resolved virtio net hdr state
- make rx_csum_sw counter 64 bit so it won't wrap (spotted by LLM review)
net: resolve uncommon partial checksums at ingress
- tunnel fixes to bugs noticed by Willem de Bruijn and Chris Arges
- restore the csum_offset checks as suggested by Willem
- parse csum_start in the tunnel header little-endian like the rest of it (spotted by LLM review)
net: add a sysctl to opt out of virtio net hdr checksum validation
- the knob is no longer writable by root inside a user
namespace: changing it now requires capable(CAP_NET_ADMIN) suggested by Willem de Bruijn
net: add ip4_parse()
- comment converted to kernel-doc format (Willem de Bruijn)
um: vector: honor virtio net hdr conversion failures
- commit log tweaks
New patch:
um: vector: trim skbs before verifying the header
found by code review
Somewhat tested, tunnel also tested now, um part compiled only
Michael S. Tsirkin (8):
net: account for ingress-resolved virtio net hdr state
um: vector: honor virtio net hdr conversion failures
um: vector: trim skbs before verifying the header
virtio_net: parse tunnel csum_start little-endian
net: resolve uncommon partial checksums at ingress
net: add a sysctl to opt out of virtio net hdr checksum validation
net: add ip4_parse()
virtio_net: cleanup protocol calculation
Documentation/admin-guide/sysctl/net.rst | 19 ++ arch/um/drivers/vector_kern.c | 6 +- arch/um/drivers/vector_transports.c | 4 +- drivers/net/tap.c | 4 +- drivers/net/tun.c | 41 +++- drivers/net/virtio_net.c | 2 +- include/linux/ip.h | 27 +++ include/linux/virtio_net.h | 272 ++++++++++++++++++++++- include/net/netns/core.h | 1 + net/bridge/br_netfilter_hooks.c | 15 +- net/core/dev.c | 6 + net/core/net_namespace.c | 1 + net/core/sysctl_net_core.c | 60 +++++ net/ipv4/ip_input.c | 13 +- net/ipv4/netfilter/nf_reject_ipv4.c | 13 +- net/packet/af_packet.c | 4 +- 16 files changed, 445 insertions(+), 43 deletions(-)
Signature · 1 line
MST
Michael S. Tsirkin
maintainerPATCH v3 1/8 net: account for ingress-resolved virtio net hdr state
The following patch will add ingress resolution of uncommon virtio net hdr checksum metadata (software checksum completion at ingress) which adds performance overhead to the packet processing.
We should make it visible, so a hypothetical workload relying on uncommon metadata can be distinguished from none at all. This way issues in the parsing will be reported and not quietly pay the host-CPU checksum overhead instead of offload.
Flip every call to virtio_net_hdr_to_skb to test for negative errno only, and have tun count these events per device as rx_csum_sw, exposed via ethtool -S.
--- drivers/net/tap.c | 4 ++-- drivers/net/tun.c | 41 ++++++++++++++++++++++++++++++++++++++-- drivers/net/virtio_net.c | 2 +- net/packet/af_packet.c | 4 ++-- 4 files changed, 44 insertions(+), 7 deletions(-)
drivers/net/tap.c +2 -2
@@ -649,7 +649,7 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,if (vnet_hdr_len) { err =tun_vnet_hdr_to_skb (q->flags, skb, &vnet_hdr); -if (err) { +if (err <0 ) {rcu_read_unlock (); drop_reason =SKB_DROP_REASON_DEV_HDR ; goto err_kfree;
@@ -1084,7 +1084,7 @@ static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp)if (vnet_hdr_len) { err =tun_vnet_hdr_to_skb (q->flags, skb, gso); -if (err) { +if (err <0 ) {rcu_read_unlock (); goto err_kfree; }
drivers/net/tun.c +39 -2
75 lines not on this page · raw message
drivers/net/virtio_net.c +1 -1
@@ -2519,7 +2519,7 @@ static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *skb_set_network_header (skb,ETH_HLEN );if (virtio_net_hdr_tnl_to_skb (skb, &hdr->tnl_hdr, vi->rx_tnl, vi->rx_tnl_csum, -virtio_is_little_endian (vi->vdev))) { +virtio_is_little_endian (vi->vdev)) <0 ) { net_warn_ratelimited("%s: bad gso: type: %x, size: %u, flags %x tunnel %d tnl csum %d\n", dev->name, hdr->hdr.gso_type, hdr->hdr.gso_size, hdr->hdr.flags,
net/packet/af_packet.c +2 -2
@@ -2913,7 +2913,7 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) }if (has_vnet_hdr) { -if (virtio_net_hdr_to_skb (skb, &vnet_hdr,vio_le ())) { +if (virtio_net_hdr_to_skb (skb, &vnet_hdr,vio_le ()) <0 ) { tp_len = -EINVAL ; goto tpacket_error; }
@@ -3136,7 +3136,7 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)if (vnet_hdr_sz) { err =virtio_net_hdr_to_skb (skb, &vnet_hdr,vio_le ()); -if (err) +if (err <0 ) goto out_free; len += vnet_hdr_sz; }
Signature · 1 line
MST
[email protected]
AI reviewRe: [PATCH net v3 1/8] net: account for ingress-resolved virtio net hdr state
Sashiko has reviewed this patch and found no issues.
Quoted 9 lines
The following patch will add ingress resolution of uncommon virtio net hdr checksum metadata (software checksum completion at ingress) which adds performance overhead to the packet processing.
We should make it visible, so a hypothetical workload relying on uncommon metadata can be distinguished from none at all. This way issues in the parsing will be reported and not quietly pay the host-CPU checksum overhead instead of offload.
Quoted 6 lines
Flip every call to
virtio_net_hdr_to_skbto test for negative errno only, and have tun count these events per device asrx_csum_sw, exposed via ethtool -S.Assisted-by LLMSigned-off-by Michael S. Tsirkin <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Willem de Bruijn
maintainerRe: [PATCH net v3 1/8] net: account for ingress-resolved virtio net hdr state
In reply to Michael S. TsirkinQuoted 36 lines · Michael S. Tsirkin
The following patch will add ingress resolution of uncommon virtio net hdr checksum metadata (software checksum completion at ingress) which adds performance overhead to the packet processing.
We should make it visible, so a hypothetical workload relying on uncommon metadata can be distinguished from none at all. This way issues in the parsing will be reported and not quietly pay the host-CPU checksum overhead instead of offload.
Flip every call to
virtio_net_hdr_to_skbto test for negative errno only, and have tun count these events per device asrx_csum_sw, exposed via ethtool -S.Assisted-by LLMSigned-off-by Michael S. Tsirkin <[email protected]>--- drivers/net/tap.c | 4 ++-- drivers/net/tun.c | 41 ++++++++++++++++++++++++++++++++++++++-- drivers/net/virtio_net.c | 2 +- net/packet/af_packet.c | 4 ++-- 4 files changed, 44 insertions(+), 7 deletions(-)drivers/net/tap.c +1
-1@@ -649,7 +649,7 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,if (vnet_hdr_len) { err =tun_vnet_hdr_to_skb (q->flags, skb, &vnet_hdr); -if (err) { +if (err <0 ) {rcu_read_unlock (); drop_reason =SKB_DROP_REASON_DEV_HDR ; goto err_kfree;
drivers/net/tap.c static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp) · line 1090
Quoted 8 lines
drivers/net/tap.c +1
-1@@ -1084,7 +1084,7 @@ static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp)if (vnet_hdr_len) { err =tun_vnet_hdr_to_skb (q->flags, skb, gso); -if (err) { +if (err <0 ) {rcu_read_unlock (); goto err_kfree; }
similar to tun, should tap count this event?
drivers/net/tun.c struct tun_struct { · line 213
Quoted 9 lines
drivers/net/tun.c +1
-0@@ -210,6 +210,7 @@ struct tun_struct {u32 flow_count;u32 rx_batched;atomic_long_t rx_frame_errors; +atomic64_t rx_csum_sw;
is a normal (and cheaper on 32b platforms) atomic_t or atomic_long_t sufficient?
Quoted 5 lines
drivers/net/tun.c +0
-0struct bpf_prog __rcu *xdp_prog;struct tun_prog __rcu *steering_prog;struct tun_prog __rcu *filter_prog;@@ -1935,11 +1936,16 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,break ;
drivers/net/tun.c static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, · line 1945
Quoted 10 lines
drivers/net/tun.c +3
-1} -if (tun_vnet_hdr_tnl_to_skb (tun->flags, features, skb, &hdr)) { + err =tun_vnet_hdr_tnl_to_skb (tun->flags, features, skb, &hdr); +if (err <0 ) {atomic_long_inc (&tun->rx_frame_errors); err = -EINVAL ; goto free_skb; } +if (err >0 ) {
} else if (err > 0) {
Quoted 9 lines
drivers/net/tun.c +3
-0+atomic64_inc (&tun->rx_csum_sw); + err =0 ; + }if ((tun->flags &TUN_TYPE_MASK ) ==IFF_TAP ) skb->protocol =eth_type_trans (skb, tun->dev);@@ -2605,12 +2611,15 @@ static int tun_xdp_one(struct tun_struct *tun, tnl_hdr = (struct virtio_net_hdr_v1_hash_tunnel *)gso; skb->dev = tun->dev;
drivers/net/tun.c static int tun_xdp_one(struct tun_struct *tun, · line 2621
Quoted 10 lines
drivers/net/tun.c +3
-1skb_set_network_header (skb,ETH_HLEN ); -if (tun_vnet_hdr_tnl_to_skb (tun->flags, features, skb, tnl_hdr)) { + ret =tun_vnet_hdr_tnl_to_skb (tun->flags, features, skb, tnl_hdr); +if (ret <0 ) {atomic_long_inc (&tun->rx_frame_errors);kfree_skb (skb); ret = -EINVAL ; goto out; } +if (ret >0 )
same
drivers/net/tun.c static int tun_xdp_one(struct tun_struct *tun, · line 2622
Quoted 1 line
drivers/net/tun.c +1
-0+atomic64_inc (&tun->rx_csum_sw);
maybe reset ret = 0 in all these branches
Quoted 44 lines
drivers/net/tun.c +28
-0skb->protocol =eth_type_trans (skb, tun->dev);skb_reset_network_header (skb);@@ -3739,7 +3748,35 @@ static void tun_get_channels(struct net_device *dev, channels->max_combined = tun->flags &IFF_MULTI_QUEUE ?MAX_TAP_QUEUES :1 ; } +#define TUN_STATS_LEN 1 +static const char tun_ethtool_strings[TUN_STATS_LEN ][ETH_GSTRING_LEN ] = { +"rx_csum_sw" , +}; + +static int tun_get_sset_count (struct net_device *dev,int sset) +{ +return sset ==ETH_SS_STATS ?TUN_STATS_LEN : -EOPNOTSUPP ; +} + +static void tun_get_strings (struct net_device *dev,u32 stringset,u8 *data) +{ +if (stringset ==ETH_SS_STATS ) +memcpy (data, tun_ethtool_strings, +sizeof (tun_ethtool_strings)); +} + +static void tun_get_ethtool_stats (struct net_device *dev, +struct ethtool_stats *stats,u64 *data) +{ +struct tun_struct *tun =netdev_priv (dev); + + data[0 ] =atomic64_read (&tun->rx_csum_sw); +} +static const struct ethtool_ops tun_ethtool_ops = { + .get_sset_count = tun_get_sset_count, + .get_strings = tun_get_strings, + .get_ethtool_stats = tun_get_ethtool_stats, .supported_coalesce_params =ETHTOOL_COALESCE_RX_MAX_FRAMES , .get_drvinfo = tun_get_drvinfo, .get_msglevel = tun_get_msglevel,drivers/net/virtio_net.c +0
-0
drivers/net/virtio_net.c static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue * · line 2525
Quoted 8 lines
drivers/net/virtio_net.c +1
-1@@ -2519,7 +2519,7 @@ static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *skb_set_network_header (skb,ETH_HLEN );if (virtio_net_hdr_tnl_to_skb (skb, &hdr->tnl_hdr, vi->rx_tnl, vi->rx_tnl_csum, -virtio_is_little_endian (vi->vdev))) { +virtio_is_little_endian (vi->vdev)) <0 ) { net_warn_ratelimited("%s: bad gso: type: %x, size: %u, flags %x tunnel %d tnl csum %d\n", dev->name, hdr->hdr.gso_type, hdr->hdr.gso_size, hdr->hdr.flags,
should virtio_net count these too? Or do we assume that devices are more trustworthy, so unlikely to cause this issue?
Quoted 25 lines
net/packet/af_packet.c +2
-2@@ -2913,7 +2913,7 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) }if (has_vnet_hdr) { -if (virtio_net_hdr_to_skb (skb, &vnet_hdr,vio_le ())) { +if (virtio_net_hdr_to_skb (skb, &vnet_hdr,vio_le ()) <0 ) { tp_len = -EINVAL ; goto tpacket_error; }@@ -3136,7 +3136,7 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)if (vnet_hdr_sz) { err =virtio_net_hdr_to_skb (skb, &vnet_hdr,vio_le ()); -if (err) +if (err <0 ) goto out_free; len += vnet_hdr_sz; }Signature · 1 line
MST
Michael S. Tsirkin
maintainerRe: [PATCH net v3 1/8] net: account for ingress-resolved virtio net hdr state
Quoted 48 lines · Willem de Bruijn
Quoted 36 lines · Michael S. Tsirkin
The following patch will add ingress resolution of uncommon virtio net hdr checksum metadata (software checksum completion at ingress) which adds performance overhead to the packet processing.
We should make it visible, so a hypothetical workload relying on uncommon metadata can be distinguished from none at all. This way issues in the parsing will be reported and not quietly pay the host-CPU checksum overhead instead of offload.
Flip every call to
virtio_net_hdr_to_skbto test for negative errno only, and have tun count these events per device asrx_csum_sw, exposed via ethtool -S.Assisted-by LLMSigned-off-by Michael S. Tsirkin <[email protected]>--- drivers/net/tap.c | 4 ++-- drivers/net/tun.c | 41 ++++++++++++++++++++++++++++++++++++++-- drivers/net/virtio_net.c | 2 +- net/packet/af_packet.c | 4 ++-- 4 files changed, 44 insertions(+), 7 deletions(-)drivers/net/tap.c +1
-1@@ -649,7 +649,7 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,if (vnet_hdr_len) { err =tun_vnet_hdr_to_skb (q->flags, skb, &vnet_hdr); -if (err) { +if (err <0 ) {rcu_read_unlock (); drop_reason =SKB_DROP_REASON_DEV_HDR ; goto err_kfree;drivers/net/tap.c static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp) · line 1090
Quoted 8 lines
drivers/net/tap.c +1
-1@@ -1084,7 +1084,7 @@ static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp)if (vnet_hdr_len) { err =tun_vnet_hdr_to_skb (q->flags, skb, gso); -if (err) { +if (err <0 ) {rcu_read_unlock (); goto err_kfree; }similar to tun, should tap count this event?
Quoted 11 lines
drivers/net/tun.c struct tun_struct { · line 213
Quoted 9 lines
drivers/net/tun.c +1
-0@@ -210,6 +210,7 @@ struct tun_struct {u32 flow_count;u32 rx_batched;atomic_long_t rx_frame_errors; +atomic64_t rx_csum_sw;is a normal (and cheaper on 32b platforms)
atomic_toratomic_long_tsufficient?
well it can overflow easily. i don't know that we care that a slow path will be even a bit slower on these ancient platforms. correct reporting seems more important.
Quoted 91 lines
Quoted 5 lines
drivers/net/tun.c +0
-0struct bpf_prog __rcu *xdp_prog;struct tun_prog __rcu *steering_prog;struct tun_prog __rcu *filter_prog;@@ -1935,11 +1936,16 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,break ;drivers/net/tun.c static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, · line 1945
Quoted 10 lines
drivers/net/tun.c +3
-1} -if (tun_vnet_hdr_tnl_to_skb (tun->flags, features, skb, &hdr)) { + err =tun_vnet_hdr_tnl_to_skb (tun->flags, features, skb, &hdr); +if (err <0 ) {atomic_long_inc (&tun->rx_frame_errors); err = -EINVAL ; goto free_skb; } +if (err >0 ) {} else if (err > 0) {
Quoted 9 lines
drivers/net/tun.c +3
-0+atomic64_inc (&tun->rx_csum_sw); + err =0 ; + }if ((tun->flags &TUN_TYPE_MASK ) ==IFF_TAP ) skb->protocol =eth_type_trans (skb, tun->dev);@@ -2605,12 +2611,15 @@ static int tun_xdp_one(struct tun_struct *tun, tnl_hdr = (struct virtio_net_hdr_v1_hash_tunnel *)gso; skb->dev = tun->dev;drivers/net/tun.c static int tun_xdp_one(struct tun_struct *tun, · line 2621
Quoted 10 lines
drivers/net/tun.c +3
-1skb_set_network_header (skb,ETH_HLEN ); -if (tun_vnet_hdr_tnl_to_skb (tun->flags, features, skb, tnl_hdr)) { + ret =tun_vnet_hdr_tnl_to_skb (tun->flags, features, skb, tnl_hdr); +if (ret <0 ) {atomic_long_inc (&tun->rx_frame_errors);kfree_skb (skb); ret = -EINVAL ; goto out; } +if (ret >0 )same
drivers/net/tun.c static int tun_xdp_one(struct tun_struct *tun, · line 2622
Quoted 1 line
drivers/net/tun.c +1
-0+atomic64_inc (&tun->rx_csum_sw);maybe reset ret = 0 in all these branches
Quoted 47 lines
drivers/net/tun.c +28
-0skb->protocol =eth_type_trans (skb, tun->dev);skb_reset_network_header (skb);@@ -3739,7 +3748,35 @@ static void tun_get_channels(struct net_device *dev, channels->max_combined = tun->flags &IFF_MULTI_QUEUE ?MAX_TAP_QUEUES :1 ; } +#define TUN_STATS_LEN 1 +static const char tun_ethtool_strings[TUN_STATS_LEN ][ETH_GSTRING_LEN ] = { +"rx_csum_sw" , +}; + +static int tun_get_sset_count (struct net_device *dev,int sset) +{ +return sset ==ETH_SS_STATS ?TUN_STATS_LEN : -EOPNOTSUPP ; +} + +static void tun_get_strings (struct net_device *dev,u32 stringset,u8 *data) +{ +if (stringset ==ETH_SS_STATS ) +memcpy (data, tun_ethtool_strings, +sizeof (tun_ethtool_strings)); +} + +static void tun_get_ethtool_stats (struct net_device *dev, +struct ethtool_stats *stats,u64 *data) +{ +struct tun_struct *tun =netdev_priv (dev); + + data[0 ] =atomic64_read (&tun->rx_csum_sw); +} +static const struct ethtool_ops tun_ethtool_ops = { + .get_sset_count = tun_get_sset_count, + .get_strings = tun_get_strings, + .get_ethtool_stats = tun_get_ethtool_stats, .supported_coalesce_params =ETHTOOL_COALESCE_RX_MAX_FRAMES , .get_drvinfo = tun_get_drvinfo, .get_msglevel = tun_get_msglevel,drivers/net/virtio_net.c +0
-0@@ -2519,7 +2519,7 @@ static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *skb_set_network_header (skb,ETH_HLEN ); if (virtio_net_hdr_tnl_to_skb(skb, &hdr->tnl_hdr, vi->rx_tnl, vi->rx_tnl_csum,
Quoted 8 lines
drivers/net/virtio_net.c static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue * · line 2525
Quoted 5 lines
drivers/net/virtio_net.c +1
-1- virtio_is_little_endian(vi->vdev))) { + virtio_is_little_endian(vi->vdev)) < 0) { net_warn_ratelimited("%s: bad gso: type: %x, size: %u, flags %x tunnel %d tnl csum %d\n", dev->name, hdr->hdr.gso_type, hdr->hdr.gso_size, hdr->hdr.flags,should
virtio_netcount these too? Or do we assume that devices are more trustworthy, so unlikely to cause this issue?
With memory encryption, from an untrusted host?
i can add that.
Quoted 26 lines
Quoted 25 lines
net/packet/af_packet.c +2
-2@@ -2913,7 +2913,7 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) }if (has_vnet_hdr) { -if (virtio_net_hdr_to_skb (skb, &vnet_hdr,vio_le ())) { +if (virtio_net_hdr_to_skb (skb, &vnet_hdr,vio_le ()) <0 ) { tp_len = -EINVAL ; goto tpacket_error; }@@ -3136,7 +3136,7 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)if (vnet_hdr_sz) { err =virtio_net_hdr_to_skb (skb, &vnet_hdr,vio_le ()); -if (err) +if (err <0 ) goto out_free; len += vnet_hdr_sz; }Signature · 1 line
MST
Willem de Bruijn
maintainerRe: [PATCH net v3 1/8] net: account for ingress-resolved virtio net hdr state
Quoted 61 lines · Michael S. Tsirkin
Quoted 48 lines · Willem de Bruijn
Quoted 36 lines · Michael S. Tsirkin
The following patch will add ingress resolution of uncommon virtio net hdr checksum metadata (software checksum completion at ingress) which adds performance overhead to the packet processing.
We should make it visible, so a hypothetical workload relying on uncommon metadata can be distinguished from none at all. This way issues in the parsing will be reported and not quietly pay the host-CPU checksum overhead instead of offload.
Flip every call to
virtio_net_hdr_to_skbto test for negative errno only, and have tun count these events per device asrx_csum_sw, exposed via ethtool -S.Assisted-by LLMSigned-off-by Michael S. Tsirkin <[email protected]>--- drivers/net/tap.c | 4 ++-- drivers/net/tun.c | 41 ++++++++++++++++++++++++++++++++++++++-- drivers/net/virtio_net.c | 2 +- net/packet/af_packet.c | 4 ++-- 4 files changed, 44 insertions(+), 7 deletions(-)drivers/net/tap.c +1
-1@@ -649,7 +649,7 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,if (vnet_hdr_len) { err =tun_vnet_hdr_to_skb (q->flags, skb, &vnet_hdr); -if (err) { +if (err <0 ) {rcu_read_unlock (); drop_reason =SKB_DROP_REASON_DEV_HDR ; goto err_kfree;drivers/net/tap.c static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp) · line 1090
Quoted 8 lines
drivers/net/tap.c +1
-1@@ -1084,7 +1084,7 @@ static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp)if (vnet_hdr_len) { err =tun_vnet_hdr_to_skb (q->flags, skb, gso); -if (err) { +if (err <0 ) {rcu_read_unlock (); goto err_kfree; }similar to tun, should tap count this event?
Quoted 11 lines
drivers/net/tun.c struct tun_struct { · line 213
Quoted 9 lines
drivers/net/tun.c +1
-0@@ -210,6 +210,7 @@ struct tun_struct {u32 flow_count;u32 rx_batched;atomic_long_t rx_frame_errors; +atomic64_t rx_csum_sw;is a normal (and cheaper on 32b platforms)
atomic_toratomic_long_tsufficient?
Quoted 3 lines
well it can overflow easily. i don't know that we care that a slow path will be even a bit slower on these ancient platforms. correct reporting seems more important.
Ok. I would hope that monitoring catches this before it hits 4B, but agreed that as slow path it doesn't matter.
Quoted 101 lines
Quoted 92 lines
Quoted 6 lines
drivers/net/tun.c +0
-0struct bpf_prog __rcu *xdp_prog;struct tun_prog __rcu *steering_prog;struct tun_prog __rcu *filter_prog;@@ -1935,11 +1936,16 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,break ;drivers/net/tun.c static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, · line 1945
Quoted 10 lines
drivers/net/tun.c +3
-1} -if (tun_vnet_hdr_tnl_to_skb (tun->flags, features, skb, &hdr)) { + err =tun_vnet_hdr_tnl_to_skb (tun->flags, features, skb, &hdr); +if (err <0 ) {atomic_long_inc (&tun->rx_frame_errors); err = -EINVAL ; goto free_skb; } +if (err >0 ) {} else if (err > 0) {
Quoted 9 lines
drivers/net/tun.c +3
-0+atomic64_inc (&tun->rx_csum_sw); + err =0 ; + }if ((tun->flags &TUN_TYPE_MASK ) ==IFF_TAP ) skb->protocol =eth_type_trans (skb, tun->dev);@@ -2605,12 +2611,15 @@ static int tun_xdp_one(struct tun_struct *tun, tnl_hdr = (struct virtio_net_hdr_v1_hash_tunnel *)gso; skb->dev = tun->dev;drivers/net/tun.c static int tun_xdp_one(struct tun_struct *tun, · line 2621
Quoted 10 lines
drivers/net/tun.c +3
-1skb_set_network_header (skb,ETH_HLEN ); -if (tun_vnet_hdr_tnl_to_skb (tun->flags, features, skb, tnl_hdr)) { + ret =tun_vnet_hdr_tnl_to_skb (tun->flags, features, skb, tnl_hdr); +if (ret <0 ) {atomic_long_inc (&tun->rx_frame_errors);kfree_skb (skb); ret = -EINVAL ; goto out; } +if (ret >0 )same
drivers/net/tun.c static int tun_xdp_one(struct tun_struct *tun, · line 2622
Quoted 1 line
drivers/net/tun.c +1
-0+atomic64_inc (&tun->rx_csum_sw);maybe reset ret = 0 in all these branches
Quoted 47 lines
drivers/net/tun.c +28
-0skb->protocol =eth_type_trans (skb, tun->dev);skb_reset_network_header (skb);@@ -3739,7 +3748,35 @@ static void tun_get_channels(struct net_device *dev, channels->max_combined = tun->flags &IFF_MULTI_QUEUE ?MAX_TAP_QUEUES :1 ; } +#define TUN_STATS_LEN 1 +static const char tun_ethtool_strings[TUN_STATS_LEN ][ETH_GSTRING_LEN ] = { +"rx_csum_sw" , +}; + +static int tun_get_sset_count (struct net_device *dev,int sset) +{ +return sset ==ETH_SS_STATS ?TUN_STATS_LEN : -EOPNOTSUPP ; +} + +static void tun_get_strings (struct net_device *dev,u32 stringset,u8 *data) +{ +if (stringset ==ETH_SS_STATS ) +memcpy (data, tun_ethtool_strings, +sizeof (tun_ethtool_strings)); +} + +static void tun_get_ethtool_stats (struct net_device *dev, +struct ethtool_stats *stats,u64 *data) +{ +struct tun_struct *tun =netdev_priv (dev); + + data[0 ] =atomic64_read (&tun->rx_csum_sw); +} +static const struct ethtool_ops tun_ethtool_ops = { + .get_sset_count = tun_get_sset_count, + .get_strings = tun_get_strings, + .get_ethtool_stats = tun_get_ethtool_stats, .supported_coalesce_params =ETHTOOL_COALESCE_RX_MAX_FRAMES , .get_drvinfo = tun_get_drvinfo, .get_msglevel = tun_get_msglevel,drivers/net/virtio_net.c +0
-0@@ -2519,7 +2519,7 @@ static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *skb_set_network_header (skb,ETH_HLEN ); if (virtio_net_hdr_tnl_to_skb(skb, &hdr->tnl_hdr, vi->rx_tnl, vi->rx_tnl_csum,Quoted 8 lines
drivers/net/virtio_net.c static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue * · line 2525
Quoted 5 lines
drivers/net/virtio_net.c +1
-1- virtio_is_little_endian(vi->vdev))) { + virtio_is_little_endian(vi->vdev)) < 0) { net_warn_ratelimited("%s: bad gso: type: %x, size: %u, flags %x tunnel %d tnl csum %d\n", dev->name, hdr->hdr.gso_type, hdr->hdr.gso_size, hdr->hdr.flags,should
virtio_netcount these too? Or do we assume that devices are more trustworthy, so unlikely to cause this issue?
Quoted 2 lines
With memory encryption, from an untrusted host?
i can add that.
I honestly don't know if it's worth the effort. Just curious whether you had purposely skipped this and tap.
Quoted 28 lines
Quoted 27 lines
Quoted 26 lines
net/packet/af_packet.c +2
-2@@ -2913,7 +2913,7 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) }if (has_vnet_hdr) { -if (virtio_net_hdr_to_skb (skb, &vnet_hdr,vio_le ())) { +if (virtio_net_hdr_to_skb (skb, &vnet_hdr,vio_le ()) <0 ) { tp_len = -EINVAL ; goto tpacket_error; }@@ -3136,7 +3136,7 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)if (vnet_hdr_sz) { err =virtio_net_hdr_to_skb (skb, &vnet_hdr,vio_le ()); -if (err) +if (err <0 ) goto out_free; len += vnet_hdr_sz; }Signature · 1 line
MST
Michael S. Tsirkin
maintainerRe: [PATCH net v3 1/8] net: account for ingress-resolved virtio net hdr state
Quoted 170 lines · Willem de Bruijn
Quoted 61 lines · Michael S. Tsirkin
Quoted 48 lines · Willem de Bruijn
Quoted 36 lines · Michael S. Tsirkin
The following patch will add ingress resolution of uncommon virtio net hdr checksum metadata (software checksum completion at ingress) which adds performance overhead to the packet processing.
We should make it visible, so a hypothetical workload relying on uncommon metadata can be distinguished from none at all. This way issues in the parsing will be reported and not quietly pay the host-CPU checksum overhead instead of offload.
Flip every call to
virtio_net_hdr_to_skbto test for negative errno only, and have tun count these events per device asrx_csum_sw, exposed via ethtool -S.Assisted-by LLMSigned-off-by Michael S. Tsirkin <[email protected]>--- drivers/net/tap.c | 4 ++-- drivers/net/tun.c | 41 ++++++++++++++++++++++++++++++++++++++-- drivers/net/virtio_net.c | 2 +- net/packet/af_packet.c | 4 ++-- 4 files changed, 44 insertions(+), 7 deletions(-)drivers/net/tap.c +1
-1@@ -649,7 +649,7 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,if (vnet_hdr_len) { err =tun_vnet_hdr_to_skb (q->flags, skb, &vnet_hdr); -if (err) { +if (err <0 ) {rcu_read_unlock (); drop_reason =SKB_DROP_REASON_DEV_HDR ; goto err_kfree;drivers/net/tap.c static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp) · line 1090
Quoted 8 lines
drivers/net/tap.c +1
-1@@ -1084,7 +1084,7 @@ static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp)if (vnet_hdr_len) { err =tun_vnet_hdr_to_skb (q->flags, skb, gso); -if (err) { +if (err <0 ) {rcu_read_unlock (); goto err_kfree; }similar to tun, should tap count this event?
Quoted 11 lines
drivers/net/tun.c struct tun_struct { · line 213
Quoted 9 lines
drivers/net/tun.c +1
-0@@ -210,6 +210,7 @@ struct tun_struct {u32 flow_count;u32 rx_batched;atomic_long_t rx_frame_errors; +atomic64_t rx_csum_sw;is a normal (and cheaper on 32b platforms)
atomic_toratomic_long_tsufficient?Quoted 3 lines
well it can overflow easily. i don't know that we care that a slow path will be even a bit slower on these ancient platforms. correct reporting seems more important.
Ok. I would hope that monitoring catches this before it hits 4B, but agreed that as slow path it doesn't matter.
Quoted 101 lines
Quoted 100 lines
Quoted 6 lines
drivers/net/tun.c +0
-0struct bpf_prog __rcu *xdp_prog;struct tun_prog __rcu *steering_prog;struct tun_prog __rcu *filter_prog;@@ -1935,11 +1936,16 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,break ;drivers/net/tun.c static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, · line 1945
Quoted 10 lines
drivers/net/tun.c +3
-1} -if (tun_vnet_hdr_tnl_to_skb (tun->flags, features, skb, &hdr)) { + err =tun_vnet_hdr_tnl_to_skb (tun->flags, features, skb, &hdr); +if (err <0 ) {atomic_long_inc (&tun->rx_frame_errors); err = -EINVAL ; goto free_skb; } +if (err >0 ) {} else if (err > 0) {
Quoted 9 lines
drivers/net/tun.c +3
-0+atomic64_inc (&tun->rx_csum_sw); + err =0 ; + }if ((tun->flags &TUN_TYPE_MASK ) ==IFF_TAP ) skb->protocol =eth_type_trans (skb, tun->dev);@@ -2605,12 +2611,15 @@ static int tun_xdp_one(struct tun_struct *tun, tnl_hdr = (struct virtio_net_hdr_v1_hash_tunnel *)gso; skb->dev = tun->dev;drivers/net/tun.c static int tun_xdp_one(struct tun_struct *tun, · line 2621
Quoted 10 lines
drivers/net/tun.c +3
-1skb_set_network_header (skb,ETH_HLEN ); -if (tun_vnet_hdr_tnl_to_skb (tun->flags, features, skb, tnl_hdr)) { + ret =tun_vnet_hdr_tnl_to_skb (tun->flags, features, skb, tnl_hdr); +if (ret <0 ) {atomic_long_inc (&tun->rx_frame_errors);kfree_skb (skb); ret = -EINVAL ; goto out; } +if (ret >0 )same
drivers/net/tun.c static int tun_xdp_one(struct tun_struct *tun, · line 2622
Quoted 1 line
drivers/net/tun.c +1
-0+atomic64_inc (&tun->rx_csum_sw);maybe reset ret = 0 in all these branches
Quoted 44 lines
drivers/net/tun.c +28
-0skb->protocol =eth_type_trans (skb, tun->dev);skb_reset_network_header (skb);@@ -3739,7 +3748,35 @@ static void tun_get_channels(struct net_device *dev, channels->max_combined = tun->flags &IFF_MULTI_QUEUE ?MAX_TAP_QUEUES :1 ; } +#define TUN_STATS_LEN 1 +static const char tun_ethtool_strings[TUN_STATS_LEN ][ETH_GSTRING_LEN ] = { +"rx_csum_sw" , +}; + +static int tun_get_sset_count (struct net_device *dev,int sset) +{ +return sset ==ETH_SS_STATS ?TUN_STATS_LEN : -EOPNOTSUPP ; +} + +static void tun_get_strings (struct net_device *dev,u32 stringset,u8 *data) +{ +if (stringset ==ETH_SS_STATS ) +memcpy (data, tun_ethtool_strings, +sizeof (tun_ethtool_strings)); +} + +static void tun_get_ethtool_stats (struct net_device *dev, +struct ethtool_stats *stats,u64 *data) +{ +struct tun_struct *tun =netdev_priv (dev); + + data[0 ] =atomic64_read (&tun->rx_csum_sw); +} +static const struct ethtool_ops tun_ethtool_ops = { + .get_sset_count = tun_get_sset_count, + .get_strings = tun_get_strings, + .get_ethtool_stats = tun_get_ethtool_stats, .supported_coalesce_params =ETHTOOL_COALESCE_RX_MAX_FRAMES , .get_drvinfo = tun_get_drvinfo, .get_msglevel = tun_get_msglevel,drivers/net/virtio_net.c +0
-0drivers/net/virtio_net.c static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue * · line 2525
Quoted 8 lines
drivers/net/virtio_net.c +1
-1@@ -2519,7 +2519,7 @@ static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *skb_set_network_header (skb,ETH_HLEN );if (virtio_net_hdr_tnl_to_skb (skb, &hdr->tnl_hdr, vi->rx_tnl, vi->rx_tnl_csum, -virtio_is_little_endian (vi->vdev))) { +virtio_is_little_endian (vi->vdev)) <0 ) { net_warn_ratelimited("%s: bad gso: type: %x, size: %u, flags %x tunnel %d tnl csum %d\n", dev->name, hdr->hdr.gso_type, hdr->hdr.gso_size, hdr->hdr.flags,should
virtio_netcount these too? Or do we assume that devices are more trustworthy, so unlikely to cause this issue?
Quoted 5 lines
Quoted 2 lines
With memory encryption, from an untrusted host?
i can add that.I honestly don't know if it's worth the effort. Just curious whether you had purposely skipped this and tap.
more like it slipped my mind.
Quoted 29 lines
Quoted 28 lines
Quoted 27 lines
Quoted 26 lines
net/packet/af_packet.c +2
-2@@ -2913,7 +2913,7 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) }if (has_vnet_hdr) { -if (virtio_net_hdr_to_skb (skb, &vnet_hdr,vio_le ())) { +if (virtio_net_hdr_to_skb (skb, &vnet_hdr,vio_le ()) <0 ) { tp_len = -EINVAL ; goto tpacket_error; }@@ -3136,7 +3136,7 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)if (vnet_hdr_sz) { err =virtio_net_hdr_to_skb (skb, &vnet_hdr,vio_le ()); -if (err) +if (err <0 ) goto out_free; len += vnet_hdr_sz; }Signature · 1 line
MST
Michael S. Tsirkin
maintainerPATCH v3 2/8 um: vector: honor virtio net hdr conversion failures
In reply to Michael S. Tsirkinvirtio_net_hdr_to_skb() refuses packets whose checksum metadata it cannot make sense of, but the vector raw transport ignored its return value: rejected packets went on into the stack with their metadata unvalidated.
Propagate the failure.
Note that a follow-up patch will modify virtio_net_hdr_to_skb() to return a negative value on error and a non-negative value on success. This is why we don't just propagate its return value directly.
--- arch/um/drivers/vector_transports.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/um/drivers/vector_transports.c +3 -1
@@ -210,7 +210,9 @@ static int raw_verify_header(return 1 ;skb_set_network_header (skb,ETH_HLEN ); -virtio_net_hdr_to_skb (skb, vheader,virtio_legacy_is_little_endian ()); +if (virtio_net_hdr_to_skb (skb, vheader, +virtio_legacy_is_little_endian ()) <0 ) +return -1 ;return 0 ; }
Signature · 1 line
MST
[email protected]
AI reviewRe: [PATCH net v3 2/8] um: vector: honor virtio net hdr conversion failures
Sashiko has reviewed this patch and found no issues.
Quoted 7 lines
virtio_net_hdr_to_skb()refuses packets whose checksum metadata it cannot make sense of, but the vector raw transport ignored its return value: rejected packets went on into the stack with their metadata unvalidated.Propagate the failure.
Quoted 7 lines
Note that a follow-up patch will modify
virtio_net_hdr_to_skb()to return a negative value on error and a non-negative value on success. This is why we don't just propagate its return value directly.Assisted-by LLMSigned-off-by Michael S. Tsirkin <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Willem de Bruijn
maintainerRe: [PATCH net v3 2/8] um: vector: honor virtio net hdr conversion failures
In reply to Michael S. TsirkinQuoted 23 lines · Michael S. Tsirkin
virtio_net_hdr_to_skb()refuses packets whose checksum metadata it cannot make sense of, but the vector raw transport ignored its return value: rejected packets went on into the stack with their metadata unvalidated.Propagate the failure.
Note that a follow-up patch will modify
virtio_net_hdr_to_skb()to return a negative value on error and a non-negative value on success. This is why we don't just propagate its return value directly.Assisted-by LLMSigned-off-by Michael S. Tsirkin <[email protected]>--- arch/um/drivers/vector_transports.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)arch/um/drivers/vector_transports.c +0
-0
arch/um/drivers/vector_transports.c static int raw_verify_header( · line 215
Quoted 7 lines
arch/um/drivers/vector_transports.c +3
-1@@ -210,7 +210,9 @@ static int raw_verify_header(return 1 ;skb_set_network_header (skb,ETH_HLEN ); -virtio_net_hdr_to_skb (skb, vheader,virtio_legacy_is_little_endian ()); +if (virtio_net_hdr_to_skb (skb, vheader, +virtio_legacy_is_little_endian ()) <0 ) +return -1 ;
fold into the previous patch? It already mentions that it covers all cases.
Quoted 6 lines
arch/um/drivers/vector_transports.c +0
-0return 0 ; }Signature · 1 line
MST
Michael S. Tsirkin
maintainerPATCH v3 3/8 um: vector: trim skbs before verifying the header
In reply to Michael S. TsirkinThe vector receive path over-allocates the skb and only trims it to the received length after the transport header is verified.
A follow up patch will complete the packet checksum in software within virtio_net_hdr_to_skb so we need the packet trimmed (otherwise the checksum would be computed incorrectly over the unreceived tail of the buffer).
Trim first, so the import sees the packet as it was received.
--- arch/um/drivers/vector_kern.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/um/drivers/vector_kern.c +3 -3
@@ -865,6 +865,7 @@ static int vector_legacy_rx(struct vector_private *vp)if (skb !=NULL ) {if (pkt_len > vp->header_size) { +pskb_trim (skb, pkt_len - vp->rx_header_size);if (vp->header_size >0 ) { header_check = vp->verify_header ( vp->header_rxbuffer, skb, vp);
@@ -879,7 +880,6 @@ static int vector_legacy_rx(struct vector_private *vp) skb->ip_summed =CHECKSUM_UNNECESSARY ; } } -pskb_trim (skb, pkt_len - vp->rx_header_size); skb->protocol =eth_type_trans (skb, skb->dev); vp->dev->stats.rx_bytes += skb->len; vp->dev->stats.rx_packets++;
@@ -981,6 +981,8 @@ static int vector_mmsg_rx(struct vector_private *vp, int budget)for (i =0 ; i < packet_count; i++) { skb = (*skbuff_vector);if (mmsg_vector->msg_len > vp->header_size) { +pskb_trim (skb, + mmsg_vector->msg_len - vp->rx_header_size);if (vp->header_size >0 ) { header_check = vp->verify_header( mmsg_vector->msg_hdr.msg_iov->iov_base,
@@ -1005,8 +1007,6 @@ static int vector_mmsg_rx(struct vector_private *vp, int budget) skb->ip_summed =CHECKSUM_UNNECESSARY ; } } -pskb_trim (skb, - mmsg_vector->msg_len - vp->rx_header_size); skb->protocol =eth_type_trans (skb, skb->dev);/* * We do not need to lock on updating stats here
Signature · 1 line
MST
[email protected]
AI reviewRe: [PATCH net v3 3/8] um: vector: trim skbs before verifying the header
Sashiko has reviewed this patch and found no issues.
Quoted 8 lines
The vector receive path over-allocates the skb and only trims it to the received length after the transport header is verified.
A follow up patch will complete the packet checksum in software within
virtio_net_hdr_to_skbso we need the packet trimmed (otherwise the checksum would be computed incorrectly over the unreceived tail of the buffer).
Quoted 4 lines
Trim first, so the import sees the packet as it was received.
Assisted-by LLMSigned-off-by Michael S. Tsirkin <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Michael S. Tsirkin
maintainerPATCH v3 4/8 virtio_net: parse tunnel csum_start little-endian
In reply to Michael S. Tsirkin<glm>
The tunnel header format is only produced by standard-compliant devices, so all of its fields, csum_start included, are little-endian. The nested virtio net header import already parsed csum_start that way, but the inner transport offset was parsed with the endianness specified by the interface.
On an interface in legacy big-endian mode the two parses of the same field disagreed, and the value stored in skb->inner_transport_header - which is only bounds-checked from below - could point past the end of the packet, where GSO segmentation later dereferences it.
</glm>
Parse the field little-endian in both places.
--- include/linux/virtio_net.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/virtio_net.h +1 -1
@@ -324,7 +324,7 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb, inner_l3min =virtio_l3min (inner_isv6); outer_l3min =ETH_HLEN +virtio_l3min (outer_isv6); - inner_th =__virtio16_to_cpu (little_endian, hdr->csum_start); + inner_th =__virtio16_to_cpu (true, hdr->csum_start); inner_nh =le16_to_cpu (vhdr->inner_nh_offset); outer_th =le16_to_cpu (vhdr->outer_th_offset); if (outer_th < outer_l3min ||
Signature · 1 line
MST
[email protected]
AI reviewRe: [PATCH net v3 4/8] virtio_net: parse tunnel csum_start little-endian
Sashiko has reviewed this patch and found no issues.
Quoted 14 lines
<glm>
The tunnel header format is only produced by standard-compliant devices, so all of its fields,csum_startincluded, are little-endian. The nested virtio net header import already parsedcsum_startthat way, but the inner transport offset was parsed with the endianness specified by the interface.On an interface in legacy big-endian mode the two parses of the same field disagreed, and the value stored in
skb->inner_transport_header- which is only bounds-checked from below - could point past the end of the packet, where GSO segmentation later dereferences it.
</glm>
Quoted 6 lines
Parse the field little-endian in both places.
Fixes a2fb4bc4e2a6 ("net: implement virtio helpers to handle UDP GSO tunneling.")Cc Paolo Abeni <[email protected]>Assisted-by LLMSigned-off-by Michael S. Tsirkin <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Michael S. Tsirkin
maintainerPATCH v3 5/8 net: resolve uncommon partial checksums at ingress
In reply to Michael S. TsirkinAs reported first by Paulos Yibelo <[email protected]> (after which others, credited below, found more ways the bug can affect users) a virtio net header can place CHECKSUM_PARTIAL at any in-packet offset. If that state is retained, later checksum completion can modify the packet after protocol processing has made decisions from it.
Let's fix that: let's validate csum_start and csum_offset and only keep CHECKSUM_PARTIAL when csum_start and csum_offset match a parsed TCP or UDP header, while handling VLAN headers, IPv4 options and IPv6 extension headers. Complete other non-GSO checksums immediately, before any protocol processing runs.
We can't easily "complete the checksum" for GSO we don't recognize - GSO assumes knowledge of the protocol. We could segment ignoring the checksum then do whatever csum_start and csum_offset say, but that seems like an overkill for something that shouldn't happen.
There's some risk here:
- non-gso packets which can't be parsed get checksum calculated in software, adding performance overhead
- gso packets which can't be parsed get dropped
To mitigate the risks, the following patch will add a sysctl knob to bypass the resolution. This is reasonable if the setup is such that there is no protocol processing at all.
UDP tunnel headers get the same handling on the inner header.
Packets without VIRTIO_NET_HDR_F_NEEDS_CSUM are unaffected.
--- include/linux/virtio_net.h | 272 ++++++++++++++++++++++++++++++++++++- 1 file changed, 268 insertions(+), 4 deletions(-)
include/linux/virtio_net.h +268 -4
@@ -5,7 +5,11 @@#include <linux/if_vlan.h> #include <linux/ip.h> #include <linux/ipv6.h> +#include <linux/netdevice.h> #include <linux/udp.h> +#include <net/gso.h> +#include <net/ip.h> +#include <net/ipv6.h> #include <net/tcp.h> #include <uapi/linux/tcp.h> #include <uapi/linux/virtio_net.h>
@@ -48,6 +52,198 @@ static inline int virtio_net_hdr_set_proto(struct sk_buff *skb,return 0 ; } +/* TODO: nothing virtio specific here, move to ip.h */ + +/* +* Pull and minimally validate the IPv4 header at @nhoff. +* Returns the header and stores the transport header offset in +* @thoff, or NULL if truncated or malformed. Only the fixed header +* is guaranteed linear; callers that need the options or the +* transport header must pull further. +*/ +static inline struct iphdr *virtio_net_ip4_parse (struct sk_buff *skb, +unsigned int nhoff, +unsigned int *thoff) +{ +struct iphdr *iph; + +if (!pskb_may_pull (skb, nhoff +sizeof (struct iphdr ))) +return NULL ; + + iph = (struct iphdr *)(skb->data + nhoff); +if (iph->version !=4 || + iph->ihl <sizeof (struct iphdr ) /sizeof (u32)) +return NULL ; + + *thoff = nhoff + iph->ihl *sizeof (u32); +return iph; +} + +/* Where does the L3 header start, and what protocol is it? +* Handles Ethernet and VLAN headers; other devices use the +* network header offset the caller set up. +*/ +static inline bool +virtio_net_l3_start (struct sk_buff *skb,int *nhoff,__be16 *proto) +{ +/* skb->dev is only guaranteed to be set since 44378d02c5aa +* ("net: always dissect GSO packets in +* __virtio_net_hdr_to_skb()"), do not crash when it is not. +*/ +if (WARN_ON_ONCE (!skb->dev)) +return false; + +if (skb->dev->type ==ARPHRD_ETHER ) { +if (!pskb_may_pull (skb,ETH_HLEN )) +return false; + *nhoff =ETH_HLEN ; + *proto =eth_hdr (skb)->h_proto; +if (eth_type_vlan (*proto)) { + *proto =__vlan_get_protocol (skb, *proto, nhoff); +if (!*proto) +return false; + } + }else { + *nhoff =skb_network_offset (skb); +/* An unset network header shows up as an offset +* outside of the packet. +*/ +if (*nhoff <0 || (unsigned int )*nhoff >= skb->len) +return false; + *proto = skb->protocol; +if (!*proto) { +u8 version; + +/* A packet socket bound to ETH_P_ALL leaves the +* protocol unset: infer it from the IP version +* nibble at the network header. +*/ +if (!pskb_may_pull (skb, *nhoff +1 )) +return false; + version = skb->data[*nhoff] >>4 ; +if (version ==4 ) + *proto =htons (ETH_P_IP ); +else if (version ==6 ) + *proto =htons (ETH_P_IPV6 ); + } + } +return true; +} + +/* Walk the L3 header at @nhoff: where does its transport header +* start, and which protocol is it? Returns false if the header +* is truncated, malformed or fragmented. +*/ +static inline bool +virtio_net_l4_start (struct sk_buff *skb,unsigned int nhoff,__be16 proto, +unsigned int *thoff,int *ip_proto) +{ +if (proto ==htons (ETH_P_IP )) { +const struct iphdr *iph =virtio_net_ip4_parse (skb, nhoff, thoff); + +if (!iph ||ip_is_fragment (iph)) +return false; + *ip_proto = iph->protocol; +return true; + } + +if (proto ==htons (ETH_P_IPV6 )) { +const struct ipv6hdr *iph; +int flags =0 ; + +if (!pskb_may_pull (skb, nhoff +sizeof (*iph))) +return false; + iph = (const void *)(skb->data + nhoff); +if (iph->version !=6 ) +return false; + *thoff = nhoff +sizeof (*iph); + *ip_proto = iph->nexthdr; +if (*ip_proto !=IPPROTO_TCP && *ip_proto !=IPPROTO_UDP ) { + *thoff = nhoff; + *ip_proto =ipv6_find_hdr (skb, thoff,-1 ,NULL , + &flags); +if (*ip_proto <0 || flags &IP6_FH_F_FRAG ) +return false; + } +return true; + } + +return false; +} + +/* Does checksum metadata match a known TCP or UDP header? */ +static inline bool +virtio_net_csum_partial_ok_at_offset (struct sk_buff *skb,u8 hdr_gso_type, +unsigned int nhoff,__be16 proto) +{ +unsigned int thoff; +int ip_proto; + +if (!virtio_net_l4_start (skb, nhoff, proto, &thoff, &ip_proto)) +return false; + +if (skb_checksum_start_offset (skb) != thoff) +return false; + +switch (ip_proto) { +case IPPROTO_TCP : +if (skb->csum_offset != offsetof(struct tcphdr , check) || + !pskb_may_pull (skb, thoff +sizeof (struct tcphdr ))) +return false; +break ; +case IPPROTO_UDP : +if (skb->csum_offset != offsetof(struct udphdr , check) || + !pskb_may_pull (skb, thoff +sizeof (struct udphdr ))) +return false; +break ; +default : +return false; + } + +switch (hdr_gso_type & ~VIRTIO_NET_HDR_GSO_ECN ) { +case VIRTIO_NET_HDR_GSO_NONE : +return true; +case VIRTIO_NET_HDR_GSO_TCPV4 : +return proto ==htons (ETH_P_IP ) && ip_proto ==IPPROTO_TCP ; +case VIRTIO_NET_HDR_GSO_TCPV6 : +return proto ==htons (ETH_P_IPV6 ) && ip_proto ==IPPROTO_TCP ; +case VIRTIO_NET_HDR_GSO_UDP : +case VIRTIO_NET_HDR_GSO_UDP_L4 : +return ip_proto ==IPPROTO_UDP ; +default : +return false; + } +} + +static inline bool +virtio_net_csum_partial_ok (struct sk_buff *skb,u8 hdr_gso_type) +{ +int nhoff; +__be16 proto; + +if (!virtio_net_l3_start (skb, &nhoff, &proto)) +return false; + +return virtio_net_csum_partial_ok_at_offset (skb, hdr_gso_type, nhoff, + proto); +} + +/* +* Handle a packet whose checksum metadata does not match the packet: +* GSO packets are dropped, as segmentation needs one checksum per +* segment; others are completed in software. +* +* Return 1 if the checksum was completed in software, else a negative +* errno. +*/ +static inline int virtio_net_resolve_csum_partial_mismatch (struct sk_buff *skb) +{ +if (skb_is_gso (skb)) +return -EINVAL ; + +return skb_checksum_help (skb) ?:1 ; +} +static inline int __virtio_net_hdr_to_skb (struct sk_buff *skb,const struct virtio_net_hdr *hdr,bool little_endian,u8 hdr_gso_type)
@@ -194,11 +390,36 @@ static inline int __virtio_net_hdr_to_skb(struct sk_buff *skb,return 0 ; } +/* +* Return value: +* 0 - success +* >0 - checksum was completed in software (slow path) +* <0 - failure +*/ static inline int virtio_net_hdr_to_skb (struct sk_buff *skb,const struct virtio_net_hdr *hdr,bool little_endian) { -return __virtio_net_hdr_to_skb (skb, hdr, little_endian, hdr->gso_type); + typeof(skb->transport_header) old_transport = skb->transport_header; +int ret =__virtio_net_hdr_to_skb (skb, hdr, little_endian, + hdr->gso_type); + +if (ret <0 ) +return ret; + +if (skb->ip_summed !=CHECKSUM_PARTIAL || +virtio_net_csum_partial_ok (skb, hdr->gso_type)) +return 0 ; + +/* +* Here we do not trust the checksum metadata. +* __virtio_net_hdr_to_skb set the transport header to csum_start +* from virtio hdr, restore it to a known good value. +*/ + skb->transport_header = old_transport; + ret =virtio_net_resolve_csum_partial_mismatch (skb); + +return ret; }/* This function must be called after virtio_net_hdr_from_skb(). */
@@ -282,6 +503,35 @@ static inline unsigned int virtio_l3min(bool is_ipv6)return is_ipv6 ?sizeof (struct ipv6hdr ) :sizeof (struct iphdr ); } +/* Do the tunnel offsets match the outer headers in the frame? +* The L3 header the tunnel claims must be the one the frame +* carries, with UDP following it at @outer_th. +*/ +static inline bool +virtio_net_tnl_offsets_ok (struct sk_buff *skb,bool outer_isv6, +unsigned int outer_th) +{ +int nhoff; +__be16 proto; +unsigned int thoff; +int ip_proto; + +if (!virtio_net_l3_start (skb, &nhoff, &proto)) +return false; +if (outer_isv6 != (proto ==htons (ETH_P_IPV6 ))) +return false; +if (!virtio_net_l4_start (skb, nhoff, proto, &thoff, &ip_proto)) +return false; + +return ip_proto ==IPPROTO_UDP && outer_th == thoff; +} + +/* +* Return value: +* 0 - success +* >0 - checksum was completed in software (slow path) +* <0 - failure +*/ static inline int virtio_net_hdr_tnl_to_skb(struct sk_buff *skb, const struct virtio_net_hdr_v1_hash_tunnel *vhdr,
@@ -293,6 +543,7 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb,unsigned int inner_nh, outer_th, inner_th;unsigned int inner_l3min, outer_l3min;u8 gso_inner_type, gso_tunnel_type; +__be16 inner_proto;bool outer_isv6, inner_isv6;int ret;
@@ -335,9 +586,12 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb,/* Let the basic parsing deal with plain GSO features. */ ret =__virtio_net_hdr_to_skb (skb, hdr, true, hdr->gso_type & ~gso_tunnel_type); -if (ret) +if (ret <0 )return ret; +if (!virtio_net_tnl_offsets_ok (skb, outer_isv6, outer_th)) +return -EINVAL ; +/* In case of USO, the inner protocol is still unknown and * `inner_isv6` is just a guess, additional parsing is needed. * The previous validation ensures that accessing an ipv4 inner
@@ -353,7 +607,7 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb, }skb_set_inner_protocol (skb, inner_isv6 ?htons (ETH_P_IPV6 ) : -htons (ETH_P_IP )); +htons (ETH_P_IP ));if (hdr->flags &VIRTIO_NET_HDR_F_UDP_TUNNEL_CSUM ) {if (!tnl_csum_negotiated)return -EINVAL ;
@@ -368,7 +622,17 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb, skb->inner_mac_header = inner_nh +skb_headroom (skb); skb->transport_header = outer_th +skb_headroom (skb); skb->encapsulation =1 ; -return 0 ; + +/* +* We know VIRTIO_NET_HDR_F_NEEDS_CSUM was set so __virtio_net_hdr_to_skb +* set CHECKSUM_PARTIAL. +*/ + inner_proto = inner_isv6 ?htons (ETH_P_IPV6 ) :htons (ETH_P_IP ); +if (virtio_net_csum_partial_ok_at_offset (skb, gso_inner_type, inner_nh, + inner_proto)) +return 0 ; + +return virtio_net_resolve_csum_partial_mismatch (skb); }/* Checksum-related fields validation for the driver */
Signature · 1 line
MST
[email protected]
AI reviewRe: [PATCH net v3 5/8] net: resolve uncommon partial checksums at ingress
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] net: skb_checksum_help() in virtio_net_hdr_to_skb() skips shared frag copy - [High] net…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] net: skb_checksum_help() in virtio_net_hdr_to_skb() skips shared frag copy
- [High] net: virtio_net_hdr_tnl_to_skb() corrupts non-GSO skb state on tunnel hdr
- [High] net: virtio_net_l4_start() skips IPv6 NEXTHDR_AUTH by omitting IP6_FH_F_AUTH
- [High] net: virtio_net_l3_start() L2/L3 offset and protocol mismatch on lo and AF_PACKET --
commit 338a1355fe27c4686a83ea9e73bf390729be2308
Author: Michael S. Tsirkin <[email protected]>
net: resolve uncommon partial checksums at ingress
This commit validates csum_start and csum_offset at ingress so that CHECKSUM_PARTIAL is only retained when matching a parsed TCP or UDP header. Non-GSO packets with unrecognized checksum offsets are completed immediately in software via skb_checksum_help(), while unrecognized GSO packets are dropped.
Quoted 4 lines
include/linux/virtio_net.h +0
-0
[ ... ]
Quoted 1 line
include/linux/virtio_net.h +0
-0@@ -48,6 +52,198 @@ static inline int virtio_net_hdr_set_proto(struct sk_buff *skb,
[ ... ]
Quoted 40 lines
include/linux/virtio_net.h +40
-0+/* Where does the L3 header start, and what protocol is it? +* Handles Ethernet and VLAN headers; other devices use the +* network header offset the caller set up. +*/ +static inline bool +virtio_net_l3_start (struct sk_buff *skb,int *nhoff,__be16 *proto) +{ +/* skb->dev is only guaranteed to be set since 44378d02c5aa +* ("net: always dissect GSO packets in +* __virtio_net_hdr_to_skb()"), do not crash when it is not. +*/ +if (WARN_ON_ONCE (!skb->dev)) +return false; + +if (skb->dev->type ==ARPHRD_ETHER ) { +if (!pskb_may_pull (skb,ETH_HLEN )) +return false; + *nhoff =ETH_HLEN ; + *proto =eth_hdr (skb)->h_proto; +if (eth_type_vlan (*proto)) { + *proto =__vlan_get_protocol (skb, *proto, nhoff); +if (!*proto) +return false; + } + }else { + *nhoff =skb_network_offset (skb); +/* An unset network header shows up as an offset +* outside of the packet. +*/ +if (*nhoff <0 || (unsigned int )*nhoff >= skb->len) +return false; + *proto = skb->protocol; +if (!*proto) { +u8 version; + +/* A packet socket bound to ETH_P_ALL leaves the +* protocol unset: infer it from the IP version +* nibble at the network header. +*/ +if (!pskb_may_pull (skb, *nhoff +1 ))
include/linux/virtio_net.h static inline int virtio_net_hdr_set_proto(struct sk_buff *skb, · line 131
Quoted 10 lines
include/linux/virtio_net.h +10
-0+return false; + version = skb->data[*nhoff] >>4 ; +if (version ==4 ) + *proto =htons (ETH_P_IP ); +else if (version ==6 ) + *proto =htons (ETH_P_IPV6 ); + } + } +return true; +}
[Severity: High]
Can virtio_net_l3_start() disagree with the networking stack on the L3 offset and protocol when packets are sent via AF_PACKET (packet_snd() or tpacket_snd()) with PACKET_VNET_HDR enabled?
First, on loopback (where dev->type is ARPHRD_LOOPBACK, but dev->hard_header_len is ETH_HLEN and loopback_xmit() calls eth_type_trans()), virtio_net_l3_start() takes the else branch and sets *nhoff = skb_network_offset(skb) (14) without inspecting eth_hdr(skb) or VLAN tags:
drivers/net/loopback.c:loopback_xmit() {
skb->protocol = eth_type_trans(skb, dev);
}If a packet on lo has eth_hdr(skb)->h_proto = htons(ETH_P_8021Q) and a 4-byte 802.1P priority tag (VLAN ID 0) with h_vlan_TCI = htons(0x6000) and h_vlan_encapsulated_proto = htons(ETH_P_IP), virtio_net_l3_start() and virtio_net_l4_start() can validate a fake IPv6 header starting at the VLAN tag (offset 14), while loopback_xmit() -> __netif_receive_skb_core() -> skb_vlan_untag() strips the 4-byte tag and delivers the inner IPv4 packet at offset 18 with CHECKSUM_PARTIAL retained at the unvalidated offset.
Second, on ARPHRD_ETHER devices, virtio_net_l3_start() always uses eth_hdr(skb)->h_proto and advances *nhoff past VLAN tags via __vlan_get_protocol(), whereas AF_PACKET SOCK_RAW preserves a user-supplied sll_protocol in skb->protocol and packet_parse_headers() only advances skb->network_header when eth_type_vlan(skb->protocol) is true:
net/packet/af_packet.c:packet_parse_headers() {
if (!skb->protocol && sock->type == SOCK_RAW)
skb->protocol = dev_parse_header_protocol(skb);
skb_probe_transport_header(skb);
/* Move network header to the right position for VLAN tagged packets */
if (likely(skb->dev->type == ARPHRD_ETHER) &&
eth_type_vlan(skb->protocol) &&
vlan_get_protocol_and_depth(skb, skb->protocol, &depth) != 0)
skb_set_network_header(skb, depth);
}If sll_protocol is ETH_P_IP while eth_hdr(skb)->h_proto is ETH_P_8021Q (or vice versa), wouldn't virtio_net_l3_start() validate csum_start against a different protocol and network header offset than what skb->protocol and skb->network_header represent for downstream processing?
Quoted 31 lines
include/linux/virtio_net.h +31
-0+ +/* Walk the L3 header at @nhoff: where does its transport header +* start, and which protocol is it? Returns false if the header +* is truncated, malformed or fragmented. +*/ +static inline bool +virtio_net_l4_start (struct sk_buff *skb,unsigned int nhoff,__be16 proto, +unsigned int *thoff,int *ip_proto) +{ +if (proto ==htons (ETH_P_IP )) { +const struct iphdr *iph =virtio_net_ip4_parse (skb, nhoff, thoff); + +if (!iph ||ip_is_fragment (iph)) +return false; + *ip_proto = iph->protocol; +return true; + } + +if (proto ==htons (ETH_P_IPV6 )) { +const struct ipv6hdr *iph; +int flags =0 ; + +if (!pskb_may_pull (skb, nhoff +sizeof (*iph))) +return false; + iph = (const void *)(skb->data + nhoff); +if (iph->version !=6 ) +return false; + *thoff = nhoff +sizeof (*iph); + *ip_proto = iph->nexthdr; +if (*ip_proto !=IPPROTO_TCP && *ip_proto !=IPPROTO_UDP ) { + *thoff = nhoff;
include/linux/virtio_net.h static inline int virtio_net_hdr_set_proto(struct sk_buff *skb, · line 172
Quoted 10 lines
include/linux/virtio_net.h +10
-0+ *ip_proto =ipv6_find_hdr (skb, thoff,-1 ,NULL , + &flags); +if (*ip_proto <0 || flags &IP6_FH_F_FRAG ) +return false; + } +return true; + } + +return false; +}
[Severity: High]
Should flags in virtio_net_l4_start() be initialized to IP6_FH_F_AUTH before calling ipv6_find_hdr()?
In ipv6_find_hdr(), NEXTHDR_AUTH is only treated as a terminal header when IP6_FH_F_AUTH is passed in flags; otherwise it skips ipv6_authlen(hp) bytes and continues walking into the following header:
net/ipv6/exthdrs_core.c:ipv6_find_hdr() {
} else if (nexthdr == NEXTHDR_AUTH) {
if (flags && (*flags & IP6_FH_F_AUTH) && (target < 0))
break;
hdrlen = ipv6_authlen(hp);
} else
}Downstream parsers such as nft_set_pktinfo_ipv6() (which sets flags = IP6_FH_F_AUTH) and __skb_flow_dissect() stop at NEXTHDR_AUTH rather than skipping it. With flags = 0, an IPv6 packet carrying NEXTHDR_AUTH followed by TCP or UDP passes virtio_net_csum_partial_ok_at_offset() and retains CHECKSUM_PARTIAL, allowing later checksum completion to overwrite the 2 bytes at csum_start + csum_offset after netfilter or flow-dissector inspection, and also allowing IPv6+AH GSO packets through even though ipv6_gso_pull_exthdrs() does not support IPPROTO_AH.
[ ... ]
Quoted 5 lines
include/linux/virtio_net.h +5
-0+/* +* Handle a packet whose checksum metadata does not match the packet: +* GSO packets are dropped, as segmentation needs one checksum per +* segment; others are completed in software. +*
include/linux/virtio_net.h static inline int virtio_net_hdr_set_proto(struct sk_buff *skb, · line 245
Quoted 10 lines
include/linux/virtio_net.h +10
-0+* Return 1 if the checksum was completed in software, else a negative +* errno. +*/ +static inline int virtio_net_resolve_csum_partial_mismatch (struct sk_buff *skb) +{ +if (skb_is_gso (skb)) +return -EINVAL ; + +return skb_checksum_help (skb) ?:1 ; +}
[Severity: High]
When virtio_net_resolve_csum_partial_mismatch() calls skb_checksum_help() from virtio_net_hdr_to_skb() or virtio_net_hdr_tnl_to_skb(), does skb_checksum_help() miss copying shared zerocopy fragments because skb_zcopy_init() has not been called yet?
In skb_checksum_help(), shared fragments are linearized before computing the checksum so that userspace cannot modify the payload pages after the checksum is calculated:
net/core/dev.c:skb_checksum_help() {
/* Before computing a checksum, we should make sure no frag could
* be modified by an external entity : checksum could be wrong.
*/
if (skb_has_shared_frag(skb)) {
ret = __skb_linearize(skb);
if (ret)
goto out;
}
}However, skb_has_shared_frag(skb) checks SKBFL_SHARED_FRAG in skb_shinfo(skb)->flags, which is only set when skb_zcopy_init() runs. In tun_get_user(), tap_get_user(), and tpacket_snd(), the userspace pages are attached to the skb first, then virtio_net_hdr_to_skb() (or tun_vnet_hdr_tnl_to_skb() / tun_vnet_hdr_to_skb()) is called, and skb_zcopy_init() is only called afterwards:
drivers/net/tun.c:tun_get_user() {
err = tun_vnet_hdr_tnl_to_skb(tun->flags, features, skb, &hdr);
...
/* copy skb_ubuf_info for callback when skb has no error */
if (zerocopy) {
skb_zcopy_init(skb, msg_control);
}
}drivers/net/tap.c:tap_get_user() {
if (vnet_hdr_len) {
err = tun_vnet_hdr_to_skb(q->flags, skb, &vnet_hdr);
...
}
...
/* copy skb_ubuf_info for callback when skb has no error */
if (zerocopy) {
skb_zcopy_init(skb, msg_control);
}
}net/packet/af_packet.c:tpacket_snd() {
if (has_vnet_hdr) {
if (virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le()) < 0) {
tp_len = -EINVAL;
goto tpacket_error;
}
}
...
uarg->ubuf.flags = SKBFL_ZEROCOPY_FRAG;
refcount_set(&uarg->ubuf.refcnt, 1);
/* Hold a sk_wmem_alloc reference until completion */
refcount_inc(&po->sk.sk_wmem_alloc);
skb_zcopy_init(skb, &uarg->ubuf);
}Because SKBFL_SHARED_FRAG is not set yet when virtio_net_resolve_csum_partial_mismatch() calls skb_checksum_help(), skb_has_shared_frag(skb) returns false, __skb_linearize(skb) is skipped, and the packet proceeds with skb->ip_summed = CHECKSUM_NONE while its fragments remain backed by writable userspace pages.
[ ... ]
Quoted 30 lines
include/linux/virtio_net.h +7
-3@@ -335,9 +586,12 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb,/* Let the basic parsing deal with plain GSO features. */ ret =__virtio_net_hdr_to_skb (skb, hdr, true, hdr->gso_type & ~gso_tunnel_type); -if (ret) +if (ret <0 )return ret; +if (!virtio_net_tnl_offsets_ok (skb, outer_isv6, outer_th)) +return -EINVAL ; +/* In case of USO, the inner protocol is still unknown and * `inner_isv6` is just a guess, additional parsing is needed. * The previous validation ensures that accessing an ipv4 inner @@ -353,7 +607,7 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb, }skb_set_inner_protocol (skb, inner_isv6 ?htons (ETH_P_IPV6 ) : -htons (ETH_P_IP )); +htons (ETH_P_IP ));if (hdr->flags &VIRTIO_NET_HDR_F_UDP_TUNNEL_CSUM ) {if (!tnl_csum_negotiated)return -EINVAL ;@@ -368,7 +622,17 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb, skb->inner_mac_header = inner_nh +skb_headroom (skb); skb->transport_header = outer_th +skb_headroom (skb); skb->encapsulation =1 ; -return 0 ; + +/*
include/linux/virtio_net.h virtio_net_hdr_tnl_to_skb(struct sk_buff *skb, · line 636
Quoted 10 lines
include/linux/virtio_net.h +9
-0+* We know VIRTIO_NET_HDR_F_NEEDS_CSUM was set so __virtio_net_hdr_to_skb +* set CHECKSUM_PARTIAL. +*/ + inner_proto = inner_isv6 ?htons (ETH_P_IPV6 ) :htons (ETH_P_IP ); +if (virtio_net_csum_partial_ok_at_offset (skb, gso_inner_type, inner_nh, + inner_proto)) +return 0 ; + +return virtio_net_resolve_csum_partial_mismatch (skb); }
[Severity: High]
What happens in virtio_net_hdr_tnl_to_skb() when a packet carries a tunnel GSO type in hdr->gso_type, but its length is not greater than gso_size?
In __virtio_net_hdr_to_skb(), small packets do not populate shinfo->gso_size or shinfo->gso_type:
include/linux/virtio_net.h:__virtio_net_hdr_to_skb() {
/* Too small packets are not really GSO ones. */
if (skb->len - nh_off > gso_size) {
shinfo->gso_size = gso_size;
shinfo->gso_type = gso_type;
/* Header must be checked, and gso_segs computed. */
shinfo->gso_type |= SKB_GSO_DODGY;
shinfo->gso_segs = 0;
}
}Because shinfo->gso_size remains 0, skb_is_gso(skb) is false, but virtio_net_hdr_tnl_to_skb() still sets shinfo->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM (or SKB_GSO_UDP_TUNNEL), sets inner header offsets from vhdr->inner_nh_offset and hdr->csum_start, overwrites skb->transport_header, and sets skb->encapsulation = 1.
If virtio_net_csum_partial_ok_at_offset() then returns false, virtio_net_resolve_csum_partial_mismatch(skb) sees !skb_is_gso(skb), completes the inner checksum via skb_checksum_help(), and returns 1 without cleaning up the tunnel state on the skb. That leaves the non-GSO skb with skb->encapsulation = 1, unvalidated inner header offsets, a non-zero shinfo->gso_type with gso_size == 0 (which triggers WARN_ON_ONCE(1) in tun_vnet_hdr_tnl_from_skb() if forwarded to another tun/virtio interface), and an uncomputed outer UDP checksum when VIRTIO_NET_HDR_F_UDP_TUNNEL_CSUM was set.
Conversely, if the payload at the caller-chosen vhdr->inner_nh_offset happens to look like a valid IP + TCP/UDP header, virtio_net_csum_partial_ok_at_offset() returns true on this non-GSO UDP packet, allowing CHECKSUM_PARTIAL to be retained at an arbitrary offset inside the UDP payload. Should virtio_net_hdr_tnl_to_skb() reject packets where !skb_is_gso(skb) after __virtio_net_hdr_to_skb()?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Willem de Bruijn
maintainerRe: [PATCH net v3 5/8] net: resolve uncommon partial checksums at ingress
In reply to Michael S. TsirkinQuoted 103 lines · Michael S. Tsirkin
As reported first by Paulos Yibelo <[email protected]> (after which others, credited below, found more ways the bug can affect users) a virtio net header can place
CHECKSUM_PARTIALat any in-packet offset. If that state is retained, later checksum completion can modify the packet after protocol processing has made decisions from it.Let's fix that: let's validate
csum_startandcsum_offsetand only keepCHECKSUM_PARTIALwhencsum_startandcsum_offsetmatch a parsed TCP or UDP header, while handling VLAN headers, IPv4 options and IPv6 extension headers. Complete other non-GSO checksums immediately, before any protocol processing runs.We can't easily "complete the checksum" for GSO we don't recognize - GSO assumes knowledge of the protocol. We could segment ignoring the checksum then do whatever
csum_startandcsum_offsetsay, but that seems like an overkill for something that shouldn't happen.There's some risk here:
- non-gso packets which can't be parsed get checksum calculated in software, adding performance overhead
- gso packets which can't be parsed get droppedTo mitigate the risks, the following patch will add a sysctl knob to bypass the resolution. This is reasonable if the setup is such that there is no protocol processing at all.
UDP tunnel headers get the same handling on the inner header.
Packets without
VIRTIO_NET_HDR_F_NEEDS_CSUMare unaffected.Assisted-by LLMReported-by Paulos Yibelo <[email protected]>Reported-by Chris Arges <[email protected]>Tested-by Chris Arges <[email protected]>Fixes f43798c27684 ("tun: Allow GSO using virtio_net_hdr")Suggested-by Willem de Bruijn <[email protected]>Signed-off-by Michael S. Tsirkin <[email protected]>--- include/linux/virtio_net.h | 272 ++++++++++++++++++++++++++++++++++++- 1 file changed, 268 insertions(+), 4 deletions(-)include/linux/virtio_net.h +46
-0@@ -5,7 +5,11 @@#include <linux/if_vlan.h> #include <linux/ip.h> #include <linux/ipv6.h> +#include <linux/netdevice.h> #include <linux/udp.h> +#include <net/gso.h> +#include <net/ip.h> +#include <net/ipv6.h> #include <net/tcp.h> #include <uapi/linux/tcp.h> #include <uapi/linux/virtio_net.h> @@ -48,6 +52,198 @@ static inline int virtio_net_hdr_set_proto(struct sk_buff *skb,return 0 ; } +/* TODO: nothing virtio specific here, move to ip.h */ + +/* +* Pull and minimally validate the IPv4 header at @nhoff. +* Returns the header and stores the transport header offset in +* @thoff, or NULL if truncated or malformed. Only the fixed header +* is guaranteed linear; callers that need the options or the +* transport header must pull further. +*/ +static inline struct iphdr *virtio_net_ip4_parse (struct sk_buff *skb, +unsigned int nhoff, +unsigned int *thoff) +{ +struct iphdr *iph; + +if (!pskb_may_pull (skb, nhoff +sizeof (struct iphdr ))) +return NULL ; + + iph = (struct iphdr *)(skb->data + nhoff); +if (iph->version !=4 || + iph->ihl <sizeof (struct iphdr ) /sizeof (u32)) +return NULL ; + + *thoff = nhoff + iph->ihl *sizeof (u32); +return iph; +} + +/* Where does the L3 header start, and what protocol is it? +* Handles Ethernet and VLAN headers; other devices use the +* network header offset the caller set up. +*/ +static inline bool +virtio_net_l3_start (struct sk_buff *skb,int *nhoff,__be16 *proto) +{ +/* skb->dev is only guaranteed to be set since 44378d02c5aa +* ("net: always dissect GSO packets in +* __virtio_net_hdr_to_skb()"), do not crash when it is not. +*/ +if (WARN_ON_ONCE (!skb->dev)) +return false; + +if (skb->dev->type ==ARPHRD_ETHER ) {
include/linux/virtio_net.h static inline int virtio_net_hdr_set_proto(struct sk_buff *skb, · line 106
Quoted 10 lines
include/linux/virtio_net.h +10
-0+if (!pskb_may_pull (skb,ETH_HLEN )) +return false; + *nhoff =ETH_HLEN ; + *proto =eth_hdr (skb)->h_proto; +if (eth_type_vlan (*proto)) { + *proto =__vlan_get_protocol (skb, *proto, nhoff); +if (!*proto) +return false; + } + }else {
loopback uses ARPHRD_LOOPBACK, but expects Ethernet (apparently). We can add that ARPHRD to the if condition.
Quoted 7 lines
include/linux/virtio_net.h +7
-0+ *nhoff =skb_network_offset (skb); +/* An unset network header shows up as an offset +* outside of the packet. +*/ +if (*nhoff <0 || (unsigned int )*nhoff >= skb->len) +return false; + *proto = skb->protocol;
include/linux/virtio_net.h static inline int virtio_net_hdr_set_proto(struct sk_buff *skb, · line 123
Quoted 10 lines
include/linux/virtio_net.h +10
-0+if (!*proto) { +u8 version; + +/* A packet socket bound to ETH_P_ALL leaves the +* protocol unset: infer it from the IP version +* nibble at the network header. +*/ +if (!pskb_may_pull (skb, *nhoff +1 )) +return false; + version = skb->data[*nhoff] >>4 ;
is this endian specific? maybe just cast to iphdr
Quoted 39 lines
include/linux/virtio_net.h +39
-0+if (version ==4 ) + *proto =htons (ETH_P_IP ); +else if (version ==6 ) + *proto =htons (ETH_P_IPV6 ); + } + } +return true; +} + +/* Walk the L3 header at @nhoff: where does its transport header +* start, and which protocol is it? Returns false if the header +* is truncated, malformed or fragmented. +*/ +static inline bool +virtio_net_l4_start (struct sk_buff *skb,unsigned int nhoff,__be16 proto, +unsigned int *thoff,int *ip_proto) +{ +if (proto ==htons (ETH_P_IP )) { +const struct iphdr *iph =virtio_net_ip4_parse (skb, nhoff, thoff); + +if (!iph ||ip_is_fragment (iph)) +return false; + *ip_proto = iph->protocol; +return true; + } + +if (proto ==htons (ETH_P_IPV6 )) { +const struct ipv6hdr *iph; +int flags =0 ; + +if (!pskb_may_pull (skb, nhoff +sizeof (*iph))) +return false; + iph = (const void *)(skb->data + nhoff); +if (iph->version !=6 ) +return false; + *thoff = nhoff +sizeof (*iph); + *ip_proto = iph->nexthdr; +if (*ip_proto !=IPPROTO_TCP && *ip_proto !=IPPROTO_UDP ) { + *thoff = nhoff;
include/linux/virtio_net.h static inline int virtio_net_hdr_set_proto(struct sk_buff *skb, · line 172
Quoted 10 lines
include/linux/virtio_net.h +10
-0+ *ip_proto =ipv6_find_hdr (skb, thoff,-1 ,NULL , + &flags); +if (*ip_proto <0 || flags &IP6_FH_F_FRAG ) +return false; + } +return true; + } + +return false; +}
for all this parsing: could we use flow dissection, like the !VIRTIO_NET_HDR_F_NEEDS_CSUM case?
Quoted 102 lines
include/linux/virtio_net.h +90
-1+ +/* Does checksum metadata match a known TCP or UDP header? */ +static inline bool +virtio_net_csum_partial_ok_at_offset (struct sk_buff *skb,u8 hdr_gso_type, +unsigned int nhoff,__be16 proto) +{ +unsigned int thoff; +int ip_proto; + +if (!virtio_net_l4_start (skb, nhoff, proto, &thoff, &ip_proto)) +return false; + +if (skb_checksum_start_offset (skb) != thoff) +return false; + +switch (ip_proto) { +case IPPROTO_TCP : +if (skb->csum_offset != offsetof(struct tcphdr , check) || + !pskb_may_pull (skb, thoff +sizeof (struct tcphdr ))) +return false; +break ; +case IPPROTO_UDP : +if (skb->csum_offset != offsetof(struct udphdr , check) || + !pskb_may_pull (skb, thoff +sizeof (struct udphdr ))) +return false; +break ; +default : +return false; + } + +switch (hdr_gso_type & ~VIRTIO_NET_HDR_GSO_ECN ) { +case VIRTIO_NET_HDR_GSO_NONE : +return true; +case VIRTIO_NET_HDR_GSO_TCPV4 : +return proto ==htons (ETH_P_IP ) && ip_proto ==IPPROTO_TCP ; +case VIRTIO_NET_HDR_GSO_TCPV6 : +return proto ==htons (ETH_P_IPV6 ) && ip_proto ==IPPROTO_TCP ; +case VIRTIO_NET_HDR_GSO_UDP : +case VIRTIO_NET_HDR_GSO_UDP_L4 : +return ip_proto ==IPPROTO_UDP ; +default : +return false; + } +} + +static inline bool +virtio_net_csum_partial_ok (struct sk_buff *skb,u8 hdr_gso_type) +{ +int nhoff; +__be16 proto; + +if (!virtio_net_l3_start (skb, &nhoff, &proto)) +return false; + +return virtio_net_csum_partial_ok_at_offset (skb, hdr_gso_type, nhoff, + proto); +} + +/* +* Handle a packet whose checksum metadata does not match the packet: +* GSO packets are dropped, as segmentation needs one checksum per +* segment; others are completed in software. +* +* Return 1 if the checksum was completed in software, else a negative +* errno. +*/ +static inline int virtio_net_resolve_csum_partial_mismatch (struct sk_buff *skb) +{ +if (skb_is_gso (skb)) +return -EINVAL ; + +return skb_checksum_help (skb) ?:1 ; +} +static inline int __virtio_net_hdr_to_skb (struct sk_buff *skb,const struct virtio_net_hdr *hdr,bool little_endian,u8 hdr_gso_type)@@ -194,11 +390,36 @@ static inline int __virtio_net_hdr_to_skb(struct sk_buff *skb,return 0 ; } +/* +* Return value: +* 0 - success +* >0 - checksum was completed in software (slow path) +* <0 - failure +*/ static inline int virtio_net_hdr_to_skb (struct sk_buff *skb,const struct virtio_net_hdr *hdr,bool little_endian) { -return __virtio_net_hdr_to_skb (skb, hdr, little_endian, hdr->gso_type); + typeof(skb->transport_header) old_transport = skb->transport_header; +int ret =__virtio_net_hdr_to_skb (skb, hdr, little_endian, + hdr->gso_type); + +if (ret <0 ) +return ret; + +if (skb->ip_summed !=CHECKSUM_PARTIAL || +virtio_net_csum_partial_ok (skb, hdr->gso_type)) +return 0 ;
include/linux/virtio_net.h static inline int __virtio_net_hdr_to_skb(struct sk_buff *skb, · line 422
Quoted 10 lines
include/linux/virtio_net.h +10
-0+ +/* +* Here we do not trust the checksum metadata. +* __virtio_net_hdr_to_skb set the transport header to csum_start +* from virtio hdr, restore it to a known good value. +*/ + skb->transport_header = old_transport; + ret =virtio_net_resolve_csum_partial_mismatch (skb); + +return ret;
can return virtio_net_resolve_csum_partial_mismatch(skb)
Quoted 62 lines
include/linux/virtio_net.h +34
-1}/* This function must be called after virtio_net_hdr_from_skb(). */ @@ -282,6 +503,35 @@ static inline unsigned int virtio_l3min(bool is_ipv6)return is_ipv6 ?sizeof (struct ipv6hdr ) :sizeof (struct iphdr ); } +/* Do the tunnel offsets match the outer headers in the frame? +* The L3 header the tunnel claims must be the one the frame +* carries, with UDP following it at @outer_th. +*/ +static inline bool +virtio_net_tnl_offsets_ok (struct sk_buff *skb,bool outer_isv6, +unsigned int outer_th) +{ +int nhoff; +__be16 proto; +unsigned int thoff; +int ip_proto; + +if (!virtio_net_l3_start (skb, &nhoff, &proto)) +return false; +if (outer_isv6 != (proto ==htons (ETH_P_IPV6 ))) +return false; +if (!virtio_net_l4_start (skb, nhoff, proto, &thoff, &ip_proto)) +return false; + +return ip_proto ==IPPROTO_UDP && outer_th == thoff; +} + +/* +* Return value: +* 0 - success +* >0 - checksum was completed in software (slow path) +* <0 - failure +*/ static inline int virtio_net_hdr_tnl_to_skb(struct sk_buff *skb, const struct virtio_net_hdr_v1_hash_tunnel *vhdr,@@ -293,6 +543,7 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb,unsigned int inner_nh, outer_th, inner_th;unsigned int inner_l3min, outer_l3min;u8 gso_inner_type, gso_tunnel_type; +__be16 inner_proto;bool outer_isv6, inner_isv6;int ret;@@ -335,9 +586,12 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb,/* Let the basic parsing deal with plain GSO features. */ ret =__virtio_net_hdr_to_skb (skb, hdr, true, hdr->gso_type & ~gso_tunnel_type); -if (ret) +if (ret <0 )return ret; +if (!virtio_net_tnl_offsets_ok (skb, outer_isv6, outer_th)) +return -EINVAL ; +/* In case of USO, the inner protocol is still unknown and * `inner_isv6` is just a guess, additional parsing is needed. * The previous validation ensures that accessing an ipv4 inner
include/linux/virtio_net.h virtio_net_hdr_tnl_to_skb(struct sk_buff *skb, · line 610
Quoted 5 lines
include/linux/virtio_net.h +1
-1@@ -353,7 +607,7 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb, }skb_set_inner_protocol (skb, inner_isv6 ?htons (ETH_P_IPV6 ) : -htons (ETH_P_IP )); +htons (ETH_P_IP ));
unintentional indentation change?
Quoted 9 lines
include/linux/virtio_net.h +1
-1if (hdr->flags &VIRTIO_NET_HDR_F_UDP_TUNNEL_CSUM ) {if (!tnl_csum_negotiated)return -EINVAL ;@@ -368,7 +622,17 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb, skb->inner_mac_header = inner_nh +skb_headroom (skb); skb->transport_header = outer_th +skb_headroom (skb); skb->encapsulation =1 ; -return 0 ; +
include/linux/virtio_net.h virtio_net_hdr_tnl_to_skb(struct sk_buff *skb, · line 635
Quoted 10 lines
include/linux/virtio_net.h +10
-0+/* +* We know VIRTIO_NET_HDR_F_NEEDS_CSUM was set so __virtio_net_hdr_to_skb +* set CHECKSUM_PARTIAL. +*/ + inner_proto = inner_isv6 ?htons (ETH_P_IPV6 ) :htons (ETH_P_IP ); +if (virtio_net_csum_partial_ok_at_offset (skb, gso_inner_type, inner_nh, + inner_proto)) +return 0 ; + +return virtio_net_resolve_csum_partial_mismatch (skb);
GSO is implied when reaching this far, so can just return -EINVAL. And as only one caller of the function than remains, can avoid the helper.
Quoted 6 lines
include/linux/virtio_net.h +0
-0}/* Checksum-related fields validation for the driver */ Signature · 1 line
MST
Michael S. Tsirkin
maintainerRe: [PATCH net v3 5/8] net: resolve uncommon partial checksums at ingress
Quoted 35 lines · Willem de Bruijn
Quoted 35 lines
include/linux/virtio_net.h +35
-0+/* Walk the L3 header at @nhoff: where does its transport header +* start, and which protocol is it? Returns false if the header +* is truncated, malformed or fragmented. +*/ +static inline bool +virtio_net_l4_start (struct sk_buff *skb,unsigned int nhoff,__be16 proto, +unsigned int *thoff,int *ip_proto) +{ +if (proto ==htons (ETH_P_IP )) { +const struct iphdr *iph =virtio_net_ip4_parse (skb, nhoff, thoff); + +if (!iph ||ip_is_fragment (iph)) +return false; + *ip_proto = iph->protocol; +return true; + } + +if (proto ==htons (ETH_P_IPV6 )) { +const struct ipv6hdr *iph; +int flags =0 ; + +if (!pskb_may_pull (skb, nhoff +sizeof (*iph))) +return false; + iph = (const void *)(skb->data + nhoff); +if (iph->version !=6 ) +return false; + *thoff = nhoff +sizeof (*iph); + *ip_proto = iph->nexthdr; +if (*ip_proto !=IPPROTO_TCP && *ip_proto !=IPPROTO_UDP ) { + *thoff = nhoff; + *ip_proto =ipv6_find_hdr (skb, thoff,-1 ,NULL , + &flags); +if (*ip_proto <0 || flags &IP6_FH_F_FRAG ) +return false; + }
Quoted 8 lines
include/linux/virtio_net.h static inline int virtio_net_hdr_set_proto(struct sk_buff *skb, · line 148
Quoted 5 lines
include/linux/virtio_net.h +5
-0+return true; + } + +return false; +}for all this parsing: could we use flow dissection, like the !VIRTIO_NET_HDR_F_NEEDS_CSUM case?
Hmm I don't see how. That one trusts the caller's protocol and network header. We could parse L2 ourselves and use the dissector for L3+. But it stops at whatever it does not know, and every miss would turn into a drop or a software checksum for a packet the stack delivers fine. Our parser deliberately covers everything the stack can deliver.
Signature · 1 line
MST
Willem de Bruijn
maintainerRe: [PATCH net v3 5/8] net: resolve uncommon partial checksums at ingress
Quoted 45 lines · Michael S. Tsirkin
Quoted 35 lines · Willem de Bruijn
Quoted 35 lines
include/linux/virtio_net.h +35
-0+/* Walk the L3 header at @nhoff: where does its transport header +* start, and which protocol is it? Returns false if the header +* is truncated, malformed or fragmented. +*/ +static inline bool +virtio_net_l4_start (struct sk_buff *skb,unsigned int nhoff,__be16 proto, +unsigned int *thoff,int *ip_proto) +{ +if (proto ==htons (ETH_P_IP )) { +const struct iphdr *iph =virtio_net_ip4_parse (skb, nhoff, thoff); + +if (!iph ||ip_is_fragment (iph)) +return false; + *ip_proto = iph->protocol; +return true; + } + +if (proto ==htons (ETH_P_IPV6 )) { +const struct ipv6hdr *iph; +int flags =0 ; + +if (!pskb_may_pull (skb, nhoff +sizeof (*iph))) +return false; + iph = (const void *)(skb->data + nhoff); +if (iph->version !=6 ) +return false; + *thoff = nhoff +sizeof (*iph); + *ip_proto = iph->nexthdr; +if (*ip_proto !=IPPROTO_TCP && *ip_proto !=IPPROTO_UDP ) { + *thoff = nhoff; + *ip_proto =ipv6_find_hdr (skb, thoff,-1 ,NULL , + &flags); +if (*ip_proto <0 || flags &IP6_FH_F_FRAG ) +return false; + }Quoted 8 lines
include/linux/virtio_net.h static inline int virtio_net_hdr_set_proto(struct sk_buff *skb, · line 148
Quoted 5 lines
include/linux/virtio_net.h +5
-0+return true; + } + +return false; +}for all this parsing: could we use flow dissection, like the !VIRTIO_NET_HDR_F_NEEDS_CSUM case?
Quoted 5 lines
Hmm I don't see how. That one trusts the caller's protocol and network header. We could parse L2 ourselves and use the dissector for L3+. But it stops at whatever it does not know, and every miss would turn into a drop or a software checksum for a packet the stack delivers fine. Our parser deliberately covers everything the stack can deliver.
You're right, the protocol and network offset need to be parsed manually. After that it could be passed to the flow dissector.
Totally untested:
static inline bool virtio_net_setup_l3_input(struct sk_buff *skb, int *nhoff, __be16 *proto)
{
const struct net_device *dev = skb->dev;
/* Devices with Ethernet framing: ARPHRD_ETHER, loopback, macvlan, etc. */
if (dev->header_ops == ð_header_ops || dev->type == ARPHRD_ETHER) {
if (!pskb_may_pull(skb, ETH_HLEN))
return false;
*nhoff = ETH_HLEN;
*proto = eth_hdr(skb)->h_proto;
} else if (dev->type == ARPHRD_NONE) {
/* Raw L3 devices (e.g. tun in IFF_TUN mode) */
*nhoff = 0;
*proto = skb->protocol;
if (!*proto) {
if (!pskb_may_pull(skb, 1))
return false;
if ((skb->data[0] >> 4) == 4)
*proto = htons(ETH_P_IP);
else if ((skb->data[0] >> 4) == 6)
*proto = htons(ETH_P_IPV6);
else
return false;
}
} else {
/* Fallback for other netdevs using caller's network header */
*nhoff = skb_network_offset(skb);
if (*nhoff < 0 || (unsigned int)*nhoff >= skb->len)
return false;
*proto = skb->protocol;
}
return true;
}
static inline bool virtio_net_csum_partial_ok(struct sk_buff *skb, u8 hdr_gso_type)
{
struct flow_keys_basic keys;
__be16 orig_proto = skb->protocol;
u16 orig_net_hdr = skb->network_header;
__be16 proto;
int nhoff;
u16 csum_offset;
size_t min_thlen;
if (!virtio_net_setup_l3_input(skb, &nhoff, &proto))
return false;
skb_set_network_header(skb, nhoff);
skb->protocol = proto;
if (!skb_flow_dissect_flow_keys_basic(dev_net(skb->dev), skb, &keys,
NULL, 0, 0, 0, 0))
goto restore_fail;
/* Disallow fragments: partial checksums cannot be retained across frags */
if (keys.control.flags & FLOW_DIS_IS_FRAGMENT)
goto restore_fail;
/* Validate protocol matching */
switch (keys.basic.ip_proto) {
case IPPROTO_TCP:
if (!virtio_net_hdr_match_proto(keys.basic.n_proto, hdr_gso_type))
goto restore_fail;
min_thlen = sizeof(struct tcphdr);
csum_offset = offsetof(struct tcphdr, check);
break;
case IPPROTO_UDP:
if (!virtio_net_hdr_match_proto(keys.basic.n_proto, hdr_gso_type))
goto restore_fail;
min_thlen = sizeof(struct udphdr);
csum_offset = offsetof(struct udphdr, check);
break;
default:
goto restore_fail;
}
/* csum_start must match the dissected transport header offset */
if (skb_transport_offset(skb) != keys.control.thoff)
goto restore_fail;
/* csum_offset must point to the transport checksum field */
if (skb->csum_offset != csum_offset)
goto restore_fail;
/* Ensure the transport header is linear */
if (!pskb_may_pull(skb, keys.control.thoff + min_thlen))
goto restore_fail;
return true;
restore_fail:
skb->protocol = orig_proto;
skb->network_header = orig_net_hdr;
return false;
}Michael S. Tsirkin
maintainerRe: [PATCH net v3 5/8] net: resolve uncommon partial checksums at ingress
Quoted 146 lines · Willem de Bruijn
Quoted 45 lines · Michael S. Tsirkin
Quoted 35 lines · Willem de Bruijn
Quoted 35 lines
include/linux/virtio_net.h +35
-0+/* Walk the L3 header at @nhoff: where does its transport header +* start, and which protocol is it? Returns false if the header +* is truncated, malformed or fragmented. +*/ +static inline bool +virtio_net_l4_start (struct sk_buff *skb,unsigned int nhoff,__be16 proto, +unsigned int *thoff,int *ip_proto) +{ +if (proto ==htons (ETH_P_IP )) { +const struct iphdr *iph =virtio_net_ip4_parse (skb, nhoff, thoff); + +if (!iph ||ip_is_fragment (iph)) +return false; + *ip_proto = iph->protocol; +return true; + } + +if (proto ==htons (ETH_P_IPV6 )) { +const struct ipv6hdr *iph; +int flags =0 ; + +if (!pskb_may_pull (skb, nhoff +sizeof (*iph))) +return false; + iph = (const void *)(skb->data + nhoff); +if (iph->version !=6 ) +return false; + *thoff = nhoff +sizeof (*iph); + *ip_proto = iph->nexthdr; +if (*ip_proto !=IPPROTO_TCP && *ip_proto !=IPPROTO_UDP ) { + *thoff = nhoff; + *ip_proto =ipv6_find_hdr (skb, thoff,-1 ,NULL , + &flags); +if (*ip_proto <0 || flags &IP6_FH_F_FRAG ) +return false; + }Quoted 8 lines
include/linux/virtio_net.h static inline int virtio_net_hdr_set_proto(struct sk_buff *skb, · line 148
Quoted 5 lines
include/linux/virtio_net.h +5
-0+return true; + } + +return false; +}for all this parsing: could we use flow dissection, like the !VIRTIO_NET_HDR_F_NEEDS_CSUM case?
Quoted 5 lines
Hmm I don't see how. That one trusts the caller's protocol and network header. We could parse L2 ourselves and use the dissector for L3+. But it stops at whatever it does not know, and every miss would turn into a drop or a software checksum for a packet the stack delivers fine. Our parser deliberately covers everything the stack can deliver.
You're right, the protocol and network offset need to be parsed manually. After that it could be passed to the flow dissector.
Totally untested:
static inline bool virtio_net_setup_l3_input(struct sk_buff *skb, int *nhoff, __be16 *proto) { const struct net_device *dev = skb->dev; /* Devices with Ethernet framing: ARPHRD_ETHER, loopback, macvlan, etc. */ if (dev->header_ops == ð_header_ops || dev->type == ARPHRD_ETHER) { if (!pskb_may_pull(skb, ETH_HLEN)) return false; *nhoff = ETH_HLEN; *proto = eth_hdr(skb)->h_proto; } else if (dev->type == ARPHRD_NONE) { /* Raw L3 devices (e.g. tun in IFF_TUN mode) */ *nhoff = 0; *proto = skb->protocol; if (!*proto) { if (!pskb_may_pull(skb, 1)) return false; if ((skb->data[0] >> 4) == 4) *proto = htons(ETH_P_IP); else if ((skb->data[0] >> 4) == 6) *proto = htons(ETH_P_IPV6); else return false; } } else { /* Fallback for other netdevs using caller's network header */ *nhoff = skb_network_offset(skb); if (*nhoff < 0 || (unsigned int)*nhoff >= skb->len) return false; *proto = skb->protocol; } return true; } static inline bool virtio_net_csum_partial_ok(struct sk_buff *skb, u8 hdr_gso_type) { struct flow_keys_basic keys; __be16 orig_proto = skb->protocol; u16 orig_net_hdr = skb->network_header; __be16 proto; int nhoff; u16 csum_offset; size_t min_thlen; if (!virtio_net_setup_l3_input(skb, &nhoff, &proto)) return false; skb_set_network_header(skb, nhoff); skb->protocol = proto; if (!skb_flow_dissect_flow_keys_basic(dev_net(skb->dev), skb, &keys, NULL, 0, 0, 0, 0)) goto restore_fail; /* Disallow fragments: partial checksums cannot be retained across frags */ if (keys.control.flags & FLOW_DIS_IS_FRAGMENT) goto restore_fail; /* Validate protocol matching */ switch (keys.basic.ip_proto) { case IPPROTO_TCP: if (!virtio_net_hdr_match_proto(keys.basic.n_proto, hdr_gso_type)) goto restore_fail; min_thlen = sizeof(struct tcphdr); csum_offset = offsetof(struct tcphdr, check); break; case IPPROTO_UDP: if (!virtio_net_hdr_match_proto(keys.basic.n_proto, hdr_gso_type)) goto restore_fail; min_thlen = sizeof(struct udphdr); csum_offset = offsetof(struct udphdr, check); break; default: goto restore_fail; } /* csum_start must match the dissected transport header offset */ if (skb_transport_offset(skb) != keys.control.thoff) goto restore_fail; /* csum_offset must point to the transport checksum field */ if (skb->csum_offset != csum_offset) goto restore_fail; /* Ensure the transport header is linear */ if (!pskb_may_pull(skb, keys.control.thoff + min_thlen)) goto restore_fail;
Quoted 7 lines
return true;
restore_fail:
skb->protocol = orig_proto;
skb->network_header = orig_net_hdr;
return false;
}Hmm, an interesting trick, but I don't see how this works for tunnels? We need to dissect at inner_nh, and on success we can not leave network_header there - the packet continues to outer IP processing. I fear making all this work will be more code than the parsing it replaces.
Signature · 1 line
MST
Michael S. Tsirkin
maintainerRe: [PATCH net v3 5/8] net: resolve uncommon partial checksums at ingress
In reply to Willem de BruijnQuoted 46 lines · Willem de Bruijn
Quoted 45 lines · Michael S. Tsirkin
Quoted 43 lines · Willem de Bruijn
Quoted 30 lines
include/linux/virtio_net.h +30
-0+/* Walk the L3 header at @nhoff: where does its transport header +* start, and which protocol is it? Returns false if the header +* is truncated, malformed or fragmented. +*/ +static inline bool +virtio_net_l4_start (struct sk_buff *skb,unsigned int nhoff,__be16 proto, +unsigned int *thoff,int *ip_proto) +{ +if (proto ==htons (ETH_P_IP )) { +const struct iphdr *iph =virtio_net_ip4_parse (skb, nhoff, thoff); + +if (!iph ||ip_is_fragment (iph)) +return false; + *ip_proto = iph->protocol; +return true; + } + +if (proto ==htons (ETH_P_IPV6 )) { +const struct ipv6hdr *iph; +int flags =0 ; + +if (!pskb_may_pull (skb, nhoff +sizeof (*iph))) +return false; + iph = (const void *)(skb->data + nhoff); +if (iph->version !=6 ) +return false; + *thoff = nhoff +sizeof (*iph); + *ip_proto = iph->nexthdr; +if (*ip_proto !=IPPROTO_TCP && *ip_proto !=IPPROTO_UDP ) { + *thoff = nhoff;include/linux/virtio_net.h static inline int virtio_net_hdr_set_proto(struct sk_buff *skb, · line 172
Quoted 10 lines
include/linux/virtio_net.h +10
-0+ *ip_proto =ipv6_find_hdr (skb, thoff,-1 ,NULL , + &flags); +if (*ip_proto <0 || flags &IP6_FH_F_FRAG ) +return false; + } +return true; + } + +return false; +}for all this parsing: could we use flow dissection, like the !VIRTIO_NET_HDR_F_NEEDS_CSUM case?
Quoted 8 lines
Quoted 5 lines
Hmm I don't see how. That one trusts the caller's protocol and network header. We could parse L2 ourselves and use the dissector for L3+. But it stops at whatever it does not know, and every miss would turn into a drop or a software checksum for a packet the stack delivers fine. Our parser deliberately covers everything the stack can deliver.
You're right, the protocol and network offset need to be parsed manually. After that it could be passed to the flow dissector.
I posted v4 with the custom parser, since i had it tested. Let's do cleanups on top? I spent too much time on this, to be frank.
Signature · 1 line
MST
Willem de Bruijn
maintainerRe: [PATCH net v3 5/8] net: resolve uncommon partial checksums at ingress
Quoted 53 lines · Michael S. Tsirkin
Quoted 52 lines · Willem de Bruijn
Quoted 50 lines · Michael S. Tsirkin
Quoted 35 lines · Willem de Bruijn
Quoted 35 lines
include/linux/virtio_net.h +35
-0+/* Walk the L3 header at @nhoff: where does its transport header +* start, and which protocol is it? Returns false if the header +* is truncated, malformed or fragmented. +*/ +static inline bool +virtio_net_l4_start (struct sk_buff *skb,unsigned int nhoff,__be16 proto, +unsigned int *thoff,int *ip_proto) +{ +if (proto ==htons (ETH_P_IP )) { +const struct iphdr *iph =virtio_net_ip4_parse (skb, nhoff, thoff); + +if (!iph ||ip_is_fragment (iph)) +return false; + *ip_proto = iph->protocol; +return true; + } + +if (proto ==htons (ETH_P_IPV6 )) { +const struct ipv6hdr *iph; +int flags =0 ; + +if (!pskb_may_pull (skb, nhoff +sizeof (*iph))) +return false; + iph = (const void *)(skb->data + nhoff); +if (iph->version !=6 ) +return false; + *thoff = nhoff +sizeof (*iph); + *ip_proto = iph->nexthdr; +if (*ip_proto !=IPPROTO_TCP && *ip_proto !=IPPROTO_UDP ) { + *thoff = nhoff; + *ip_proto =ipv6_find_hdr (skb, thoff,-1 ,NULL , + &flags); +if (*ip_proto <0 || flags &IP6_FH_F_FRAG ) +return false; + }Quoted 8 lines
include/linux/virtio_net.h static inline int virtio_net_hdr_set_proto(struct sk_buff *skb, · line 148
Quoted 5 lines
include/linux/virtio_net.h +5
-0+return true; + } + +return false; +}for all this parsing: could we use flow dissection, like the !VIRTIO_NET_HDR_F_NEEDS_CSUM case?
Hmm I don't see how. That one trusts the caller's protocol and network header. We could parse L2 ourselves and use the dissector for L3+. But it stops at whatever it does not know, and every miss would turn into a drop or a software checksum for a packet the stack delivers fine. Our parser deliberately covers everything the stack can deliver.
Quoted 5 lines
Quoted 2 lines
You're right, the protocol and network offset need to be parsed manually. After that it could be passed to the flow dissector.
I posted v4 with the custom parser, since i had it tested. Let's do cleanups on top? I spent too much time on this, to be frank.
Yes, as a matter of fact, on investigating this approach in more detail, I agree that flow dissection is a worse choice.
Quoted 5 lines
Hmm, an interesting trick, but I don't see how this works for tunnels? We need to dissect at
inner_nh, and on success we can not leavenetwork_headerthere - the packet continues to outer IP processing. I fear making all this work will be more code than the parsing it replaces.
On the upsides, quoting an LLM:
"
Using the flow dissector replaces all of virtio_net_ip4_parse(), virtio_net_l3_start(), virtio_net_l4_start(), and virtio_net_csum_partial_ok_at_offset() with two compact functions:
Function | Lines of Code | Description ------------------------------------------|------------------------------------------|----------------------------------------------------- virtio_net_setup_l3_input() | ~28 lines | Anchors L2 framing (eth_header_ops vs raw L3) virtio_net_csum_partial_ok() | ~42 lines | Runs flow dissector and validates thoff/csum_offset virtio_net_tnl_offsets_ok() | ~8 lines | Reuses outer dissection keys for tunnel validation Total dedicated parsing code | ~78 lines |
──────
### Summary of Differences
• Net Code Reduction: ~103 fewer lines of C code added to virtio_net.h (~57% reduction in new parsing code).
• Eliminated Series Dependencies: Patch 7/8 (net: add ip4_parse()) is completely eliminated, reducing the patch series by an entire patch.
• Deduplication: The existing non-checksum GSO validation in __virtio_net_hdr_to_skb() (lines 120–146, 27 lines) already calls skb_flow_dissect_flow_keys_basic(); sharing virtio_net_setup_l3_input() consolidates both paths onto a single ingress dissection mechanism.
"
I particularly like that we use the same for NEEDS_CSUM and !NEEDS_CSUM. Also the lack of code duplication.
But, the goal of this patch is to minimize overhead in the hot path of standard TCP/IP and UDP code. And flow dissection is significantly more expensive that open coding those specific checks.
Michael S. Tsirkin
maintainerPATCH v3 6/8 net: add a sysctl to opt out of virtio net hdr checksum validation
In reply to Michael S. TsirkinThe ingress validation as implemented by the previous patch is stricter than what it replaced: GSO
packets whose checksum metadata matches no transport header are dropped, and non-GSO ones get their checksum completed in place. Such packets used to pass with the metadata unvalidated, and with no overhead.
I judge that this should not affect any deployments, but there is still some risk that it does. In case a deployment depends on them anyway, provide a per-netns switch to get the old behavior back:
echo 0 > /proc/sys/net/core/virtio_net_hdr_csum_check
The default is validating.
--- Documentation/admin-guide/sysctl/net.rst | 19 ++++++++ include/linux/virtio_net.h | 23 +++++++++ include/net/netns/core.h | 1 + net/core/dev.c | 6 +++ net/core/net_namespace.c | 1 + net/core/sysctl_net_core.c | 60 ++++++++++++++++++++++++ 6 files changed, 110 insertions(+)
Documentation/admin-guide/sysctl/net.rst +19 -0
@@ -246,6 +246,25 @@ processes are dropped unless socket option SOF_TIMESTAMPING_OPT_TSONLY is set. Default: 1 (on) +virtio_net_hdr_csum_check +------------------------- + +When processing packets with a virtio net header (through tun, tap, a +packet socket or a virtio network device) that request checksum offload +(VIRTIO_NET_HDR_F_NEEDS_CSUM), validate the packet: complete the checksum +in software, or drop the packet where the header does not match it. + +When disabled, the header is trusted not to corrupt the packet, and the +checksum is offloaded to hardware (or not verified if it never reaches +hardware). This is only safe if no protocol is processing the packets. + +The setting is per network namespace, but only writable by the host +root: root inside a network namespace created by an unprivileged user +cannot disable the validation. + +Default: 1 (on) + + wmem_default ------------
include/linux/virtio_net.h +23 -0
@@ -5,6 +5,7 @@#include <linux/if_vlan.h> #include <linux/ip.h> #include <linux/ipv6.h> +#include <linux/jump_label.h> #include <linux/netdevice.h> #include <linux/udp.h> #include <net/gso.h>
@@ -78,6 +79,19 @@ static inline struct iphdr *virtio_net_ip4_parse(struct sk_buff *skb, *thoff = nhoff + iph->ihl *sizeof (u32);return iph; } +/* Defined in net/core/dev.c, enabled while any network namespace +* has net.core.virtio_net_hdr_csum_check set to 0. +*/ +DECLARE_STATIC_KEY_FALSE (virtio_net_hdr_csum_check_disabled_key); + +/* Failsafe: a namespace can disable the check with +* net.core.virtio_net_hdr_csum_check. +*/ +static inline bool virtio_net_hdr_csum_check_disabled (struct sk_buff *skb) +{ +return static_branch_unlikely (&virtio_net_hdr_csum_check_disabled_key) && + !READ_ONCE (dev_net (skb->dev)->core.sysctl_virtio_net_hdr_csum_check); +}/* Where does the L3 header start, and what protocol is it? * Handles Ethernet and VLAN headers; other devices use the
@@ -179,6 +193,12 @@ virtio_net_csum_partial_ok_at_offset(struct sk_buff *skb, u8 hdr_gso_type,unsigned int thoff;int ip_proto; +/* Failsafe: a namespace opted out of the validation, +* trust the checksum metadata as before. +*/ +if (virtio_net_hdr_csum_check_disabled (skb)) +return true; +if (!virtio_net_l4_start (skb, nhoff, proto, &thoff, &ip_proto))return false;
@@ -516,6 +536,9 @@ virtio_net_tnl_offsets_ok(struct sk_buff *skb, bool outer_isv6,unsigned int thoff;int ip_proto; +if (virtio_net_hdr_csum_check_disabled (skb)) +return true; +if (!virtio_net_l3_start (skb, &nhoff, &proto))return false;if (outer_isv6 != (proto ==htons (ETH_P_IPV6 )))
include/net/netns/core.h +1 -0
@@ -18,6 +18,7 @@ struct netns_core {u8 sysctl_txrehash;u8 sysctl_tstamp_allow_data;u8 sysctl_bypass_prot_mem; +u8 sysctl_virtio_net_hdr_csum_check;#ifdef CONFIG_PROC_FS struct prot_inuse __percpu *prot_inuse;
net/core/dev.c +6 -0
@@ -2389,6 +2389,12 @@ EXPORT_SYMBOL(tcf_sw_enabled_key);DEFINE_STATIC_KEY_FALSE (netstamp_needed_key);EXPORT_SYMBOL (netstamp_needed_key); + +/* Enabled while any network namespace has net.core.virtio_net_hdr_csum_check +* set to 0. +*/ +DEFINE_STATIC_KEY_FALSE (virtio_net_hdr_csum_check_disabled_key); +EXPORT_SYMBOL (virtio_net_hdr_csum_check_disabled_key);#ifdef CONFIG_JUMP_LABEL static atomic_t netstamp_needed_deferred;static atomic_t netstamp_wanted;
net/core/net_namespace.c +1 -0
@@ -396,6 +396,7 @@ static __net_init void preinit_net_sysctl(struct net *net) net->core.sysctl_optmem_max =128 *1024 ; net->core.sysctl_txrehash =SOCK_TXREHASH_ENABLED ; net->core.sysctl_tstamp_allow_data =1 ; + net->core.sysctl_virtio_net_hdr_csum_check =1 ; net->core.sysctl_txq_reselection =msecs_to_jiffies (1000 ); }
net/core/sysctl_net_core.c +60 -0
84 lines not on this page · raw message
Signature · 1 line
MST
Re: [PATCH net v3 6/8] net: add a sysctl to opt out of virtio net hdr checksum validation
Quoted 167 lines · Michael S. Tsirkin
The ingress validation as implemented by the previous patch is stricter than what it replaced: GSO
packets whose checksum metadata matches no transport header are dropped, and non-GSO ones get their checksum completed in place. Such packets used to pass with the metadata unvalidated, and with no overhead.I judge that this should not affect any deployments, but there is still some risk that it does. In case a deployment depends on them anyway, provide a per-netns switch to get the old behavior back:
echo 0 >
/proc/sys/net/core/virtio_net_hdr_csum_checkThe default is validating.
Assisted-by LLMSigned-off-by Michael S. Tsirkin <[email protected]>--- Documentation/admin-guide/sysctl/net.rst | 19 ++++++++ include/linux/virtio_net.h | 23 +++++++++ include/net/netns/core.h | 1 + net/core/dev.c | 6 +++ net/core/net_namespace.c | 1 + net/core/sysctl_net_core.c | 60 ++++++++++++++++++++++++ 6 files changed, 110 insertions(+)Documentation/admin-guide/sysctl/net.rst +19
-0@@ -246,6 +246,25 @@ processes are dropped unless socket option SOF_TIMESTAMPING_OPT_TSONLY is set. Default: 1 (on) +virtio_net_hdr_csum_check +------------------------- + +When processing packets with a virtio net header (through tun, tap, a +packet socket or a virtio network device) that request checksum offload +(VIRTIO_NET_HDR_F_NEEDS_CSUM), validate the packet: complete the checksum +in software, or drop the packet where the header does not match it. + +When disabled, the header is trusted not to corrupt the packet, and the +checksum is offloaded to hardware (or not verified if it never reaches +hardware). This is only safe if no protocol is processing the packets. + +The setting is per network namespace, but only writable by the host +root: root inside a network namespace created by an unprivileged user +cannot disable the validation. + +Default: 1 (on) + + wmem_default ------------include/linux/virtio_net.h +23
-0@@ -5,6 +5,7 @@#include <linux/if_vlan.h> #include <linux/ip.h> #include <linux/ipv6.h> +#include <linux/jump_label.h> #include <linux/netdevice.h> #include <linux/udp.h> #include <net/gso.h> @@ -78,6 +79,19 @@ static inline struct iphdr *virtio_net_ip4_parse(struct sk_buff *skb, *thoff = nhoff + iph->ihl *sizeof (u32);return iph; } +/* Defined in net/core/dev.c, enabled while any network namespace +* has net.core.virtio_net_hdr_csum_check set to 0. +*/ +DECLARE_STATIC_KEY_FALSE (virtio_net_hdr_csum_check_disabled_key); + +/* Failsafe: a namespace can disable the check with +* net.core.virtio_net_hdr_csum_check. +*/ +static inline bool virtio_net_hdr_csum_check_disabled (struct sk_buff *skb) +{ +return static_branch_unlikely (&virtio_net_hdr_csum_check_disabled_key) && + !READ_ONCE (dev_net (skb->dev)->core.sysctl_virtio_net_hdr_csum_check); +}/* Where does the L3 header start, and what protocol is it? * Handles Ethernet and VLAN headers; other devices use the @@ -179,6 +193,12 @@ virtio_net_csum_partial_ok_at_offset(struct sk_buff *skb, u8 hdr_gso_type,unsigned int thoff;int ip_proto; +/* Failsafe: a namespace opted out of the validation, +* trust the checksum metadata as before. +*/ +if (virtio_net_hdr_csum_check_disabled (skb)) +return true; +if (!virtio_net_l4_start (skb, nhoff, proto, &thoff, &ip_proto))return false;@@ -516,6 +536,9 @@ virtio_net_tnl_offsets_ok(struct sk_buff *skb, bool outer_isv6,unsigned int thoff;int ip_proto; +if (virtio_net_hdr_csum_check_disabled (skb)) +return true; +if (!virtio_net_l3_start (skb, &nhoff, &proto))return false;if (outer_isv6 != (proto ==htons (ETH_P_IPV6 )))include/net/netns/core.h +1
-0@@ -18,6 +18,7 @@ struct netns_core {u8 sysctl_txrehash;u8 sysctl_tstamp_allow_data;u8 sysctl_bypass_prot_mem; +u8 sysctl_virtio_net_hdr_csum_check;#ifdef CONFIG_PROC_FS struct prot_inuse __percpu *prot_inuse;net/core/dev.c +6
-0@@ -2389,6 +2389,12 @@ EXPORT_SYMBOL(tcf_sw_enabled_key);DEFINE_STATIC_KEY_FALSE (netstamp_needed_key);EXPORT_SYMBOL (netstamp_needed_key); + +/* Enabled while any network namespace has net.core.virtio_net_hdr_csum_check +* set to 0. +*/ +DEFINE_STATIC_KEY_FALSE (virtio_net_hdr_csum_check_disabled_key); +EXPORT_SYMBOL (virtio_net_hdr_csum_check_disabled_key);#ifdef CONFIG_JUMP_LABEL static atomic_t netstamp_needed_deferred;static atomic_t netstamp_wanted;net/core/net_namespace.c +1
-0@@ -396,6 +396,7 @@ static __net_init void preinit_net_sysctl(struct net *net) net->core.sysctl_optmem_max =128 *1024 ; net->core.sysctl_txrehash =SOCK_TXREHASH_ENABLED ; net->core.sysctl_tstamp_allow_data =1 ; + net->core.sysctl_virtio_net_hdr_csum_check =1 ; net->core.sysctl_txq_reselection =msecs_to_jiffies (1000 ); }net/core/sysctl_net_core.c +1
-0@@ -18,6 +18,7 @@#include <linux/slab.h> #include <linux/sched/isolation.h> #include <linux/hex.h> +#include <linux/jump_label.h> #include <net/ip.h> #include <net/sock.h> @@ -678,6 +679,54 @@ static struct ctl_table net_core_table[] = { }, };
net/core/sysctl_net_core.c static struct ctl_table net_core_table[] = { · line 690
Quoted 10 lines
net/core/sysctl_net_core.c +9
-0+/* Defined in net/core/dev.c, declared for the gate in +* include/linux/virtio_net.h. +*/ +DECLARE_STATIC_KEY_FALSE (virtio_net_hdr_csum_check_disabled_key); + +/* +* Protects changes to virtio_net_hdr_csum_check_disabled_key and each +* net->core.sysctl_virtio_net_hdr_csum_check. Readers don't use a lock. +*/
nit: OCD doesn't like mixing old netdev comments with vanilla style... one VVV as well.
Quoted 9 lines
net/core/sysctl_net_core.c +8
-0+static DEFINE_MUTEX (virtio_net_hdr_csum_check_mutex); + +static int virtio_net_hdr_csum_check_sysctl (const struct ctl_table *table, +int write,void *buffer, +size_t *lenp,loff_t *ppos) +{ +struct net *net = (struct net *)table->data; +/* checkpatch warns but this can't be const: tmp.data is set below */
nit: Redundant comment...
net/core/sysctl_net_core.c static struct ctl_table net_core_table[] = { · line 699
Quoted 1 line
net/core/sysctl_net_core.c +1
-0+struct ctl_table tmp = *table;
...and here, can't we instead get rid of tmp, and do something like:
| u8 *value = table->data;
| u8 old, new;Quoted 9 lines
net/core/sysctl_net_core.c +9
-0+u8 old; +int ret; + +/* Root in a user namespace must not be able to weaken the +* validation for the network namespace it controls. +*/ +if (write && !capable (CAP_NET_ADMIN )) +return -EPERM ; +
net/core/sysctl_net_core.c static struct ctl_table net_core_table[] = { · line 718
Quoted 10 lines
net/core/sysctl_net_core.c +10
-0+/* +* Make sure virtio_net_hdr_csum_check_disabled_key counts zero values of +* net->core.sysctl_virtio_net_hdr_csum_check. +*/ +mutex_lock (&virtio_net_hdr_csum_check_mutex); + + old = net->core.sysctl_virtio_net_hdr_csum_check; + tmp.data = &net->core.sysctl_virtio_net_hdr_csum_check; + ret =proc_dou8vec_minmax (&tmp, write, buffer, lenp, ppos); +if (write && !ret && old != net->core.sysctl_virtio_net_hdr_csum_check) {
and then:
| old = READ_ONCE(*value);
| ret = proc_dou8vec_minmax(table, write, buffer, lenp, ppos);
| new = READ_ONCE(*value);
|
| if (write && !ret && old != new) {at least that's easier to read for me. (No blocker!)
Michael S. Tsirkin
maintainerRe: [PATCH net v3 6/8] net: add a sysctl to opt out of virtio net hdr checksum validation
Quoted 228 lines · Björn Töpel
Quoted 167 lines · Michael S. Tsirkin
The ingress validation as implemented by the previous patch is stricter than what it replaced: GSO
packets whose checksum metadata matches no transport header are dropped, and non-GSO ones get their checksum completed in place. Such packets used to pass with the metadata unvalidated, and with no overhead.I judge that this should not affect any deployments, but there is still some risk that it does. In case a deployment depends on them anyway, provide a per-netns switch to get the old behavior back:
echo 0 >
/proc/sys/net/core/virtio_net_hdr_csum_checkThe default is validating.
Assisted-by LLMSigned-off-by Michael S. Tsirkin <[email protected]>--- Documentation/admin-guide/sysctl/net.rst | 19 ++++++++ include/linux/virtio_net.h | 23 +++++++++ include/net/netns/core.h | 1 + net/core/dev.c | 6 +++ net/core/net_namespace.c | 1 + net/core/sysctl_net_core.c | 60 ++++++++++++++++++++++++ 6 files changed, 110 insertions(+)Documentation/admin-guide/sysctl/net.rst +19
-0@@ -246,6 +246,25 @@ processes are dropped unless socket option SOF_TIMESTAMPING_OPT_TSONLY is set. Default: 1 (on) +virtio_net_hdr_csum_check +------------------------- + +When processing packets with a virtio net header (through tun, tap, a +packet socket or a virtio network device) that request checksum offload +(VIRTIO_NET_HDR_F_NEEDS_CSUM), validate the packet: complete the checksum +in software, or drop the packet where the header does not match it. + +When disabled, the header is trusted not to corrupt the packet, and the +checksum is offloaded to hardware (or not verified if it never reaches +hardware). This is only safe if no protocol is processing the packets. + +The setting is per network namespace, but only writable by the host +root: root inside a network namespace created by an unprivileged user +cannot disable the validation. + +Default: 1 (on) + + wmem_default ------------include/linux/virtio_net.h +23
-0@@ -5,6 +5,7 @@#include <linux/if_vlan.h> #include <linux/ip.h> #include <linux/ipv6.h> +#include <linux/jump_label.h> #include <linux/netdevice.h> #include <linux/udp.h> #include <net/gso.h> @@ -78,6 +79,19 @@ static inline struct iphdr *virtio_net_ip4_parse(struct sk_buff *skb, *thoff = nhoff + iph->ihl *sizeof (u32);return iph; } +/* Defined in net/core/dev.c, enabled while any network namespace +* has net.core.virtio_net_hdr_csum_check set to 0. +*/ +DECLARE_STATIC_KEY_FALSE (virtio_net_hdr_csum_check_disabled_key); + +/* Failsafe: a namespace can disable the check with +* net.core.virtio_net_hdr_csum_check. +*/ +static inline bool virtio_net_hdr_csum_check_disabled (struct sk_buff *skb) +{ +return static_branch_unlikely (&virtio_net_hdr_csum_check_disabled_key) && + !READ_ONCE (dev_net (skb->dev)->core.sysctl_virtio_net_hdr_csum_check); +}/* Where does the L3 header start, and what protocol is it? * Handles Ethernet and VLAN headers; other devices use the @@ -179,6 +193,12 @@ virtio_net_csum_partial_ok_at_offset(struct sk_buff *skb, u8 hdr_gso_type,unsigned int thoff;int ip_proto; +/* Failsafe: a namespace opted out of the validation, +* trust the checksum metadata as before. +*/ +if (virtio_net_hdr_csum_check_disabled (skb)) +return true; +if (!virtio_net_l4_start (skb, nhoff, proto, &thoff, &ip_proto))return false;@@ -516,6 +536,9 @@ virtio_net_tnl_offsets_ok(struct sk_buff *skb, bool outer_isv6,unsigned int thoff;int ip_proto; +if (virtio_net_hdr_csum_check_disabled (skb)) +return true; +if (!virtio_net_l3_start (skb, &nhoff, &proto))return false;if (outer_isv6 != (proto ==htons (ETH_P_IPV6 )))include/net/netns/core.h +1
-0@@ -18,6 +18,7 @@ struct netns_core {u8 sysctl_txrehash;u8 sysctl_tstamp_allow_data;u8 sysctl_bypass_prot_mem; +u8 sysctl_virtio_net_hdr_csum_check;#ifdef CONFIG_PROC_FS struct prot_inuse __percpu *prot_inuse;net/core/dev.c +6
-0@@ -2389,6 +2389,12 @@ EXPORT_SYMBOL(tcf_sw_enabled_key);DEFINE_STATIC_KEY_FALSE (netstamp_needed_key);EXPORT_SYMBOL (netstamp_needed_key); + +/* Enabled while any network namespace has net.core.virtio_net_hdr_csum_check +* set to 0. +*/ +DEFINE_STATIC_KEY_FALSE (virtio_net_hdr_csum_check_disabled_key); +EXPORT_SYMBOL (virtio_net_hdr_csum_check_disabled_key);#ifdef CONFIG_JUMP_LABEL static atomic_t netstamp_needed_deferred;static atomic_t netstamp_wanted;net/core/net_namespace.c +1
-0@@ -396,6 +396,7 @@ static __net_init void preinit_net_sysctl(struct net *net) net->core.sysctl_optmem_max =128 *1024 ; net->core.sysctl_txrehash =SOCK_TXREHASH_ENABLED ; net->core.sysctl_tstamp_allow_data =1 ; + net->core.sysctl_virtio_net_hdr_csum_check =1 ; net->core.sysctl_txq_reselection =msecs_to_jiffies (1000 ); }net/core/sysctl_net_core.c +1
-0@@ -18,6 +18,7 @@#include <linux/slab.h> #include <linux/sched/isolation.h> #include <linux/hex.h> +#include <linux/jump_label.h> #include <net/ip.h> #include <net/sock.h> @@ -678,6 +679,54 @@ static struct ctl_table net_core_table[] = { }, };net/core/sysctl_net_core.c static struct ctl_table net_core_table[] = { · line 690
Quoted 10 lines
net/core/sysctl_net_core.c +9
-0+/* Defined in net/core/dev.c, declared for the gate in +* include/linux/virtio_net.h. +*/ +DECLARE_STATIC_KEY_FALSE (virtio_net_hdr_csum_check_disabled_key); + +/* +* Protects changes to virtio_net_hdr_csum_check_disabled_key and each +* net->core.sysctl_virtio_net_hdr_csum_check. Readers don't use a lock. +*/ nit: OCD doesn't like mixing old netdev comments with vanilla style... one VVV as well.
Quoted 9 lines
net/core/sysctl_net_core.c +8
-0+static DEFINE_MUTEX (virtio_net_hdr_csum_check_mutex); + +static int virtio_net_hdr_csum_check_sysctl (const struct ctl_table *table, +int write,void *buffer, +size_t *lenp,loff_t *ppos) +{ +struct net *net = (struct net *)table->data; +/* checkpatch warns but this can't be const: tmp.data is set below */
nit: Redundant comment...
net/core/sysctl_net_core.c static struct ctl_table net_core_table[] = { · line 699
Quoted 1 line
net/core/sysctl_net_core.c +1
-0+struct ctl_table tmp = *table;...and here, can't we instead get rid of tmp, and do something like:
| u8 *value = table->data; | u8 old, new;Quoted 9 lines
net/core/sysctl_net_core.c +9
-0+u8 old; +int ret; + +/* Root in a user namespace must not be able to weaken the +* validation for the network namespace it controls. +*/ +if (write && !capable (CAP_NET_ADMIN )) +return -EPERM ; +net/core/sysctl_net_core.c static struct ctl_table net_core_table[] = { · line 718
Quoted 10 lines
net/core/sysctl_net_core.c +10
-0+/* +* Make sure virtio_net_hdr_csum_check_disabled_key counts zero values of +* net->core.sysctl_virtio_net_hdr_csum_check. +*/ +mutex_lock (&virtio_net_hdr_csum_check_mutex); + + old = net->core.sysctl_virtio_net_hdr_csum_check; + tmp.data = &net->core.sysctl_virtio_net_hdr_csum_check; + ret =proc_dou8vec_minmax (&tmp, write, buffer, lenp, ppos); +if (write && !ret && old != net->core.sysctl_virtio_net_hdr_csum_check) {and then:
| old = READ_ONCE(*value); | ret = proc_dou8vec_minmax(table, write, buffer, lenp, ppos); | new = READ_ONCE(*value); | | if (write && !ret && old != new) {
Quoted 4 lines
at least that's easier to read for me. (No blocker!)
Reviewed-by Björn Töpel <[email protected]>
Thanks, I'm addressing sashiko review points, will address this in v4.
[email protected]
AI reviewRe: [PATCH net v3 6/8] net: add a sysctl to opt out of virtio net hdr checksum validation
In reply to Michael S. TsirkinThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] net: virtio_net_csum_partial_ok() fails to bypass virtio_net_l3_start() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] net: virtio_net_csum_partial_ok() fails to bypass virtio_net_l3_start() --
commit 559fb10ce506a1c7276bf12adfdeed16a12a4f0c
Author: Michael S. Tsirkin <[email protected]>
net: add a sysctl to opt out of virtio net hdr checksum validation
This commit adds a per-netns sysctl net.core.virtio_net_hdr_csum_check guarded by a static key to allow opting out of ingress virtio_net_hdr checksum metadata validation.
Quoted 4 lines
include/linux/virtio_net.h +0
-0
[ ... ]
Quoted 14 lines
include/linux/virtio_net.h +6
-0@@ -179,6 +193,12 @@ virtio_net_csum_partial_ok_at_offset(struct sk_buff *skb, u8 hdr_gso_type,unsigned int thoff;int ip_proto; +/* Failsafe: a namespace opted out of the validation, +* trust the checksum metadata as before. +*/ +if (virtio_net_hdr_csum_check_disabled (skb)) +return true; +if (!virtio_net_l4_start (skb, nhoff, proto, &thoff, &ip_proto))return false;
include/linux/virtio_net.h virtio_net_tnl_offsets_ok(struct sk_buff *skb, bool outer_isv6, · line 544
Quoted 9 lines
include/linux/virtio_net.h +3
-0@@ -516,6 +536,9 @@ virtio_net_tnl_offsets_ok(struct sk_buff *skb, bool outer_isv6,unsigned int thoff;int ip_proto; +if (virtio_net_hdr_csum_check_disabled (skb)) +return true; +if (!virtio_net_l3_start (skb, &nhoff, &proto))return false;if (outer_isv6 != (proto ==htons (ETH_P_IPV6 )))
[Severity: Medium]
In virtio_net_tnl_offsets_ok(), virtio_net_hdr_csum_check_disabled(skb) is checked before calling virtio_net_l3_start(). However, in the non-tunnel path, virtio_net_hdr_to_skb() calls virtio_net_csum_partial_ok(), which calls virtio_net_l3_start() before reaching the check in virtio_net_csum_partial_ok_at_offset():
include/linux/virtio_net.h:virtio_net_csum_partial_ok() {
int nhoff;
__be16 proto;
if (!virtio_net_l3_start(skb, &nhoff, &proto))
return false;
return virtio_net_csum_partial_ok_at_offset(skb, hdr_gso_type, nhoff,
proto);
}When net.core.virtio_net_hdr_csum_check is set to 0 and a packet with VIRTIO_NET_HDR_F_NEEDS_CSUM enters virtio_net_hdr_to_skb(), virtio_net_l3_start() still runs pskb_may_pull() and __vlan_get_protocol() to compute nhoff and proto, which virtio_net_csum_partial_ok_at_offset() then immediately discards.
Furthermore, if virtio_net_l3_start() returns false (for example, when __vlan_get_protocol() returns 0 on VLAN tags exceeding VLAN_MAX_DEPTH, or on a non-ARPHRD_ETHER device where skb_network_offset(skb) is unset or out of bounds), virtio_net_csum_partial_ok() returns false without consulting virtio_net_hdr_csum_check_disabled(skb). virtio_net_hdr_to_skb() then falls through to virtio_net_resolve_csum_partial_mismatch(), dropping GSO packets or completing non-GSO checksums in software despite the sysctl opt-out.
Should virtio_net_csum_partial_ok() check virtio_net_hdr_csum_check_disabled(skb) before calling virtio_net_l3_start()?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Willem de Bruijn
maintainerRe: [PATCH net v3 6/8] net: add a sysctl to opt out of virtio net hdr checksum validation
In reply to Michael S. TsirkinQuoted 31 lines · Michael S. Tsirkin
The ingress validation as implemented by the previous patch is stricter than what it replaced: GSO
packets whose checksum metadata matches no transport header are dropped, and non-GSO ones get their checksum completed in place. Such packets used to pass with the metadata unvalidated, and with no overhead.I judge that this should not affect any deployments, but there is still some risk that it does. In case a deployment depends on them anyway, provide a per-netns switch to get the old behavior back:
echo 0 >
/proc/sys/net/core/virtio_net_hdr_csum_checkThe default is validating.
Assisted-by LLMSigned-off-by Michael S. Tsirkin <[email protected]>--- Documentation/admin-guide/sysctl/net.rst | 19 ++++++++ include/linux/virtio_net.h | 23 +++++++++ include/net/netns/core.h | 1 + net/core/dev.c | 6 +++ net/core/net_namespace.c | 1 + net/core/sysctl_net_core.c | 60 ++++++++++++++++++++++++ 6 files changed, 110 insertions(+)Documentation/admin-guide/sysctl/net.rst +0
-0
Documentation/admin-guide/sysctl/net.rst processes are dropped unless socket option SOF_TIMESTAMPING_OPT_TSONLY is set. · line 250
Quoted 5 lines
Documentation/admin-guide/sysctl/net.rst +2
-0@@ -246,6 +246,25 @@ processes are dropped unless socket option SOF_TIMESTAMPING_OPT_TSONLY is set. Default: 1 (on) +virtio_net_hdr_csum_check +-------------------------
virtio_net_hdr_validate? In case we do have to add more validation later.
Quoted 53 lines
Documentation/admin-guide/sysctl/net.rst +17
-0+ +When processing packets with a virtio net header (through tun, tap, a +packet socket or a virtio network device) that request checksum offload +(VIRTIO_NET_HDR_F_NEEDS_CSUM), validate the packet: complete the checksum +in software, or drop the packet where the header does not match it. + +When disabled, the header is trusted not to corrupt the packet, and the +checksum is offloaded to hardware (or not verified if it never reaches +hardware). This is only safe if no protocol is processing the packets. + +The setting is per network namespace, but only writable by the host +root: root inside a network namespace created by an unprivileged user +cannot disable the validation. + +Default: 1 (on) + + wmem_default ------------include/linux/virtio_net.h +14
-0@@ -5,6 +5,7 @@#include <linux/if_vlan.h> #include <linux/ip.h> #include <linux/ipv6.h> +#include <linux/jump_label.h> #include <linux/netdevice.h> #include <linux/udp.h> #include <net/gso.h> @@ -78,6 +79,19 @@ static inline struct iphdr *virtio_net_ip4_parse(struct sk_buff *skb, *thoff = nhoff + iph->ihl *sizeof (u32);return iph; } +/* Defined in net/core/dev.c, enabled while any network namespace +* has net.core.virtio_net_hdr_csum_check set to 0. +*/ +DECLARE_STATIC_KEY_FALSE (virtio_net_hdr_csum_check_disabled_key); + +/* Failsafe: a namespace can disable the check with +* net.core.virtio_net_hdr_csum_check. +*/ +static inline bool virtio_net_hdr_csum_check_disabled (struct sk_buff *skb) +{ +return static_branch_unlikely (&virtio_net_hdr_csum_check_disabled_key) && + !READ_ONCE (dev_net (skb->dev)->core.sysctl_virtio_net_hdr_csum_check); +}/* Where does the L3 header start, and what protocol is it? * Handles Ethernet and VLAN headers; other devices use the
include/linux/virtio_net.h virtio_net_csum_partial_ok_at_offset(struct sk_buff *skb, u8 hdr_gso_type, · line 200
Quoted 8 lines
include/linux/virtio_net.h +5
-0@@ -179,6 +193,12 @@ virtio_net_csum_partial_ok_at_offset(struct sk_buff *skb, u8 hdr_gso_type,unsigned int thoff;int ip_proto; +/* Failsafe: a namespace opted out of the validation, +* trust the checksum metadata as before. +*/ +if (virtio_net_hdr_csum_check_disabled (skb)) +return true;
virtio_net_csum_partial_ok can apparently be called before this check, which would still drop packets.
Quoted 69 lines
include/linux/virtio_net.h +4
-0+if (!virtio_net_l4_start (skb, nhoff, proto, &thoff, &ip_proto))return false;@@ -516,6 +536,9 @@ virtio_net_tnl_offsets_ok(struct sk_buff *skb, bool outer_isv6,unsigned int thoff;int ip_proto; +if (virtio_net_hdr_csum_check_disabled (skb)) +return true; +if (!virtio_net_l3_start (skb, &nhoff, &proto))return false;if (outer_isv6 != (proto ==htons (ETH_P_IPV6 )))include/net/netns/core.h +1
-0@@ -18,6 +18,7 @@ struct netns_core {u8 sysctl_txrehash;u8 sysctl_tstamp_allow_data;u8 sysctl_bypass_prot_mem; +u8 sysctl_virtio_net_hdr_csum_check;#ifdef CONFIG_PROC_FS struct prot_inuse __percpu *prot_inuse;net/core/dev.c +6
-0@@ -2389,6 +2389,12 @@ EXPORT_SYMBOL(tcf_sw_enabled_key);DEFINE_STATIC_KEY_FALSE (netstamp_needed_key);EXPORT_SYMBOL (netstamp_needed_key); + +/* Enabled while any network namespace has net.core.virtio_net_hdr_csum_check +* set to 0. +*/ +DEFINE_STATIC_KEY_FALSE (virtio_net_hdr_csum_check_disabled_key); +EXPORT_SYMBOL (virtio_net_hdr_csum_check_disabled_key);#ifdef CONFIG_JUMP_LABEL static atomic_t netstamp_needed_deferred;static atomic_t netstamp_wanted;net/core/net_namespace.c +1
-0@@ -396,6 +396,7 @@ static __net_init void preinit_net_sysctl(struct net *net) net->core.sysctl_optmem_max =128 *1024 ; net->core.sysctl_txrehash =SOCK_TXREHASH_ENABLED ; net->core.sysctl_tstamp_allow_data =1 ; + net->core.sysctl_virtio_net_hdr_csum_check =1 ; net->core.sysctl_txq_reselection =msecs_to_jiffies (1000 ); }net/core/sysctl_net_core.c +1
-0@@ -18,6 +18,7 @@#include <linux/slab.h> #include <linux/sched/isolation.h> #include <linux/hex.h> +#include <linux/jump_label.h> #include <net/ip.h> #include <net/sock.h> @@ -678,6 +679,54 @@ static struct ctl_table net_core_table[] = { },
net/core/sysctl_net_core.c static struct ctl_table net_core_table[] = { · line 689
Quoted 10 lines
net/core/sysctl_net_core.c +8
-0}; +/* Defined in net/core/dev.c, declared for the gate in +* include/linux/virtio_net.h. +*/ +DECLARE_STATIC_KEY_FALSE (virtio_net_hdr_csum_check_disabled_key); + +/* +* Protects changes to virtio_net_hdr_csum_check_disabled_key and each +* net->core.sysctl_virtio_net_hdr_csum_check. Readers don't use a lock.
it appears that they take the mutex_lock below
Quoted 44 lines
net/core/sysctl_net_core.c +40
-0+*/ +static DEFINE_MUTEX (virtio_net_hdr_csum_check_mutex); + +static int virtio_net_hdr_csum_check_sysctl (const struct ctl_table *table, +int write,void *buffer, +size_t *lenp,loff_t *ppos) +{ +struct net *net = (struct net *)table->data; +/* checkpatch warns but this can't be const: tmp.data is set below */ +struct ctl_table tmp = *table; +u8 old; +int ret; + +/* Root in a user namespace must not be able to weaken the +* validation for the network namespace it controls. +*/ +if (write && !capable (CAP_NET_ADMIN )) +return -EPERM ; + +/* +* Make sure virtio_net_hdr_csum_check_disabled_key counts zero values of +* net->core.sysctl_virtio_net_hdr_csum_check. +*/ +mutex_lock (&virtio_net_hdr_csum_check_mutex); + + old = net->core.sysctl_virtio_net_hdr_csum_check; + tmp.data = &net->core.sysctl_virtio_net_hdr_csum_check; + ret =proc_dou8vec_minmax (&tmp, write, buffer, lenp, ppos); +if (write && !ret && old != net->core.sysctl_virtio_net_hdr_csum_check) { +if (net->core.sysctl_virtio_net_hdr_csum_check) +static_branch_dec (&virtio_net_hdr_csum_check_disabled_key); +else +static_branch_inc (&virtio_net_hdr_csum_check_disabled_key); + } + +mutex_unlock (&virtio_net_hdr_csum_check_mutex); + +return ret; +} +static const struct ctl_table netns_core_table[] = { #if IS_ENABLED(CONFIG_RPS) {
net/core/sysctl_net_core.c static const struct ctl_table netns_core_table[] = { · line 791
Quoted 6 lines
net/core/sysctl_net_core.c +3
-0@@ -737,6 +786,15 @@ static const struct ctl_table netns_core_table[] = { .extra1 = SYSCTL_ZERO, .extra2 =SYSCTL_ONE }, + { + .procname = "virtio_net_hdr_csum_check", + .data = &init_net,
&init_net.core.sysctl_virtio_net_hdr_csum_check
Quoted 21 lines
net/core/sysctl_net_core.c +8
-0+ .maxlen = sizeof(u8), + .mode = 0644, + .proc_handler = virtio_net_hdr_csum_check_sysctl, + .extra1 = SYSCTL_ZERO, + .extra2 =SYSCTL_ONE + },/* sysctl_core_net_init() will set the values after this * to readonly in network namespaces */ @@ -841,6 +899,8 @@ static __net_exit void sysctl_core_net_exit(struct net *net) tbl = net->core.sysctl_hdr->ctl_table_arg;unregister_net_sysctl_table (net->core.sysctl_hdr);BUG_ON (tbl == netns_core_table); +if (!net->core.sysctl_virtio_net_hdr_csum_check) +static_branch_dec (&virtio_net_hdr_csum_check_disabled_key);#if IS_ENABLED (CONFIG_RPS )kfree (net->core.rps_default_mask);#endif Signature · 1 line
MST
Michael S. Tsirkin
maintainerRe: [PATCH net v3 6/8] net: add a sysctl to opt out of virtio net hdr checksum validation
Quoted 35 lines · Willem de Bruijn
Quoted 34 lines · Michael S. Tsirkin
The ingress validation as implemented by the previous patch is stricter than what it replaced: GSO
packets whose checksum metadata matches no transport header are dropped, and non-GSO ones get their checksum completed in place. Such packets used to pass with the metadata unvalidated, and with no overhead.I judge that this should not affect any deployments, but there is still some risk that it does. In case a deployment depends on them anyway, provide a per-netns switch to get the old behavior back:
echo 0 >
/proc/sys/net/core/virtio_net_hdr_csum_checkThe default is validating.
Assisted-by LLMSigned-off-by Michael S. Tsirkin <[email protected]>--- Documentation/admin-guide/sysctl/net.rst | 19 ++++++++ include/linux/virtio_net.h | 23 +++++++++ include/net/netns/core.h | 1 + net/core/dev.c | 6 +++ net/core/net_namespace.c | 1 + net/core/sysctl_net_core.c | 60 ++++++++++++++++++++++++ 6 files changed, 110 insertions(+)Documentation/admin-guide/sysctl/net.rst +0
-0@@ -246,6 +246,25 @@ processes are dropped unless socket option SOF_TIMESTAMPING_OPT_TSONLY is set. Default: 1 (on)
Quoted 5 lines
Documentation/admin-guide/sysctl/net.rst processes are dropped unless socket option SOF_TIMESTAMPING_OPT_TSONLY is set. · line 250
Quoted 2 lines
Documentation/admin-guide/sysctl/net.rst +2
-0+virtio_net_hdr_csum_check +-------------------------
virtio_net_hdr_validate? In case we do have to add more validation later.
If you insist. My worry would be what if that validation is again user-visible somehow and we need to distinguish users who want to disable that future thing and users who want to disable just this current thing.
Hard to predict the future. But I will go with your intuition.
Will check the rest.
Willem de Bruijn
maintainerRe: [PATCH net v3 6/8] net: add a sysctl to opt out of virtio net hdr checksum validation
Quoted 42 lines · Michael S. Tsirkin
Quoted 35 lines · Willem de Bruijn
Quoted 34 lines · Michael S. Tsirkin
The ingress validation as implemented by the previous patch is stricter than what it replaced: GSO
packets whose checksum metadata matches no transport header are dropped, and non-GSO ones get their checksum completed in place. Such packets used to pass with the metadata unvalidated, and with no overhead.I judge that this should not affect any deployments, but there is still some risk that it does. In case a deployment depends on them anyway, provide a per-netns switch to get the old behavior back:
echo 0 >
/proc/sys/net/core/virtio_net_hdr_csum_checkThe default is validating.
Assisted-by LLMSigned-off-by Michael S. Tsirkin <[email protected]>--- Documentation/admin-guide/sysctl/net.rst | 19 ++++++++ include/linux/virtio_net.h | 23 +++++++++ include/net/netns/core.h | 1 + net/core/dev.c | 6 +++ net/core/net_namespace.c | 1 + net/core/sysctl_net_core.c | 60 ++++++++++++++++++++++++ 6 files changed, 110 insertions(+)Documentation/admin-guide/sysctl/net.rst +0
-0@@ -246,6 +246,25 @@ processes are dropped unless socket option SOF_TIMESTAMPING_OPT_TSONLY is set. Default: 1 (on)Quoted 5 lines
Documentation/admin-guide/sysctl/net.rst processes are dropped unless socket option SOF_TIMESTAMPING_OPT_TSONLY is set. · line 250
Quoted 2 lines
Documentation/admin-guide/sysctl/net.rst +2
-0+virtio_net_hdr_csum_check +-------------------------
virtio_net_hdr_validate? In case we do have to add more validation later.
Quoted 6 lines
If you insist. My worry would be what if that validation is again user-visible somehow and we need to distinguish users who want to disable that future thing and users who want to disable just this current thing.
Hard to predict the future. But I will go with your intuition.
Thanks, but definitely feel free to ignore my suggestions.
My intuition would be to define as
0: off
1: everything one
2+: sadly needed fudge values because we keep having to tighten,
but some crazy workloads need to opt-out to avoid breakage.
Michael S. Tsirkin
maintainerRe: [PATCH net v3 6/8] net: add a sysctl to opt out of virtio net hdr checksum validation
Quoted 50 lines · Willem de Bruijn
Quoted 42 lines · Michael S. Tsirkin
Quoted 35 lines · Willem de Bruijn
Quoted 34 lines · Michael S. Tsirkin
The ingress validation as implemented by the previous patch is stricter than what it replaced: GSO
packets whose checksum metadata matches no transport header are dropped, and non-GSO ones get their checksum completed in place. Such packets used to pass with the metadata unvalidated, and with no overhead.I judge that this should not affect any deployments, but there is still some risk that it does. In case a deployment depends on them anyway, provide a per-netns switch to get the old behavior back:
echo 0 >
/proc/sys/net/core/virtio_net_hdr_csum_checkThe default is validating.
Assisted-by LLMSigned-off-by Michael S. Tsirkin <[email protected]>--- Documentation/admin-guide/sysctl/net.rst | 19 ++++++++ include/linux/virtio_net.h | 23 +++++++++ include/net/netns/core.h | 1 + net/core/dev.c | 6 +++ net/core/net_namespace.c | 1 + net/core/sysctl_net_core.c | 60 ++++++++++++++++++++++++ 6 files changed, 110 insertions(+)Documentation/admin-guide/sysctl/net.rst +0
-0@@ -246,6 +246,25 @@ processes are dropped unless socket option SOF_TIMESTAMPING_OPT_TSONLY is set. Default: 1 (on)Quoted 5 lines
Documentation/admin-guide/sysctl/net.rst processes are dropped unless socket option SOF_TIMESTAMPING_OPT_TSONLY is set. · line 250
Quoted 2 lines
Documentation/admin-guide/sysctl/net.rst +2
-0+virtio_net_hdr_csum_check +-------------------------
virtio_net_hdr_validate? In case we do have to add more validation later.Quoted 6 lines
If you insist. My worry would be what if that validation is again user-visible somehow and we need to distinguish users who want to disable that future thing and users who want to disable just this current thing.
Hard to predict the future. But I will go with your intuition.
Quoted 8 lines
Thanks, but definitely feel free to ignore my suggestions.
My intuition would be to define as
0: off
1: everything one
2+: sadly needed fudge values because we keep having to tighten,
but some crazy workloads need to opt-out to avoid breakage.
yes you said i just do not get how it will work for userspace, maybe u can explain?
and what specifically is needed right now, while we just have 1.
Willem de Bruijn
maintainerRe: [PATCH net v3 6/8] net: add a sysctl to opt out of virtio net hdr checksum validation
Quoted 60 lines · Michael S. Tsirkin
Quoted 50 lines · Willem de Bruijn
Quoted 42 lines · Michael S. Tsirkin
Quoted 35 lines · Willem de Bruijn
Quoted 34 lines · Michael S. Tsirkin
The ingress validation as implemented by the previous patch is stricter than what it replaced: GSO
packets whose checksum metadata matches no transport header are dropped, and non-GSO ones get their checksum completed in place. Such packets used to pass with the metadata unvalidated, and with no overhead.I judge that this should not affect any deployments, but there is still some risk that it does. In case a deployment depends on them anyway, provide a per-netns switch to get the old behavior back:
echo 0 >
/proc/sys/net/core/virtio_net_hdr_csum_checkThe default is validating.
Assisted-by LLMSigned-off-by Michael S. Tsirkin <[email protected]>--- Documentation/admin-guide/sysctl/net.rst | 19 ++++++++ include/linux/virtio_net.h | 23 +++++++++ include/net/netns/core.h | 1 + net/core/dev.c | 6 +++ net/core/net_namespace.c | 1 + net/core/sysctl_net_core.c | 60 ++++++++++++++++++++++++ 6 files changed, 110 insertions(+)Documentation/admin-guide/sysctl/net.rst +0
-0@@ -246,6 +246,25 @@ processes are dropped unless socket option SOF_TIMESTAMPING_OPT_TSONLY is set. Default: 1 (on)Quoted 5 lines
Documentation/admin-guide/sysctl/net.rst processes are dropped unless socket option SOF_TIMESTAMPING_OPT_TSONLY is set. · line 250
Quoted 2 lines
Documentation/admin-guide/sysctl/net.rst +2
-0+virtio_net_hdr_csum_check +-------------------------
virtio_net_hdr_validate? In case we do have to add more validation later.Quoted 6 lines
If you insist. My worry would be what if that validation is again user-visible somehow and we need to distinguish users who want to disable that future thing and users who want to disable just this current thing.
Hard to predict the future. But I will go with your intuition.
Quoted 8 lines
Thanks, but definitely feel free to ignore my suggestions.
My intuition would be to define as
0: off
1: everything one
2+: sadly needed fudge values because we keep having to tighten,
but some crazy workloads need to opt-out to avoid breakage.
Quoted 5 lines
yes you said i just do not get how it will work for userspace, maybe u can explain?
and what specifically is needed right now, while we just have 1.
Right now the sysctl is boolean.
If we define it as a narrow, very precisely defined sysctl, then its exact behavior is set in stone.
If we then add additional validation later, that can similarly have unintended breakage of weird users, we need to add another sysctl.
And if that validation is not entirely independent, i.e., builds on this, you get a weird set of interdependent sysctls.
While in general systems will run with all validation enabled, unless they do something weird that gets hit by validation.
A single broadly defined sysctl ("validate input at the source") can be refined over time, including to expand to validation of other virtio_net_hdr fields or protocol combinations.
Even if defining it more broadly we can still leave it a boolean:
all validation on or off.
Or, optionally, we can also define additional validation levels, e.g, 0:off, 1:everything-on, 2:validate-csum-only.
Michael S. Tsirkin
maintainerRe: [PATCH net v3 6/8] net: add a sysctl to opt out of virtio net hdr checksum validation
Quoted 85 lines · Willem de Bruijn
Quoted 60 lines · Michael S. Tsirkin
Quoted 50 lines · Willem de Bruijn
Quoted 42 lines · Michael S. Tsirkin
Quoted 35 lines · Willem de Bruijn
Quoted 34 lines · Michael S. Tsirkin
The ingress validation as implemented by the previous patch is stricter than what it replaced: GSO
packets whose checksum metadata matches no transport header are dropped, and non-GSO ones get their checksum completed in place. Such packets used to pass with the metadata unvalidated, and with no overhead.I judge that this should not affect any deployments, but there is still some risk that it does. In case a deployment depends on them anyway, provide a per-netns switch to get the old behavior back:
echo 0 >
/proc/sys/net/core/virtio_net_hdr_csum_checkThe default is validating.
Assisted-by LLMSigned-off-by Michael S. Tsirkin <[email protected]>--- Documentation/admin-guide/sysctl/net.rst | 19 ++++++++ include/linux/virtio_net.h | 23 +++++++++ include/net/netns/core.h | 1 + net/core/dev.c | 6 +++ net/core/net_namespace.c | 1 + net/core/sysctl_net_core.c | 60 ++++++++++++++++++++++++ 6 files changed, 110 insertions(+)Documentation/admin-guide/sysctl/net.rst +0
-0@@ -246,6 +246,25 @@ processes are dropped unless socket option SOF_TIMESTAMPING_OPT_TSONLY is set. Default: 1 (on)Quoted 5 lines
Documentation/admin-guide/sysctl/net.rst processes are dropped unless socket option SOF_TIMESTAMPING_OPT_TSONLY is set. · line 250
Quoted 2 lines
Documentation/admin-guide/sysctl/net.rst +2
-0+virtio_net_hdr_csum_check +-------------------------
virtio_net_hdr_validate? In case we do have to add more validation later.Quoted 6 lines
If you insist. My worry would be what if that validation is again user-visible somehow and we need to distinguish users who want to disable that future thing and users who want to disable just this current thing.
Hard to predict the future. But I will go with your intuition.
Quoted 8 lines
Thanks, but definitely feel free to ignore my suggestions.
My intuition would be to define as
0: off
1: everything one
2+: sadly needed fudge values because we keep having to tighten,
but some crazy workloads need to opt-out to avoid breakage.Quoted 5 lines
yes you said i just do not get how it will work for userspace, maybe u can explain?
and what specifically is needed right now, while we just have 1.
Right now the sysctl is boolean.
If we define it as a narrow, very precisely defined sysctl, then its exact behavior is set in stone.
If we then add additional validation later, that can similarly have unintended breakage of weird users, we need to add another sysctl.
And if that validation is not entirely independent, i.e., builds on this, you get a weird set of interdependent sysctls.
While in general systems will run with all validation enabled, unless they do something weird that gets hit by validation.
A single broadly defined sysctl ("validate input at the source") can be refined over time, including to expand to validation of other
virtio_net_hdrfields or protocol combinations.
Quoted 8 lines
Even if defining it more broadly we can still leave it a boolean:
all validation on or off.Or, optionally, we can also define additional validation levels, e.g, 0:off, 1:everything-on, 2:validate-csum-only.
So I will just rename it to validate, we will see down the road how it plays out?
Signature · 1 line
MST
Michael S. Tsirkin
maintainerRe: [PATCH net v3 6/8] net: add a sysctl to opt out of virtio net hdr checksum validation
In reply to Willem de BruijnQuoted 5 lines · Willem de Bruijn
Quoted 3 lines
+/*
+ * Protects changes tovirtio_net_hdr_csum_check_disabled_keyand each
+ *net->core.sysctl_virtio_net_hdr_csum_check. Readers don't use a lock.it appears that they take the
mutex_lockbelow
Some of them? Butvirtio_net_hdr_csum_check_disabled doesn't, that's the point, right?
Willem de Bruijn
maintainerRe: [PATCH net v3 6/8] net: add a sysctl to opt out of virtio net hdr checksum validation
Quoted 10 lines · Michael S. Tsirkin
Quoted 5 lines · Willem de Bruijn
Quoted 3 lines
+/*
+ * Protects changes tovirtio_net_hdr_csum_check_disabled_keyand each
+ *net->core.sysctl_virtio_net_hdr_csum_check. Readers don't use a lock.it appears that they take the
mutex_lockbelowSome of them? But
virtio_net_hdr_csum_check_disableddoesn't, that's the point, right?
Yes, but the statement is then too absolute:
_some_ (or most) readers don't use a lock.
Michael S. Tsirkin
maintainerRe: [PATCH net v3 6/8] net: add a sysctl to opt out of virtio net hdr checksum validation
Quoted 6 lines · Willem de Bruijn
Quoted 5 lines · Michael S. Tsirkin
Quoted 4 lines · Willem de Bruijn
Quoted 3 lines
+/*
+ * Protects changes tovirtio_net_hdr_csum_check_disabled_keyand each
+ *net->core.sysctl_virtio_net_hdr_csum_check. Readers don't use a lock.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
it appears that they take the
mutex_lockbelowSome of them? But
virtio_net_hdr_csum_check_disableddoesn't, that's the point, right?Yes, but the statement is then too absolute:
_some_ (or most) readers don't use a lock.
don't use->don't need?
Michael S. Tsirkin
maintainerPATCH v3 7/8 net: add ip4_parse()
In reply to Michael S. TsirkinMultiple places in the net stack open-code the same minimal IPv4 header
validation: pull the fixed header, check version 4 and IHL >= 5 (where 5 is the fixed header size in 32-bit units) - and locate the transport header at IHL * 4 past the network header.
Add a helper for doing that: it returns the validated header and the transport header offset, or NULL if truncated or malformed.
As noted by Phil Sutter, there are more places that could use this helper, e.g. ip_mc_check_iphdr(). They will be converted in a follow-up patch.
--- include/linux/ip.h | 27 +++++++++++++++++++++++++++ include/linux/virtio_net.h | 28 +--------------------------- net/bridge/br_netfilter_hooks.c | 15 +++++---------- net/ipv4/ip_input.c | 13 +++++-------- net/ipv4/netfilter/nf_reject_ipv4.c | 13 +++++-------- 5 files changed, 43 insertions(+), 53 deletions(-)
include/linux/ip.h +27 -0
@@ -56,4 +56,31 @@ static inline void iph_set_totlen(struct iphdr *iph, unsigned int len) { iph->tot_len = len <=IP_MAX_MTU ?htons (len) :0 ; } + +/** +* ip4_parse - pull and minimally validate an IPv4 header +* @skb: buffer to parse +* @nhoff: offset of the IPv4 header within the linear data +* @thoff: returns the transport header offset +* +* Only the fixed header is guaranteed linear on return: callers +* that need the options or the transport header must pull +* further. +*/ +static inline struct iphdr *ip4_parse (struct sk_buff *skb, +unsigned int nhoff, +unsigned int *thoff) +{ +struct iphdr *iph; + +if (!pskb_may_pull (skb, nhoff +sizeof (struct iphdr ))) +return NULL ; + + iph = (struct iphdr *)(skb->data + nhoff); +if (iph->version !=4 || iph->ihl <sizeof (struct iphdr ) /sizeof (u32)) +return NULL ; + + *thoff = nhoff + iph->ihl *sizeof (u32); +return iph; +}#endif /* _LINUX_IP_H */
include/linux/virtio_net.h +1 -27
@@ -53,32 +53,6 @@ static inline int virtio_net_hdr_set_proto(struct sk_buff *skb,return 0 ; } -/* TODO: nothing virtio specific here, move to ip.h */ - -/* -* Pull and minimally validate the IPv4 header at @nhoff. -* Returns the header and stores the transport header offset in -* @thoff, or NULL if truncated or malformed. Only the fixed header -* is guaranteed linear; callers that need the options or the -* transport header must pull further. -*/ -static inline struct iphdr *virtio_net_ip4_parse (struct sk_buff *skb, -unsigned int nhoff, -unsigned int *thoff) -{ -struct iphdr *iph; - -if (!pskb_may_pull (skb, nhoff +sizeof (struct iphdr ))) -return NULL ; - - iph = (struct iphdr *)(skb->data + nhoff); -if (iph->version !=4 || - iph->ihl <sizeof (struct iphdr ) /sizeof (u32)) -return NULL ; - - *thoff = nhoff + iph->ihl *sizeof (u32); -return iph; -}/* Defined in net/core/dev.c, enabled while any network namespace * has net.core.virtio_net_hdr_csum_check set to 0. */
@@ -153,7 +127,7 @@ virtio_net_l4_start(struct sk_buff *skb, unsigned int nhoff, __be16 proto, unsigned int *thoff, int *ip_proto) {if (proto ==htons (ETH_P_IP )) { -const struct iphdr *iph =virtio_net_ip4_parse (skb, nhoff, thoff); +const struct iphdr *iph =ip4_parse (skb, nhoff, thoff);if (!iph ||ip_is_fragment (iph))return false;
net/bridge/br_netfilter_hooks.c +5 -10
@@ -206,18 +206,13 @@ static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)static int br_validate_ipv4 (struct net *net,struct sk_buff *skb) {const struct iphdr *iph; -u32 len; +u32 len, thoff; -if (!pskb_may_pull (skb,sizeof (struct iphdr ))) + iph =ip4_parse (skb,0 , &thoff); +if (!iph) goto inhdr_error; - iph =ip_hdr (skb); - -/* Basic sanity checks */ -if (iph->ihl <5 || iph->version !=4 ) - goto inhdr_error; - -if (!pskb_may_pull (skb, iph->ihl*4 )) +if (!pskb_may_pull (skb, thoff)) goto inhdr_error; iph =ip_hdr (skb);
@@ -228,7 +223,7 @@ static int br_validate_ipv4(struct net *net, struct sk_buff *skb)if (skb->len < len) {__IP_INC_STATS (net,IPSTATS_MIB_INTRUNCATEDPKTS ); goto drop; - }else if (len < (iph->ihl*4 )) + }else if (len < thoff) goto inhdr_error;if (pskb_trim_rcsum (skb, len)) {
net/ipv4/ip_input.c +5 -8
@@ -500,7 +500,7 @@ static struct sk_buff *ip_rcv_core(struct sk_buff *skb, struct net *net) {const struct iphdr *iph;int drop_reason; -u32 len; +u32 len, thoff;/* When the interface is in promisc. mode, drop all the crap * that it receives, do not try to analyse it.
@@ -520,10 +520,6 @@ static struct sk_buff *ip_rcv_core(struct sk_buff *skb, struct net *net) } drop_reason =SKB_DROP_REASON_NOT_SPECIFIED ; -if (!pskb_may_pull (skb,sizeof (struct iphdr ))) - goto inhdr_error; - - iph =ip_hdr (skb);/* * RFC1122: 3.2.1.2 MUST silently discard any IP frame that fails the checksum.
@@ -536,7 +532,8 @@ static struct sk_buff *ip_rcv_core(struct sk_buff *skb, struct net *net)* 4. Doesn't have a bogus length */ -if (iph->ihl <5 || iph->version !=4 ) + iph =ip4_parse (skb,0 , &thoff); +if (!iph) goto inhdr_error;BUILD_BUG_ON (IPSTATS_MIB_ECT1PKTS !=IPSTATS_MIB_NOECTPKTS +INET_ECN_ECT_1 );
@@ -546,7 +543,7 @@ static struct sk_buff *ip_rcv_core(struct sk_buff *skb, struct net *net)IPSTATS_MIB_NOECTPKTS + (iph->tos &INET_ECN_MASK ), max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs)); -if (!pskb_may_pull (skb, iph->ihl*4 )) +if (!pskb_may_pull (skb, thoff)) goto inhdr_error; iph =ip_hdr (skb);
@@ -559,7 +556,7 @@ static struct sk_buff *ip_rcv_core(struct sk_buff *skb, struct net *net) drop_reason =SKB_DROP_REASON_PKT_TOO_SMALL ;__IP_INC_STATS (net,IPSTATS_MIB_INTRUNCATEDPKTS ); goto drop; - }else if (len < (iph->ihl*4 )) + }else if (len < thoff) goto inhdr_error;/* Our transport medium may have padded the buffer out. Now we know it
net/ipv4/netfilter/nf_reject_ipv4.c +5 -8
@@ -25,22 +25,19 @@ nf_reject_ip_tcphdr_get(struct sk_buff *oldskb,static int nf_reject_iphdr_validate (struct sk_buff *skb) {struct iphdr *iph; -u32 len; +u32 len, thoff; -if (!pskb_may_pull (skb,sizeof (struct iphdr ))) -return 0 ; - - iph =ip_hdr (skb); -if (iph->ihl <5 || iph->version !=4 ) + iph =ip4_parse (skb,0 , &thoff); +if (!iph)return 0 ; len =ntohs (iph->tot_len);if (skb->len < len)return 0 ; -else if (len < (iph->ihl*4 )) +else if (len < thoff)return 0 ; -if (!pskb_may_pull (skb, iph->ihl*4 )) +if (!pskb_may_pull (skb, thoff))return 0 ;return 1 ;
Signature · 1 line
MST
[email protected]
AI reviewRe: [PATCH net v3 7/8] net: add ip4_parse()
Sashiko has reviewed this patch and found no issues.
Quoted 5 lines
Multiple places in the net stack open-code the same minimal IPv4 header
validation: pull the fixed header, check version 4 and IHL >= 5 (where 5 is the fixed header size in 32-bit units) - and locate the transport header at IHL * 4 past the network header.
Quoted 8 lines
Add a helper for doing that: it returns the validated header and the transport header offset, or NULL if truncated or malformed.
As noted by Phil Sutter, there are more places that could use this helper, e.g.
ip_mc_check_iphdr(). They will be converted in a follow-up patch.Signed-off-by Michael S. Tsirkin <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Michael S. Tsirkin
maintainerPATCH v3 8/8 virtio_net: cleanup protocol calculation
In reply to Michael S. TsirkinA trivial cleanup removing a duplicated code line.
--- include/linux/virtio_net.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
include/linux/virtio_net.h +2 -3
@@ -603,8 +603,8 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb,return -EINVAL ; } -skb_set_inner_protocol (skb, inner_isv6 ?htons (ETH_P_IPV6 ) : -htons (ETH_P_IP )); + inner_proto = inner_isv6 ?htons (ETH_P_IPV6 ) :htons (ETH_P_IP ); +skb_set_inner_protocol (skb, inner_proto);if (hdr->flags &VIRTIO_NET_HDR_F_UDP_TUNNEL_CSUM ) {if (!tnl_csum_negotiated)return -EINVAL ;
@@ -624,7 +624,6 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb,* We know VIRTIO_NET_HDR_F_NEEDS_CSUM was set so __virtio_net_hdr_to_skb * set CHECKSUM_PARTIAL. */ - inner_proto = inner_isv6 ?htons (ETH_P_IPV6 ) :htons (ETH_P_IP );if (virtio_net_csum_partial_ok_at_offset (skb, gso_inner_type, inner_nh, inner_proto))return 0 ;
Signature · 1 line
MST
[email protected]
AI reviewRe: [PATCH net v3 8/8] virtio_net: cleanup protocol calculation
Sashiko has reviewed this patch and found no issues.
Quoted 3 lines
A trivial cleanup removing a duplicated code line.
Signed-off-by Michael S. Tsirkin <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
[email protected]
AI reviewThis is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- Whether the issue was actually triggered, or is only theoretical (e.g. found by code inspection). If it was triggered please include the symptoms, like the stack trace or error messages.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Michael S. Tsirkin
maintainerQuoted 9 lines · On Mon, Oct 19, 2026 at 12:09:19PM +0000, [email protected] wrote:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- Whether the issue was actually triggered, or is only theoretical (e.g. found by code inspection). If it was triggered please include the symptoms, like the stack trace or error messages.
Quoted 8 lines
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
To answer the question, the virtio header is checked at input by Eric's patch. I'm not aware of a way to bypass that, but given the amount of pain and work this is causing, it seems prudent to validate at several levels.
Patch 4 is a bugfix - i reproduced it easily but it is not normally encountered in the field - the combination is non practical.
Signature · 1 line
MST
Quoted 77 lines · Michael S. Tsirkin
As reported first by Paulos Yibelo <[email protected]> (after which others, credited below, found more ways the bug can affect users):
allowing untrusted hardware to specify csum start/offset for
CHECKSUM_PARTIALwas not a good idea, because, if it edits the packet outside the actual checksum, and depending on the setup, the networking stack could be looking at the packet both before and after the edit, and seeing different results.Previously, commit
6785011f8b16("ipv4: validatechecksum_startbefore completing checksum") validated all known instances where this causes the most grief, but the real fix is to make sure that either only actual checksum fields are ever edited,
or the checksum is completed before the net stack looks at it, so it is always consistent.This basically goes back to
what we did beforef43798c27684("tun: Allow GSO usingvirtio_net_hdr"), except in a narrow list of specified known good cases.Summary of the patchset:
These are definitely net material (and seem to be too big for stable, or I would recommend backporting there, too):
net: account for ingress-resolved virtio net hdr state
- accounting, not the fix itself, dependency of the next one
net: resolve uncommon partial checksums at ingress
- fix itself
net: add a sysctl to opt out of virtio net hdr checksum validation
- reducing risk of the fix by adding a sysctl to disable itThese are cleanups, can be deferred to net-next, too:
net: add
ip4_parse()
virtio_net: cleanup protocol calculation
- cleanup resulting code duplication
(kept separate to simplify backporting)Fixes f43798c27684 ("tun: Allow GSO using virtio_net_hdr")Changes since v1:
- reorder patches to fix bisectability, addressing a comment by Björn Töpel.
- code refactoring, different patch splitup,
a sysctl knob to disable as suggested by Willem de Bruijn.
- fix a bunch of minor issues spotted by LLMsChanges since v2:
virtio_net: parse tunnelcsum_startlittle-endian
- new patch. correctly parsing the header is a dependency of validating it, otherwise i do not know what to validatenet: account for ingress-resolved virtio net hdr state
- makerx_csum_swcounter 64 bit so it won't wrap (spotted by LLM review)net: resolve uncommon partial checksums at ingress
- tunnel fixes to bugs noticed by Willem de Bruijn and Chris Arges
- restore thecsum_offsetchecks as suggested by Willem
- parsecsum_startin the tunnel header little-endian like the rest of it (spotted by LLM review)net: add a sysctl to opt out of virtio net hdr checksum validation
- the knob is no longer writable by root inside a user
namespace: changing it now requirescapable(CAP_NET_ADMIN)suggested by Willem de Bruijnnet: add
ip4_parse()
- comment converted to kernel-doc format (Willem de Bruijn)
Quoted 9 lines
um: vector: honor virtio net hdr conversion failures
- commit log tweaksNew patch:
um: vector: trim skbs before verifying the header
found by code reviewSomewhat tested, tunnel also tested now, um part compiled only
I was able to run my tests and can confirm no more KASAN errors. I did not test the um patches.
Quoted 32 lines
Michael S. Tsirkin (8):
net: account for ingress-resolved virtio net hdr state
um: vector: honor virtio net hdr conversion failures
um: vector: trim skbs before verifying the header
virtio_net: parse tunnelcsum_startlittle-endian
net: resolve uncommon partial checksums at ingress
net: add a sysctl to opt out of virtio net hdr checksum validation
net: addip4_parse()
virtio_net: cleanup protocol calculationDocumentation/admin-guide/sysctl/net.rst | 19 ++ arch/um/drivers/vector_kern.c | 6 +- arch/um/drivers/vector_transports.c | 4 +- drivers/net/tap.c | 4 +- drivers/net/tun.c | 41 +++- drivers/net/virtio_net.c | 2 +- include/linux/ip.h | 27 +++ include/linux/virtio_net.h | 272 ++++++++++++++++++++++- include/net/netns/core.h | 1 + net/bridge/br_netfilter_hooks.c | 15 +- net/core/dev.c | 6 + net/core/net_namespace.c | 1 + net/core/sysctl_net_core.c | 60 +++++ net/ipv4/ip_input.c | 13 +- net/ipv4/netfilter/nf_reject_ipv4.c | 13 +- net/packet/af_packet.c | 4 +- 16 files changed, 445 insertions(+), 43 deletions(-)Signature · 1 line
MST
Jakub Kicinski
maintainerQuoted 1 line · On Mon, 19 Oct 2026 08:03:00 -0400 Michael S. Tsirkin wrote:
Subject: [PATCH net v3 0/8] more virtio-net csum hardening
FWIW patch 6
net/core/dev.c:2396:1: warning: symbol 'virtio_net_hdr_csum_check_disabled_key' was not declared. Should it be static?
patch 7
Warning: include/linux/ip.h:72 No description found for return value of 'ip4_parse'
Warning: include/linux/ip.h:72 No description found for return value of 'ip4_parse'