diff mbox series

[net] llc: verify mac len before reading mac header

Message ID 20231024194958.3522281-1-willemdebruijn.kernel@gmail.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [net] llc: verify mac len before reading mac header | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net
netdev/fixes_present fail Series targets non-next tree, but doesn't contain any Fixes tags
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1363 this patch: 1363
netdev/cc_maintainers warning 1 maintainers not CCed: kuniyu@amazon.com
netdev/build_clang success Errors and warnings before: 1386 this patch: 1386
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1387 this patch: 1387
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 34 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Willem de Bruijn Oct. 24, 2023, 7:49 p.m. UTC
From: Willem de Bruijn <willemb@google.com>

LLC reads the mac header with eth_hdr without verifying that the skb
has an Ethernet header.

Syzbot was able to enter llc_rcv on a tun device. Tun can insert
packets without mac len and with user configurable skb->protocol
(passing a tun_pi header when not configuring IFF_NO_PI).

    BUG: KMSAN: uninit-value in llc_station_ac_send_test_r net/llc/llc_station.c:81 [inline]
    BUG: KMSAN: uninit-value in llc_station_rcv+0x6fb/0x1290 net/llc/llc_station.c:111
    llc_station_ac_send_test_r net/llc/llc_station.c:81 [inline]
    llc_station_rcv+0x6fb/0x1290 net/llc/llc_station.c:111
    llc_rcv+0xc5d/0x14a0 net/llc/llc_input.c:218
    __netif_receive_skb_one_core net/core/dev.c:5523 [inline]
    __netif_receive_skb+0x1a6/0x5a0 net/core/dev.c:5637
    netif_receive_skb_internal net/core/dev.c:5723 [inline]
    netif_receive_skb+0x58/0x660 net/core/dev.c:5782
    tun_rx_batched+0x3ee/0x980 drivers/net/tun.c:1555
    tun_get_user+0x54c5/0x69c0 drivers/net/tun.c:2002

Add a mac_len test before all three eth_hdr(skb) calls under net/llc.

There are further uses in include/net/llc_pdu.h. All these are
protected by a test skb->protocol == ETH_P_802_2. Which does not
protect against this tun scenario.

But the mac_len test added in this patch in llc_fixup_skb will
indirectly protect those too. That is called from llc_rcv before any
other LLC code.

It is tempting to just add a blanket mac_len check in llc_rcv, but
not sure whether that could break valid LLC paths that do not assume
an Ethernet header. 802.2 LLC may be used on top of non-802.3
protocols in principle.

Reported-by: syzbot+a8c7be6dee0de1b669cc@syzkaller.appspotmail.com
Signed-off-by: Willem de Bruijn <willemb@google.com>
---
 net/llc/llc_input.c   | 10 ++++++++--
 net/llc/llc_s_ac.c    |  3 +++
 net/llc/llc_station.c |  3 +++
 3 files changed, 14 insertions(+), 2 deletions(-)

Comments

Willem de Bruijn Oct. 25, 2023, 3:12 a.m. UTC | #1
On Tue, Oct 24, 2023 at 3:50 PM Willem de Bruijn
<willemdebruijn.kernel@gmail.com> wrote:
>
> From: Willem de Bruijn <willemb@google.com>
>
> LLC reads the mac header with eth_hdr without verifying that the skb
> has an Ethernet header.
>
> Syzbot was able to enter llc_rcv on a tun device. Tun can insert
> packets without mac len and with user configurable skb->protocol
> (passing a tun_pi header when not configuring IFF_NO_PI).
>
>     BUG: KMSAN: uninit-value in llc_station_ac_send_test_r net/llc/llc_station.c:81 [inline]
>     BUG: KMSAN: uninit-value in llc_station_rcv+0x6fb/0x1290 net/llc/llc_station.c:111
>     llc_station_ac_send_test_r net/llc/llc_station.c:81 [inline]
>     llc_station_rcv+0x6fb/0x1290 net/llc/llc_station.c:111
>     llc_rcv+0xc5d/0x14a0 net/llc/llc_input.c:218
>     __netif_receive_skb_one_core net/core/dev.c:5523 [inline]
>     __netif_receive_skb+0x1a6/0x5a0 net/core/dev.c:5637
>     netif_receive_skb_internal net/core/dev.c:5723 [inline]
>     netif_receive_skb+0x58/0x660 net/core/dev.c:5782
>     tun_rx_batched+0x3ee/0x980 drivers/net/tun.c:1555
>     tun_get_user+0x54c5/0x69c0 drivers/net/tun.c:2002
>
> Add a mac_len test before all three eth_hdr(skb) calls under net/llc.
>
> There are further uses in include/net/llc_pdu.h. All these are
> protected by a test skb->protocol == ETH_P_802_2. Which does not
> protect against this tun scenario.
>
> But the mac_len test added in this patch in llc_fixup_skb will
> indirectly protect those too. That is called from llc_rcv before any
> other LLC code.
>
> It is tempting to just add a blanket mac_len check in llc_rcv, but
> not sure whether that could break valid LLC paths that do not assume
> an Ethernet header. 802.2 LLC may be used on top of non-802.3
> protocols in principle.
>
> Reported-by: syzbot+a8c7be6dee0de1b669cc@syzkaller.appspotmail.com
> Signed-off-by: Willem de Bruijn <willemb@google.com>

I forgot to add:

Fixes: f83f1768f833 ("[LLC]: skb allocation size for responses")

Can respin if necessary.

At least one of the three eth_hdr uses goes back to before the start
of git history. But the one that syzbot exercises is introduced in
this commit.

That commit is old enough (2008), that effectively all stable kernels
should receive this. This commit also introduces llc_mac_header_len,
which shows at least one valid L2 header that is not an Ethernet
header, back in the day:

+#ifdef CONFIG_TR
+       case ARPHRD_IEEE802_TR:
+               return sizeof(struct trh_hdr);
+#endif

But that token ring variant was removed in 2012 in commit 211ed865108e
("net: delete all instances of special processing for token ring").
Eric Dumazet Oct. 25, 2023, 7:09 a.m. UTC | #2
On Tue, Oct 24, 2023 at 9:50 PM Willem de Bruijn
<willemdebruijn.kernel@gmail.com> wrote:
>
> From: Willem de Bruijn <willemb@google.com>
>
> LLC reads the mac header with eth_hdr without verifying that the skb
> has an Ethernet header.
>
> Syzbot was able to enter llc_rcv on a tun device. Tun can insert
> packets without mac len and with user configurable skb->protocol
> (passing a tun_pi header when not configuring IFF_NO_PI).
>
>     BUG: KMSAN: uninit-value in llc_station_ac_send_test_r net/llc/llc_station.c:81 [inline]
>     BUG: KMSAN: uninit-value in llc_station_rcv+0x6fb/0x1290 net/llc/llc_station.c:111
>     llc_station_ac_send_test_r net/llc/llc_station.c:81 [inline]
>     llc_station_rcv+0x6fb/0x1290 net/llc/llc_station.c:111
>     llc_rcv+0xc5d/0x14a0 net/llc/llc_input.c:218
>     __netif_receive_skb_one_core net/core/dev.c:5523 [inline]
>     __netif_receive_skb+0x1a6/0x5a0 net/core/dev.c:5637
>     netif_receive_skb_internal net/core/dev.c:5723 [inline]
>     netif_receive_skb+0x58/0x660 net/core/dev.c:5782
>     tun_rx_batched+0x3ee/0x980 drivers/net/tun.c:1555
>     tun_get_user+0x54c5/0x69c0 drivers/net/tun.c:2002
>
> Add a mac_len test before all three eth_hdr(skb) calls under net/llc.
>
> There are further uses in include/net/llc_pdu.h. All these are
> protected by a test skb->protocol == ETH_P_802_2. Which does not
> protect against this tun scenario.
>
> But the mac_len test added in this patch in llc_fixup_skb will
> indirectly protect those too. That is called from llc_rcv before any
> other LLC code.
>
> It is tempting to just add a blanket mac_len check in llc_rcv, but
> not sure whether that could break valid LLC paths that do not assume
> an Ethernet header. 802.2 LLC may be used on top of non-802.3
> protocols in principle.
>
> Reported-by: syzbot+a8c7be6dee0de1b669cc@syzkaller.appspotmail.com

Note that the syzbot report is still private in syzbot infra.

Reviewed-by: Eric Dumazet <edumazet@google.com>

> Signed-off-by: Willem de Bruijn <willemb@google.com>
> ---
>  net/llc/llc_input.c   | 10 ++++++++--
>  net/llc/llc_s_ac.c    |  3 +++
>  net/llc/llc_station.c |  3 +++
>  3 files changed, 14 insertions(+), 2 deletions(-)
>
> diff --git a/net/llc/llc_input.c b/net/llc/llc_input.c
> index 7cac441862e21..51bccfb00a9cd 100644
> --- a/net/llc/llc_input.c
> +++ b/net/llc/llc_input.c
> @@ -127,8 +127,14 @@ static inline int llc_fixup_skb(struct sk_buff *skb)
>         skb->transport_header += llc_len;
>         skb_pull(skb, llc_len);
>         if (skb->protocol == htons(ETH_P_802_2)) {
> -               __be16 pdulen = eth_hdr(skb)->h_proto;
> -               s32 data_size = ntohs(pdulen) - llc_len;
> +               __be16 pdulen;
> +               s32 data_size;
> +
> +               if (skb->mac_len < ETH_HLEN)
> +                       return 0;
> +
> +               pdulen = eth_hdr(skb)->h_proto;
> +               data_size = ntohs(pdulen) - llc_len;
>
>                 if (data_size < 0 ||
>                     !pskb_may_pull(skb, data_size))
> diff --git a/net/llc/llc_s_ac.c b/net/llc/llc_s_ac.c
> index 79d1cef8f15a9..7923c064773cc 100644
> --- a/net/llc/llc_s_ac.c
> +++ b/net/llc/llc_s_ac.c
> @@ -153,6 +153,9 @@ int llc_sap_action_send_test_r(struct llc_sap *sap, struct sk_buff *skb)
>         int rc = 1;
>         u32 data_size;
>
> +       if (skb->mac_len < ETH_HLEN)
> +               return 0;
> +
>         llc_pdu_decode_sa(skb, mac_da);
>         llc_pdu_decode_da(skb, mac_sa);
>         llc_pdu_decode_ssap(skb, &dsap);
> diff --git a/net/llc/llc_station.c b/net/llc/llc_station.c
> index 05c6ae0920534..f506542925109 100644
> --- a/net/llc/llc_station.c
> +++ b/net/llc/llc_station.c
> @@ -76,6 +76,9 @@ static int llc_station_ac_send_test_r(struct sk_buff *skb)
>         u32 data_size;
>         struct sk_buff *nskb;
>
> +       if (skb->mac_len < ETH_HLEN)
> +               goto out;
> +
>         /* The test request command is type U (llc_len = 3) */
>         data_size = ntohs(eth_hdr(skb)->h_proto) - 3;
>         nskb = llc_alloc_frame(NULL, skb->dev, LLC_PDU_TYPE_U, data_size);
> --
> 2.42.0.758.gaed0368e0e-goog
>
Jakub Kicinski Oct. 25, 2023, 11 p.m. UTC | #3
On Tue, 24 Oct 2023 23:12:13 -0400 Willem de Bruijn wrote:
> Fixes: f83f1768f833 ("[LLC]: skb allocation size for responses")
> 
> Can respin if necessary.
> 
> At least one of the three eth_hdr uses goes back to before the start
> of git history.

Right, good enough.

> But the one that syzbot exercises is introduced in this commit.
> 
> That commit is old enough (2008), that effectively all stable kernels
> should receive this. This commit also introduces llc_mac_header_len,
> which shows at least one valid L2 header that is not an Ethernet
> header, back in the day:
> 
> +#ifdef CONFIG_TR
> +       case ARPHRD_IEEE802_TR:
> +               return sizeof(struct trh_hdr);
> +#endif
> 
> But that token ring variant was removed in 2012 in commit 211ed865108e
> ("net: delete all instances of special processing for token ring").
Jakub Kicinski Oct. 25, 2023, 11:01 p.m. UTC | #4
On Tue, 24 Oct 2023 15:49:36 -0400 Willem de Bruijn wrote:
> @@ -153,6 +153,9 @@ int llc_sap_action_send_test_r(struct llc_sap *sap, struct sk_buff *skb)
>  	int rc = 1;
>  	u32 data_size;
>  
> +	if (skb->mac_len < ETH_HLEN)
> +		return 0;

I think this one may want 1 to indicate error, technically. No?
Willem de Bruijn Oct. 25, 2023, 11:05 p.m. UTC | #5
On Wed, Oct 25, 2023 at 7:01 PM Jakub Kicinski <kuba@kernel.org> wrote:
>
> On Tue, 24 Oct 2023 15:49:36 -0400 Willem de Bruijn wrote:
> > @@ -153,6 +153,9 @@ int llc_sap_action_send_test_r(struct llc_sap *sap, struct sk_buff *skb)
> >       int rc = 1;
> >       u32 data_size;
> >
> > +     if (skb->mac_len < ETH_HLEN)
> > +             return 0;
>
> I think this one may want 1 to indicate error, technically. No?

Absolutely, thanks. For both tests.

Will send a v2, with that Fixes tag too.
Jakub Kicinski Oct. 25, 2023, 11:14 p.m. UTC | #6
On Wed, 25 Oct 2023 19:05:59 -0400 Willem de Bruijn wrote:
> > I think this one may want 1 to indicate error, technically. No?  
> 
> Absolutely, thanks. For both tests.

Both state machine callbacks (functions with names ending with 'test_r'
in the patch), right? Perhaps 'goto out' is what's expected here.  

The fixup function seems to have inverse return semantics, because 
why not.
Willem de Bruijn Oct. 25, 2023, 11:18 p.m. UTC | #7
On Wed, Oct 25, 2023 at 7:14 PM Jakub Kicinski <kuba@kernel.org> wrote:
>
> On Wed, 25 Oct 2023 19:05:59 -0400 Willem de Bruijn wrote:
> > > I think this one may want 1 to indicate error, technically. No?
> >
> > Absolutely, thanks. For both tests.
>
> Both state machine callbacks (functions with names ending with 'test_r'
> in the patch), right? Perhaps 'goto out' is what's expected here.
>
> The fixup function seems to have inverse return semantics, because
> why not.

Exactly :) Extrapolating from that is where I went wrong.
diff mbox series

Patch

diff --git a/net/llc/llc_input.c b/net/llc/llc_input.c
index 7cac441862e21..51bccfb00a9cd 100644
--- a/net/llc/llc_input.c
+++ b/net/llc/llc_input.c
@@ -127,8 +127,14 @@  static inline int llc_fixup_skb(struct sk_buff *skb)
 	skb->transport_header += llc_len;
 	skb_pull(skb, llc_len);
 	if (skb->protocol == htons(ETH_P_802_2)) {
-		__be16 pdulen = eth_hdr(skb)->h_proto;
-		s32 data_size = ntohs(pdulen) - llc_len;
+		__be16 pdulen;
+		s32 data_size;
+
+		if (skb->mac_len < ETH_HLEN)
+			return 0;
+
+		pdulen = eth_hdr(skb)->h_proto;
+		data_size = ntohs(pdulen) - llc_len;
 
 		if (data_size < 0 ||
 		    !pskb_may_pull(skb, data_size))
diff --git a/net/llc/llc_s_ac.c b/net/llc/llc_s_ac.c
index 79d1cef8f15a9..7923c064773cc 100644
--- a/net/llc/llc_s_ac.c
+++ b/net/llc/llc_s_ac.c
@@ -153,6 +153,9 @@  int llc_sap_action_send_test_r(struct llc_sap *sap, struct sk_buff *skb)
 	int rc = 1;
 	u32 data_size;
 
+	if (skb->mac_len < ETH_HLEN)
+		return 0;
+
 	llc_pdu_decode_sa(skb, mac_da);
 	llc_pdu_decode_da(skb, mac_sa);
 	llc_pdu_decode_ssap(skb, &dsap);
diff --git a/net/llc/llc_station.c b/net/llc/llc_station.c
index 05c6ae0920534..f506542925109 100644
--- a/net/llc/llc_station.c
+++ b/net/llc/llc_station.c
@@ -76,6 +76,9 @@  static int llc_station_ac_send_test_r(struct sk_buff *skb)
 	u32 data_size;
 	struct sk_buff *nskb;
 
+	if (skb->mac_len < ETH_HLEN)
+		goto out;
+
 	/* The test request command is type U (llc_len = 3) */
 	data_size = ntohs(eth_hdr(skb)->h_proto) - 3;
 	nskb = llc_alloc_frame(NULL, skb->dev, LLC_PDU_TYPE_U, data_size);