Skip to contentSkip to conversation

netfilter: nft_exthdr: Fix non-linear header modification

PATCH Superseded Xiao Liang 2 participants 5 messages Aug 25, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Xiao Liang

author
#0 lore raw

PATCH 1/1 netfilter: nft_exthdr: Fix non-linear header modification

nft_tcp_header_pointer() may copy TCP header if it's not linear. In that case, we should modify the packet rather than the buffer, after proper skb_ensure_writable().

---
 net/netfilter/nft_exthdr.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
net/netfilter/nft_exthdr.c +7 -8
index 7f856ceb3a66..2189ccc1119c 100644
@@ -254,13 +254,12 @@ static void nft_exthdr_tcp_set_eval(const struct nft_expr *expr,
 			goto err;
 
 		if (skb_ensure_writable(pkt->skb,
-					nft_thoff(pkt) + i + priv->len))
+					nft_thoff(pkt) + i + priv->offset +
+					priv->len))
 			goto err;
 
-		tcph = nft_tcp_header_pointer(pkt, sizeof(buff), buff,
-					      &tcphdr_len);
-		if (!tcph)
-			goto err;
+		tcph = (struct tcphdr *)(pkt->skb->data + nft_thoff(pkt));
+		opt = (u8 *)tcph;
 
 		offset = i + priv->offset;
 
@@ -325,9 +324,9 @@ static void nft_exthdr_tcp_strip_eval(const struct nft_expr *expr,
 	if (skb_ensure_writable(pkt->skb, nft_thoff(pkt) + tcphdr_len))
 		goto drop;
 
-	opt = (u8 *)nft_tcp_header_pointer(pkt, sizeof(buff), buff, &tcphdr_len);
-	if (!opt)
-		goto err;
+	tcph = (struct tcphdr *)(pkt->skb->data + nft_thoff(pkt));
+	opt = (u8 *)tcph;
+
 	for (i = sizeof(*tcph); i < tcphdr_len - 1; i += optl) {
 		unsigned int j;
 
Signature · 1 line
2.42.0
maintainer
#1 lore raw
Quoted 3 lines · Xiao Liang

nft_tcp_header_pointer() may copy TCP header if it's not linear. In that case, we should modify the packet rather than the buffer, after proper skb_ensure_writable().

Fixes 99d1712bc41c ("netfilter: exthdr: tcp option set support")

I do not understand this changelog.

The bug is that skb_ensure_writable() size is too small, hence nft_tcp_header_pointer() may return a pointer to local stack buffer.

Quoted 16 lines
---
 net/netfilter/nft_exthdr.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
net/netfilter/nft_exthdr.c +2 -1
index 7f856ceb3a66..2189ccc1119c 100644
@@ -254,13 +254,12 @@ static void nft_exthdr_tcp_set_eval(const struct nft_expr *expr,
 			goto err;
 
 		if (skb_ensure_writable(pkt->skb,
-					nft_thoff(pkt) + i + priv->len))
+					nft_thoff(pkt) + i + priv->offset +
+					priv->len))

[..]

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

Quoted 6 lines
net/netfilter/nft_exthdr.c +2 -4
-		tcph = nft_tcp_header_pointer(pkt, sizeof(buff), buff,
-					      &tcphdr_len);
-		if (!tcph)
-			goto err;
+		tcph = (struct tcphdr *)(pkt->skb->data + nft_thoff(pkt));
+		opt = (u8 *)tcph;

This modification is not related to the bug?

If you think this is better, then please say that the 'do not use nft_tcp_header_pointer' is an unrelated cleanup in the commit message.

But I would prefer to not mix functional and non-functional changes. Also, the use of the nft_tcp_header_pointer() helper is the reason why this doesn't result in memory corruption.

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

Quoted 2 lines
net/netfilter/nft_exthdr.c +0 -0
@@ -325,9 +324,9 @@ static void nft_exthdr_tcp_strip_eval(const struct nft_expr *expr,
 	if (skb_ensure_writable(pkt->skb, nft_thoff(pkt) + tcphdr_len))

Just use the above in nft_exthdr_tcp_set_eval and place it before the loop?

author
#2 lore raw
Quoted 5 lines · Florian Westphal

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

Quoted 3 lines
net/netfilter/nft_exthdr.c +0 -0
@@ -325,9 +324,9 @@ static void nft_exthdr_tcp_strip_eval(const struct nft_expr *expr,
      if (skb_ensure_writable(pkt->skb, nft_thoff(pkt) + tcphdr_len))

Just use the above in nft_exthdr_tcp_set_eval and place it before the loop?

Sure. Need to pull the entire TCP header with skb_ensure_writable() then.

author
#3 lore raw
In reply to Florian Westphal
Quoted 7 lines · Florian Westphal

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

Quoted 6 lines
net/netfilter/nft_exthdr.c +2 -4
-             tcph = nft_tcp_header_pointer(pkt, sizeof(buff), buff,
-                                           &tcphdr_len);
-             if (!tcph)
-                     goto err;
+             tcph = (struct tcphdr *)(pkt->skb->data + nft_thoff(pkt));
+             opt = (u8 *)tcph;
Quoted 8 lines

This modification is not related to the bug?

If you think this is better, then please say that the 'do not use nft_tcp_header_pointer' is an unrelated cleanup in the commit message.

But I would prefer to not mix functional and non-functional changes. Also, the use of the nft_tcp_header_pointer() helper is the reason why this doesn't result in memory corruption.

I think this makes it explicit that
"we are modifying the original packet"
rather than
"we are modifying the packet because above skb_ensure_writable() is enough"

Quoted 5 lines

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

Quoted 3 lines
net/netfilter/nft_exthdr.c +0 -0
@@ -325,9 +324,9 @@ static void nft_exthdr_tcp_strip_eval(const struct nft_expr *expr,
      if (skb_ensure_writable(pkt->skb, nft_thoff(pkt) + tcphdr_len))

Just use the above in nft_exthdr_tcp_set_eval and place it before the loop?

In this case, all TCP headers will be pulled even if they don't have the target option.

maintainer
#4 lore raw
Quoted 8 lines · Xiao Liang
Quoted 3 lines

But I would prefer to not mix functional and non-functional changes. Also, the use of the nft_tcp_header_pointer() helper is the reason why this doesn't result in memory corruption.

I think this makes it explicit that
"we are modifying the original packet"
rather than
"we are modifying the packet because above skb_ensure_writable() is enough"

OK, I won't argue here.

Quoted 4 lines
Quoted 1 line

Just use the above in nft_exthdr_tcp_set_eval and place it before the loop?

In this case, all TCP headers will be pulled even if they don't have the target option.

Keep it simple.