Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Commit 314760e

Browse files
minipligregkh
authored andcommitted
filter: prevent nla extensions to peek beyond the end of the message
[ Upstream commit 05ab8f2 ] 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 and 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 torvalds#42 | ld #nla | ret a `--- ,-[ PoC for the same bug in BPF_S_ANC_NLATTR_NEST ]-- | ld #0x87654321 | ldx torvalds#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 torvalds#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: 4738c1d ("[SKFILTER]: Add SKF_ADF_NLATTR instruction") Fixes: d214c75 ("filter: add SKF_AD_NLATTR_NEST to look for nested..") Cc: Patrick McHardy <[email protected]> Cc: Pablo Neira Ayuso <[email protected]> Signed-off-by: Mathias Krause <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Signed-off-by: David S. Miller <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent da1bba1 commit 314760e

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

Diff for: net/core/filter.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ unsigned int sk_run_filter(const struct sk_buff *skb,
355355

356356
if (skb_is_nonlinear(skb))
357357
return 0;
358+
if (skb->len < sizeof(struct nlattr))
359+
return 0;
358360
if (A > skb->len - sizeof(struct nlattr))
359361
return 0;
360362

@@ -371,11 +373,13 @@ unsigned int sk_run_filter(const struct sk_buff *skb,
371373

372374
if (skb_is_nonlinear(skb))
373375
return 0;
376+
if (skb->len < sizeof(struct nlattr))
377+
return 0;
374378
if (A > skb->len - sizeof(struct nlattr))
375379
return 0;
376380

377381
nla = (struct nlattr *)&skb->data[A];
378-
if (nla->nla_len > A - skb->len)
382+
if (nla->nla_len > skb->len - A)
379383
return 0;
380384

381385
nla = nla_find_nested(nla, X);

0 commit comments

Comments
 (0)