Skip to contentSkip to conversation

netfilter: x_tables: avoid holding mutex over faultable user copies

PATCH New Zihan Xi 1 participant 2 messages Oct 8

Subsystems (2)
lore
Share & tools
lore

Zihan Xi

author
#0 lore raw

v5 cover netfilter: x_tables: avoid holding mutex over faultable user copies

Hi Linux kernel maintainers,

This resend keeps faultable user-page accesses outside the per-family xtables mutexes in the legacy GET_INFO/GET_ENTRIES paths implemented by net/ipv4/netfilter/arp_tables.c, net/ipv4/netfilter/ip_tables.c, and net/ipv6/netfilter/ip6_tables.c. The ebtables ebt_mutex path remains outside this series.

The source change is unchanged from v4 and is rebased onto the current nf.git main at ab699d296e06 ("Merge branch 'net-always-dissect-gso-packets-in-__virtio_net_hdr_to_skb'").

v4 Link: https://lore.kernel.org/all/[email protected]/

Zihan Xi (1):
netfilter: x_tables: avoid holding mutex over faultable user copies

 net/ipv4/netfilter/arp_tables.c | 49 ++++++++++++++++---
 net/ipv4/netfilter/ip_tables.c  | 87 +++++++++++++++++++++++++++++----
 net/ipv6/netfilter/ip6_tables.c | 87 +++++++++++++++++++++++++++++----
 3 files changed, 197 insertions(+), 26 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v5 1/1 netfilter: x_tables: avoid holding mutex over faultable user copies

The legacy IPv4, IPv6 and ARP table GET_INFO and GET_ENTRIES paths hold the per-family xtables mutexes while copying table data to userspace. A faultable destination may sleep indefinitely while the mutex is held, blocking unrelated table and registry operations.

Disable page faults while each locked user copy runs. For GET_ENTRIES, validate the table and requested size while holding the table mutex, then release it before faulting in the output range. Reacquire the mutex and revalidate the table and size before the nofault copy. If a nofault copy fails, fault the range outside the lock and retry the operation up to three times after the initial attempt. A failed fault-in returns -EFAULT; repeated nofault failures are limited to four attempts in total and then return -EFAULT. This avoids discarding a locked counter snapshot on the common first fault. Move GET_INFO's fixed-size copy outside the table locks and apply the same fault-safe handling to the IPv4/IPv6 compat GET_ENTRIES paths.

The ebtables GET paths use a separate ebt_mutex and are outside this IPv4/IPv6/ARP series.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Assisted-by LLM

---
changes in v5:
- Rebase the unchanged three-file fix onto nf.git main at

    ab699d296e06 ("Merge branch
    'net-always-dissect-gso-packets-in-__virtio_net_hdr_to_skb'").
  - Resend as a new thread because the v4 series has aged; retain the
    v4 lore link in the cover and below.
  - v4 Link:
    https://lore.kernel.org/all/[email protected]/

changes in v4:
- Keep the source diff unchanged from v3 while rebasing it onto refreshed nf.git main at 9c572a83037a ("net/sched: fix potential stack infoleak in em_text_dump()").
- Replace the detailed v3 cover text with a concise summary and add the v3

    link:
    https://lore.kernel.org/all/[email protected]/

changes in v3:
- Validate the table and requested size before fault-in, then revalidate both after lock reacquisition in native and IPv4/IPv6 compat GET_ENTRIES.
- Make retry semantics explicit: one initial nofault attempt followed by at most three retries. Keep fault-in and retry decisions outside family mutexes; failed fault-in and exhausted retries return -EFAULT.
- Keep the fixed-size GET_INFO copy outside table locks and brace compat lookup error arms.
- Clarify ARP-only runtime coverage, privilege scope, helper build, holder/waiter evidence, QEMU configuration, and the NOT RUN crash-log status.
- Regenerate the numbered patch and cover with LF line endings; use the

    standard 0001-*.patch filename.
  - v2 Link:
    https://lore.kernel.org/all/[email protected]/

changes in v2:
- Rebase onto current nf.git after 0bd7ed1a3263c ("netfilter: arp_tables: remove the 32bit compat interface"). ARP GET_INFO and GET_ENTRIES use native paths only; IPv4 and IPv6 retain compat handling.
- Drop hung_task_panic and the 10-second hung_task timeout from the reproducer, as pointed out by Pablo Neira Ayuso; observe holder/waiter wchan instead.
- v1 Link:

    https://lore.kernel.org/all/[email protected]/
 net/ipv4/netfilter/arp_tables.c | 49 ++++++++++++++++---
 net/ipv4/netfilter/ip_tables.c  | 87 +++++++++++++++++++++++++++++----
 net/ipv6/netfilter/ip6_tables.c | 87 +++++++++++++++++++++++++++++----
 3 files changed, 197 insertions(+), 26 deletions(-)
net/ipv4/netfilter/arp_tables.c +41 -8
index db307fa..a857e67 100644
@@ -23,6 +23,7 @@
 #include <linux/init.h>
 #include <linux/mutex.h>
 #include <linux/err.h>
+#include <linux/pagemap.h>
 #include <net/sock.h>
 #include <linux/uaccess.h>
 
@@ -695,6 +696,7 @@ static int copy_entries_to_user(unsigned int total_size,
 
 	loc_cpu_entry = private->entries;
 
+	pagefault_disable();
 	/* FIXME: use iterator macros --RR */
 	/* ... then go back and fix counters and names */
 	for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
@@ -719,12 +721,14 @@ static int copy_entries_to_user(unsigned int total_size,
 	}
 
  free_counters:
+	pagefault_enable();
 	vfree(counters);
 	return ret;
 }
 
 static int get_info(struct net *net, void __user *user, const int *len)
 {
+	struct arpt_getinfo info;
 	char name[XT_TABLE_MAXNAMELEN];
 	struct xt_table *t;
 	int ret;
@@ -738,7 +742,6 @@ static int get_info(struct net *net, void __user *user, const int *len)
 	name[XT_TABLE_MAXNAMELEN-1] = '\0';
 	t = xt_request_find_table_lock(net, NFPROTO_ARP, name);
 	if (!IS_ERR(t)) {
-		struct arpt_getinfo info;
 		const struct xt_table_info *private = t->private;
 
 		memset(&info, 0, sizeof(info));
@@ -751,15 +754,14 @@ static int get_info(struct net *net, void __user *user, const int *len)
 		info.size = private->size;
 		strscpy(info.name, name);
 
-		if (copy_to_user(user, &info, *len) != 0)
-			ret = -EFAULT;
-		else
-			ret = 0;
+		ret = 0;
 		xt_table_unlock(t);
 		module_put(t->me);
 	} else
 		ret = PTR_ERR(t);
 
+	if (!ret && copy_to_user(user, &info, *len) != 0)
+		ret = -EFAULT;
 	return ret;
 }
 
@@ -769,6 +771,7 @@ static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
 	int ret;
 	struct arpt_get_entries get;
 	struct xt_table *t;
+	unsigned int retries = 0;
 
 	if (*len < sizeof(get))
 		return -EINVAL;
@@ -779,21 +782,51 @@ static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
 
 	get.name[sizeof(get.name) - 1] = '\0';
 
+retry:
+	t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
+	if (IS_ERR(t)) {
+		ret = PTR_ERR(t);
+		goto out;
+	}
+
+	if (get.size != t->private->size) {
+		ret = -EAGAIN;
+		module_put(t->me);
+		xt_table_unlock(t);
+		goto out;
+	}
+
+	module_put(t->me);
+	xt_table_unlock(t);
+
+	/* Fault in only after validating the table and requested size. */
+	if (fault_in_safe_writeable((char __user *)uptr->entrytable,
+				    get.size))
+		return -EFAULT;
+
 	t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
 	if (!IS_ERR(t)) {
 		const struct xt_table_info *private = t->private;
 
-		if (get.size == private->size)
+		if (get.size != private->size) {
+			ret = -EAGAIN;
+		} else {
 			ret = copy_entries_to_user(private->size,
 						   t, uptr->entrytable);
-		else
-			ret = -EAGAIN;
+		}
 
 		module_put(t->me);
 		xt_table_unlock(t);
 	} else
 		ret = PTR_ERR(t);
 
+out:
+	/* Allow three retries after the initial nofault copy. */
+	if (ret == -EFAULT && retries < 3) {
+		retries++;
+		goto retry;
+	}
+
 	return ret;
 }
 
net/ipv4/netfilter/ip_tables.c +78 -9

185 lines not on this page · raw message

net/ipv6/netfilter/ip6_tables.c +78 -9

185 lines not on this page · raw message

Signature · 1 line
2.25.1