Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] net/packet: framing mismatch in packet_vnet_hdr_to_skb() via
  dev->type
- [Medium] net: packet: skb->protocol set after validation in
  packet_vnet_hdr_to_skb()
--

commit 4a97d36173ad7a2c090af1623638b2b508d7f3b0
Author: Michael S. Tsirkin <[email protected]>

net: resolve uncommon partial checksums at ingress

This commit validates virtio_net_hdr csum_start and csum_offset against
parsed TCP or UDP headers at ingress, retaining CHECKSUM_PARTIAL only when
they match and completing non-GSO checksums or dropping GSO packets when
they do not.

> diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
> index 930e85e4bf739..c07a0196911b5 100644
> --- a/net/packet/af_packet.c
> +++ b/net/packet/af_packet.c
> @@ -2778,6 +2778,16 @@ static int tpacket_parse_header(struct packet_sock 
> *po, void *frame,
>       return tp_len;
>  }
>  
> +/* The device says how its frames start. */
> +static int packet_vnet_hdr_to_skb(struct sk_buff *skb,
> +                               const struct virtio_net_hdr *vnet_hdr)
> +{
> +     bool has_eth = skb->dev->type == ARPHRD_ETHER ||
> +                    skb->dev->type == ARPHRD_LOOPBACK;
> +
> +     return virtio_net_hdr_to_skb(skb, vnet_hdr, has_eth, vio_le());
> +}

[Severity: Medium]
Can deriving has_eth from skb->dev->type cause packet_vnet_hdr_to_skb() to
parse a different frame layout than packet_snd(), tpacket_fill_skb() and
packet_parse_headers()?

As noted above virtio_net_l3_start(), ioctl(TUNSETLINK) changes dev->type on
a tun or tap device without changing dev->hard_header_len or dev->header_ops.
On an IFF_TUN device whose dev->type is set to ARPHRD_ETHER via TUNSETLINK,
packet_snd() places the L3 header at offset 0, but packet_vnet_hdr_to_skb()
passes has_eth = true.  virtio_net_l3_start() then treats the first 14 bytes
of the L3 header as an Ethernet header and validates csum_start and
csum_offset against a fake L3/L4 header inside the payload, retaining
CHECKSUM_PARTIAL while packet_parse_headers() leaves skb->network_header at
offset 0 and derives skb->protocol from the real L3 header via
ip_tunnel_parse_protocol().

In addition, packet_vnet_hdr_to_skb() includes ARPHRD_LOOPBACK in has_eth, so
virtio_net_l3_start() walks past VLAN tags at ETH_HLEN when validating the
headers, whereas packet_parse_headers() only advances skb->network_header past
VLAN tags when skb->dev->type == ARPHRD_ETHER:

net/packet/af_packet.c:packet_parse_headers() {
        ...
        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);
        ...
}

> @@ -2941,7 +2951,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()) < 
> 0) {
> +                     if (packet_vnet_hdr_to_skb(skb, &vnet_hdr) < 0) {
>                               tp_len = -EINVAL;
>                               goto tpacket_error;
>                       }
> @@ -3163,7 +3173,7 @@ static int packet_snd(struct socket *sock, struct 
> msghdr *msg, size_t len)
>               skb->no_fcs = 1;
>  
>       if (vnet_hdr_sz) {
> -             err = virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le());
> +             err = packet_vnet_hdr_to_skb(skb, &vnet_hdr);
>               if (err < 0)
>                       goto out_free;
>               len += vnet_hdr_sz;

[Severity: Medium]
Should packet_parse_headers() and virtio_net_hdr_set_proto() run before
packet_vnet_hdr_to_skb() in tpacket_snd() and packet_snd()?

When the caller passes proto == htons(ETH_P_ALL) or 0, skb->protocol is still
0 when packet_vnet_hdr_to_skb() runs, so virtio_net_l3_start() skips its
skb->protocol consistency check.  After validation succeeds and retains
CHECKSUM_PARTIAL and GSO, packet_parse_headers() and
virtio_net_hdr_set_proto() populate skb->protocol.  Whenever
packet_parse_headers() leaves skb->protocol as 0 (for example on SOCK_DGRAM
sockets, or on SOCK_RAW sockets bound to devices without
header_ops->parse_protocol), virtio_net_hdr_set_proto() unconditionally sets
skb->protocol = htons(ETH_P_IP) for VIRTIO_NET_HDR_GSO_UDP_L4 and
VIRTIO_NET_HDR_GSO_UDP even when virtio_net_csum_partial_ok() just validated
an IPv6 packet, causing downstream GSO and transmit paths to process the IPv6
packet as IPv4.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=4

Reply via email to