Skip to contentSkip to conversation

netfilter: nf_tables: add NFT_HOOK_UNREGISTERED and use it

PATCH Superseded Pablo Neira Ayuso 1 participant 5 messages Dec 1

Subsystems (2)

v5 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH v5 1/5 netfilter: nf_tables: add NFT_HOOK_UNREGISTERED and use it

Commit f9a43007d3f7 ("netfilter: nf_tables: double hook unregistration in netns path") fixes a race in the netns exit path with netdev device event handler. The netns pre_exit handler unregisters the hooks, then exit release them. In between this two calls, the netdev event might show up and try to unregister the hooks again. This patch revisit this fix by adding a new NFT_HOOK_UNREGISTERED flag which allows us to skip the hook from the netdevice and flowtable event handlers.

The chatbot also suggests to document this:

First, __nft_release_hook() no longer frees chain and flowtable hooks during netns pre_exit or nft_rcv_nl_event(). They are now freed later by nf_tables_chain_destroy() and nf_tables_flowtable_destroy()-> nft_hooks_destroy().

Second, the chain update abort in __nf_tables_abort() no longer frees the hooks, stats or name, and no longer calls nft_trans_destroy(). The transaction now stays on commit_list. That makes the update branch in nf_tables_abort_release() live, and the freeing now happens there after synchronize_rcu().

This patch is a prerequisit for another follow up fix for the flowtable hardware offload which allows to unregister the flow_block once the entries in hardware have been removed.

---
v5: add two-liner check for NFT_HOOK_UNREGISTERED to deal with the
unlikely case where hook was unregistered and register net_device event tries to register it again. This is defensive, I don't see how that can really ever happen. This is control plane, it should not harm anyway.

 include/net/netfilter/nf_tables.h |  3 +-
 net/netfilter/nf_tables_api.c     | 57 ++++++++++++++++---------------
 net/netfilter/nft_chain_filter.c  |  6 ++--
 3 files changed, 35 insertions(+), 31 deletions(-)
include/net/netfilter/nf_tables.h +2 -1
index 9d597482363d..5edff5d7dacd 100644
@@ -1209,7 +1209,8 @@ struct nft_stats {
 	struct u64_stats_sync	syncp;
 };
 
-#define NFT_HOOK_REMOVE	(1 << 0)
+#define NFT_HOOK_REMOVE		(1 << 0)
+#define NFT_HOOK_UNREGISTERED	(1 << 1)
 
 struct nft_hook {
 	struct list_head	list;
net/netfilter/nf_tables_api.c +29 -28

158 lines not on this page · raw message

net/netfilter/nft_chain_filter.c +4 -2
index e04e689f2228..ffbf5dad3139 100644
@@ -337,7 +337,8 @@ static int nft_netdev_event(unsigned long event, struct net_device *dev,
 			if (!ops || (changename && match))
 				continue;
 
-			if (!(table->flags & NFT_TABLE_F_DORMANT))
+			if (!(table->flags & NFT_TABLE_F_DORMANT) &&
+			    !(hook->flags & NFT_HOOK_UNREGISTERED))
 				nf_unregister_net_hook(dev_net(dev), ops);
 
 			list_del_rcu(&ops->list);
@@ -356,7 +357,8 @@ static int nft_netdev_event(unsigned long event, struct net_device *dev,
 
 			ops->dev = dev;
 
-			if (!(table->flags & NFT_TABLE_F_DORMANT) &&
+			if ((!(table->flags & NFT_TABLE_F_DORMANT) &&
+			     !(hook->flags & NFT_HOOK_UNREGISTERED)) &&
 			    nf_register_net_hook(dev_net(dev), ops)) {
 				kfree(ops);
 				return 1;
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH v5 2/5 netfilter: nf_tables: unbind flowtable on hook release

Commit 13210fc63f35 ("netfilter: nf_tables: imbalance in flowtable binding") breaks hardware offload by unbinding the flow_block too early, this results in leaving entries in the hardware. Any pending offload work will be ignored because there is no flow_block anymore in place.

This patch restores the strategy followed by commit 5acab91458ce ("netfilter: nf_tables: unbind callbacks from flowtable destroy path") by unregistering the hook first, then unbind the flow_block from the error and destroy paths, right before the hook is released.

Fixes 13210fc63f35 ("netfilter: nf_tables: imbalance in flowtable binding")

---
v5: no changes

 net/netfilter/nf_tables_api.c | 65 +++++++++++++++++++++++++----------
 1 file changed, 47 insertions(+), 18 deletions(-)
net/netfilter/nf_tables_api.c +47 -18
index e367da8198ce..70ac5bbe7877 100644
@@ -380,6 +380,22 @@ static void nft_netdev_hook_unlink_free_rcu(struct nft_hook *hook)
 	nft_netdev_hook_free_rcu(hook);
 }
 
+static void nft_flowtable_hook_unlink_free_rcu(struct nft_flowtable *flowtable,
+					       struct nft_hook *hook)
+{
+	struct nf_hook_ops *ops;
+
+	/* This unbind command needs to come this late, otherwise offload
+	 * worker finds no flow_block and entries are not cleaned up from
+	 * hardware.
+	 */
+	list_for_each_entry(ops, &hook->ops_list, list) {
+		flowtable->data.type->setup(&flowtable->data, ops->dev,
+					    FLOW_BLOCK_UNBIND);
+	}
+	nft_netdev_hook_unlink_free_rcu(hook);
+}
+
 static void nft_trans_hook_destroy(struct nft_trans_hook *trans_hook)
 {
 	list_del(&trans_hook->list);
@@ -9026,12 +9042,9 @@ nft_flowtable_type_get(struct net *net, u8 family)
 
 /* Only called from error and netdev event paths. */
 static void nft_unregister_flowtable_ops(struct net *net,
-					 struct nft_flowtable *flowtable,
 					 struct nf_hook_ops *ops)
 {
 	nf_unregister_net_hook(net, ops);
-	flowtable->data.type->setup(&flowtable->data, ops->dev,
-				    FLOW_BLOCK_UNBIND);
 }
 
 static void __nft_unregister_flowtable_net_hooks(struct net *net,
@@ -9043,7 +9056,7 @@ static void __nft_unregister_flowtable_net_hooks(struct net *net,
 
 	list_for_each_entry_safe(hook, next, hook_list, list) {
 		list_for_each_entry(ops, &hook->ops_list, list)
-			nft_unregister_flowtable_ops(net, flowtable, ops);
+			nft_unregister_flowtable_ops(net, ops);
 
 		hook->flags |= NFT_HOOK_UNREGISTERED;
 	}
@@ -9114,7 +9127,9 @@ static int nft_register_flowtable_net_hooks(struct net *net,
 			if (i-- <= 0)
 				break;
 
-			nft_unregister_flowtable_ops(net, flowtable, ops);
+			nft_unregister_flowtable_ops(net, ops);
+			flowtable->data.type->setup(&flowtable->data, ops->dev,
+						    FLOW_BLOCK_UNBIND);
 		}
 		nft_netdev_hook_unlink_free_rcu(hook);
 	}
@@ -9141,9 +9156,9 @@ static void nft_flowtable_unregister_trans_hook(struct net *net,
 	list_for_each_entry_safe(trans_hook, next, hook_list, list) {
 		hook = trans_hook->hook;
 		list_for_each_entry(ops, &hook->ops_list, list)
-			nft_unregister_flowtable_ops(net, flowtable, ops);
+			nft_unregister_flowtable_ops(net, ops);
 
-		nft_netdev_hook_unlink_free_rcu(hook);
+		nft_flowtable_hook_unlink_free_rcu(flowtable, trans_hook->hook);
 		nft_trans_hook_destroy(trans_hook);
 	}
 }
@@ -9229,9 +9244,11 @@ static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh,
 err_flowtable_update_hook:
 	list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) {
 		if (unregister) {
-			list_for_each_entry(ops, &hook->ops_list, list)
-				nft_unregister_flowtable_ops(ctx->net,
-							     flowtable, ops);
+			list_for_each_entry(ops, &hook->ops_list, list) {
+				nft_unregister_flowtable_ops(ctx->net, ops);
+				flowtable->data.type->setup(&flowtable->data, ops->dev,
+							    FLOW_BLOCK_UNBIND);
+			}
 		}
 		nft_netdev_hook_unlink_free_rcu(hook);
 	}
@@ -9752,10 +9769,19 @@ static void nf_tables_flowtable_notify(struct nft_ctx *ctx,
 	nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
 }
 
+static void nft_flowtable_hooks_destroy(struct nft_flowtable *flowtable,
+					struct list_head *hook_list)
+{
+	struct nft_hook *hook, *next;
+
+	list_for_each_entry_safe(hook, next, hook_list, list)
+		nft_flowtable_hook_unlink_free_rcu(flowtable, hook);
+}
+
 static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable)
 {
 	flowtable->data.type->free(&flowtable->data);
-	nft_hooks_destroy(&flowtable->hook_list);
+	nft_flowtable_hooks_destroy(flowtable, &flowtable->hook_list);
 	kfree(flowtable->name);
 	module_put(flowtable->data.type->owner);
 	kfree(flowtable);
@@ -9830,10 +9856,11 @@ static int nft_flowtable_event(unsigned long event, struct net_device *dev,
 				continue;
 
 			/* flow_offload_netdev_event() cleans up entries for us. */
-			if (!(hook->flags & NFT_HOOK_UNREGISTERED)) {
-				nft_unregister_flowtable_ops(dev_net(dev),
-							     flowtable, ops);
-			}
+			if (!(hook->flags & NFT_HOOK_UNREGISTERED))
+				nft_unregister_flowtable_ops(dev_net(dev), ops);
+
+			flowtable->data.type->setup(&flowtable->data, ops->dev,
+						    FLOW_BLOCK_UNBIND);
 			list_del_rcu(&ops->list);
 			kfree_rcu(ops, rcu);
 			break;
@@ -11315,10 +11342,12 @@ static void nf_tables_abort_release(struct nft_trans *trans)
 		nft_obj_destroy(&ctx, nft_trans_obj(trans));
 		break;
 	case NFT_MSG_NEWFLOWTABLE:
-		if (nft_trans_flowtable_update(trans))
-			nft_hooks_destroy(&nft_trans_flowtable_hooks(trans));
-		else
+		if (nft_trans_flowtable_update(trans)) {
+			nft_flowtable_hooks_destroy(nft_trans_flowtable(trans),
+						    &nft_trans_flowtable_hooks(trans));
+		} else {
 			nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
+		}
 		break;
 	}
 	kfree(trans);
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH v5 3/5 netfilter: nf_tables: flush destroy work from flowtable netdev event handler

In reply to Pablo Neira Ayuso

Sashiko reports that the following race is possible while dismantling the flowtable with hardware offload:

CPU0 CPU1 (kworker)
nf_tables_commit()
list_del_rcu(&flowtable->list)
nft_unregister_flowtable_net_hooks()
nf_tables_commit_release()
schedule_work(&destroy_work)
mutex_unlock(&commit_mutex)

                                      nf_tables_trans_destroy_work()
                                        synchronize_rcu()
                                        ...

unregister_netdevice()
nf_tables_flowtable_event()
/* flowtable not in
* table->flowtables, ops->dev
* stays on flowtable->hook_list
*/
free_netdev(dev)

                                        nf_tables_flowtable_destroy()
                                          nft_flowtable_hooks_destroy()
                                            ...
                                            flowtable->data.type->setup(
                                              ..., ops->dev,
                                              FLOW_BLOCK_UNBIND)
                                              <-- use-after-free

A netdevice event could happen in between the commit and commit_release. After commit, the flowtable has been removed from the table list, therefore, it is not reachable by the flowtable netdevice handler so the hook cannot be unregistered and a stale reference to the netdevice remains in place, leading to possible UaF from the commit_release path when accessing the netdevice.

Fix this by flushing the pending destroy work before handling netdevice events.

Fixes 13210fc63f35 ("netfilter: nf_tables: imbalance in flowtable binding")

---
v5: no changes

 net/netfilter/nf_tables_api.c | 2 ++
 1 file changed, 2 insertions(+)
net/netfilter/nf_tables_api.c +2 -0
index 70ac5bbe7877..4f48a11aeb9f 100644
@@ -9933,6 +9933,8 @@ static int nf_tables_flowtable_event(struct notifier_block *this,
 	nft_net = nft_pernet(net);
 	mutex_lock(&nft_net->commit_mutex);
 
+	nf_tables_trans_destroy_flush_work(net);
+
 	if (event == NETDEV_CHANGENAME) {
 		if (__nf_tables_flowtable_event(NETDEV_REGISTER, dev, true)) {
 			ret = NOTIFY_BAD;
Signature · 1 line
2.47.3
author maintainer
#3 lore raw

PATCH v5 4/5 netfilter: nf_tables: clean up flowtable entries in hardware on updates

In reply to Pablo Neira Ayuso

Flowtable updates adding or deleting net_devices result in leaving entries behind in the hardware. This patch completes the flowtable update support by cleaning up stale entries per net_device in two scenarios:

- If an existing net_device is deleted from the flowtable.
- If a new net_device is added, but then transaction is aborted.

Fixes 78d9f48f7f44 ("netfilter: nf_tables: add devices to existing flowtable")
Fixes abadb2f865d7 ("netfilter: nf_tables: delete devices from flowtable")

---
v5: new in the series.

 include/net/netfilter/nf_flow_table.h |  5 +++
 net/netfilter/nf_flow_table_inet.c    |  3 ++
 net/netfilter/nf_flow_table_offload.c | 11 +++++++
 net/netfilter/nf_tables_api.c         | 44 +++++++++++++++++++++++----
 4 files changed, 57 insertions(+), 6 deletions(-)
include/net/netfilter/nf_flow_table.h +5 -0
index 5b22d31cb140..a4750882c115 100644
@@ -57,6 +57,8 @@ struct nf_flowtable_type {
 	int				(*setup)(struct nf_flowtable *ft,
 						 struct net_device *dev,
 						 enum flow_block_command cmd);
+	void				(*cleanup)(struct nf_flowtable *ft,
+						   struct net_device *dev);
 	int				(*action)(struct net *net,
 						  struct flow_offload *flow,
 						  enum flow_offload_tuple_dir dir,
@@ -380,6 +382,9 @@ void nf_flow_table_offload_flush_cleanup(struct nf_flowtable *flowtable);
 int nf_flow_table_offload_setup(struct nf_flowtable *flowtable,
 				struct net_device *dev,
 				enum flow_block_command cmd);
+void nf_flow_table_offload_cleanup(struct nf_flowtable *flowtable,
+				   struct net_device *dev);
+
 int nf_flow_rule_route_ipv4(struct net *net, struct flow_offload *flow,
 			    enum flow_offload_tuple_dir dir,
 			    struct nf_flow_rule *flow_rule);
net/netfilter/nf_flow_table_inet.c +3 -0
index b0f199171932..c296249812fe 100644
@@ -69,6 +69,7 @@ static struct nf_flowtable_type flowtable_inet = {
 	.family		= NFPROTO_INET,
 	.init		= nf_flow_table_init,
 	.setup		= nf_flow_table_offload_setup,
+	.cleanup	= nf_flow_table_offload_cleanup,
 	.action		= nf_flow_rule_route_inet,
 	.free		= nf_flow_table_free,
 	.hook		= nf_flow_offload_inet_hook,
@@ -79,6 +80,7 @@ static struct nf_flowtable_type flowtable_ipv4 = {
 	.family		= NFPROTO_IPV4,
 	.init		= nf_flow_table_init,
 	.setup		= nf_flow_table_offload_setup,
+	.cleanup	= nf_flow_table_offload_cleanup,
 	.action		= nf_flow_rule_route_ipv4,
 	.free		= nf_flow_table_free,
 	.hook		= nf_flow_offload_ip_hook,
@@ -89,6 +91,7 @@ static struct nf_flowtable_type flowtable_ipv6 = {
 	.family		= NFPROTO_IPV6,
 	.init		= nf_flow_table_init,
 	.setup		= nf_flow_table_offload_setup,
+	.cleanup	= nf_flow_table_offload_cleanup,
 	.action		= nf_flow_rule_route_ipv6,
 	.free		= nf_flow_table_free,
 	.hook		= nf_flow_offload_ipv6_hook,
net/netfilter/nf_flow_table_offload.c +11 -0
index 4365859220e6..89a2e310776c 100644
@@ -1268,6 +1268,17 @@ static int nf_flow_table_offload_cmd(struct flow_block_offload *bo,
 	return 0;
 }
 
+void nf_flow_table_offload_cleanup(struct nf_flowtable *flowtable,
+				   struct net_device *dev)
+{
+	if (!nf_flowtable_hw_offload(flowtable))
+		return;
+
+	nf_flow_table_offload_flush(flowtable);
+	nf_flow_table_gc_cleanup(flowtable, dev);
+}
+EXPORT_SYMBOL_GPL(nf_flow_table_offload_cleanup);
+
 int nf_flow_table_offload_setup(struct nf_flowtable *flowtable,
 				struct net_device *dev,
 				enum flow_block_command cmd)
net/netfilter/nf_tables_api.c +38 -6

80 lines not on this page · raw message

Signature · 1 line
2.47.3
author maintainer
#4 lore raw

PATCH v5 5/5 netfilter: nf_tables: flush pending destroy work on new flowtable

In reply to Pablo Neira Ayuso

If userspace deletes a hardware offloaded flowtable and then adds a new one on the same device in the next transaction (classic ruleset reload). The flow_block BIND command from nf_tables_newflowtable() can reach the driver while the old block_cb is pending to be unregistered via destroy worker. Force a flush of pending work in this case.

Fixes 13210fc63f35 ("netfilter: nf_tables: imbalance in flowtable binding")

---
v5: new in this series

 net/netfilter/nf_tables_api.c | 2 ++
 1 file changed, 2 insertions(+)
net/netfilter/nf_tables_api.c +2 -0
index f856b1daff37..d5a3911fea5b 100644
@@ -9335,6 +9335,8 @@ static int nf_tables_newflowtable(struct sk_buff *skb,
 			err = -EOPNOTSUPP;
 			goto err3;
 		}
+		if (flowtable->data.flags & NFT_FLOWTABLE_HW_OFFLOAD)
+			nf_tables_trans_destroy_flush_work(net);
 	}
 
 	write_pnet(&flowtable->data.net, net);
Signature · 1 line
2.47.3