Skip to contentSkip to conversation

netfilter: nf_tables: autoload modules from the abort path

PATCH Superseded Pablo Neira Ayuso 2 participants 5 messages Jan 22, 2020 – Jan 23, 2020

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH v2 1/1 netfilter: nf_tables: autoload modules from the abort path

This patch introduces a list of pending module requests that is updated from preparation phase. Then, at the very end of the abort phase, iterate over this list to load the requested modules. Given the mutex is released when loading modules, the list of modules is zapped so this is iteration occurs over a local list. Thus, the request_module() call occurs when object lists in consistent state and the commit list is empty.

This new module list is composed of nft_module_request objects that contain the module name and one status field that tells if the module has been already loaded (the 'done' field).

The module list handling must be the very last thing, and the commit list must be already empty.

This patch also updates nfnetlink to include the reason to enter the abort phase, which is required for this new autoload module rationale.

Fixes 452238e8d5ff ("netfilter: nf_tables: add and use helper for module autoload")
Reported-by [email protected]

---
v2: call nf_tables_module_autoload_cleanup() holding the mutex from
the nf_tables_commit_release() path. Only one Fixes: tag.

 include/linux/netfilter/nfnetlink.h |   2 +-
 include/net/netns/nftables.h        |   1 +
 net/netfilter/nf_tables_api.c       | 121 ++++++++++++++++++++++++------------
 net/netfilter/nfnetlink.c           |   6 +-
 4 files changed, 86 insertions(+), 44 deletions(-)
include/linux/netfilter/nfnetlink.h +1 -1
index cf09ab37b45b..851425c3178f 100644
@@ -31,7 +31,7 @@ struct nfnetlink_subsystem {
 	const struct nfnl_callback *cb;	/* callback for individual types */
 	struct module *owner;
 	int (*commit)(struct net *net, struct sk_buff *skb);
-	int (*abort)(struct net *net, struct sk_buff *skb);
+	int (*abort)(struct net *net, struct sk_buff *skb, bool autoload);
 	void (*cleanup)(struct net *net);
 	bool (*valid_genid)(struct net *net, u32 genid);
 };
include/net/netns/nftables.h +1 -0
index 286fd960896f..a1a8d45adb42 100644
@@ -7,6 +7,7 @@
 struct netns_nftables {
 	struct list_head	tables;
 	struct list_head	commit_list;
+	struct list_head	module_list;
 	struct mutex		commit_mutex;
 	unsigned int		base_seq;
 	u8			gencursor;
net/netfilter/nf_tables_api.c +81 -40

236 lines not on this page · raw message

net/netfilter/nfnetlink.c +3 -3
index 4abbb452cf6c..99127e2d95a8 100644
@@ -476,7 +476,7 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
 	}
 done:
 	if (status & NFNL_BATCH_REPLAY) {
-		ss->abort(net, oskb);
+		ss->abort(net, oskb, true);
 		nfnl_err_reset(&err_list);
 		kfree_skb(skb);
 		module_put(ss->owner);
@@ -487,11 +487,11 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
 			status |= NFNL_BATCH_REPLAY;
 			goto done;
 		} else if (err) {
-			ss->abort(net, oskb);
+			ss->abort(net, oskb, false);
 			netlink_ack(oskb, nlmsg_hdr(oskb), err, NULL);
 		}
 	} else {
-		ss->abort(net, oskb);
+		ss->abort(net, oskb, false);
 	}
 	if (ss->cleanup)
 		ss->cleanup(net);
Signature · 1 line
2.11.0
maintainer
#1 lore raw

net/netfilter/nf_tables_api.c __nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family) · line 604

Quoted 4 lines · Pablo Neira Ayuso
net/netfilter/nf_tables_api.c +4 -0
+	list_for_each_entry(req, &net->nft.module_list, list) {
+		if (!strcmp(req->module, module_name) && req->done)
+			return 0;
+	}

If the module is already on this list, why does it need to be added a second time?

Other than that I like this idea as it avoids the entire "drop transaction mutex while inside a transaction" mess.

author maintainer
#2 lore raw
Quoted 8 lines · Florian Westphal

net/netfilter/nf_tables_api.c __nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family) · line 604

Quoted 4 lines · Pablo Neira Ayuso
net/netfilter/nf_tables_api.c +4 -0
+	list_for_each_entry(req, &net->nft.module_list, list) {
+		if (!strcmp(req->module, module_name) && req->done)
+			return 0;
+	}

If the module is already on this list, why does it need to be added a second time?

The first time this finds no module on the list, then the module is added to the list and nft_request_module() returns -EAGAIN. This triggers abort path with autoload parameter set to true from nfnetlink, this sets the module done field to true.

Now, on the second path, it will find that this already tried to load the module, so it does not add it again, nft_request_module() returns 0. Then, there is a look up to find the object that was missing. If module was successfully load, the object will be in place, otherwise -ENOENT is reported to userspace.

So the code above is just checking for the second pass after one abort with autoload parameter set on, not to read it again.

I can include this logic in the patch description in a v3.

Quoted 2 lines

Other than that I like this idea as it avoids the entire "drop transaction mutex while inside a transaction" mess.

request_module() and the transaction logic was not fitting well, after this there will be a well-defined location to do this.

I run the syzbot reproducer for 1 hour and no problems, not sure how much I have to run it more. I guess the more time the better.

Thanks.

author maintainer
#3 lore raw
Quoted 10 lines · Pablo Neira Ayuso
Quoted 8 lines · Florian Westphal

net/netfilter/nf_tables_api.c __nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family) · line 604

Quoted 4 lines · Pablo Neira Ayuso
net/netfilter/nf_tables_api.c +4 -0
+	list_for_each_entry(req, &net->nft.module_list, list) {
+		if (!strcmp(req->module, module_name) && req->done)
+			return 0;
+	}

If the module is already on this list, why does it need to be added a second time?

Quoted 6 lines

The first time this finds no module on the list, then the module is added to the list and nft_request_module() returns -EAGAIN. This triggers abort path with autoload parameter set to true from nfnetlink, this sets the module done field to true.

Now, on the second path, it will find that this already tried to load

s/second path/second pass

Quoted 20 lines

the module, so it does not add it again, nft_request_module() returns 0. Then, there is a look up to find the object that was missing. If module was successfully load, the object will be in place, otherwise -ENOENT is reported to userspace.

So the code above is just checking for the second pass after one abort with autoload parameter set on, not to read it again.

I can include this logic in the patch description in a v3.

Quoted 2 lines

Other than that I like this idea as it avoids the entire "drop transaction mutex while inside a transaction" mess.

request_module() and the transaction logic was not fitting well, after this there will be a well-defined location to do this.

I run the syzbot reproducer for 1 hour and no problems, not sure how much I have to run it more. I guess the more time the better.

Thanks.

maintainer
#4 lore raw
In reply to Pablo Neira Ayuso
Quoted 10 lines · Pablo Neira Ayuso
Quoted 8 lines · Florian Westphal

net/netfilter/nf_tables_api.c __nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family) · line 604

Quoted 4 lines · Pablo Neira Ayuso
net/netfilter/nf_tables_api.c +4 -0
+	list_for_each_entry(req, &net->nft.module_list, list) {
+		if (!strcmp(req->module, module_name) && req->done)
+			return 0;
+	}

If the module is already on this list, why does it need to be added a second time?

Quoted 4 lines

The first time this finds no module on the list, then the module is added to the list and nft_request_module() returns -EAGAIN. This triggers abort path with autoload parameter set to true from nfnetlink, this sets the module done field to true.

I guess I was confused by the need for the "&& req->done" part.

AFAIU req->done is always true here.

Quoted 2 lines

Now, on the second path, it will find that this already tried to load the module, so it does not add it again, nft_request_module() returns 0.

But the "I already tried this" is already implied by the presence of the module name? Or did I misunderstand?

Quoted 3 lines

Then, there is a look up to find the object that was missing. If module was successfully load, the object will be in place, otherwise -ENOENT is reported to userspace.

Good, that will prevent infite retries in case userspace requests non-existent module.

Quoted 1 line

I can include this logic in the patch description in a v3.

That would be good, thanks!

Quoted 2 lines

I run the syzbot reproducer for 1 hour and no problems, not sure how much I have to run it more. I guess the more time the better.

It triggers instantly for me provided:
1. CONFIG_MODULES=y (with MODULES=n the faulty code part isn't built...)
2. set "sysctl kernel.modprobe=/root/sleep1.sh"
I found that with normal modprobe the race window is rather small and the thread doing the request_module has a decent chance of re-locking the mutex before another syzkaller thread has a chance to alter the current generation.