diff mbox

[net] filter: prevent nla extensions to peek beyond the end of the message

Message ID 1397406213-15452-1-git-send-email-minipli@googlemail.com
State Accepted, archived
Delegated to: David Miller
Headers show

Commit Message

Mathias Krause April 13, 2014, 4:23 p.m. UTC
The BPF_S_ANC_NLATTR and BPF_S_ANC_NLATTR_NEST extensions fail to check
for a minimal message length before testing the supplied offset to be
within the bounds of the message. This allows the subtraction of the nla
header to underflow and therefore -- as the data type is unsigned --
allowing far to big offset and length values for the search of the
netlink attribute.

The remainder calculation for the BPF_S_ANC_NLATTR_NEST extension is
also wrong. It has the minuend und subtrahend mixed up, therefore
calculates a huge length value, allowing to overrun the end of the
message while looking for the netlink attribute.

The following three BPF snippets will trigger the bugs when attached to
a UNIX datagram socket and parsing a message with length 1, 2 or 3.

 ,-[ PoC for missing size check in BPF_S_ANC_NLATTR ]--
 | ld	#0x87654321
 | ldx	#42
 | ld	#nla
 | ret	a
 `---

 ,-[ PoC for the same bug in BPF_S_ANC_NLATTR_NEST ]--
 | ld	#0x87654321
 | ldx	#42
 | ld	#nlan
 | ret	a
 `---

 ,-[ PoC for wrong remainder calculation in BPF_S_ANC_NLATTR_NEST ]--
 | ; (needs a fake netlink header at offset 0)
 | ld	#0
 | ldx	#42
 | ld	#nlan
 | ret	a
 `---

Fix the first issue by ensuring the message length fulfills the minimal
size constrains of a nla header. Fix the second bug by getting the math
for the remainder calculation right.

Fixes: 4738c1db15 ("[SKFILTER]: Add SKF_ADF_NLATTR instruction")
Fixes: d214c7537b ("filter: add SKF_AD_NLATTR_NEST to look for nested..")
Cc: Patrick McHardy <kaber@trash.net>
Cc: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Mathias Krause <minipli@googlemail.com>
---
Backporters will find the code in question in the switch block of
sk_run_filter().

 net/core/filter.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

Comments

Daniel Borkmann April 13, 2014, 5:34 p.m. UTC | #1
On 04/13/2014 06:23 PM, Mathias Krause wrote:
> The BPF_S_ANC_NLATTR and BPF_S_ANC_NLATTR_NEST extensions fail to check
> for a minimal message length before testing the supplied offset to be
> within the bounds of the message. This allows the subtraction of the nla
> header to underflow and therefore -- as the data type is unsigned --
> allowing far to big offset and length values for the search of the
> netlink attribute.
>
> The remainder calculation for the BPF_S_ANC_NLATTR_NEST extension is
> also wrong. It has the minuend und subtrahend mixed up, therefore
> calculates a huge length value, allowing to overrun the end of the
> message while looking for the netlink attribute.
>
> The following three BPF snippets will trigger the bugs when attached to
> a UNIX datagram socket and parsing a message with length 1, 2 or 3.
>
>   ,-[ PoC for missing size check in BPF_S_ANC_NLATTR ]--
>   | ld	#0x87654321
>   | ldx	#42
>   | ld	#nla
>   | ret	a
>   `---
>
>   ,-[ PoC for the same bug in BPF_S_ANC_NLATTR_NEST ]--
>   | ld	#0x87654321
>   | ldx	#42
>   | ld	#nlan
>   | ret	a
>   `---
>
>   ,-[ PoC for wrong remainder calculation in BPF_S_ANC_NLATTR_NEST ]--
>   | ; (needs a fake netlink header at offset 0)
>   | ld	#0
>   | ldx	#42
>   | ld	#nlan
>   | ret	a
>   `---
>
> Fix the first issue by ensuring the message length fulfills the minimal
> size constrains of a nla header. Fix the second bug by getting the math
> for the remainder calculation right.
>
> Fixes: 4738c1db15 ("[SKFILTER]: Add SKF_ADF_NLATTR instruction")
> Fixes: d214c7537b ("filter: add SKF_AD_NLATTR_NEST to look for nested..")
> Cc: Patrick McHardy <kaber@trash.net>
> Cc: Pablo Neira Ayuso <pablo@netfilter.org>
> Signed-off-by: Mathias Krause <minipli@googlemail.com>

Acked-by: Daniel Borkmann <dborkman@redhat.com>
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
David Miller April 14, 2014, 3:33 a.m. UTC | #2
From: Mathias Krause <minipli@googlemail.com>
Date: Sun, 13 Apr 2014 18:23:33 +0200

> The BPF_S_ANC_NLATTR and BPF_S_ANC_NLATTR_NEST extensions fail to check
> for a minimal message length before testing the supplied offset to be
> within the bounds of the message. This allows the subtraction of the nla
> header to underflow and therefore -- as the data type is unsigned --
> allowing far to big offset and length values for the search of the
> netlink attribute.
> 
> The remainder calculation for the BPF_S_ANC_NLATTR_NEST extension is
> also wrong. It has the minuend und subtrahend mixed up, therefore

I translated the German into English here, re: und --> and :-)

> calculates a huge length value, allowing to overrun the end of the
> message while looking for the netlink attribute.
> 
> The following three BPF snippets will trigger the bugs when attached to
> a UNIX datagram socket and parsing a message with length 1, 2 or 3.
> 
>  ,-[ PoC for missing size check in BPF_S_ANC_NLATTR ]--
>  | ld	#0x87654321
>  | ldx	#42
>  | ld	#nla
>  | ret	a
>  `---
> 
>  ,-[ PoC for the same bug in BPF_S_ANC_NLATTR_NEST ]--
>  | ld	#0x87654321
>  | ldx	#42
>  | ld	#nlan
>  | ret	a
>  `---
> 
>  ,-[ PoC for wrong remainder calculation in BPF_S_ANC_NLATTR_NEST ]--
>  | ; (needs a fake netlink header at offset 0)
>  | ld	#0
>  | ldx	#42
>  | ld	#nlan
>  | ret	a
>  `---
> 
> Fix the first issue by ensuring the message length fulfills the minimal
> size constrains of a nla header. Fix the second bug by getting the math
> for the remainder calculation right.
> 
> Fixes: 4738c1db15 ("[SKFILTER]: Add SKF_ADF_NLATTR instruction")
> Fixes: d214c7537b ("filter: add SKF_AD_NLATTR_NEST to look for nested..")
> Cc: Patrick McHardy <kaber@trash.net>
> Cc: Pablo Neira Ayuso <pablo@netfilter.org>
> Signed-off-by: Mathias Krause <minipli@googlemail.com>
> ---
> Backporters will find the code in question in the switch block of
> sk_run_filter().

Thanks, applied and queued up for -stable.
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Mathias Krause April 14, 2014, 5:53 a.m. UTC | #3
On 14 April 2014 05:33, David Miller <davem@davemloft.net> wrote:
> From: Mathias Krause <minipli@googlemail.com>
> Date: Sun, 13 Apr 2014 18:23:33 +0200
>
>> The BPF_S_ANC_NLATTR and BPF_S_ANC_NLATTR_NEST extensions fail to check
>> for a minimal message length before testing the supplied offset to be
>> within the bounds of the message. This allows the subtraction of the nla
>> header to underflow and therefore -- as the data type is unsigned --
>> allowing far to big offset and length values for the search of the
>> netlink attribute.
>>
>> The remainder calculation for the BPF_S_ANC_NLATTR_NEST extension is
>> also wrong. It has the minuend und subtrahend mixed up, therefore
>
> I translated the German into English here, re: und --> and :-)
>

Ups! :D Thanks for the review!

Cheers,
Mathias
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/net/core/filter.c b/net/core/filter.c
index e08b3822c7..0e0856f5d7 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -600,6 +600,9 @@  static u64 __skb_get_nlattr(u64 ctx, u64 A, u64 X, u64 r4, u64 r5)
 	if (skb_is_nonlinear(skb))
 		return 0;
 
+	if (skb->len < sizeof(struct nlattr))
+		return 0;
+
 	if (A > skb->len - sizeof(struct nlattr))
 		return 0;
 
@@ -618,11 +621,14 @@  static u64 __skb_get_nlattr_nest(u64 ctx, u64 A, u64 X, u64 r4, u64 r5)
 	if (skb_is_nonlinear(skb))
 		return 0;
 
+	if (skb->len < sizeof(struct nlattr))
+		return 0;
+
 	if (A > skb->len - sizeof(struct nlattr))
 		return 0;
 
 	nla = (struct nlattr *) &skb->data[A];
-	if (nla->nla_len > A - skb->len)
+	if (nla->nla_len > skb->len - A)
 		return 0;
 
 	nla = nla_find_nested(nla, X);