linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net v3 0/2] net: prevent infinite loop caused by incorrect proto from virtio_net_hdr_set_proto
@ 2021-03-09 11:30 Balazs Nemeth
  2021-03-09 11:31 ` [PATCH net v3 1/2] net: check if protocol extracted by virtio_net_hdr_set_proto is correct Balazs Nemeth
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Balazs Nemeth @ 2021-03-09 11:30 UTC (permalink / raw)
  To: netdev
  Cc: linux-kernel, mst, jasowang, dsahern, davem, willemb,
	virtualization, bnemeth

These patches prevent an infinite loop for gso packets with a protocol
from virtio net hdr that doesn't match the protocol in the packet.
Note that packets coming from a device without
header_ops->parse_protocol being implemented will not be caught by
the check in virtio_net_hdr_to_skb, but the infinite loop will still
be prevented by the check in the gso layer.

Changes from v2 to v3:
  - Remove unused *eth.
  - Use MPLS_HLEN to also check if the MPLS header length is a multiple
    of four.

Balazs Nemeth (2):
  net: check if protocol extracted by virtio_net_hdr_set_proto is
    correct
  net: avoid infinite loop in mpls_gso_segment when mpls_hlen == 0

 include/linux/virtio_net.h | 7 ++++++-
 net/mpls/mpls_gso.c        | 3 +++
 2 files changed, 9 insertions(+), 1 deletion(-)

--
2.29.2


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH net v3 1/2] net: check if protocol extracted by virtio_net_hdr_set_proto is correct
  2021-03-09 11:30 [PATCH net v3 0/2] net: prevent infinite loop caused by incorrect proto from virtio_net_hdr_set_proto Balazs Nemeth
@ 2021-03-09 11:31 ` Balazs Nemeth
  2021-03-09 14:35   ` Willem de Bruijn
  2021-03-09 11:31 ` [PATCH net v3 2/2] net: avoid infinite loop in mpls_gso_segment when mpls_hlen == 0 Balazs Nemeth
  2021-03-10  0:20 ` [PATCH net v3 0/2] net: prevent infinite loop caused by incorrect proto from virtio_net_hdr_set_proto patchwork-bot+netdevbpf
  2 siblings, 1 reply; 7+ messages in thread
From: Balazs Nemeth @ 2021-03-09 11:31 UTC (permalink / raw)
  To: netdev
  Cc: linux-kernel, mst, jasowang, dsahern, davem, willemb,
	virtualization, bnemeth

For gso packets, virtio_net_hdr_set_proto sets the protocol (if it isn't
set) based on the type in the virtio net hdr, but the skb could contain
anything since it could come from packet_snd through a raw socket. If
there is a mismatch between what virtio_net_hdr_set_proto sets and
the actual protocol, then the skb could be handled incorrectly later
on.

An example where this poses an issue is with the subsequent call to
skb_flow_dissect_flow_keys_basic which relies on skb->protocol being set
correctly. A specially crafted packet could fool
skb_flow_dissect_flow_keys_basic preventing EINVAL to be returned.

Avoid blindly trusting the information provided by the virtio net header
by checking that the protocol in the packet actually matches the
protocol set by virtio_net_hdr_set_proto. Note that since the protocol
is only checked if skb->dev implements header_ops->parse_protocol,
packets from devices without the implementation are not checked at this
stage.

Fixes: 9274124f023b ("net: stricter validation of untrusted gso packets")
Signed-off-by: Balazs Nemeth <bnemeth@redhat.com>
---
 include/linux/virtio_net.h | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/include/linux/virtio_net.h b/include/linux/virtio_net.h
index e8a924eeea3d..6b5fcfa1e555 100644
--- a/include/linux/virtio_net.h
+++ b/include/linux/virtio_net.h
@@ -79,8 +79,13 @@ static inline int virtio_net_hdr_to_skb(struct sk_buff *skb,
 		if (gso_type && skb->network_header) {
 			struct flow_keys_basic keys;
 
-			if (!skb->protocol)
+			if (!skb->protocol) {
+				__be16 protocol = dev_parse_header_protocol(skb);
+
 				virtio_net_hdr_set_proto(skb, hdr);
+				if (protocol && protocol != skb->protocol)
+					return -EINVAL;
+			}
 retry:
 			if (!skb_flow_dissect_flow_keys_basic(NULL, skb, &keys,
 							      NULL, 0, 0, 0,
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH net v3 2/2] net: avoid infinite loop in mpls_gso_segment when mpls_hlen == 0
  2021-03-09 11:30 [PATCH net v3 0/2] net: prevent infinite loop caused by incorrect proto from virtio_net_hdr_set_proto Balazs Nemeth
  2021-03-09 11:31 ` [PATCH net v3 1/2] net: check if protocol extracted by virtio_net_hdr_set_proto is correct Balazs Nemeth
@ 2021-03-09 11:31 ` Balazs Nemeth
  2021-03-09 14:29   ` Willem de Bruijn
  2021-03-09 15:55   ` David Ahern
  2021-03-10  0:20 ` [PATCH net v3 0/2] net: prevent infinite loop caused by incorrect proto from virtio_net_hdr_set_proto patchwork-bot+netdevbpf
  2 siblings, 2 replies; 7+ messages in thread
From: Balazs Nemeth @ 2021-03-09 11:31 UTC (permalink / raw)
  To: netdev
  Cc: linux-kernel, mst, jasowang, dsahern, davem, willemb,
	virtualization, bnemeth

A packet with skb_inner_network_header(skb) == skb_network_header(skb)
and ETH_P_MPLS_UC will prevent mpls_gso_segment from pulling any headers
from the packet. Subsequently, the call to skb_mac_gso_segment will
again call mpls_gso_segment with the same packet leading to an infinite
loop. In addition, ensure that the header length is a multiple of four,
which should hold irrespective of the number of stacked labels.

Signed-off-by: Balazs Nemeth <bnemeth@redhat.com>
---
 net/mpls/mpls_gso.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/net/mpls/mpls_gso.c b/net/mpls/mpls_gso.c
index b1690149b6fa..1482259de9b5 100644
--- a/net/mpls/mpls_gso.c
+++ b/net/mpls/mpls_gso.c
@@ -14,6 +14,7 @@
 #include <linux/netdev_features.h>
 #include <linux/netdevice.h>
 #include <linux/skbuff.h>
+#include <net/mpls.h>
 
 static struct sk_buff *mpls_gso_segment(struct sk_buff *skb,
 				       netdev_features_t features)
@@ -27,6 +28,8 @@ static struct sk_buff *mpls_gso_segment(struct sk_buff *skb,
 
 	skb_reset_network_header(skb);
 	mpls_hlen = skb_inner_network_header(skb) - skb_network_header(skb);
+	if (unlikely(!mpls_hlen || mpls_hlen % MPLS_HLEN))
+		goto out;
 	if (unlikely(!pskb_may_pull(skb, mpls_hlen)))
 		goto out;
 
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH net v3 2/2] net: avoid infinite loop in mpls_gso_segment when mpls_hlen == 0
  2021-03-09 11:31 ` [PATCH net v3 2/2] net: avoid infinite loop in mpls_gso_segment when mpls_hlen == 0 Balazs Nemeth
@ 2021-03-09 14:29   ` Willem de Bruijn
  2021-03-09 15:55   ` David Ahern
  1 sibling, 0 replies; 7+ messages in thread
From: Willem de Bruijn @ 2021-03-09 14:29 UTC (permalink / raw)
  To: Balazs Nemeth
  Cc: Network Development, linux-kernel, Michael S. Tsirkin,
	Jason Wang, David Ahern, David Miller, virtualization

On Tue, Mar 9, 2021 at 6:32 AM Balazs Nemeth <bnemeth@redhat.com> wrote:
>
> A packet with skb_inner_network_header(skb) == skb_network_header(skb)
> and ETH_P_MPLS_UC will prevent mpls_gso_segment from pulling any headers
> from the packet. Subsequently, the call to skb_mac_gso_segment will
> again call mpls_gso_segment with the same packet leading to an infinite
> loop. In addition, ensure that the header length is a multiple of four,
> which should hold irrespective of the number of stacked labels.
>
> Signed-off-by: Balazs Nemeth <bnemeth@redhat.com>

Acked-by: Willem de Bruijn <willemb@google.com>

The compiler will convert that modulo into a cheap & (ETH_HLEN - 1)
test for this constant.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH net v3 1/2] net: check if protocol extracted by virtio_net_hdr_set_proto is correct
  2021-03-09 11:31 ` [PATCH net v3 1/2] net: check if protocol extracted by virtio_net_hdr_set_proto is correct Balazs Nemeth
@ 2021-03-09 14:35   ` Willem de Bruijn
  0 siblings, 0 replies; 7+ messages in thread
From: Willem de Bruijn @ 2021-03-09 14:35 UTC (permalink / raw)
  To: Balazs Nemeth
  Cc: Network Development, linux-kernel, Michael S. Tsirkin,
	Jason Wang, David Ahern, David Miller, virtualization

On Tue, Mar 9, 2021 at 6:32 AM Balazs Nemeth <bnemeth@redhat.com> wrote:
>
> For gso packets, virtio_net_hdr_set_proto sets the protocol (if it isn't
> set) based on the type in the virtio net hdr, but the skb could contain
> anything since it could come from packet_snd through a raw socket. If
> there is a mismatch between what virtio_net_hdr_set_proto sets and
> the actual protocol, then the skb could be handled incorrectly later
> on.
>
> An example where this poses an issue is with the subsequent call to
> skb_flow_dissect_flow_keys_basic which relies on skb->protocol being set
> correctly. A specially crafted packet could fool
> skb_flow_dissect_flow_keys_basic preventing EINVAL to be returned.
>
> Avoid blindly trusting the information provided by the virtio net header
> by checking that the protocol in the packet actually matches the
> protocol set by virtio_net_hdr_set_proto. Note that since the protocol
> is only checked if skb->dev implements header_ops->parse_protocol,
> packets from devices without the implementation are not checked at this
> stage.
>
> Fixes: 9274124f023b ("net: stricter validation of untrusted gso packets")
> Signed-off-by: Balazs Nemeth <bnemeth@redhat.com>

Acked-by: Willem de Bruijn <willemb@google.com>

This still relies entirely on data from the untrusted process. But it
adds the constraint that the otherwise untrusted data at least has to
be consistent, closing one loophole.

As responded in v2, we may want to look at the (few) callers and make
sure that they initialize skb->protocol before the call to
virtio_net_hdr_to_skb where possible. That will avoid this entire
branch.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH net v3 2/2] net: avoid infinite loop in mpls_gso_segment when mpls_hlen == 0
  2021-03-09 11:31 ` [PATCH net v3 2/2] net: avoid infinite loop in mpls_gso_segment when mpls_hlen == 0 Balazs Nemeth
  2021-03-09 14:29   ` Willem de Bruijn
@ 2021-03-09 15:55   ` David Ahern
  1 sibling, 0 replies; 7+ messages in thread
From: David Ahern @ 2021-03-09 15:55 UTC (permalink / raw)
  To: Balazs Nemeth, netdev
  Cc: linux-kernel, mst, jasowang, davem, willemb, virtualization

On 3/9/21 4:31 AM, Balazs Nemeth wrote:
> A packet with skb_inner_network_header(skb) == skb_network_header(skb)
> and ETH_P_MPLS_UC will prevent mpls_gso_segment from pulling any headers
> from the packet. Subsequently, the call to skb_mac_gso_segment will
> again call mpls_gso_segment with the same packet leading to an infinite
> loop. In addition, ensure that the header length is a multiple of four,
> which should hold irrespective of the number of stacked labels.
> 
> Signed-off-by: Balazs Nemeth <bnemeth@redhat.com>
> ---
>  net/mpls/mpls_gso.c | 3 +++
>  1 file changed, 3 insertions(+)
> 


Reviewed-by: David Ahern <dsahern@kernel.org>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH net v3 0/2] net: prevent infinite loop caused by incorrect proto from virtio_net_hdr_set_proto
  2021-03-09 11:30 [PATCH net v3 0/2] net: prevent infinite loop caused by incorrect proto from virtio_net_hdr_set_proto Balazs Nemeth
  2021-03-09 11:31 ` [PATCH net v3 1/2] net: check if protocol extracted by virtio_net_hdr_set_proto is correct Balazs Nemeth
  2021-03-09 11:31 ` [PATCH net v3 2/2] net: avoid infinite loop in mpls_gso_segment when mpls_hlen == 0 Balazs Nemeth
@ 2021-03-10  0:20 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 7+ messages in thread
From: patchwork-bot+netdevbpf @ 2021-03-10  0:20 UTC (permalink / raw)
  To: Balazs Nemeth
  Cc: netdev, linux-kernel, mst, jasowang, dsahern, davem, willemb,
	virtualization

Hello:

This series was applied to netdev/net.git (refs/heads/master):

On Tue,  9 Mar 2021 12:30:59 +0100 you wrote:
> These patches prevent an infinite loop for gso packets with a protocol
> from virtio net hdr that doesn't match the protocol in the packet.
> Note that packets coming from a device without
> header_ops->parse_protocol being implemented will not be caught by
> the check in virtio_net_hdr_to_skb, but the infinite loop will still
> be prevented by the check in the gso layer.
> 
> [...]

Here is the summary with links:
  - [net,v3,1/2] net: check if protocol extracted by virtio_net_hdr_set_proto is correct
    https://git.kernel.org/netdev/net/c/924a9bc362a5
  - [net,v3,2/2] net: avoid infinite loop in mpls_gso_segment when mpls_hlen == 0
    https://git.kernel.org/netdev/net/c/d348ede32e99

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-03-10  0:21 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-09 11:30 [PATCH net v3 0/2] net: prevent infinite loop caused by incorrect proto from virtio_net_hdr_set_proto Balazs Nemeth
2021-03-09 11:31 ` [PATCH net v3 1/2] net: check if protocol extracted by virtio_net_hdr_set_proto is correct Balazs Nemeth
2021-03-09 14:35   ` Willem de Bruijn
2021-03-09 11:31 ` [PATCH net v3 2/2] net: avoid infinite loop in mpls_gso_segment when mpls_hlen == 0 Balazs Nemeth
2021-03-09 14:29   ` Willem de Bruijn
2021-03-09 15:55   ` David Ahern
2021-03-10  0:20 ` [PATCH net v3 0/2] net: prevent infinite loop caused by incorrect proto from virtio_net_hdr_set_proto patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).