more virtio-net csum hardening
Subsystems (11)
Michael S. Tsirkin
maintainerv4 cover more virtio-net csum hardening
v4 and I'm pretty tired of this family of issues.
Willem suggested more cleanups, but I don't think I will be able to dedicate much more time to this, there's a pile of fixes I queued that need to get tested and go into rc2, and a bunch of qemu features that need review.
Hopefully if any changes are required this can be done by patches on top, or if someone wants to take over this, very welcome.
------
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 probabably 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 fix
um: vector: trim skbs before verifying the header
- fixes the um receive path: without it the checksum would be completed over the unreceived tail of the buffer
virtio_net: parse tunnel csum_start little-endian
- bugfix, a dependency of validating the header
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
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: trim skbs before verifying the header
- new patch, found by code review
Changes since v3:
net: account for ingress-resolved virtio net hdr state
- um: vector: honor virtio net hdr conversion failures folded in (Björn Töpel)
- count on tap and virtio_net, too (Willem de Bruijn)
net: resolve uncommon partial checksums at ingress
- walk the frame on loopback, too: loopback_xmit() runs eth_type_trans() on it (Willem de Bruijn)
- return the mismatch handling directly, GSO is implied in the tunnel path so drop there outright, and fold the helper into its one remaining caller (Willem de Bruijn)
- read the IP version through struct iphdr (Willem de Bruijn)
- document the ipv6_find_hdr() flags choice
net: add a sysctl to opt out of virtio net hdr checksum validation
- renamed to virtio_net_hdr_validate, in case more validation is gated by the same knob later (Willem de Bruijn)
- point .data at the value like the neighboring entries, the per-netns registration rebases it, and the handler reads the value directly - no ctl_table copy (Björn Töpel, Willem de Bruijn)
- check the opt-out before parsing: with the knob off, packets the parser cannot handle were still dropped (Willem de Bruijn)
- include virtio_net.h in net/core/dev.c so the static key definition sees its declaration (build warning reported by Jakub Kicinski)
net: add ip4_parse()
- add the missing kernel-doc Return: section
(Jakub Kicinski)
Tested: full suite on the final tree, tunnel included. um part compiled only.
Michael S. Tsirkin (7):
net: account for ingress-resolved virtio net hdr state
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/ipvlan/ipvtap.c | 36 ++++ drivers/net/macvtap.c | 36 ++++ drivers/net/tap.c | 8 +- drivers/net/tun.c | 41 ++++- drivers/net/virtio_net.c | 14 +- include/linux/if_tap.h | 1 + include/linux/ip.h | 30 ++++ include/linux/virtio_net.h | 279 ++++++++++++++++++++++++++++++- include/net/netns/core.h | 1 + net/bridge/br_netfilter_hooks.c | 15 +- net/core/dev.c | 7 + net/core/net_namespace.c | 1 + net/core/sysctl_net_core.c | 50 ++++++ net/ipv4/ip_input.c | 13 +- net/ipv4/netfilter/nf_reject_ipv4.c | 13 +- net/packet/af_packet.c | 4 +- 19 files changed, 533 insertions(+), 45 deletions(-)
Signature · 1 line
MST
Michael S. Tsirkin
maintainerPATCH v4 1/7 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. The um vector raw transport even ignored the return value
altogether: rejected packets went on into the stack with their metadata unvalidated, so check it there as well.
Have tun, tap and virtio_net count these events per device as rx_csum_sw, exposed via ethtool -S.
--- arch/um/drivers/vector_transports.c | 4 ++- drivers/net/ipvlan/ipvtap.c | 36 +++++++++++++++++++++++++ drivers/net/macvtap.c | 36 +++++++++++++++++++++++++ drivers/net/tap.c | 8 ++++-- drivers/net/tun.c | 41 +++++++++++++++++++++++++++-- drivers/net/virtio_net.c | 15 ++++++++--- include/linux/if_tap.h | 1 + net/packet/af_packet.c | 4 +-- 8 files changed, 135 insertions(+), 10 deletions(-)
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 ; }
drivers/net/ipvlan/ipvtap.c +36 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only #include <linux/etherdevice.h> +#include <linux/ethtool.h> #include "ipvlan.h" #include <linux/if_vlan.h> #include <linux/if_tap.h>
@@ -74,6 +75,32 @@ static void ipvtap_update_features(struct tap_dev *tap,netdev_update_features (vlan->dev); } +static int ipvtap_get_sset_count (struct net_device *dev,int sset) +{ +return sset ==ETH_SS_STATS ?1 : -EOPNOTSUPP ; +} + +static void ipvtap_get_strings (struct net_device *dev,u32 stringset, +u8 *data) +{ +if (stringset ==ETH_SS_STATS ) +ethtool_puts (&data,"rx_csum_sw" ); +} + +static void ipvtap_get_ethtool_stats (struct net_device *dev, +struct ethtool_stats *stats, +u64 *data) +{ +struct ipvtap_dev *vlantap =netdev_priv (dev); + + data[0 ] =atomic64_read (&vlantap->tap.rx_csum_sw); +} + +/* checkpatch warns but this can't be const: cloned from ipvlan's +* ops at newlink time, with the statistics above added. +*/ +static struct ethtool_ops ipvtap_ethtool_ops; +static int ipvtap_newlink (struct net_device *dev,struct rtnl_newlink_params *params,struct netlink_ext_ack *extack)
@@ -91,6 +118,15 @@ static int ipvtap_newlink(struct net_device *dev, vlantap->tap.update_features = ipvtap_update_features; vlantap->tap.count_rx_dropped = ipvtap_count_rx_dropped; +/* ipvlan's ethtool ops are already installed at this point; +* clone them and add the tap statistics on top. +*/ + ipvtap_ethtool_ops = *dev->ethtool_ops; + ipvtap_ethtool_ops.get_sset_count = ipvtap_get_sset_count; + ipvtap_ethtool_ops.get_strings = ipvtap_get_strings; + ipvtap_ethtool_ops.get_ethtool_stats = ipvtap_get_ethtool_stats; + dev->ethtool_ops = &ipvtap_ethtool_ops; + err =netdev_rx_handler_register (dev, tap_handle_frame, &vlantap->tap);if (err)return err;
drivers/net/macvtap.c +36 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only #include <linux/etherdevice.h> +#include <linux/ethtool.h> #include <linux/if_macvlan.h> #include <linux/if_tap.h> #include <linux/if_vlan.h>
@@ -78,6 +79,32 @@ static void macvtap_update_features(struct tap_dev *tap,netdev_update_features (vlan->dev); } +static int macvtap_get_sset_count (struct net_device *dev,int sset) +{ +return sset ==ETH_SS_STATS ?1 : -EOPNOTSUPP ; +} + +static void macvtap_get_strings (struct net_device *dev,u32 stringset, +u8 *data) +{ +if (stringset ==ETH_SS_STATS ) +ethtool_puts (&data,"rx_csum_sw" ); +} + +static void macvtap_get_ethtool_stats (struct net_device *dev, +struct ethtool_stats *stats, +u64 *data) +{ +struct macvtap_dev *vlantap =netdev_priv (dev); + + data[0 ] =atomic64_read (&vlantap->tap.rx_csum_sw); +} + +/* checkpatch warns but this can't be const: cloned from macvlan's +* ops at newlink time, with the statistics above added. +*/ +static struct ethtool_ops macvtap_ethtool_ops; +static int macvtap_newlink (struct net_device *dev,struct rtnl_newlink_params *params,struct netlink_ext_ack *extack)
@@ -99,6 +126,15 @@ static int macvtap_newlink(struct net_device *dev, vlantap->tap.count_rx_dropped = macvtap_count_rx_dropped; vlantap->tap.update_features = macvtap_update_features; +/* macvlan's ethtool ops are already installed at this point; +* clone them and add the tap statistics on top. +*/ + macvtap_ethtool_ops = *dev->ethtool_ops; + macvtap_ethtool_ops.get_sset_count = macvtap_get_sset_count; + macvtap_ethtool_ops.get_strings = macvtap_get_strings; + macvtap_ethtool_ops.get_ethtool_stats = macvtap_get_ethtool_stats; + dev->ethtool_ops = &macvtap_ethtool_ops; + err =netdev_rx_handler_register (dev, tap_handle_frame, &vlantap->tap);if (err)return err;
drivers/net/tap.c +6 -2
@@ -649,11 +649,13 @@ 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; } +if (err >0 ) +atomic64_inc (&tap->rx_csum_sw); }skb_probe_transport_header (skb);
@@ -1084,10 +1086,12 @@ 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; } +if (err >0 ) +atomic64_inc (&tap->rx_csum_sw); }skb_probe_transport_header (skb);
drivers/net/tun.c +39 -2
75 lines not on this page · raw message
drivers/net/virtio_net.c +12 -3
@@ -125,6 +125,7 @@ struct virtnet_rq_stats {u64_stats_t packets;u64_stats_t bytes;u64_stats_t drops; +u64_stats_t rx_csum_sw;u64_stats_t xdp_packets;u64_stats_t xdp_tx;u64_stats_t xdp_redirects;
@@ -158,6 +159,7 @@ static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {VIRTNET_RQ_STAT ("drops" , drops), +VIRTNET_RQ_STAT ("rx_csum_sw" , rx_csum_sw),VIRTNET_RQ_STAT ("xdp_packets" , xdp_packets),VIRTNET_RQ_STAT ("xdp_tx" , xdp_tx),VIRTNET_RQ_STAT ("xdp_redirects" , xdp_redirects),
@@ -2502,6 +2504,7 @@ static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue * {struct virtio_net_common_hdr *hdr;struct net_device *dev = vi->dev; +int ret; hdr =skb_vnet_common_hdr (skb);if (dev->features &NETIF_F_RXHASH && vi->has_rss_hash_report)
@@ -2517,15 +2520,21 @@ static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue * skb->dev = dev;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))) { + ret =virtio_net_hdr_tnl_to_skb (skb, &hdr->tnl_hdr, vi->rx_tnl, + vi->rx_tnl_csum, +virtio_is_little_endian (vi->vdev)); +if (ret <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, vi->rx_tnl, vi->rx_tnl_csum); goto frame_err; } +if (ret >0 ) { +u64_stats_update_begin (&rq->stats.syncp); +u64_stats_inc (&rq->stats.rx_csum_sw); +u64_stats_update_end (&rq->stats.syncp); + }skb_record_rx_queue (skb,vq2rxq (rq->vq)); skb->protocol =eth_type_trans (skb, dev);
include/linux/if_tap.h +1 -0
@@ -41,6 +41,7 @@ struct tap_dev {struct list_head queue_list;int numvtaps;int numqueues; +atomic64_t rx_csum_sw;netdev_features_t tap_features;int minor;
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 v4 2/7 um: vector: trim skbs before verifying the header
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_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
Re: [PATCH net v4 2/7] um: vector: trim skbs before verifying the header
Quoted 11 lines · Michael S. Tsirkin
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).Trim first, so the import sees the packet as it was received.
Signed-off-by Michael S. Tsirkin <[email protected]>
[email protected]
AI reviewRe: [PATCH net v4 2/7] um: vector: trim skbs before verifying the header
In reply to Michael S. TsirkinSashiko has reviewed this patch and found no issues.
Quoted 11 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).Trim first, so the import sees the packet as it was received.
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=2
Michael S. Tsirkin
maintainerPATCH v4 3/7 virtio_net: parse tunnel csum_start little-endian
In reply to Michael S. TsirkinAll tunnel related virtio features are > 32, so none of this functionality can be enabled on legacy mixed-endian device.
Accordingly, all tunnel header fields, csum_start included, are little-endian.
The nested virtio net header import parses csum_start that way, but the inner transport offset is parsed with the endianness specified by the interface.
If tun is misconfigured to legacy big-endian mode while the tunnel features are enabled, the two parses of this field disagree, and the value stored in skb->inner_transport_header - which is only bounds-checked from below - can point past the end of the packet, where GSO segmentation later dereferences it.
To fix, 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 v4 3/7] virtio_net: parse tunnel csum_start little-endian
Sashiko has reviewed this patch and found no issues.
Quoted 16 lines
All tunnel related virtio features are > 32, so none of this functionality can be enabled on legacy mixed-endian device.
Accordingly, all tunnel header fields,
csum_startincluded, are little-endian.The nested virtio net header import parses
csum_startthat way, but the inner transport offset is parsed with the endianness specified by the interface.If tun is misconfigured to legacy big-endian mode while the tunnel features are enabled, the two parses of this field disagree, and the value stored in
skb->inner_transport_header- which is only bounds-checked from below - can point past the end of the packet, where GSO segmentation later dereferences it.
Quoted 5 lines
To fix, parse the field little-endian in both places.
Fixes a2fb4bc4e2a6 ("net: implement virtio helpers to handle UDP GSO tunneling.")Cc Paolo Abeni <[email protected]>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=3
Michael S. Tsirkin
maintainerPATCH v4 4/7 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.
--- drivers/net/tap.c | 8 +- drivers/net/tun.c | 8 +- drivers/net/virtio_net.c | 3 +- include/linux/virtio_net.h | 276 ++++++++++++++++++++++++++++++++++++- 4 files changed, 281 insertions(+), 14 deletions(-)
drivers/net/tap.c +4 -4
@@ -653,9 +653,9 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,rcu_read_unlock (); drop_reason =SKB_DROP_REASON_DEV_HDR ; goto err_kfree; - } -if (err >0 ) + }else if (err >0 ) {atomic64_inc (&tap->rx_csum_sw); + } }skb_probe_transport_header (skb);
@@ -1089,9 +1089,9 @@ static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp)if (err <0 ) {rcu_read_unlock (); goto err_kfree; - } -if (err >0 ) + }else if (err >0 ) {atomic64_inc (&tap->rx_csum_sw); + } }skb_probe_transport_header (skb);
drivers/net/tun.c +4 -4
@@ -1941,8 +1941,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,atomic_long_inc (&tun->rx_frame_errors); err = -EINVAL ; goto free_skb; - } -if (err >0 ) { + }else if (err >0 ) {atomic64_inc (&tun->rx_csum_sw); err =0 ; }
@@ -2617,9 +2616,10 @@ static int tun_xdp_one(struct tun_struct *tun,kfree_skb (skb); ret = -EINVAL ; goto out; - } -if (ret >0 ) + }else if (ret >0 ) {atomic64_inc (&tun->rx_csum_sw); + ret =0 ; + } skb->protocol =eth_type_trans (skb, tun->dev);skb_reset_network_header (skb);
drivers/net/virtio_net.c +1 -2
@@ -2529,8 +2529,7 @@ static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue * hdr->hdr.gso_size, hdr->hdr.flags, vi->rx_tnl, vi->rx_tnl_csum); goto frame_err; - } -if (ret >0 ) { + }else if (ret >0 ) {u64_stats_update_begin (&rq->stats.syncp);u64_stats_inc (&rq->stats.rx_csum_sw);u64_stats_update_end (&rq->stats.syncp);
include/linux/virtio_net.h +272 -4
331 lines not on this page · raw message
Signature · 1 line
MST
Willem de Bruijn
maintainerRe: [PATCH net v4 4/7] net: resolve uncommon partial checksums at ingress
Quoted 60 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.Reported-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]>--- drivers/net/tap.c | 8 +- drivers/net/tun.c | 8 +- drivers/net/virtio_net.c | 3 +- include/linux/virtio_net.h | 276 ++++++++++++++++++++++++++++++++++++- 4 files changed, 281 insertions(+), 14 deletions(-)drivers/net/tap.c +2
-2@@ -653,9 +653,9 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,rcu_read_unlock (); drop_reason =SKB_DROP_REASON_DEV_HDR ; goto err_kfree; - } -if (err >0 ) + }else if (err >0 ) {atomic64_inc (&tap->rx_csum_sw); + } }skb_probe_transport_header (skb);
drivers/net/tap.c static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp) · line 1094
Quoted 8 lines
drivers/net/tap.c +2
-2@@ -1089,9 +1089,9 @@ static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp)if (err <0 ) {rcu_read_unlock (); goto err_kfree; - } -if (err >0 ) + }else if (err >0 ) {atomic64_inc (&tap->rx_csum_sw); + }
All these belong in patch 1 that introduces these branches
Quoted 3 lines
drivers/net/tap.c +0
-0}skb_probe_transport_header (skb);
[email protected]
AI reviewRe: [PATCH net v4 4/7] net: resolve uncommon partial checksums at ingress
In reply to Michael S. TsirkinThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] net: virtio_net_hdr_to_skb() runs skb_checksum_help() before skb_zcopy_init() - [High] net…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] net: virtio_net_hdr_to_skb() runs skb_checksum_help() before skb_zcopy_init()
- [High] net: skb->protocol ignored on Ethernet/loopback in virtio_net_l3_start()
- [High] virtio_net: csum validation bypass on non-GSO skb in virtio_net_hdr_tnl_to_skb() --
commit 69b2836e804af5188d86e52f3cfbb8777bb548d1
Author: Michael S. Tsirkin <[email protected]>
net: resolve uncommon partial checksums at ingress
This patch validates csum_start and csum_offset on incoming packets with virtio net headers, retaining CHECKSUM_PARTIAL only when the offsets match a parsed TCP or UDP header. Non-GSO packets with unrecognized partial checksum offsets have their checksums completed immediately in software via skb_checksum_help(), while unrecognized GSO packets are dropped.
Quoted 53 lines
include/linux/virtio_net.h +45
-0@@ -48,6 +52,202 @@ 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; + +/* Loopback carries ethernet frames, too: +* loopback_xmit() calls eth_type_trans() on them. +*/ + if (skb->dev->type == ARPHRD_ETHER ||
include/linux/virtio_net.h static inline int virtio_net_hdr_set_proto(struct sk_buff *skb, · line 109
Quoted 10 lines
include/linux/virtio_net.h +10
-0+ skb->dev->type == ARPHRD_LOOPBACK) { +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; + }
[Severity: High]
What happens here when an AF_PACKET SOCK_RAW socket with PACKET_VNET_HDR sends a frame whose skb->protocol disagrees with eth_hdr(skb)->h_proto?
In net/packet/af_packet.c, both packet_snd() and tpacket_snd() (via tpacket_fill_skb()) set skb->protocol from the socket or sll_protocol and place skb->network_header at ETH_HLEN before calling virtio_net_hdr_to_skb(), and then call packet_parse_headers():
net/packet/af_packet.c:packet_snd() {
...
if (proto != htons(ETH_P_ALL))
skb->protocol = proto;
skb->dev = dev;
...
if (vnet_hdr_sz) {
err = virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le());
if (err < 0)
goto out_free;
len += vnet_hdr_sz;
}
packet_parse_headers(skb, sock);
...
}In packet_parse_headers(), a pre-set skb->protocol is preserved, and skb->network_header is only advanced past VLAN tags when skb->dev->type is ARPHRD_ETHER and 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);
}Because virtio_net_l3_start() ignores a pre-set skb->protocol on ARPHRD_ETHER and ARPHRD_LOOPBACK devices and unconditionally parses eth_hdr(skb)->h_proto and VLAN tags via __vlan_get_protocol(), a sender can set sll_protocol to htons(ETH_P_IP) while placing htons(ETH_P_8021Q) in eth_hdr(skb)->h_proto (or send a VLAN-tagged frame on ARPHRD_LOOPBACK).
In that case, virtio_net_l3_start() advances *nhoff to 18 and validates csum_start and csum_offset against a crafted IPv4 and UDP/TCP header embedded inside the real IPv4 header at offset 14, keeping CHECKSUM_PARTIAL on the skb. Downstream paths that rely on skb->protocol and skb_network_offset(skb) at ETH_HLEN (such as nf_hook_egress(), sch_handle_egress(), ipvlan_process_outbound(), and skb_mac_gso_segment()) will then inspect or segment the packet at offset 14 before checksum completion overwrites the two bytes at csum_start + csum_offset.
Should virtio_net_l3_start() honor a pre-set skb->protocol (and avoid advancing past VLAN headers on ARPHRD_LOOPBACK when skb->network_header will remain at ETH_HLEN) so that checksum validation parses the same L3 header and offset as the rest of the stack?
[ ... ]
Quoted 41 lines
include/linux/virtio_net.h +29
-1+/* +* 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. +*/ +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 +394,35 @@ 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 426
Quoted 10 lines
include/linux/virtio_net.h +9
-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; + +return __virtio_net_resolve_csum_partial_mismatch (skb); }
[Severity: High]
Can calling skb_checksum_help() inside virtio_net_hdr_to_skb() bypass the shared fragment linearization check on zerocopy SKBs?
In net/core/dev.c, skb_checksum_help() checks skb_has_shared_frag(skb) (which tests SKBFL_SHARED_FRAG in skb_shinfo(skb)->flags) and linearizes the skb so that userspace-mapped pages cannot be mutated during or after checksum calculation:
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;
}
...
*(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
out_set_summed:
skb->ip_summed = CHECKSUM_NONE;
}However, in tap_get_user() (drivers/net/tap.c), tun_get_user() (drivers/net/tun.c), and tpacket_snd() (net/packet/af_packet.c), userspace pages are attached to skb_shinfo(skb)->frags[] first (via zerocopy_sg_from_iter() or tpacket_fill_skb()), then virtio_net_hdr_to_skb() is called, and skb_zcopy_init() (which sets SKBFL_ZEROCOPY_FRAG, i.e., SKBFL_ZEROCOPY_ENABLE | SKBFL_SHARED_FRAG) is only called afterwards:
drivers/net/tap.c:tap_get_user() {
...
if (zerocopy)
err = zerocopy_sg_from_iter(skb, from);
...
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);
}
}When a non-GSO zerocopy packet takes the slow path in virtio_net_hdr_to_skb() -> __virtio_net_resolve_csum_partial_mismatch() -> skb_checksum_help(), SKBFL_SHARED_FRAG is not yet set on the skb. As a result, skb_has_shared_frag(skb) evaluates to false, __skb_linearize(skb) is skipped, skb_checksum_help() computes the checksum directly over writable userspace pages and sets skb->ip_summed = CHECKSUM_NONE, and skb_zcopy_init() then marks the fragments as shared after the checksum has already been finalized.
[ ... ]
Quoted 31 lines
include/linux/virtio_net.h +8
-3@@ -335,9 +589,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 +610,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 +625,18 @@ 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
include/linux/virtio_net.h virtio_net_hdr_tnl_to_skb(struct sk_buff *skb, · line 640
Quoted 10 lines
include/linux/virtio_net.h +9
-0+* 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 ; + +/* A tunnel header implies GSO, so drop. */ +return -EINVAL ; }
[Severity: High]
Does virtio_net_hdr_tnl_to_skb() need to verify that skb_is_gso(skb) is true after calling __virtio_net_hdr_to_skb()?
In __virtio_net_hdr_to_skb(), when skb->len - nh_off <= gso_size, the packet is treated as non-GSO and shinfo->gso_size and shinfo->gso_type are left as 0, while skb->ip_summed remains CHECKSUM_PARTIAL at hdr->csum_start:
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;
}
}
return 0;
}If a sender on tun (tun_get_user() or tun_xdp_one()) or virtio_net (virtnet_receive_done()) submits a small UDP packet with a tunnel GSO type, VIRTIO_NET_HDR_F_NEEDS_CSUM, and gso_size >= skb->len - nh_off, __virtio_net_hdr_to_skb() returns 0 with skb_is_gso(skb) false.
virtio_net_hdr_tnl_to_skb() then sets SKB_GSO_UDP_TUNNEL (or SKB_GSO_UDP_TUNNEL_CSUM) on the non-GSO skb and validates csum_start against the untrusted vhdr->inner_nh_offset inside the UDP payload via virtio_net_csum_partial_ok_at_offset(). By placing a fake 20-byte IPv4 header stub at inner_nh_offset inside the UDP payload and setting csum_start to inner_nh + 20, the packet passes validation and enters the stack as a non-GSO UDP packet with CHECKSUM_PARTIAL pointing inside the UDP payload (and with shinfo->gso_type non-zero while shinfo->gso_size is 0, which also triggers WARN_ON_ONCE(1) in tun_vnet_hdr_tnl_from_skb() if forwarded to a tun device without NETIF_F_GSO_UDP_TUNNEL).
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Michael S. Tsirkin
maintainerPATCH v4 5/7 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_validate
The default is validating.
--- Documentation/admin-guide/sysctl/net.rst | 19 +++++++++ include/linux/virtio_net.h | 26 ++++++++++++ include/net/netns/core.h | 1 + net/core/dev.c | 7 ++++ net/core/net_namespace.c | 1 + net/core/sysctl_net_core.c | 50 ++++++++++++++++++++++++ 6 files changed, 104 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_validate +----------------------- + +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 +26 -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_validate set to 0. +*/ +DECLARE_STATIC_KEY_FALSE (virtio_net_hdr_validate_disabled_key); + +/* Failsafe: a namespace can disable the check with +* net.core.virtio_net_hdr_validate. +*/ +static inline bool virtio_net_hdr_validate_disabled (struct sk_buff *skb) +{ +return static_branch_unlikely (&virtio_net_hdr_validate_disabled_key) && + !READ_ONCE (dev_net (skb->dev)->core.sysctl_virtio_net_hdr_validate); +}/* Where does the L3 header start, and what protocol is it? * Handles Ethernet and VLAN headers; other devices use the
@@ -186,6 +200,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_validate_disabled (skb)) +return true; +if (!virtio_net_l4_start (skb, nhoff, proto, &thoff, &ip_proto))return false;
@@ -228,6 +248,9 @@ virtio_net_csum_partial_ok(struct sk_buff *skb, u8 hdr_gso_type)int nhoff;__be16 proto; +if (virtio_net_hdr_validate_disabled (skb)) +return true; +if (!virtio_net_l3_start (skb, &nhoff, &proto))return false;
@@ -519,6 +542,9 @@ virtio_net_tnl_offsets_ok(struct sk_buff *skb, bool outer_isv6,unsigned int thoff;int ip_proto; +if (virtio_net_hdr_validate_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_validate;#ifdef CONFIG_PROC_FS struct prot_inuse __percpu *prot_inuse;
net/core/dev.c +7 -0
@@ -128,6 +128,7 @@#include <linux/if_vlan.h> #include <linux/ip.h> #include <net/ip.h> +#include <linux/virtio_net.h> #include <net/mpls.h> #include <linux/ipv6.h> #include <linux/in.h>
@@ -2389,6 +2390,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_validate +* set to 0. +*/ +DEFINE_STATIC_KEY_FALSE (virtio_net_hdr_validate_disabled_key); +EXPORT_SYMBOL (virtio_net_hdr_validate_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_validate =1 ; net->core.sysctl_txq_reselection =msecs_to_jiffies (1000 ); }
net/core/sysctl_net_core.c +50 -0
74 lines not on this page · raw message
Signature · 1 line
MST
Re: [PATCH net v4 5/7] net: add a sysctl to opt out of virtio net hdr checksum validation
Quoted 11 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:
Quoted 6 lines
echo 0 >
/proc/sys/net/core/virtio_net_hdr_validateThe default is validating.
Reviewed-by Björn Töpel <[email protected]>Signed-off-by Michael S. Tsirkin <[email protected]>
[email protected]
AI reviewRe: [PATCH net v4 5/7] 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: - [High] net: RCU violation reading dev_net() in virtio_net_hdr_validate_disabled() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] net: RCU violation reading dev_net() in virtio_net_hdr_validate_disabled() --
commit 69b1f5f4a234fc132ca5965bd53c81fd86975566
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_validate, guarded by a static key to allow opting out of virtio_net_hdr checksum offset validation.
Quoted 4 lines
include/linux/virtio_net.h +0
-0
[ ... ]
Quoted 7 lines
include/linux/virtio_net.h +3
-0@@ -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_validate set to 0. +*/
include/linux/virtio_net.h static inline struct iphdr *virtio_net_ip4_parse(struct sk_buff *skb, · line 94
Quoted 10 lines
include/linux/virtio_net.h +10
-0+DECLARE_STATIC_KEY_FALSE (virtio_net_hdr_validate_disabled_key); + +/* Failsafe: a namespace can disable the check with +* net.core.virtio_net_hdr_validate. +*/ +static inline bool virtio_net_hdr_validate_disabled (struct sk_buff *skb) +{ +return static_branch_unlikely (&virtio_net_hdr_validate_disabled_key) && + !READ_ONCE (dev_net (skb->dev)->core.sysctl_virtio_net_hdr_validate); +}
[Severity: High]
Does virtio_net_hdr_validate_disabled() need RCU read-side protection when fetching dev_net(skb->dev) and reading core.sysctl_virtio_net_hdr_validate?
When net.core.virtio_net_hdr_validate is set to 0 in any network namespace (enabling virtio_net_hdr_validate_disabled_key), several callers of virtio_net_hdr_to_skb() and virtio_net_hdr_tnl_to_skb() invoke virtio_net_hdr_validate_disabled(skb) from preemptible process context while holding only dev_hold(skb->dev) and without holding rcu_read_lock().
Specifically, tun_get_user() calls tun_vnet_hdr_tnl_to_skb() -> virtio_net_hdr_tnl_to_skb() before acquiring rcu_read_lock():
drivers/net/tun.c:tun_get_user() {
...
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;
}
...
rcu_read_lock();
if (unlikely(!(tun->dev->flags & IFF_UP))) {
...
}Similarly, tpacket_snd() and packet_snd() in net/packet/af_packet.c call virtio_net_hdr_to_skb() in process context without rcu_read_lock():
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;
}
}
...
}net/packet/af_packet.c:packet_snd() {
...
if (vnet_hdr_sz) {
err = virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le());
if (err < 0)
goto out_free;
len += vnet_hdr_sz;
}
...
}Holding dev_hold(skb->dev) does not prevent __dev_change_net_namespace() from moving skb->dev to another network namespace via dev_net_set(dev, net). If the old network namespace then exits and is freed via cleanup_net(), a task preempted after loading skb->dev->nd_net.net outside rcu_read_lock() could dereference a freed struct net.
Should virtio_net_hdr_validate_disabled() use dev_net_rcu(skb->dev) inside rcu_read_lock() and rcu_read_unlock(), similar to how commit afec62cd0a4191 ("flow_dissector: use RCU protection to fetch dev_net()") fixed the same pattern in __skb_flow_dissect()?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Michael S. Tsirkin
maintainerPATCH v4 6/7 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 | 30 +++++++++++++++++++++++++++++ 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, 46 insertions(+), 53 deletions(-)
include/linux/ip.h +30 -0
@@ -56,4 +56,34 @@ 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. +* +* Return: the validated IPv4 header, or NULL if truncated or +* malformed. +*/ +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_validate set to 0. */
@@ -157,7 +131,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
Willem de Bruijn
maintainerRe: [PATCH net v4 6/7] net: add ip4_parse()
Quoted 28 lines · Michael S. Tsirkin
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.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]>Reviewed-by Björn Töpel <[email protected]>Reviewed-by Phil Sutter <[email protected]>--- include/linux/ip.h | 30 +++++++++++++++++++++++++++++ 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, 46 insertions(+), 53 deletions(-)include/linux/ip.h +0
-0
include/linux/ip.h static inline void iph_set_totlen(struct iphdr *iph, unsigned int len) · line 61
Quoted 6 lines
include/linux/ip.h +3
-0@@ -56,4 +56,34 @@ 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
This only pulls sizeof(struct iphdr).
To match function comment and generally be robust: pull again if ihl != 0.
Apparently virtio_net_hdr_tnl_to_skb will set skb->transport_header to the thoff reported here, which may be beyond the end of the linear packet.
Quoted 197 lines
include/linux/ip.h +27
-0+* @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. +* +* Return: the validated IPv4 header, or NULL if truncated or +* malformed. +*/ +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_validate set to 0. */ @@ -157,7 +131,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 v4 6/7] net: add ip4_parse()
In reply to Michael S. TsirkinSashiko 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=6
Michael S. Tsirkin
maintainerPATCH v4 7/7 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
@@ -609,8 +609,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 ;
@@ -630,7 +630,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 v4 7/7] 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=7
Willem de Bruijn
maintainerRe: [PATCH net v4 1/7] net: account for ingress-resolved virtio net hdr state
In reply to Michael S. TsirkinQuoted 91 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. The um vector raw transport even ignored the return value
altogether: rejected packets went on into the stack with their metadata unvalidated, so check it there as well.Have tun, tap and
virtio_netcount these events per device asrx_csum_sw, exposed via ethtool -S.Signed-off-by Michael S. Tsirkin <[email protected]>--- arch/um/drivers/vector_transports.c | 4 ++- drivers/net/ipvlan/ipvtap.c | 36 +++++++++++++++++++++++++ drivers/net/macvtap.c | 36 +++++++++++++++++++++++++ drivers/net/tap.c | 8 ++++-- drivers/net/tun.c | 41 +++++++++++++++++++++++++++-- drivers/net/virtio_net.c | 15 ++++++++--- include/linux/if_tap.h | 1 + net/packet/af_packet.c | 4 +-- 8 files changed, 135 insertions(+), 10 deletions(-)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 ; }drivers/net/ipvlan/ipvtap.c +27
-0@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only #include <linux/etherdevice.h> +#include <linux/ethtool.h> #include "ipvlan.h" #include <linux/if_vlan.h> #include <linux/if_tap.h> @@ -74,6 +75,32 @@ static void ipvtap_update_features(struct tap_dev *tap,netdev_update_features (vlan->dev); } +static int ipvtap_get_sset_count (struct net_device *dev,int sset) +{ +return sset ==ETH_SS_STATS ?1 : -EOPNOTSUPP ; +} + +static void ipvtap_get_strings (struct net_device *dev,u32 stringset, +u8 *data) +{ +if (stringset ==ETH_SS_STATS ) +ethtool_puts (&data,"rx_csum_sw" ); +} + +static void ipvtap_get_ethtool_stats (struct net_device *dev, +struct ethtool_stats *stats, +u64 *data) +{ +struct ipvtap_dev *vlantap =netdev_priv (dev); + + data[0 ] =atomic64_read (&vlantap->tap.rx_csum_sw); +} + +/* checkpatch warns but this can't be const: cloned from ipvlan's +* ops at newlink time, with the statistics above added. +*/ +static struct ethtool_ops ipvtap_ethtool_ops; +static int ipvtap_newlink (struct net_device *dev,struct rtnl_newlink_params *params,struct netlink_ext_ack *extack)@@ -91,6 +118,15 @@ static int ipvtap_newlink(struct net_device *dev, vlantap->tap.update_features = ipvtap_update_features; vlantap->tap.count_rx_dropped = ipvtap_count_rx_dropped;
drivers/net/ipvlan/ipvtap.c static int ipvtap_newlink(struct net_device *dev, · line 129
Quoted 10 lines
drivers/net/ipvlan/ipvtap.c +9
-0+/* ipvlan's ethtool ops are already installed at this point; +* clone them and add the tap statistics on top. +*/ + ipvtap_ethtool_ops = *dev->ethtool_ops; + ipvtap_ethtool_ops.get_sset_count = ipvtap_get_sset_count; + ipvtap_ethtool_ops.get_strings = ipvtap_get_strings; + ipvtap_ethtool_ops.get_ethtool_stats = ipvtap_get_ethtool_stats; + dev->ethtool_ops = &ipvtap_ethtool_ops; +
LLM:
This cloning pattern in ipvtap_newlink (and identically in macvtap_newlink below) has serious concurrency and hardening issues:
1. Concurrent Data Race: ipvtap_ethtool_ops is a single static global struct. Although newlink runs under rtnl_lock(), ethtool readers on existing devices (e.g. netlink stats_prepare_data() and strset_prepare_data()) run under rcu_read_lock() without RTNL. Whenever a new ipvtap/macvtap device is created, ipvtap_ethtool_ops = *dev->ethtool_ops overwrites the global ops table in-place, temporarily clearing get_sset_count and get_strings to NULL before re-populating them. Any concurrent ethtool query on an existing device can observe NULL or torn function pointers.
2. Writable Function Pointer Table: ethtool_ops contains function pointers and should reside in .rodata (const) to prevent kernel exploit targets.
3. Hardcoded Set Count: ipvtap_get_sset_count returns 1 unconditionally; if the lower device ever adds ethtool stats in the future, they are completely masked.
Instead of dynamically mutating a non-const global ops struct on every newlink, either:
• Export the base ops from macvlan/ipvlan so tap can declare a const struct ethtool_ops directly, or
• At the very least, initialize it once (e.g. guarded by a check or at module init) so it is not re-clobbered while readers are active.
Additionally, in general, ipvlan/macvlan/tun/tap changes are preferred to be individual patches.
I don't think this is as important as the main patches 4 and 5. If you want to submit quickly, I would drop/postpone these second order features. Maybe go back to the previous tun-only counter for now, if you consider that important enough.
Quoted 83 lines
drivers/net/ipvlan/ipvtap.c +0
-0err =netdev_rx_handler_register (dev, tap_handle_frame, &vlantap->tap);if (err)return err;drivers/net/macvtap.c +36
-0@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only #include <linux/etherdevice.h> +#include <linux/ethtool.h> #include <linux/if_macvlan.h> #include <linux/if_tap.h> #include <linux/if_vlan.h> @@ -78,6 +79,32 @@ static void macvtap_update_features(struct tap_dev *tap,netdev_update_features (vlan->dev); } +static int macvtap_get_sset_count (struct net_device *dev,int sset) +{ +return sset ==ETH_SS_STATS ?1 : -EOPNOTSUPP ; +} + +static void macvtap_get_strings (struct net_device *dev,u32 stringset, +u8 *data) +{ +if (stringset ==ETH_SS_STATS ) +ethtool_puts (&data,"rx_csum_sw" ); +} + +static void macvtap_get_ethtool_stats (struct net_device *dev, +struct ethtool_stats *stats, +u64 *data) +{ +struct macvtap_dev *vlantap =netdev_priv (dev); + + data[0 ] =atomic64_read (&vlantap->tap.rx_csum_sw); +} + +/* checkpatch warns but this can't be const: cloned from macvlan's +* ops at newlink time, with the statistics above added. +*/ +static struct ethtool_ops macvtap_ethtool_ops; +static int macvtap_newlink (struct net_device *dev,struct rtnl_newlink_params *params,struct netlink_ext_ack *extack)@@ -99,6 +126,15 @@ static int macvtap_newlink(struct net_device *dev, vlantap->tap.count_rx_dropped = macvtap_count_rx_dropped; vlantap->tap.update_features = macvtap_update_features; +/* macvlan's ethtool ops are already installed at this point; +* clone them and add the tap statistics on top. +*/ + macvtap_ethtool_ops = *dev->ethtool_ops; + macvtap_ethtool_ops.get_sset_count = macvtap_get_sset_count; + macvtap_ethtool_ops.get_strings = macvtap_get_strings; + macvtap_ethtool_ops.get_ethtool_stats = macvtap_get_ethtool_stats; + dev->ethtool_ops = &macvtap_ethtool_ops; + err =netdev_rx_handler_register (dev, tap_handle_frame, &vlantap->tap);if (err)return err;drivers/net/tap.c +3
-1@@ -649,11 +649,13 @@ 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; } +if (err >0 ) +atomic64_inc (&tap->rx_csum_sw); }skb_probe_transport_header (skb);
drivers/net/tap.c static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp) · line 1094
Quoted 10 lines
drivers/net/tap.c +3
-1@@ -1084,10 +1086,12 @@ 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; } +if (err >0 ) +atomic64_inc (&tap->rx_csum_sw);
(repeat) reset err to 0 in all paths.
Quoted 126 lines
drivers/net/tap.c +0
-0}skb_probe_transport_header (skb);drivers/net/tun.c +39
-2@@ -210,6 +210,7 @@ struct tun_struct {u32 flow_count;u32 rx_batched;atomic_long_t rx_frame_errors; +atomic64_t rx_csum_sw;struct 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 ; } -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 ) { +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;skb_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 ) +atomic64_inc (&tun->rx_csum_sw); skb->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 +7
-3@@ -125,6 +125,7 @@ struct virtnet_rq_stats {u64_stats_t packets;u64_stats_t bytes;u64_stats_t drops; +u64_stats_t rx_csum_sw;u64_stats_t xdp_packets;u64_stats_t xdp_tx;u64_stats_t xdp_redirects;@@ -158,6 +159,7 @@ static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {VIRTNET_RQ_STAT ("drops" , drops), +VIRTNET_RQ_STAT ("rx_csum_sw" , rx_csum_sw),VIRTNET_RQ_STAT ("xdp_packets" , xdp_packets),VIRTNET_RQ_STAT ("xdp_tx" , xdp_tx),VIRTNET_RQ_STAT ("xdp_redirects" , xdp_redirects),@@ -2502,6 +2504,7 @@ static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue * {struct virtio_net_common_hdr *hdr;struct net_device *dev = vi->dev; +int ret; hdr =skb_vnet_common_hdr (skb);if (dev->features &NETIF_F_RXHASH && vi->has_rss_hash_report)@@ -2517,15 +2520,21 @@ static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue * skb->dev = dev;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))) { + ret =virtio_net_hdr_tnl_to_skb (skb, &hdr->tnl_hdr, vi->rx_tnl, + vi->rx_tnl_csum, +virtio_is_little_endian (vi->vdev)); +if (ret <0 ) { net_warn_ratelimited("%s: bad gso: type: %x, size: %u, flags %x tunnel %d tnl csum %d\n",
drivers/net/virtio_net.c static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue * · line 2537
Quoted 10 lines
drivers/net/virtio_net.c +5
-0dev->name, hdr->hdr.gso_type, hdr->hdr.gso_size, hdr->hdr.flags, vi->rx_tnl, vi->rx_tnl_csum); goto frame_err; } +if (ret >0 ) { +u64_stats_update_begin (&rq->stats.syncp); +u64_stats_inc (&rq->stats.rx_csum_sw); +u64_stats_update_end (&rq->stats.syncp); + }
Because rx_csum_sw was added to virtnet_rq_stats_desc[], virtnet_poll() already iterates through virtnet_rq_stats_desc[] and syncs each field from the local stack stats into rq->stats at the end of the NAPI polling cycle inside a single u64_stats_update_begin/end(&rq-
Quoted 1 line
stats.syncp).
Calling u64_stats_update_begin/end(&rq->stats.syncp) directly per-packet in virtnet_receive_done() bypasses that batching pattern. While SW csum is off the fast path, it is inconsistent with how other rq stats are handled.
Quoted 40 lines
drivers/net/virtio_net.c +0
-0skb_record_rx_queue (skb,vq2rxq (rq->vq)); skb->protocol =eth_type_trans (skb, dev);include/linux/if_tap.h +1
-0@@ -41,6 +41,7 @@ struct tap_dev {struct list_head queue_list;int numvtaps;int numqueues; +atomic64_t rx_csum_sw;netdev_features_t tap_features;int minor;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 v4 1/7] net: account for ingress-resolved virtio net hdr state
Quoted 120 lines · Willem de Bruijn
Quoted 91 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. The um vector raw transport even ignored the return value
altogether: rejected packets went on into the stack with their metadata unvalidated, so check it there as well.Have tun, tap and
virtio_netcount these events per device asrx_csum_sw, exposed via ethtool -S.Signed-off-by Michael S. Tsirkin <[email protected]>--- arch/um/drivers/vector_transports.c | 4 ++- drivers/net/ipvlan/ipvtap.c | 36 +++++++++++++++++++++++++ drivers/net/macvtap.c | 36 +++++++++++++++++++++++++ drivers/net/tap.c | 8 ++++-- drivers/net/tun.c | 41 +++++++++++++++++++++++++++-- drivers/net/virtio_net.c | 15 ++++++++--- include/linux/if_tap.h | 1 + net/packet/af_packet.c | 4 +-- 8 files changed, 135 insertions(+), 10 deletions(-)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 ; }drivers/net/ipvlan/ipvtap.c +27
-0@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only #include <linux/etherdevice.h> +#include <linux/ethtool.h> #include "ipvlan.h" #include <linux/if_vlan.h> #include <linux/if_tap.h> @@ -74,6 +75,32 @@ static void ipvtap_update_features(struct tap_dev *tap,netdev_update_features (vlan->dev); } +static int ipvtap_get_sset_count (struct net_device *dev,int sset) +{ +return sset ==ETH_SS_STATS ?1 : -EOPNOTSUPP ; +} + +static void ipvtap_get_strings (struct net_device *dev,u32 stringset, +u8 *data) +{ +if (stringset ==ETH_SS_STATS ) +ethtool_puts (&data,"rx_csum_sw" ); +} + +static void ipvtap_get_ethtool_stats (struct net_device *dev, +struct ethtool_stats *stats, +u64 *data) +{ +struct ipvtap_dev *vlantap =netdev_priv (dev); + + data[0 ] =atomic64_read (&vlantap->tap.rx_csum_sw); +} + +/* checkpatch warns but this can't be const: cloned from ipvlan's +* ops at newlink time, with the statistics above added. +*/ +static struct ethtool_ops ipvtap_ethtool_ops; +static int ipvtap_newlink (struct net_device *dev,struct rtnl_newlink_params *params,struct netlink_ext_ack *extack)@@ -91,6 +118,15 @@ static int ipvtap_newlink(struct net_device *dev, vlantap->tap.update_features = ipvtap_update_features; vlantap->tap.count_rx_dropped = ipvtap_count_rx_dropped;drivers/net/ipvlan/ipvtap.c static int ipvtap_newlink(struct net_device *dev, · line 129
Quoted 10 lines
drivers/net/ipvlan/ipvtap.c +9
-0+/* ipvlan's ethtool ops are already installed at this point; +* clone them and add the tap statistics on top. +*/ + ipvtap_ethtool_ops = *dev->ethtool_ops; + ipvtap_ethtool_ops.get_sset_count = ipvtap_get_sset_count; + ipvtap_ethtool_ops.get_strings = ipvtap_get_strings; + ipvtap_ethtool_ops.get_ethtool_stats = ipvtap_get_ethtool_stats; + dev->ethtool_ops = &ipvtap_ethtool_ops; +LLM:
This cloning pattern in
ipvtap_newlink(and identically inmacvtap_newlinkbelow) has serious concurrency and hardening issues:
1. Concurrent Data Race:ipvtap_ethtool_opsis a single static global struct. Although newlink runs underrtnl_lock(), ethtool readers on existing devices (e.g. netlinkstats_prepare_data()andstrset_prepare_data()) run underrcu_read_lock()without RTNL. Whenever a new ipvtap/macvtap device is created,ipvtap_ethtool_ops=*dev->ethtool_opsoverwrites the global ops table in-place, temporarily clearingget_sset_countandget_stringsto NULL before re-populating them. Any concurrent ethtool query on an existing device can observe NULL or torn function pointers.
2. Writable Function Pointer Table:ethtool_opscontains function pointers and should reside in .rodata (const) to prevent kernel exploit targets.
3. Hardcoded Set Count:ipvtap_get_sset_countreturns 1 unconditionally; if the lower device ever adds ethtool stats in the future, they are completely masked.Instead of dynamically mutating a non-const global ops struct on every newlink, either:
• Export the base ops from macvlan/ipvlan so tap can declare a const structethtool_opsdirectly, or
• At the very least, initialize it once (e.g. guarded by a check or at module init) so it is not re-clobbered while readers are active.
Quoted 7 lines
Additionally, in general, ipvlan/macvlan/tun/tap changes are preferred to be individual patches.
I don't think this is as important as the main patches 4 and 5. If you want to submit quickly, I would drop/postpone these second order features. Maybe go back to the previous tun-only counter for now, if you consider that important enough.
ok I will do tun only.
Quoted 280 lines
Quoted 83 lines
drivers/net/ipvlan/ipvtap.c +0
-0err =netdev_rx_handler_register (dev, tap_handle_frame, &vlantap->tap);if (err)return err;drivers/net/macvtap.c +36
-0@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only #include <linux/etherdevice.h> +#include <linux/ethtool.h> #include <linux/if_macvlan.h> #include <linux/if_tap.h> #include <linux/if_vlan.h> @@ -78,6 +79,32 @@ static void macvtap_update_features(struct tap_dev *tap,netdev_update_features (vlan->dev); } +static int macvtap_get_sset_count (struct net_device *dev,int sset) +{ +return sset ==ETH_SS_STATS ?1 : -EOPNOTSUPP ; +} + +static void macvtap_get_strings (struct net_device *dev,u32 stringset, +u8 *data) +{ +if (stringset ==ETH_SS_STATS ) +ethtool_puts (&data,"rx_csum_sw" ); +} + +static void macvtap_get_ethtool_stats (struct net_device *dev, +struct ethtool_stats *stats, +u64 *data) +{ +struct macvtap_dev *vlantap =netdev_priv (dev); + + data[0 ] =atomic64_read (&vlantap->tap.rx_csum_sw); +} + +/* checkpatch warns but this can't be const: cloned from macvlan's +* ops at newlink time, with the statistics above added. +*/ +static struct ethtool_ops macvtap_ethtool_ops; +static int macvtap_newlink (struct net_device *dev,struct rtnl_newlink_params *params,struct netlink_ext_ack *extack)@@ -99,6 +126,15 @@ static int macvtap_newlink(struct net_device *dev, vlantap->tap.count_rx_dropped = macvtap_count_rx_dropped; vlantap->tap.update_features = macvtap_update_features; +/* macvlan's ethtool ops are already installed at this point; +* clone them and add the tap statistics on top. +*/ + macvtap_ethtool_ops = *dev->ethtool_ops; + macvtap_ethtool_ops.get_sset_count = macvtap_get_sset_count; + macvtap_ethtool_ops.get_strings = macvtap_get_strings; + macvtap_ethtool_ops.get_ethtool_stats = macvtap_get_ethtool_stats; + dev->ethtool_ops = &macvtap_ethtool_ops; + err =netdev_rx_handler_register (dev, tap_handle_frame, &vlantap->tap);if (err)return err;drivers/net/tap.c +3
-1@@ -649,11 +649,13 @@ 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; } +if (err >0 ) +atomic64_inc (&tap->rx_csum_sw); }skb_probe_transport_header (skb);drivers/net/tap.c static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp) · line 1094
Quoted 10 lines
drivers/net/tap.c +3
-1@@ -1084,10 +1086,12 @@ 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; } +if (err >0 ) +atomic64_inc (&tap->rx_csum_sw);(repeat) reset err to 0 in all paths.
Quoted 126 lines
drivers/net/tap.c +0
-0}skb_probe_transport_header (skb);drivers/net/tun.c +39
-2@@ -210,6 +210,7 @@ struct tun_struct {u32 flow_count;u32 rx_batched;atomic_long_t rx_frame_errors; +atomic64_t rx_csum_sw;struct 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 ; } -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 ) { +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;skb_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 ) +atomic64_inc (&tun->rx_csum_sw); skb->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 +7
-3@@ -125,6 +125,7 @@ struct virtnet_rq_stats {u64_stats_t packets;u64_stats_t bytes;u64_stats_t drops; +u64_stats_t rx_csum_sw;u64_stats_t xdp_packets;u64_stats_t xdp_tx;u64_stats_t xdp_redirects;@@ -158,6 +159,7 @@ static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {VIRTNET_RQ_STAT ("drops" , drops), +VIRTNET_RQ_STAT ("rx_csum_sw" , rx_csum_sw),VIRTNET_RQ_STAT ("xdp_packets" , xdp_packets),VIRTNET_RQ_STAT ("xdp_tx" , xdp_tx),VIRTNET_RQ_STAT ("xdp_redirects" , xdp_redirects),@@ -2502,6 +2504,7 @@ static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue * {struct virtio_net_common_hdr *hdr;struct net_device *dev = vi->dev; +int ret; hdr =skb_vnet_common_hdr (skb);if (dev->features &NETIF_F_RXHASH && vi->has_rss_hash_report)@@ -2517,15 +2520,21 @@ static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue * skb->dev = dev;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))) { + ret =virtio_net_hdr_tnl_to_skb (skb, &hdr->tnl_hdr, vi->rx_tnl, + vi->rx_tnl_csum, +virtio_is_little_endian (vi->vdev)); +if (ret <0 ) { net_warn_ratelimited("%s: bad gso: type: %x, size: %u, flags %x tunnel %d tnl csum %d\n",drivers/net/virtio_net.c static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue * · line 2537
Quoted 10 lines
drivers/net/virtio_net.c +5
-0dev->name, hdr->hdr.gso_type, hdr->hdr.gso_size, hdr->hdr.flags, vi->rx_tnl, vi->rx_tnl_csum); goto frame_err; } +if (ret >0 ) { +u64_stats_update_begin (&rq->stats.syncp); +u64_stats_inc (&rq->stats.rx_csum_sw); +u64_stats_update_end (&rq->stats.syncp); + }Because
rx_csum_swwas added to virtnet_rq_stats_desc[],virtnet_poll()already iterates through virtnet_rq_stats_desc[] and syncs each field from the local stack stats intorq->statsat the end of the NAPI polling cycle inside a single u64_stats_update_begin/end(&rq-Quoted 1 line
stats.syncp).
Calling u64_stats_update_begin/end(&rq->stats.syncp) directly per-packet in
virtnet_receive_done()bypasses that batching pattern. While SW csum is off the fast path, it is inconsistent with how other rq stats are handled.Quoted 40 lines
drivers/net/virtio_net.c +0
-0skb_record_rx_queue (skb,vq2rxq (rq->vq)); skb->protocol =eth_type_trans (skb, dev);include/linux/if_tap.h +1
-0@@ -41,6 +41,7 @@ struct tap_dev {struct list_head queue_list;int numvtaps;int numqueues; +atomic64_t rx_csum_sw;netdev_features_t tap_features;int minor;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 v4 1/7] net: account for ingress-resolved virtio net hdr state
In reply to Michael S. TsirkinThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] virtio_net: duplicate rx_ prefix for rx_csum_sw in virtnet_rq_stats_desc - [Medium] net: live…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] virtio_net: duplicate rx_ prefix for rx_csum_sw in virtnet_rq_stats_desc
- [Medium] net: live ethtool_ops overwritten in ipvtap_newlink() and macvtap_newlink() --
commit aec871fb755137e08ae8b9205b5e122143828b6d
Author: Michael S. Tsirkin <[email protected]>
net: account for ingress-resolved virtio net hdr state
This commit updates callers of virtio_net_hdr_to_skb() and virtio_net_hdr_tnl_to_skb() to check only for negative return values as errors, and adds an rx_csum_sw ethtool statistic to tun, tap (ipvtap and macvtap), and virtio_net to count packets requiring software checksum resolution at ingress.
Quoted 4 lines
drivers/net/ipvlan/ipvtap.c +0
-0
[ ... ]
Quoted 20 lines
drivers/net/ipvlan/ipvtap.c +13
-0+/* checkpatch warns but this can't be const: cloned from ipvlan's +* ops at newlink time, with the statistics above added. +*/ +static struct ethtool_ops ipvtap_ethtool_ops; +static int ipvtap_newlink (struct net_device *dev,struct rtnl_newlink_params *params,struct netlink_ext_ack *extack)@@ -91,6 +118,15 @@ static int ipvtap_newlink(struct net_device *dev, vlantap->tap.update_features = ipvtap_update_features; vlantap->tap.count_rx_dropped = ipvtap_count_rx_dropped; +/* ipvlan's ethtool ops are already installed at this point; +* clone them and add the tap statistics on top. +*/ + ipvtap_ethtool_ops = *dev->ethtool_ops; + ipvtap_ethtool_ops.get_sset_count = ipvtap_get_sset_count; + ipvtap_ethtool_ops.get_strings = ipvtap_get_strings; + ipvtap_ethtool_ops.get_ethtool_stats = ipvtap_get_ethtool_stats; + dev->ethtool_ops = &ipvtap_ethtool_ops;
[ ... ]
Quoted 4 lines
drivers/net/macvtap.c +0
-0
[ ... ]
Quoted 10 lines
drivers/net/macvtap.c +5
-0+/* checkpatch warns but this can't be const: cloned from macvlan's +* ops at newlink time, with the statistics above added. +*/ +static struct ethtool_ops macvtap_ethtool_ops; +static int macvtap_newlink (struct net_device *dev,struct rtnl_newlink_params *params,struct netlink_ext_ack *extack)@@ -99,6 +126,15 @@ static int macvtap_newlink(struct net_device *dev, vlantap->tap.count_rx_dropped = macvtap_count_rx_dropped;
drivers/net/macvtap.c static int macvtap_newlink(struct net_device *dev, · line 136
Quoted 10 lines
drivers/net/macvtap.c +8
-0vlantap->tap.update_features = macvtap_update_features; +/* macvlan's ethtool ops are already installed at this point; +* clone them and add the tap statistics on top. +*/ + macvtap_ethtool_ops = *dev->ethtool_ops; + macvtap_ethtool_ops.get_sset_count = macvtap_get_sset_count; + macvtap_ethtool_ops.get_strings = macvtap_get_strings; + macvtap_ethtool_ops.get_ethtool_stats = macvtap_get_ethtool_stats; + dev->ethtool_ops = &macvtap_ethtool_ops;
[Severity: Medium]
Can overwriting the shared global ipvtap_ethtool_ops in drivers/net/ipvlan/ipvtap.c:ipvtap_newlink() and macvtap_ethtool_ops in drivers/net/macvtap.c:macvtap_newlink() race with concurrent readers on already-registered devices?
When the first ipvtap or macvtap interface is created and registered, its dev->ethtool_ops points to the shared static ipvtap_ethtool_ops or macvtap_ethtool_ops structure.
When a second ipvtap or macvtap interface is created via rtnl_newlink_create(), ipvtap_setup() or macvtap_setup() first sets the new device's dev->ethtool_ops to &ipvlan_ethtool_ops or &macvlan_ethtool_ops (where get_sset_count, get_strings, and get_ethtool_stats are null). Then ipvtap_newlink() or macvtap_newlink() copies *dev->ethtool_ops over the shared global ipvtap_ethtool_ops or macvtap_ethtool_ops structure in-place before restoring the three statistics callbacks.
Meanwhile, several paths read dev->ethtool_ops on the first registered interface without holding rtnl_lock():
net/ethtool/common.c:__ethtool_get_ts_info() {
...
const struct ethtool_ops *ops = dev->ethtool_ops;
struct phy_device *phydev = dev->phydev;
...
} else if (ops->get_ts_info) {
err = ops->get_ts_info(dev, info);
...
}which is called under rcu_read_lock() from sock_timestamping_bind_phc()->ethtool_get_phc_vclocks(), as well as net/ethtool/netlink.c:ethnl_default_set_doit() calling ops->set_validate() before acquiring rtnl_lock(), and net/core/net-sysfs.c:speed_show() and duplex_show() reading netdev->ethtool_ops->get_link_ksettings before sysfs_rtnl_lock().
Could ipvtap_ethtool_ops and macvtap_ethtool_ops be initialized once at module init time (for example in ipvtap_init() and macvtap_init()) instead of being mutated on each newlink call?
[ ... ]
Quoted 13 lines
drivers/net/virtio_net.c +1
-0@@ -125,6 +125,7 @@ struct virtnet_rq_stats {u64_stats_t packets;u64_stats_t bytes;u64_stats_t drops; +u64_stats_t rx_csum_sw;u64_stats_t xdp_packets;u64_stats_t xdp_tx;u64_stats_t xdp_redirects;
drivers/net/virtio_net.c static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = { · line 165
Quoted 7 lines
drivers/net/virtio_net.c +1
-0@@ -158,6 +159,7 @@ static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {VIRTNET_RQ_STAT ("drops" , drops), +VIRTNET_RQ_STAT ("rx_csum_sw" , rx_csum_sw),VIRTNET_RQ_STAT ("xdp_packets" , xdp_packets),VIRTNET_RQ_STAT ("xdp_tx" , xdp_tx),VIRTNET_RQ_STAT ("xdp_redirects" , xdp_redirects),
[Severity: Medium]
Will defining the descriptor string as "rx_csum_sw" in drivers/net/virtio_net.c:virtnet_rq_stats_desc cause ethtool -S to report a duplicated "rx_" prefix?
When userspace queries ethtool -S strings via virtnet_get_strings()->virtnet_get_stats_string()->virtnet_stats_sprintf(), virtnet_get_stats_string() automatically prepends "rx_" and "rx%u_" to every entry in virtnet_rq_stats_desc:
drivers/net/virtio_net.c:virtnet_get_stats_string() {
...
if (type == VIRTNET_Q_TYPE_RX) {
fmt = "rx%u_%s";
noq_fmt = "rx_%s";
desc = &virtnet_rq_stats_desc[0];
num = ARRAY_SIZE(virtnet_rq_stats_desc);
virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
...
}Because of this formatting, "rx_csum_sw" becomes "rx_rx_csum_sw" for the device total and "rx0_rx_csum_sw" for the per-queue counters. Should the descriptor string in virtnet_rq_stats_desc be "csum_sw" to match the other entries and produce "rx_csum_sw"?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 10 lines · Michael S. Tsirkin
v4 and I'm pretty tired of this family of issues.
Willem suggested more cleanups, but I don't think I will be able to dedicate much more time to this, there's a pile of fixes I queued that need to get tested and go into rc2, and a bunch of qemu features that need review.
Hopefully if any changes are required this can be done by patches on top, or if someone wants to take over this, very welcome.
Happy to help if needed.
Again, I was able to test this patchset minus the UM portions and verify that it fixes my tests.
--chris
Quoted 155 lines
------
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 probabably 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 fix
um: vector: trim skbs before verifying the header
- fixes the um receive path: without it the checksum would be completed over the unreceived tail of the buffer
virtio_net: parse tunnelcsum_startlittle-endian
- bugfix, a dependency of validating the header
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 Willemnet: 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)um: vector: trim skbs before verifying the header
- new patch, found by code reviewChanges since v3:
net: account for ingress-resolved virtio net hdr state
- um: vector: honor virtio net hdr conversion failures folded in (Björn Töpel)
- count on tap andvirtio_net, too (Willem de Bruijn)net: resolve uncommon partial checksums at ingress
- walk the frame on loopback, too:loopback_xmit()runseth_type_trans()on it (Willem de Bruijn)
- return the mismatch handling directly, GSO is implied in the tunnel path so drop there outright, and fold the helper into its one remaining caller (Willem de Bruijn)
- read the IP version through struct iphdr (Willem de Bruijn)
- document theipv6_find_hdr()flags choicenet: add a sysctl to opt out of virtio net hdr checksum validation
- renamed tovirtio_net_hdr_validate, in case more validation is gated by the same knob later (Willem de Bruijn)
- point .data at the value like the neighboring entries, the per-netns registration rebases it, and the handler reads the value directly - noctl_tablecopy (Björn Töpel, Willem de Bruijn)
- check the opt-out before parsing: with the knob off, packets the parser cannot handle were still dropped (Willem de Bruijn)
- includevirtio_net.hinnet/core/dev.cso the static key definition sees its declaration (build warning reported by Jakub Kicinski)net: add
ip4_parse()
- add the missing kernel-doc Return: section
(Jakub Kicinski)Tested: full suite on the final tree, tunnel included. um part compiled only.
Michael S. Tsirkin (7):
net: account for ingress-resolved virtio net hdr state
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/ipvlan/ipvtap.c | 36 ++++ drivers/net/macvtap.c | 36 ++++ drivers/net/tap.c | 8 +- drivers/net/tun.c | 41 ++++- drivers/net/virtio_net.c | 14 +- include/linux/if_tap.h | 1 + include/linux/ip.h | 30 ++++ include/linux/virtio_net.h | 279 ++++++++++++++++++++++++++++++- include/net/netns/core.h | 1 + net/bridge/br_netfilter_hooks.c | 15 +- net/core/dev.c | 7 + net/core/net_namespace.c | 1 + net/core/sysctl_net_core.c | 50 ++++++ net/ipv4/ip_input.c | 13 +- net/ipv4/netfilter/nf_reject_ipv4.c | 13 +- net/packet/af_packet.c | 4 +- 19 files changed, 533 insertions(+), 45 deletions(-)Signature · 1 line
MST