Skip to contentSkip to conversation

netfilter: nft_exthdr: Fix for unsafe packet data read

PATCH Applied Phil Sutter 3 participants 6 messages Jun 8, 2021

Subsystems (2)
lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH v2 1/1 netfilter: nft_exthdr: Fix for unsafe packet data read

While iterating through an SCTP packet's chunks, skb_header_pointer() is called for the minimum expected chunk header size. If (that part of) the skbuff is non-linear, the following memcpy() may read data past temporary buffer '_sch'. Use skb_copy_bits() instead which does the right thing in this situation.

Fixes 133dc203d77df ("netfilter: nft_exthdr: Support SCTP chunks")

---
Changes since v1:
- skb_copy_bits() call error handling added

---
 net/netfilter/nft_exthdr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/netfilter/nft_exthdr.c +3 -1
index 1b0579cb62d08..7f705b5c09de8 100644
@@ -327,7 +327,9 @@ static void nft_exthdr_sctp_eval(const struct nft_expr *expr,
 				break;
 
 			dest[priv->len / NFT_REG32_SIZE] = 0;
-			memcpy(dest, (char *)sch + priv->offset, priv->len);
+			if (skb_copy_bits(pkt->skb, offset + priv->offset,
+					  dest, priv->len) < 0)
+				break;
 			return;
 		}
 		offset += SCTP_PAD4(ntohs(sch->length));
Signature · 1 line
2.31.1
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · Phil Sutter

While iterating through an SCTP packet's chunks, skb_header_pointer() is called for the minimum expected chunk header size. If (that part of) the skbuff is non-linear, the following memcpy() may read data past temporary buffer '_sch'. Use skb_copy_bits() instead which does the right thing in this situation.

Fixes 133dc203d77df ("netfilter: nft_exthdr: Support SCTP chunks")
#2 lore raw
In reply to Phil Sutter
Quoted 21 lines · Phil Sutter

While iterating through an SCTP packet's chunks, skb_header_pointer() is called for the minimum expected chunk header size. If (that part of) the skbuff is non-linear, the following memcpy() may read data past temporary buffer '_sch'. Use skb_copy_bits() instead which does the right thing in this situation.

Fixes 133dc203d77df ("netfilter: nft_exthdr: Support SCTP chunks")

---
Changes since v1:
- skb_copy_bits() call error handling added

---
 net/netfilter/nft_exthdr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/netfilter/nft_exthdr.c +0 -0
index 1b0579cb62d08..7f705b5c09de8 100644

net/netfilter/nft_exthdr.c static void nft_exthdr_sctp_eval(const struct nft_expr *expr, · line 332

Quoted 7 lines
net/netfilter/nft_exthdr.c +3 -1
@@ -327,7 +327,9 @@ static void nft_exthdr_sctp_eval(const struct nft_expr *expr,
 				break;
 
 			dest[priv->len / NFT_REG32_SIZE] = 0;
-			memcpy(dest, (char *)sch + priv->offset, priv->len);
+			if (skb_copy_bits(pkt->skb, offset + priv->offset,
+					  dest, priv->len) < 0)
+				break;

Hm, it looks like tcp exthdt matching has the same problem?

Quoted 6 lines
net/netfilter/nft_exthdr.c +0 -0
 			return;
 		}
 		offset += SCTP_PAD4(ntohs(sch->length));
Signature · 1 line
2.31.1
maintainer
#3 lore raw
Quoted 24 lines · Pablo Neira Ayuso
Quoted 23 lines · Phil Sutter

While iterating through an SCTP packet's chunks, skb_header_pointer() is called for the minimum expected chunk header size. If (that part of) the skbuff is non-linear, the following memcpy() may read data past temporary buffer '_sch'. Use skb_copy_bits() instead which does the right thing in this situation.

Fixes 133dc203d77df ("netfilter: nft_exthdr: Support SCTP chunks")

---
Changes since v1:
- skb_copy_bits() call error handling added

---
 net/netfilter/nft_exthdr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/netfilter/nft_exthdr.c +0 -0
index 1b0579cb62d08..7f705b5c09de8 100644
@@ -327,7 +327,9 @@ static void nft_exthdr_sctp_eval(const struct nft_expr *expr,
 				break;
 
Quoted 7 lines

net/netfilter/nft_exthdr.c static void nft_exthdr_sctp_eval(const struct nft_expr *expr, · line 332

Quoted 5 lines
net/netfilter/nft_exthdr.c +3 -1
 			dest[priv->len / NFT_REG32_SIZE] = 0;
-			memcpy(dest, (char *)sch + priv->offset, priv->len);
+			if (skb_copy_bits(pkt->skb, offset + priv->offset,
+					  dest, priv->len) < 0)
+				break;

Hm, it looks like tcp exthdt matching has the same problem?

Tcp exthdr either copies from skb linear area or a on-stack scratch space, it looks ok to me.

#4 lore raw
Quoted 33 lines · Florian Westphal
Quoted 24 lines · Pablo Neira Ayuso
Quoted 23 lines · Phil Sutter

While iterating through an SCTP packet's chunks, skb_header_pointer() is called for the minimum expected chunk header size. If (that part of) the skbuff is non-linear, the following memcpy() may read data past temporary buffer '_sch'. Use skb_copy_bits() instead which does the right thing in this situation.

Fixes 133dc203d77df ("netfilter: nft_exthdr: Support SCTP chunks")

---
Changes since v1:
- skb_copy_bits() call error handling added

---
 net/netfilter/nft_exthdr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/netfilter/nft_exthdr.c +0 -0
index 1b0579cb62d08..7f705b5c09de8 100644
@@ -327,7 +327,9 @@ static void nft_exthdr_sctp_eval(const struct nft_expr *expr,
 				break;
 
Quoted 7 lines

net/netfilter/nft_exthdr.c static void nft_exthdr_sctp_eval(const struct nft_expr *expr, · line 332

Quoted 5 lines
net/netfilter/nft_exthdr.c +3 -1
 			dest[priv->len / NFT_REG32_SIZE] = 0;
-			memcpy(dest, (char *)sch + priv->offset, priv->len);
+			if (skb_copy_bits(pkt->skb, offset + priv->offset,
+					  dest, priv->len) < 0)
+				break;

Hm, it looks like tcp exthdt matching has the same problem?

Quoted 2 lines

Tcp exthdr either copies from skb linear area or a on-stack scratch space, it looks ok to me.

Indeed, I overlook the nft_tcp_header_pointer() call a few lines before.

maintainer Applied
#5 lore raw
In reply to Phil Sutter
Quoted 5 lines · Phil Sutter

While iterating through an SCTP packet's chunks, skb_header_pointer() is called for the minimum expected chunk header size. If (that part of) the skbuff is non-linear, the following memcpy() may read data past temporary buffer '_sch'. Use skb_copy_bits() instead which does the right thing in this situation.

Applied, thanks.