Skip to contentSkip to conversation

netfilter: xt_TCPMSS: check skb_dst before path-MTU clamping

PATCH In review Weiming Shi 3 participants 5 messages Apr 18 – Apr 19

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: xt_TCPMSS: check skb_dst before path-MTU clamping

When TCPMSS with CLAMP_PMTU is used via nft_compat in a non-base chain, par->hook_mask is set to 0, bypassing the checkentry hook validation. The target can then run at PRE_ROUTING where skb_dst is NULL, causing a null-ptr-deref in tcpmss_mangle_packet():

 KASAN: null-ptr-deref in range [0x0000000000000008-0x000000000000000f]
 RIP: 0010:tcpmss_mangle_packet (include/net/dst.h:219 net/netfilter/xt_TCPMSS.c:105)
  tcpmss_tg4 (net/netfilter/xt_TCPMSS.c:202)
  nft_target_eval_xt (net/netfilter/nft_compat.c:87)
  nft_do_chain (net/netfilter/nf_tables_core.c:287)
  nf_hook_slow (net/netfilter/core.c:623)

Check skb_dst() for NULL before calling dst_mtu().

Fixes 493618a92c6a ("netfilter: nft_compat: fix hook validation for non-base chains")
---
 net/netfilter/xt_TCPMSS.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
net/netfilter/xt_TCPMSS.c +6 -1
index 116a885adb3c..79b5e475e23e 100644
@@ -102,7 +102,12 @@ tcpmss_mangle_packet(struct sk_buff *skb,
 	if (info->mss == XT_TCPMSS_CLAMP_PMTU) {
 		struct net *net = xt_net(par);
 		unsigned int in_mtu = tcpmss_reverse_mtu(net, skb, family);
-		unsigned int min_mtu = min(dst_mtu(skb_dst(skb)), in_mtu);
+		unsigned int min_mtu;
+
+		if (!skb_dst(skb))
+			return -1;
+
+		min_mtu = min(dst_mtu(skb_dst(skb)), in_mtu);
 
 		if (min_mtu <= minlen) {
 			net_err_ratelimited("unknown or invalid path-MTU (%u)\n",
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 5 lines · Weiming Shi

When TCPMSS with CLAMP_PMTU is used via nft_compat in a non-base chain, par->hook_mask is set to 0, bypassing the checkentry hook validation. The target can then run at PRE_ROUTING where skb_dst is NULL, causing a null-ptr-deref in tcpmss_mangle_packet():

Quoted 8 lines
 KASAN: null-ptr-deref in range [0x0000000000000008-0x000000000000000f]
 RIP: 0010:tcpmss_mangle_packet (include/net/dst.h:219 net/netfilter/xt_TCPMSS.c:105)
  tcpmss_tg4 (net/netfilter/xt_TCPMSS.c:202)
  nft_target_eval_xt (net/netfilter/nft_compat.c:87)
  nft_do_chain (net/netfilter/nf_tables_core.c:287)
  nf_hook_slow (net/netfilter/core.c:623)

Check skb_dst() for NULL before calling dst_mtu().

FWIW I will apply this patch even though its wrong.

nft_compat.c is just too broken, I don't see how it can be fixed in any reasonable amount of time.

validation is done too early, at expression instantiation time.

This doesn't work because we have incomplete graph, it has to be done at final table validation time.

But then all required compat info (xtables hints) is gone and no longer available.

AFAICS the only way to resolve this is to cache the info in the nft_expr priv area (WHERE IS ABSOLUTELY DOESN'T BELONG!) because thats the only storage thewre is.

*puke*

maintainer
#2 lore raw
Quoted 13 lines · Florian Westphal
Quoted 12 lines · Weiming Shi

When TCPMSS with CLAMP_PMTU is used via nft_compat in a non-base chain, par->hook_mask is set to 0, bypassing the checkentry hook validation. The target can then run at PRE_ROUTING where skb_dst is NULL, causing a null-ptr-deref in tcpmss_mangle_packet():

 KASAN: null-ptr-deref in range [0x0000000000000008-0x000000000000000f]
 RIP: 0010:tcpmss_mangle_packet (include/net/dst.h:219 net/netfilter/xt_TCPMSS.c:105)
  tcpmss_tg4 (net/netfilter/xt_TCPMSS.c:202)
  nft_target_eval_xt (net/netfilter/nft_compat.c:87)
  nft_do_chain (net/netfilter/nf_tables_core.c:287)
  nf_hook_slow (net/netfilter/core.c:623)
Quoted 6 lines
Quoted 1 line

Check skb_dst() for NULL before calling dst_mtu().

FWIW I will apply this patch even though its wrong.

nft_compat.c is just too broken, I don't see how it can be fixed in any reasonable amount of time.

net/netfilter/xt_TCPMSS.c:          (par->hook_mask & ~((1 << NF_INET_FORWARD) |
net/netfilter/xt_addrtype.c:    if (par->hook_mask & ((1 << NF_INET_PRE_ROUTING) |
net/netfilter/xt_devgroup.c:        par->hook_mask & ~((1 << NF_INET_PRE_ROUTING) |
net/netfilter/xt_physdev.c:         par->hook_mask & (1 << NF_INET_LOCAL_OUT)) {
net/netfilter/xt_policy.c:      if (par->hook_mask & ((1 << NF_INET_POST_ROUTING) |
net/netfilter/xt_set.c:              (par->hook_mask & ~(1 << NF_INET_FORWARD |

Look at this I don't see an alternative to mixing nft specific bits into x_tables, i.e.:

include/linux/netfilter/x_tables.h +2 -0
@@ -187,6 +187,8 @@ struct xt_target {
        /* Should return 0 on success or an error code otherwise (-Exxxx). */
        int (*checkentry)(const struct xt_tgchk_param *);
 
+       int (*nft_validate_chain)(const void *targinfo, unsigned int hook_mask);
+
        /* Called when entry of this type deleted. */
        void (*destroy)(const struct xt_tgdtor_param *);
 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT

.. and then call that from nft_compat.c for TCPSS. Same for matches.

#3 lore raw
In reply to Florian Westphal
Quoted 17 lines · Florian Westphal
Quoted 5 lines · Weiming Shi

When TCPMSS with CLAMP_PMTU is used via nft_compat in a non-base chain, par->hook_mask is set to 0, bypassing the checkentry hook validation. The target can then run at PRE_ROUTING where skb_dst is NULL, causing a null-ptr-deref in tcpmss_mangle_packet():

Quoted 8 lines
 KASAN: null-ptr-deref in range [0x0000000000000008-0x000000000000000f]
 RIP: 0010:tcpmss_mangle_packet (include/net/dst.h:219 net/netfilter/xt_TCPMSS.c:105)
  tcpmss_tg4 (net/netfilter/xt_TCPMSS.c:202)
  nft_target_eval_xt (net/netfilter/nft_compat.c:87)
  nft_do_chain (net/netfilter/nf_tables_core.c:287)
  nf_hook_slow (net/netfilter/core.c:623)

Check skb_dst() for NULL before calling dst_mtu().

FWIW I will apply this patch even though its wrong.

Quoted 8 lines

nft_compat.c is just too broken, I don't see how it can be fixed in any reasonable amount of time.

validation is done too early, at expression instantiation time.

This doesn't work because we have incomplete graph, it has to be done at final table validation time.

I remember this used to work, maybe it broke with recent updates on the chain graph detection?

Once the non-basechain is added it should consider the basechain where this can be reached.

Quoted 2 lines

But then all required compat info (xtables hints) is gone and no longer available.

What?

Quoted 3 lines

AFAICS the only way to resolve this is to cache the info in the nft_expr priv area (WHERE IS ABSOLUTELY DOESN'T BELONG!) because thats the only storage thewre is.

No.

#4 lore raw
In reply to Florian Westphal
Quoted 13 lines · Florian Westphal
Quoted 12 lines · Weiming Shi

When TCPMSS with CLAMP_PMTU is used via nft_compat in a non-base chain, par->hook_mask is set to 0, bypassing the checkentry hook validation. The target can then run at PRE_ROUTING where skb_dst is NULL, causing a null-ptr-deref in tcpmss_mangle_packet():

 KASAN: null-ptr-deref in range [0x0000000000000008-0x000000000000000f]
 RIP: 0010:tcpmss_mangle_packet (include/net/dst.h:219 net/netfilter/xt_TCPMSS.c:105)
  tcpmss_tg4 (net/netfilter/xt_TCPMSS.c:202)
  nft_target_eval_xt (net/netfilter/nft_compat.c:87)
  nft_do_chain (net/netfilter/nf_tables_core.c:287)
  nf_hook_slow (net/netfilter/core.c:623)
Quoted 3 lines
Quoted 1 line

Check skb_dst() for NULL before calling dst_mtu().

FWIW I will apply this patch even though its wrong.

And no please, do not apply this.

This needs to be fixes from the chain graph detection.