Skip to contentSkip to conversation

netns: add and use net_ns_barrier

PATCH Applied Florian Westphal 7 participants 18 messages May 30, 2017 – Jun 19, 2017

Subsystems (3)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 netns: add and use net_ns_barrier

Quoting Joe Stringer:
If a user loads nf_conntrack_ftp, sends FTP traffic through a network namespace, destroys that namespace then unloads the FTP helper module, then the kernel will crash.

Events that lead to the crash:
1. conntrack is created with ftp helper in netns x
2. This netns is destroyed
3. netns destruction is scheduled
4. netns destruction wq starts, removes netns from global list
5. ftp helper is unloaded, which resets all helpers of the conntracks via for_each_net()

but because netns is already gone from list the for_each_net() loop doesn't include it, therefore all of these conntracks are unaffected.

6. helper module unload finishes
7. netns wq invokes destructor for rmmod'ed helper

---
Eric, I'd like an explicit (n)ack from you for this one.

 include/net/net_namespace.h       |  3 +++
 net/core/net_namespace.c          | 17 +++++++++++++++++
 net/netfilter/nf_conntrack_core.c |  9 +++++++++
 3 files changed, 29 insertions(+)
include/net/net_namespace.h +3 -0
index fe80bb48ab1f..a24a57593202 100644
@@ -158,6 +158,7 @@ extern struct net init_net;
 struct net *copy_net_ns(unsigned long flags, struct user_namespace *user_ns,
 			struct net *old_net);
 
+void net_ns_barrier(void);
 #else /* CONFIG_NET_NS */
 #include <linux/sched.h>
 #include <linux/nsproxy.h>
@@ -168,6 +169,8 @@ static inline struct net *copy_net_ns(unsigned long flags,
 		return ERR_PTR(-EINVAL);
 	return old_net;
 }
+
+static inline void net_ns_barrier(void) {}
 #endif /* CONFIG_NET_NS */
 
 
net/core/net_namespace.c +17 -0
index 1934efd4a9d4..1f15abb1d733 100644
@@ -482,6 +482,23 @@ static void cleanup_net(struct work_struct *work)
 		net_drop_ns(net);
 	}
 }
+
+/**
+ * net_ns_barrier - wait until concurrent net_cleanup_work is done
+ *
+ * cleanup_net runs from work queue and will first remove namespaces
+ * from the global list, then run net exit functions.
+ *
+ * Call this in module exit path to make sure that all netns
+ * ->exit ops have been invoked before the function is removed.
+ */
+void net_ns_barrier(void)
+{
+	mutex_lock(&net_mutex);
+	mutex_unlock(&net_mutex);
+}
+EXPORT_SYMBOL(net_ns_barrier);
+
 static DECLARE_WORK(net_cleanup_work, cleanup_net);
 
 void __put_net(struct net *net)
net/netfilter/nf_conntrack_core.c +9 -0
index c3bd9b086dcc..ee972ee7bf81 100644
@@ -1720,6 +1720,8 @@ EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup_net);
  * Like nf_ct_iterate_cleanup, but first marks conntracks on the
  * unconfirmed list as dying (so they will not be inserted into
  * main table).
+ *
+ * Can only be called in module exit path.
  */
 void
 nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data), void *data)
@@ -1734,6 +1736,13 @@ nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data), void *data)
 	}
 	rtnl_unlock();
 
+	/* Need to wait for netns cleanup worker to finish, if its
+	 * running -- it might have deleted a net namespace from
+	 * the global list, so our __nf_ct_unconfirmed_destroy() might
+	 * not have affected all namespaces.
+	 */
+	net_ns_barrier();
+
 	/* a conntrack could have been unlinked from unconfirmed list
 	 * before we grabbed pcpu lock in __nf_ct_unconfirmed_destroy().
 	 * This makes sure its inserted into conntrack table.
Signature · 1 line
2.13.0
maintainer Acked-by on v1 1/1
#1 lore raw

From: Florian Westphal <[email protected]>
Date: Tue, 30 May 2017 11:38:12 +0200

Quoted 16 lines

Quoting Joe Stringer:
If a user loads nf_conntrack_ftp, sends FTP traffic through a network namespace, destroys that namespace then unloads the FTP helper module, then the kernel will crash.

Events that lead to the crash:
1. conntrack is created with ftp helper in netns x
2. This netns is destroyed
3. netns destruction is scheduled
4. netns destruction wq starts, removes netns from global list
5. ftp helper is unloaded, which resets all helpers of the conntracks via for_each_net()

but because netns is already gone from list the for_each_net() loop doesn't include it, therefore all of these conntracks are unaffected.

Quoted 8 lines

6. helper module unload finishes
7. netns wq invokes destructor for rmmod'ed helper

---
Eric, I'd like an explicit (n)ack from you for this one.

Indeed, Eric, please do.

Otherwise I'm fine with the generic parts:

#2 lore raw
Quoted 22 lines · David Miller

From: Florian Westphal <[email protected]>
Date: Tue, 30 May 2017 11:38:12 +0200

Quoted 19 lines

Quoting Joe Stringer:
If a user loads nf_conntrack_ftp, sends FTP traffic through a network namespace, destroys that namespace then unloads the FTP helper module, then the kernel will crash.

Events that lead to the crash:
1. conntrack is created with ftp helper in netns x
2. This netns is destroyed
3. netns destruction is scheduled
4. netns destruction wq starts, removes netns from global list
5. ftp helper is unloaded, which resets all helpers of the conntracks via for_each_net()

but because netns is already gone from list the for_each_net() loop doesn't include it, therefore all of these conntracks are unaffected.

6. helper module unload finishes
7. netns wq invokes destructor for rmmod'ed helper

Quoted 7 lines
Quoted 5 lines

---
Eric, I'd like an explicit (n)ack from you for this one.

Indeed, Eric, please do.

Taking a look now. The original didn't make it's way into my inbox. I just have a copy from netdev. Florian there may be a bit of an email black hole between us.

Quoted 3 lines

Otherwise I'm fine with the generic parts:

Eric

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

Quoting Joe Stringer:
If a user loads nf_conntrack_ftp, sends FTP traffic through a network namespace, destroys that namespace then unloads the FTP helper module, then the kernel will crash.

Events that lead to the crash:
1. conntrack is created with ftp helper in netns x
2. This netns is destroyed
3. netns destruction is scheduled
4. netns destruction wq starts, removes netns from global list
5. ftp helper is unloaded, which resets all helpers of the conntracks via for_each_net()

but because netns is already gone from list the for_each_net() loop doesn't include it, therefore all of these conntracks are unaffected.

Quoted 8 lines

6. helper module unload finishes
7. netns wq invokes destructor for rmmod'ed helper

---
Eric, I'd like an explicit (n)ack from you for this one.

This doesn't look too scary but I have the impression we have addressed this elsewhere with a different solution.

Looking...

Ok. unregister_pernet_operations takes the net_mutex and thus gives you this barrier automatically.

Hmm. Why isn't this working for conntrack, looking...

nf_conntrack_ftp doesn't use unregister_pernet_operations... nf_conntract_ftp does use nf_conntrack_helpers_unregister

I think I almost see the problem.

What is the per net code that stops dealing with the nf_conntract_ftp?

I am trying to figure out if your netns_barrier is reasonable or if it treating the symptom. I am having trouble seeing enough of what conntrack is doing to judge.

Am I correct in understanding that the root problem is there is something pointing to ftp_exp_policy at the time of module unload?

Eric

Quoted 81 lines
 include/net/net_namespace.h       |  3 +++
 net/core/net_namespace.c          | 17 +++++++++++++++++
 net/netfilter/nf_conntrack_core.c |  9 +++++++++
 3 files changed, 29 insertions(+)
include/net/net_namespace.h +3 -0
index fe80bb48ab1f..a24a57593202 100644
@@ -158,6 +158,7 @@ extern struct net init_net;
 struct net *copy_net_ns(unsigned long flags, struct user_namespace *user_ns,
 			struct net *old_net);
 
+void net_ns_barrier(void);
 #else /* CONFIG_NET_NS */
 #include <linux/sched.h>
 #include <linux/nsproxy.h>
@@ -168,6 +169,8 @@ static inline struct net *copy_net_ns(unsigned long flags,
 		return ERR_PTR(-EINVAL);
 	return old_net;
 }
+
+static inline void net_ns_barrier(void) {}
 #endif /* CONFIG_NET_NS */
 
 
net/core/net_namespace.c +17 -0
index 1934efd4a9d4..1f15abb1d733 100644
@@ -482,6 +482,23 @@ static void cleanup_net(struct work_struct *work)
 		net_drop_ns(net);
 	}
 }
+
+/**
+ * net_ns_barrier - wait until concurrent net_cleanup_work is done
+ *
+ * cleanup_net runs from work queue and will first remove namespaces
+ * from the global list, then run net exit functions.
+ *
+ * Call this in module exit path to make sure that all netns
+ * ->exit ops have been invoked before the function is removed.
+ */
+void net_ns_barrier(void)
+{
+	mutex_lock(&net_mutex);
+	mutex_unlock(&net_mutex);
+}
+EXPORT_SYMBOL(net_ns_barrier);
+
 static DECLARE_WORK(net_cleanup_work, cleanup_net);
 
 void __put_net(struct net *net)
net/netfilter/nf_conntrack_core.c +9 -0
index c3bd9b086dcc..ee972ee7bf81 100644
@@ -1720,6 +1720,8 @@ EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup_net);
  * Like nf_ct_iterate_cleanup, but first marks conntracks on the
  * unconfirmed list as dying (so they will not be inserted into
  * main table).
+ *
+ * Can only be called in module exit path.
  */
 void
 nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data), void *data)
@@ -1734,6 +1736,13 @@ nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data), void *data)
 	}
 	rtnl_unlock();
 
+	/* Need to wait for netns cleanup worker to finish, if its
+	 * running -- it might have deleted a net namespace from
+	 * the global list, so our __nf_ct_unconfirmed_destroy() might
+	 * not have affected all namespaces.
+	 */
+	net_ns_barrier();
+
 	/* a conntrack could have been unlinked from unconfirmed list
 	 * before we grabbed pcpu lock in __nf_ct_unconfirmed_destroy().
 	 * This makes sure its inserted into conntrack table.
#4 lore raw
Quoted 42 lines · Eric W. Biederman
Quoted 16 lines · Florian Westphal

Quoting Joe Stringer:
If a user loads nf_conntrack_ftp, sends FTP traffic through a network namespace, destroys that namespace then unloads the FTP helper module, then the kernel will crash.

Events that lead to the crash:
1. conntrack is created with ftp helper in netns x
2. This netns is destroyed
3. netns destruction is scheduled
4. netns destruction wq starts, removes netns from global list
5. ftp helper is unloaded, which resets all helpers of the conntracks via for_each_net()

but because netns is already gone from list the for_each_net() loop doesn't include it, therefore all of these conntracks are unaffected.

Quoted 8 lines

6. helper module unload finishes
7. netns wq invokes destructor for rmmod'ed helper

---
Eric, I'd like an explicit (n)ack from you for this one.

This doesn't look too scary but I have the impression we have addressed this elsewhere with a different solution.

Looking...

Ok. unregister_pernet_operations takes the net_mutex and thus gives you this barrier automatically.

Hmm. Why isn't this working for conntrack, looking...

nf_conntrack_ftp doesn't use unregister_pernet_operations... nf_conntract_ftp does use nf_conntrack_helpers_unregister

I think I almost see the problem.

Quoted 8 lines

What is the per net code that stops dealing with the nf_conntract_ftp?

I am trying to figure out if your netns_barrier is reasonable or if it treating the symptom. I am having trouble seeing enough of what conntrack is doing to judge.

Am I correct in understanding that the root problem is there is something pointing to ftp_exp_policy at the time of module unload?

I believe it's just that there is a conntrack entry with a 'struct nf_conn_help' whose 'helper' parameter is pointing to the 'struct nf_conntrack_helper ftp[...]' that is declared immediately above the ftp_exp_policy. nf_ct_helper_destroy() would expect that nfct_help(ct)->helper is NULL if the module was unloaded, but because the netns was removed from the global list prior to unloading the FTP module, there was no way to clear it. So, when the netns workqueue invokes the destruction of all conntrack entries in the namespace, it calls down to delete each conntrack entry and the ones with helpers attached point to the now-unloaded helper module.

If I follow correctly, the approach proposed in the patch here ensures that:

1) nf_conntrack_helper_unregister() disengages the helper so it won't be attached to new connections.
2) nf_conntrack_helper_unregister() clears the expectations for this helper.
3) nf_ct_iterate_destroy() iterates through all namespaces to clear per-netns unconfirmed lists so they don't have any references to the helper.
4) (NEW) we barrier on net_mutex to ensure that any concurrent netns workqueue deletion which may have removed a netns prior to (3) will finish. Now we know that we have iterated all net namespaces.
5) Iterate through the conntrack table, applying the 'unhelp' operation to all conntrack entries. This removes the helper from all entries while leaving the entries in the table.

Once this is all done, the helper module can finally unload.

FWIW, the previous iteration is here:
https://www.mail-archive.com/[email protected]/msg109343.html

Joe

author maintainer
#5 lore raw
In reply to Eric W. Biederman
Quoted 42 lines · Eric W. Biederman
Quoted 16 lines · Florian Westphal

Quoting Joe Stringer:
If a user loads nf_conntrack_ftp, sends FTP traffic through a network namespace, destroys that namespace then unloads the FTP helper module, then the kernel will crash.

Events that lead to the crash:
1. conntrack is created with ftp helper in netns x
2. This netns is destroyed
3. netns destruction is scheduled
4. netns destruction wq starts, removes netns from global list
5. ftp helper is unloaded, which resets all helpers of the conntracks via for_each_net()

but because netns is already gone from list the for_each_net() loop doesn't include it, therefore all of these conntracks are unaffected.

Quoted 8 lines

6. helper module unload finishes
7. netns wq invokes destructor for rmmod'ed helper

---
Eric, I'd like an explicit (n)ack from you for this one.

This doesn't look too scary but I have the impression we have addressed this elsewhere with a different solution.

Looking...

Ok. unregister_pernet_operations takes the net_mutex and thus gives you this barrier automatically.

Hmm. Why isn't this working for conntrack, looking...

nf_conntrack_ftp doesn't use unregister_pernet_operations... nf_conntract_ftp does use nf_conntrack_helpers_unregister

I think I almost see the problem.

Quoted 8 lines

What is the per net code that stops dealing with the nf_conntract_ftp?

I am trying to figure out if your netns_barrier is reasonable or if it treating the symptom. I am having trouble seeing enough of what conntrack is doing to judge.

Am I correct in understanding that the root problem is there is something pointing to ftp_exp_policy at the time of module unload?

Joe described it nicely, problem is that after unload we may have conntracks that still have a nf_conn_help extension attached that has a pointer to a structure that resided in the (unloaded) module.

Normally these references should have been NULL'd out by nf_ct_iterate_destroy(), however, there is a small chance that its for_each_net() misses namespaces already removed-from-list by concurrent netns workqueue cleanup.

I guess another solution to fix this would be to add dummy pernet ops to all conntrack helpers so they block on unregister_pernet_subsys().

But thats rather ugly IMO since they don't have any notion of a net namespace in first place.

#15 lore raw
In reply to Florian Westphal

From: Florian Westphal

Quoted 21 lines

Sent: 30 May 2017 10:38

Quoting Joe Stringer:
If a user loads nf_conntrack_ftp, sends FTP traffic through a network namespace, destroys that namespace then unloads the FTP helper module, then the kernel will crash.

Events that lead to the crash:
1. conntrack is created with ftp helper in netns x
2. This netns is destroyed
3. netns destruction is scheduled
4. netns destruction wq starts, removes netns from global list
5. ftp helper is unloaded, which resets all helpers of the conntracks via for_each_net()

but because netns is already gone from list the for_each_net() loop doesn't include it, therefore all of these conntracks are unaffected.

6. helper module unload finishes
7. netns wq invokes destructor for rmmod'ed helper

...

net/netfilter/nf_conntrack_core.c nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data), void *data) · line 1745

Quoted 13 lines

void
nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data), void *data)

net/netfilter/nf_conntrack_core.c +7 -0
@@ -1734,6 +1736,13 @@ nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data), void *data)
 	}
 	rtnl_unlock();
 
+	/* Need to wait for netns cleanup worker to finish, if its
+	 * running -- it might have deleted a net namespace from
+	 * the global list, so our __nf_ct_unconfirmed_destroy() might
+	 * not have affected all namespaces.
+	 */
+	net_ns_barrier();
+

A problem I see is that nothing obvious guarantees that the cleanup worker has actually started.

David

author maintainer
#16 lore raw
Quoted 29 lines · David Laight

From: Florian Westphal

Quoted 21 lines

Sent: 30 May 2017 10:38

Quoting Joe Stringer:
If a user loads nf_conntrack_ftp, sends FTP traffic through a network namespace, destroys that namespace then unloads the FTP helper module, then the kernel will crash.

Events that lead to the crash:
1. conntrack is created with ftp helper in netns x
2. This netns is destroyed
3. netns destruction is scheduled
4. netns destruction wq starts, removes netns from global list
5. ftp helper is unloaded, which resets all helpers of the conntracks via for_each_net()

but because netns is already gone from list the for_each_net() loop doesn't include it, therefore all of these conntracks are unaffected.

6. helper module unload finishes
7. netns wq invokes destructor for rmmod'ed helper

...

Quoted 6 lines

void
nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data), void *data)

net/netfilter/nf_conntrack_core.c +0 -0
@@ -1734,6 +1736,13 @@ nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data), void *data)
 	}
 	rtnl_unlock();
 
Quoted 10 lines

net/netfilter/nf_conntrack_core.c nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data), void *data) · line 1745

Quoted 7 lines
net/netfilter/nf_conntrack_core.c +7 -0
+	/* Need to wait for netns cleanup worker to finish, if its
+	 * running -- it might have deleted a net namespace from
+	 * the global list, so our __nf_ct_unconfirmed_destroy() might
+	 * not have affected all namespaces.
+	 */
+	net_ns_barrier();
+

A problem I see is that nothing obvious guarantees that the cleanup worker has actually started.

If it hasn't even started, the earlier for_each_net() has seen all net namespaces and we managed to clear helper extensions of all conntracks.

Same in case it has finished already: netns cleanup work queue has free'd all the affected conntracks we might have missed.

We only are in trouble if netns work queue is running concurrently:
netns cleanup first removes net namespaces from the global list, so nf_ct_iterate_destroy might have missed these.

#14 lore raw
In reply to Eric W. Biederman
Quoted 21 lines · Eric W. Biederman
Quoted 19 lines · Florian Westphal

Quoting Joe Stringer:
If a user loads nf_conntrack_ftp, sends FTP traffic through a network namespace, destroys that namespace then unloads the FTP helper module, then the kernel will crash.

Events that lead to the crash:
1. conntrack is created with ftp helper in netns x
2. This netns is destroyed
3. netns destruction is scheduled
4. netns destruction wq starts, removes netns from global list
5. ftp helper is unloaded, which resets all helpers of the conntracks via for_each_net()

but because netns is already gone from list the for_each_net() loop doesn't include it, therefore all of these conntracks are unaffected.

6. helper module unload finishes
7. netns wq invokes destructor for rmmod'ed helper

Quoted 8 lines
Quoted 5 lines

---
Eric, I'd like an explicit (n)ack from you for this one.

This doesn't look too scary but I have the impression we have addressed this elsewhere with a different solution.

Eric, so you hold your nose there and I take this ;-)

Or let me know if you want a different path.

Thanks !

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

Joe described it nicely, problem is that after unload we may have conntracks that still have a nf_conn_help extension attached that has a pointer to a structure that resided in the (unloaded) module.

Why not hold a refcnt for its module?

author maintainer
#7 lore raw
Quoted 6 lines · Cong Wang
Quoted 3 lines · Florian Westphal

Joe described it nicely, problem is that after unload we may have conntracks that still have a nf_conn_help extension attached that has a pointer to a structure that resided in the (unloaded) module.

Why not hold a refcnt for its module?

That would work as well.

I'm not sure its nice to disallow rmmod of helper modules if they are used by a connection however.

Right now you can "rmmod nf_conntrack_foo" at any time and this should work just fine without first having to flush affected conntracks manually.

#8 lore raw
Quoted 6 lines · Florian Westphal
Quoted 5 lines · Cong Wang
Quoted 3 lines · Florian Westphal

Joe described it nicely, problem is that after unload we may have conntracks that still have a nf_conn_help extension attached that has a pointer to a structure that resided in the (unloaded) module.

Quoted 6 lines
Quoted 1 line

Why not hold a refcnt for its module?

That would work as well.

I'm not sure its nice to disallow rmmod of helper modules if they are used by a connection however.

I am _not_ suggesting to disallow rmmod.

Quoted 4 lines

Right now you can "rmmod nf_conntrack_foo" at any time and this should work just fine without first having to flush affected conntracks manually.

My point is that since netns wq could invoke code of that module, why it doesn't hold a refcnt of that module?

I am not familiar with netfilter code base so not sure if that is hard to do or not, but it looks more elegant than this barrier.

author maintainer
#9 lore raw
Quoted 7 lines · Cong Wang
Quoted 6 lines · Florian Westphal
Quoted 5 lines · Cong Wang
Quoted 3 lines · Florian Westphal

Joe described it nicely, problem is that after unload we may have conntracks that still have a nf_conn_help extension attached that has a pointer to a structure that resided in the (unloaded) module.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Why not hold a refcnt for its module?

That would work as well.

I'm not sure its nice to disallow rmmod of helper modules if they are used by a connection however.

I am _not_ suggesting to disallow rmmod.

My point was that if you hold reference counts to the module users will need to manually flush conntrack table (or at least manually remove affected connections), else rmmod won't work as refcount might be gt 0.

Quoted 6 lines
Quoted 3 lines

Right now you can "rmmod nf_conntrack_foo" at any time and this should work just fine without first having to flush affected conntracks manually.

My point is that since netns wq could invoke code of that module, why it doesn't hold a refcnt of that module?

*shrug*, I did not write this stuff.

Historically it wasn't needed because we just clear out the helper area in the affected conntracks (i.e, future packets are not inspected by the helper anymore).

When conntracks were made per-netns this problem was added as we're not guaranteed to see all net namespace because module_exit and netns cleanup can run concurrently.

We can still use the "old" model if we guarantee that we wait for netns cleanup to finish (which is what this patch does).

The alternative, as you pointed out, is to take a module reference for each conntrack that uses the helper (and put again when connection is destroyed).

I don't really care that much except that if we go for the latter solution users cannot "just rmmod" the module anymore but might have to manually remove the affected connections first.

Pablo, I can rework things to do module get/put but I ask for explicit instructions to do so (I prefer the "barrier" approach).

#10 lore raw
Quoted 42 lines · Florian Westphal
Quoted 7 lines · Cong Wang
Quoted 6 lines · Florian Westphal
Quoted 5 lines · Cong Wang
Quoted 3 lines · Florian Westphal

Joe described it nicely, problem is that after unload we may have conntracks that still have a nf_conn_help extension attached that has a pointer to a structure that resided in the (unloaded) module.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Why not hold a refcnt for its module?

That would work as well.

I'm not sure its nice to disallow rmmod of helper modules if they are used by a connection however.

I am _not_ suggesting to disallow rmmod.

My point was that if you hold reference counts to the module users will need to manually flush conntrack table (or at least manually remove affected connections), else rmmod won't work as refcount might be gt 0.

Quoted 6 lines
Quoted 3 lines

Right now you can "rmmod nf_conntrack_foo" at any time and this should work just fine without first having to flush affected conntracks manually.

My point is that since netns wq could invoke code of that module, why it doesn't hold a refcnt of that module?

*shrug*, I did not write this stuff.

Historically it wasn't needed because we just clear out the helper area in the affected conntracks (i.e, future packets are not inspected by the helper anymore).

When conntracks were made per-netns this problem was added as we're not guaranteed to see all net namespace because module_exit and netns cleanup can run concurrently.

We can still use the "old" model if we guarantee that we wait for netns cleanup to finish (which is what this patch does).

Quoted 7 lines

The alternative, as you pointed out, is to take a module reference for each conntrack that uses the helper (and put again when connection is destroyed).

I don't really care that much except that if we go for the latter solution users cannot "just rmmod" the module anymore but might have to manually remove the affected connections first.

The barrier approach sounds less surprising from user perspective for this very reason.

#11 lore raw
In reply to Florian Westphal
Quoted 8 lines · Florian Westphal

Historically it wasn't needed because we just clear out the helper area in the affected conntracks (i.e, future packets are not inspected by the helper anymore).

When conntracks were made per-netns this problem was added as we're not guaranteed to see all net namespace because module_exit and netns cleanup can run concurrently.

Quoted 6 lines

We can still use the "old" model if we guarantee that we wait for netns cleanup to finish (which is what this patch does).

The alternative, as you pointed out, is to take a module reference for each conntrack that uses the helper (and put again when connection is destroyed).

Yeah, this is exactly what I am suggesting and I fully expect this could need more work than this barrier.

Quoted 4 lines

I don't really care that much except that if we go for the latter solution users cannot "just rmmod" the module anymore but might have to manually remove the affected connections first.

This is not bad because the module is indeed being used in this scenario so EBUSY is expected, or do we need to guarantee conntrack modules are not held by existing connections?

#12 lore raw
In reply to Cong Wang

Hi!

Quoted 21 lines · Cong Wang
Quoted 6 lines · Florian Westphal
Quoted 5 lines · Cong Wang
Quoted 3 lines · Florian Westphal

Joe described it nicely, problem is that after unload we may have conntracks that still have a nf_conn_help extension attached that has a pointer to a structure that resided in the (unloaded) module.

Quoted 6 lines
Quoted 1 line

Why not hold a refcnt for its module?

That would work as well.

I'm not sure its nice to disallow rmmod of helper modules if they are used by a connection however.

I am _not_ suggesting to disallow rmmod.

Quoted 4 lines

Right now you can "rmmod nf_conntrack_foo" at any time and this should work just fine without first having to flush affected conntracks manually.

Quoted 5 lines

My point is that since netns wq could invoke code of that module, why it doesn't hold a refcnt of that module?

I am not familiar with netfilter code base so not sure if that is hard to do or not, but it looks more elegant than this barrier.

Florian has added a new native interface to integrate helpers into nftables in a much better way than we do now, that allows much more fine grain configuration. This new interface bumps refcounts on helpers as you suggest.

However, we still have to sort of keep the existing behaviour around, people has been relying on this rmmod feature to globally disable helpers. It's very old thing indeed and as you can see, very sparse grain for the netns era... But still I think we need this.

So I'm inclined to take this, and keep an eye to deprecate this behaviour in a several years ahead once. Probably we can get rid of this barrier at some point.

#13 lore raw
Quoted 35 lines · Pablo Neira Ayuso

Hi!

Quoted 21 lines · Cong Wang
Quoted 6 lines · Florian Westphal
Quoted 5 lines · Cong Wang
Quoted 3 lines · Florian Westphal

Joe described it nicely, problem is that after unload we may have conntracks that still have a nf_conn_help extension attached that has a pointer to a structure that resided in the (unloaded) module.

Quoted 6 lines
Quoted 1 line

Why not hold a refcnt for its module?

That would work as well.

I'm not sure its nice to disallow rmmod of helper modules if they are used by a connection however.

I am _not_ suggesting to disallow rmmod.

Quoted 4 lines

Right now you can "rmmod nf_conntrack_foo" at any time and this should work just fine without first having to flush affected conntracks manually.

Quoted 5 lines

My point is that since netns wq could invoke code of that module, why it doesn't hold a refcnt of that module?

I am not familiar with netfilter code base so not sure if that is hard to do or not, but it looks more elegant than this barrier.

Florian has added a new native interface to integrate helpers into nftables in a much better way than we do now, that allows much more fine grain configuration. This new interface bumps refcounts on helpers as you suggest.

Quoted 8 lines

However, we still have to sort of keep the existing behaviour around, people has been relying on this rmmod feature to globally disable helpers. It's very old thing indeed and as you can see, very sparse grain for the netns era... But still I think we need this.

So I'm inclined to take this, and keep an eye to deprecate this behaviour in a several years ahead once. Probably we can get rid of this barrier at some point.

If it works I don't have any problems with the code and it sounds like it works.

My apologies for the delay. There is an email black hole between Forian and myself and I missed his replies. Which gave me a very distored picture of the conversation.

Eric

maintainer Applied
#17 lore raw
In reply to Florian Westphal
Quoted 13 lines · Florian Westphal

Quoting Joe Stringer:
If a user loads nf_conntrack_ftp, sends FTP traffic through a network namespace, destroys that namespace then unloads the FTP helper module, then the kernel will crash.

Events that lead to the crash:
1. conntrack is created with ftp helper in netns x
2. This netns is destroyed
3. netns destruction is scheduled
4. netns destruction wq starts, removes netns from global list
5. ftp helper is unloaded, which resets all helpers of the conntracks via for_each_net()

Quoted 5 lines

but because netns is already gone from list the for_each_net() loop doesn't include it, therefore all of these conntracks are unaffected.

6. helper module unload finishes
7. netns wq invokes destructor for rmmod'ed helper

Applied, thanks everyone.