Skip to contentSkip to conversation

netfilter: nf_flow_table_offload: fix heap overflow in flow_action_entry_next()

PATCH In review Hyunwoo Kim 3 participants 13 messages Mar 7 – Mar 26

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

PATCH 1/1 netfilter: nf_flow_table_offload: fix heap overflow in flow_action_entry_next()

flow_action_entry_next() increments num_entries and returns a pointer into the flow_action_entry array without any bounds checking. The array is allocated with a fixed size of NF_FLOW_RULE_ACTION_MAX (16) entries, but certain combinations of IPv6 + SNAT + DNAT + double VLAN (QinQ) require 17 or more entries, causing a slab-out-of-bounds write in the kmalloc-4k slab.

The maximum possible entry count is:
tunnel(2) + eth(4) + VLAN(4) + IPv6_NAT(10) + redirect(1) = 21

Increase NF_FLOW_RULE_ACTION_MAX to 24 (with headroom) to cover the worst case.

Fixes efce49dfe6a8 ("netfilter: flowtable: add vlan pop action offload support")
---
 net/netfilter/nf_flow_table_offload.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_flow_table_offload.c +1 -1
index 9b677e116487..e843f6d0355e 100644
@@ -727,7 +727,7 @@ int nf_flow_rule_route_ipv6(struct net *net, struct flow_offload *flow,
 }
 EXPORT_SYMBOL_GPL(nf_flow_rule_route_ipv6);
 
-#define NF_FLOW_RULE_ACTION_MAX	16
+#define NF_FLOW_RULE_ACTION_MAX	24
 
 static struct nf_flow_rule *
 nf_flow_offload_rule_alloc(struct net *net,
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 12 lines · Hyunwoo Kim

flow_action_entry_next() increments num_entries and returns a pointer into the flow_action_entry array without any bounds checking. The array is allocated with a fixed size of NF_FLOW_RULE_ACTION_MAX (16) entries, but certain combinations of IPv6 + SNAT + DNAT + double VLAN (QinQ) require 17 or more entries, causing a slab-out-of-bounds write in the kmalloc-4k slab.

The maximum possible entry count is:
tunnel(2) + eth(4) + VLAN(4) + IPv6_NAT(10) + redirect(1) = 21

Increase NF_FLOW_RULE_ACTION_MAX to 24 (with headroom) to cover the

net/netfilter/nf_flow_table_offload.c int nf_flow_rule_route_ipv6(struct net *net, struct flow_offload *flow, · line 730

Quoted 2 lines
net/netfilter/nf_flow_table_offload.c +1 -1
-#define NF_FLOW_RULE_ACTION_MAX	16
+#define NF_FLOW_RULE_ACTION_MAX	24

This fix looks rather fragile.

What guarantees that this stays right-sized?

Can you add a BUILD_BUG_ON or if needed, run-time check?

#11 lore raw
In reply to Hyunwoo Kim
Quoted 9 lines · Hyunwoo Kim

flow_action_entry_next() increments num_entries and returns a pointer into the flow_action_entry array without any bounds checking. The array is allocated with a fixed size of NF_FLOW_RULE_ACTION_MAX (16) entries, but certain combinations of IPv6 + SNAT + DNAT + double VLAN (QinQ) require 17 or more entries, causing a slab-out-of-bounds write in the kmalloc-4k slab.

The maximum possible entry count is:
tunnel(2) + eth(4) + VLAN(4) + IPv6_NAT(10) + redirect(1) = 21

There is no hardware offload for the:

- tunnel support
- IPv6 NAT

This is all very hypothetical.

Quoted 25 lines

Increase NF_FLOW_RULE_ACTION_MAX to 24 (with headroom) to cover the worst case.

Fixes efce49dfe6a8 ("netfilter: flowtable: add vlan pop action offload support")
---
 net/netfilter/nf_flow_table_offload.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_flow_table_offload.c +1 -1
index 9b677e116487..e843f6d0355e 100644
@@ -727,7 +727,7 @@ int nf_flow_rule_route_ipv6(struct net *net, struct flow_offload *flow,
 }
 EXPORT_SYMBOL_GPL(nf_flow_rule_route_ipv6);
 
-#define NF_FLOW_RULE_ACTION_MAX	16
+#define NF_FLOW_RULE_ACTION_MAX	24
 
 static struct nf_flow_rule *
 nf_flow_offload_rule_alloc(struct net *net,
Signature · 1 line
2.43.0
maintainer
#2 lore raw
In reply to Florian Westphal
Quoted 13 lines · Florian Westphal
Quoted 12 lines · Hyunwoo Kim

flow_action_entry_next() increments num_entries and returns a pointer into the flow_action_entry array without any bounds checking. The array is allocated with a fixed size of NF_FLOW_RULE_ACTION_MAX (16) entries, but certain combinations of IPv6 + SNAT + DNAT + double VLAN (QinQ) require 17 or more entries, causing a slab-out-of-bounds write in the kmalloc-4k slab.

The maximum possible entry count is:
tunnel(2) + eth(4) + VLAN(4) + IPv6_NAT(10) + redirect(1) = 21

Increase NF_FLOW_RULE_ACTION_MAX to 24 (with headroom) to cover the

Quoted 8 lines

net/netfilter/nf_flow_table_offload.c int nf_flow_rule_route_ipv6(struct net *net, struct flow_offload *flow, · line 730

Quoted 2 lines
net/netfilter/nf_flow_table_offload.c +1 -1
-#define NF_FLOW_RULE_ACTION_MAX	16
+#define NF_FLOW_RULE_ACTION_MAX	24

This fix looks rather fragile.

What guarantees that this stays right-sized?

Can you add a BUILD_BUG_ON or if needed, run-time check?

Ping. I'm not even sure if there is a bug to begin with, see Pablos response. How did you conclude there is a missing bounds check and that this increase is the best fix?

Normally there should be a check that prevents such a configuration. If thats missing, please add one instead of increasing this define.

#3 lore raw
Quoted 19 lines · Florian Westphal
Quoted 18 lines · Florian Westphal
Quoted 12 lines · Hyunwoo Kim

flow_action_entry_next() increments num_entries and returns a pointer into the flow_action_entry array without any bounds checking. The array is allocated with a fixed size of NF_FLOW_RULE_ACTION_MAX (16) entries, but certain combinations of IPv6 + SNAT + DNAT + double VLAN (QinQ) require 17 or more entries, causing a slab-out-of-bounds write in the kmalloc-4k slab.

The maximum possible entry count is:
tunnel(2) + eth(4) + VLAN(4) + IPv6_NAT(10) + redirect(1) = 21

Increase NF_FLOW_RULE_ACTION_MAX to 24 (with headroom) to cover the

net/netfilter/nf_flow_table_offload.c int nf_flow_rule_route_ipv6(struct net *net, struct flow_offload *flow, · line 730

Quoted 2 lines
net/netfilter/nf_flow_table_offload.c +1 -1
-#define NF_FLOW_RULE_ACTION_MAX	16
+#define NF_FLOW_RULE_ACTION_MAX	24

This fix looks rather fragile.

Quoted 5 lines
Quoted 3 lines

What guarantees that this stays right-sized?

Can you add a BUILD_BUG_ON or if needed, run-time check?

Ping. I'm not even sure if there is a bug to begin with, see Pablos

Sorry for the late reply.

To clarify, I triggered the overflow using a dummy device that accepts TC_SETUP_FT, as I don't have real offload-capable hardware. The 17 entry scenario requires double VLAN (QinQ) + IPv6 + SNAT + DNAT simultaneously, which is unlikely in real-world deployments, so it is hypothetical.

Quoted 5 lines

response. How did you conclude there is a missing bounds check and that this increase is the best fix?

Normally there should be a check that prevents such a configuration. If thats missing, please add one instead of increasing this define.

So, should I send a v2 with a bounds check, or drop this patch?

#4 lore raw
Quoted 33 lines · Hyunwoo Kim
Quoted 19 lines · Florian Westphal
Quoted 18 lines · Florian Westphal
Quoted 12 lines · Hyunwoo Kim

flow_action_entry_next() increments num_entries and returns a pointer into the flow_action_entry array without any bounds checking. The array is allocated with a fixed size of NF_FLOW_RULE_ACTION_MAX (16) entries, but certain combinations of IPv6 + SNAT + DNAT + double VLAN (QinQ) require 17 or more entries, causing a slab-out-of-bounds write in the kmalloc-4k slab.

The maximum possible entry count is:
tunnel(2) + eth(4) + VLAN(4) + IPv6_NAT(10) + redirect(1) = 21

Increase NF_FLOW_RULE_ACTION_MAX to 24 (with headroom) to cover the

net/netfilter/nf_flow_table_offload.c int nf_flow_rule_route_ipv6(struct net *net, struct flow_offload *flow, · line 730

Quoted 2 lines
net/netfilter/nf_flow_table_offload.c +1 -1
-#define NF_FLOW_RULE_ACTION_MAX	16
+#define NF_FLOW_RULE_ACTION_MAX	24

This fix looks rather fragile.

Quoted 5 lines
Quoted 3 lines

What guarantees that this stays right-sized?

Can you add a BUILD_BUG_ON or if needed, run-time check?

Ping. I'm not even sure if there is a bug to begin with, see Pablos

Sorry for the late reply.

To clarify, I triggered the overflow using a dummy device that accepts TC_SETUP_FT, as I don't have real offload-capable hardware. The 17 entry scenario requires double VLAN (QinQ) + IPv6 + SNAT + DNAT simultaneously, which is unlikely in real-world deployments, so it is hypothetical.

Quoted 7 lines
Quoted 5 lines

response. How did you conclude there is a missing bounds check and that this increase is the best fix?

Normally there should be a check that prevents such a configuration. If thats missing, please add one instead of increasing this define.

So, should I send a v2 with a bounds check, or drop this patch?

+ Since this is not a real-world environment, it seems better not to apply the patch.

maintainer
#5 lore raw
In reply to Hyunwoo Kim
Quoted 8 lines · Hyunwoo Kim
Quoted 1 line

Ping. I'm not even sure if there is a bug to begin with, see Pablos

Sorry for the late reply.

To clarify, I triggered the overflow using a dummy device that accepts TC_SETUP_FT, as I don't have real offload-capable hardware. The 17 entry scenario requires double VLAN (QinQ) + IPv6 + SNAT + DNAT simultaneously, which is unlikely in real-world deployments, so it is hypothetical.

If you triggered it, its not hyptothetical and needs to be fixed.

Quoted 4 lines
Quoted 2 lines

Normally there should be a check that prevents such a configuration. If thats missing, please add one instead of increasing this define.

So, should I send a v2 with a bounds check, or drop this patch?

Yes, please send a v2 that prevents the overflow at configuration time.

#6 lore raw
Quoted 11 lines · Florian Westphal
Quoted 8 lines · Hyunwoo Kim
Quoted 1 line

Ping. I'm not even sure if there is a bug to begin with, see Pablos

Sorry for the late reply.

To clarify, I triggered the overflow using a dummy device that accepts TC_SETUP_FT, as I don't have real offload-capable hardware. The 17 entry scenario requires double VLAN (QinQ) + IPv6 + SNAT + DNAT simultaneously, which is unlikely in real-world deployments, so it is hypothetical.

If you triggered it, its not hyptothetical and needs to be fixed.

He triggered it with... a device which is not in the tree? How is tunnel really supported with TC_SETUP_FT? What driver did gain support for this? And SNAT and DNAT !?!?

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Normally there should be a check that prevents such a configuration. If thats missing, please add one instead of increasing this define.

So, should I send a v2 with a bounds check, or drop this patch?

Yes, please send a v2 that prevents the overflow at configuration time.

Just rising the maximum amount is a workaround, I did not check yet the implications of this.

Yes, better checks would be good to have here, I agree, because this is fragile, for future proofing.

#7 lore raw
In reply to Florian Westphal
Quoted 10 lines · Florian Westphal
Quoted 8 lines · Hyunwoo Kim
Quoted 1 line

Ping. I'm not even sure if there is a bug to begin with, see Pablos

Sorry for the late reply.

To clarify, I triggered the overflow using a dummy device that accepts TC_SETUP_FT, as I don't have real offload-capable hardware. The 17 entry scenario requires double VLAN (QinQ) + IPv6 + SNAT + DNAT simultaneously, which is unlikely in real-world deployments, so it is hypothetical.

Quoted 8 lines

If you triggered it, its not hyptothetical and needs to be fixed.

Quoted 4 lines
Quoted 2 lines

Normally there should be a check that prevents such a configuration. If thats missing, please add one instead of increasing this define.

So, should I send a v2 with a bounds check, or drop this patch?

Yes, please send a v2 that prevents the overflow at configuration time.

hmm. So, based on what you said, I assume the run-time check would look something like this?

net/netfilter/nf_flow_table_offload.c +3 -0
index 9b677e116487..69ffefbdd5e8 100644
@@ -218,6 +218,9 @@ flow_action_entry_next(struct nf_flow_rule *flow_rule)
 {
        int i = flow_rule->rule->action.num_entries++;
 
+       if (WARN_ON_ONCE(i >= NF_FLOW_RULE_ACTION_MAX))
+               return NULL;
+
        return &flow_rule->rule->action.entries[i];
 }
 

However, if we add a runtime check in this way, all callers of flow_action_entry_next() would also need to handle a NULL return value, since none of them currently perform a null check.

Because of the potential risk, this would require modifying quite a number of call sites carefully. What do you think about this approach?

maintainer
#8 lore raw
Quoted 21 lines · Hyunwoo Kim

hmm. So, based on what you said, I assume the run-time check would look something like this?

net/netfilter/nf_flow_table_offload.c +3 -0
index 9b677e116487..69ffefbdd5e8 100644
@@ -218,6 +218,9 @@ flow_action_entry_next(struct nf_flow_rule *flow_rule)
 {
        int i = flow_rule->rule->action.num_entries++;
 
+       if (WARN_ON_ONCE(i >= NF_FLOW_RULE_ACTION_MAX))
+               return NULL;
+
        return &flow_rule->rule->action.entries[i];
 }
 

However, if we add a runtime check in this way, all callers of flow_action_entry_next() would also need to handle a NULL return value, since none of them currently perform a null check.

Quoted 2 lines

Because of the potential risk, this would require modifying quite a number of call sites carefully. What do you think about this approach?

Can't we reject this at configuration time?

I mean, userspace has to ask for this action sequence, no?

maintainer
#9 lore raw
Quoted 22 lines · Florian Westphal
Quoted 21 lines · Hyunwoo Kim

hmm. So, based on what you said, I assume the run-time check would look something like this?

net/netfilter/nf_flow_table_offload.c +3 -0
index 9b677e116487..69ffefbdd5e8 100644
@@ -218,6 +218,9 @@ flow_action_entry_next(struct nf_flow_rule *flow_rule)
 {
        int i = flow_rule->rule->action.num_entries++;
 
+       if (WARN_ON_ONCE(i >= NF_FLOW_RULE_ACTION_MAX))
+               return NULL;
+
        return &flow_rule->rule->action.entries[i];
 }
 

However, if we add a runtime check in this way, all callers of flow_action_entry_next() would also need to handle a NULL return value, since none of them currently perform a null check.

Quoted 6 lines
Quoted 2 lines

Because of the potential risk, this would require modifying quite a number of call sites carefully. What do you think about this approach?

Can't we reject this at configuration time?

I mean, userspace has to ask for this action sequence, no?

Guess:

net/netfilter/nf_tables_offload.c +3 -0
@@ -105,6 +105,9 @@ struct nft_flow_rule *nft_flow_rule_create(struct net *net,
        if (num_actions == 0)
                return ERR_PTR(-EOPNOTSUPP);
 
+       if (num_actions > NF_FLOW_RULE_ACTION_MAX)
+               return ERR_PTR(-EOPNOTSUPP);
+
        flow = nft_flow_rule_alloc(num_actions);
        if (!flow)
                return ERR_PTR(-ENOMEM);
#10 lore raw
Quoted 37 lines · Florian Westphal
Quoted 22 lines · Florian Westphal
Quoted 21 lines · Hyunwoo Kim

hmm. So, based on what you said, I assume the run-time check would look something like this?

net/netfilter/nf_flow_table_offload.c +3 -0
index 9b677e116487..69ffefbdd5e8 100644
@@ -218,6 +218,9 @@ flow_action_entry_next(struct nf_flow_rule *flow_rule)
 {
        int i = flow_rule->rule->action.num_entries++;
 
+       if (WARN_ON_ONCE(i >= NF_FLOW_RULE_ACTION_MAX))
+               return NULL;
+
        return &flow_rule->rule->action.entries[i];
 }
 

However, if we add a runtime check in this way, all callers of flow_action_entry_next() would also need to handle a NULL return value, since none of them currently perform a null check.

Quoted 6 lines
Quoted 2 lines

Because of the potential risk, this would require modifying quite a number of call sites carefully. What do you think about this approach?

Can't we reject this at configuration time?

I mean, userspace has to ask for this action sequence, no?

Guess:

net/netfilter/nf_tables_offload.c struct nft_flow_rule *nft_flow_rule_create(struct net *net, · line 113

Quoted 9 lines
net/netfilter/nf_tables_offload.c +3 -0
@@ -105,6 +105,9 @@ struct nft_flow_rule *nft_flow_rule_create(struct net *net,
        if (num_actions == 0)
                return ERR_PTR(-EOPNOTSUPP);
 
+       if (num_actions > NF_FLOW_RULE_ACTION_MAX)
+               return ERR_PTR(-EOPNOTSUPP);
+
        flow = nft_flow_rule_alloc(num_actions);
        if (!flow)
                return ERR_PTR(-ENOMEM);

That is good enough, thanks.

Would you submit this?

This is nf-next material, it is not possible to reach such number of actions (16) in the flowtable.

#12 lore raw
In reply to Hyunwoo Kim
Quoted 7 lines · Hyunwoo Kim

flow_action_entry_next() increments num_entries and returns a pointer into the flow_action_entry array without any bounds checking. The array is allocated with a fixed size of NF_FLOW_RULE_ACTION_MAX (16) entries, but certain combinations of IPv6 + SNAT + DNAT + double VLAN (QinQ) require 17 or more entries, causing a slab-out-of-bounds write in the kmalloc-4k slab.

Quoted 5 lines

The maximum possible entry count is:
tunnel(2) + eth(4) + VLAN(4) + IPv6_NAT(10) + redirect(1) = 21

Increase NF_FLOW_RULE_ACTION_MAX to 24 (with headroom) to cover the worst case.

For the record, proposed patch:

https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/