Skip to contentSkip to conversation

net/tap: fix FD leak on netns change

PATCH Superseded David Marchand 3 participants 14 messages Nov 3 – Nov 10 Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 net/tap: fix FD leak on netns change

When a tap interface moves to another namespace, tap_netns_change() calls tap_lsc_intr_handle_set(dev, 1) to recreate the LSC interrupt without first tearing down the old one. The previous LSC netlink socket fd is never closed.

Defer the full LSC teardown and re-registration to an alarm callback which runs in the interrupt thread but outside any active source context. The alarm switches to the device's network namespace, tears down the old LSC handle, sets up a new one (opening a fresh netlink socket in the correct namespace), restores the original namespace, and forces carrier state back up.

Extract tap_dev_ns_switch/tap_dev_ns_restore helpers to share the namespace switch logic between tap_netns_change and the alarm.

Fixes aae3a38e9e03 ("net/tap: detect namespace change")
---
 drivers/net/tap/rte_eth_tap.c | 100 ++++++++++++++++++++++------------
 1 file changed, 65 insertions(+), 35 deletions(-)
drivers/net/tap/rte_eth_tap.c +65 -35
index 1d2b71675a..15320c4b7e 100644
@@ -17,6 +17,7 @@
 #include <rte_ip.h>
 #include <rte_ethdev.h>
 #include <rte_errno.h>
+#include <rte_alarm.h>
 #include <rte_cycles.h>
 
 #include <assert.h>
@@ -1796,41 +1797,57 @@ tap_mac_addr_remove(struct rte_eth_dev *dev __rte_unused,
 
 static void tap_dev_intr_handler(void *cb_arg);
 static int tap_lsc_intr_handle_set(struct rte_eth_dev *dev, int set);
+static void tap_lsc_register_alarm(void *arg);
 
+#ifdef TUNGETDEVNETNS
 static int
-tap_netns_change(struct rte_eth_dev *dev)
+tap_dev_ns_switch(struct pmd_internals *pmd, int *orig_netns_fd)
 {
-	struct pmd_internals *pmd = dev->data->dev_private;
-#ifdef TUNGETDEVNETNS
-	int netns_fd, orig_netns_fd, new_nlsk_fd;
+	int netns_fd;
 
 	netns_fd = ioctl(pmd->ka_fd, TUNGETDEVNETNS);
-	if (netns_fd < 0) {
-		TAP_LOG(INFO, "%s: interface deleted", pmd->name);
-		return 0;
-	}
-
-	/* Interface was moved to another namespace */
-	pmd->if_index = 0;
+	if (netns_fd < 0)
+		return -1;
 
-	/* Save current namespace */
-	orig_netns_fd = open("/proc/self/ns/net", O_RDONLY);
-	if (orig_netns_fd < 0) {
-		TAP_LOG(ERR, "%s: failed to open original netns: %s",
-			pmd->name, strerror(errno));
+	*orig_netns_fd = open("/proc/self/ns/net", O_RDONLY);
+	if (*orig_netns_fd < 0) {
 		close(netns_fd);
 		return -1;
 	}
 
-	/* Switch to new namespace */
 	if (setns(netns_fd, CLONE_NEWNET) < 0) {
-		TAP_LOG(ERR, "%s: failed to enter new netns: %s",
-			pmd->name, strerror(errno));
 		close(netns_fd);
-		close(orig_netns_fd);
+		close(*orig_netns_fd);
 		return -1;
 	}
 
+	close(netns_fd);
+	return 0;
+}
+
+static void
+tap_dev_ns_restore(int orig_netns_fd)
+{
+	setns(orig_netns_fd, CLONE_NEWNET);
+	close(orig_netns_fd);
+}
+#endif
+
+static int
+tap_netns_change(struct rte_eth_dev *dev)
+{
+	struct pmd_internals *pmd = dev->data->dev_private;
+#ifdef TUNGETDEVNETNS
+	int orig_netns_fd, new_nlsk_fd;
+
+	if (tap_dev_ns_switch(pmd, &orig_netns_fd) < 0) {
+		TAP_LOG(INFO, "%s: interface deleted", pmd->name);
+		return 0;
+	}
+
+	/* Interface was moved to another namespace */
+	pmd->if_index = 0;
+
 	/*
 	 * Update ifindex by querying interface name.
 	 * The interface now has a new ifindex in the new namespace.
@@ -1840,22 +1857,10 @@ tap_netns_change(struct rte_eth_dev *dev)
 	/* Recreate netlink socket in new namespace */
 	new_nlsk_fd = tap_nl_init(0);
 
-	/* Recreate LSC interrupt netlink socket in new namespace */
-	rte_intr_callback_unregister_pending(pmd->intr_handle, tap_dev_intr_handler, dev, NULL);
-	if (tap_lsc_intr_handle_set(dev, 1) < 0)
-		TAP_LOG(WARNING, "%s: failed to recreate LSC interrupt socket",
-			pmd->name);
+	/* Defer LSC re-registration outside callback context */
+	rte_eal_alarm_set(1, tap_lsc_register_alarm, dev);
 
-	/* Force carrier back after switching netns */
-	tap_carrier_set(pmd, 1);
-
-	/* Switch back to original namespace */
-	if (setns(orig_netns_fd, CLONE_NEWNET) < 0)
-		TAP_LOG(ERR, "%s: failed to return to original netns: %s",
-			pmd->name, strerror(errno));
-
-	close(orig_netns_fd);
-	close(netns_fd);
+	tap_dev_ns_restore(orig_netns_fd);
 
 	if (pmd->if_index == 0) {
 		TAP_LOG(WARNING, "%s: interface moved to another namespace, "
@@ -1952,6 +1957,7 @@ tap_lsc_intr_handle_set(struct rte_eth_dev *dev, int set)
 	}
 
 clean:
+	rte_eal_alarm_cancel(tap_lsc_register_alarm, dev);
 	do {
 		ret = rte_intr_callback_unregister(pmd->intr_handle,
 			tap_dev_intr_handler, dev);
@@ -1975,6 +1981,30 @@ tap_lsc_intr_handle_set(struct rte_eth_dev *dev, int set)
 	return 0;
 }
 
+static void
+tap_lsc_register_alarm(void *arg)
+{
+#ifdef TUNGETDEVNETNS
+	struct rte_eth_dev *dev = arg;
+	struct pmd_internals *pmd = dev->data->dev_private;
+	int orig_netns_fd;
+
+	if (tap_dev_ns_switch(pmd, &orig_netns_fd) < 0)
+		return;
+
+	tap_lsc_intr_handle_set(dev, 0);
+	if (tap_lsc_intr_handle_set(dev, 1) < 0)
+		TAP_LOG(ERR, "%s: failed to register LSC interrupt", pmd->name);
+
+	tap_dev_ns_restore(orig_netns_fd);
+
+	tap_carrier_set(pmd, 1);
+	tap_link_update(dev, 0);
+#else
+	RTE_SET_USED(arg);
+#endif
+}
+
 static int
 tap_intr_handle_set(struct rte_eth_dev *dev, int set)
 {
Signature · 1 line
2.56.0
#1 lore raw

PATCH 2/2 net/tap: fix use after free on interrupt handle

This driver was not unregistering its interrupt callbacks when closing the port, which now means use after free after the interrupt handle is not duplicated anymore by the interrupt thread.

Fixes c663018eb7f3 ("interrupts: do not duplicate interrupt handles")
---
 drivers/net/tap/rte_eth_tap.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/net/tap/rte_eth_tap.c +5 -0
index 15320c4b7e..15dca58c8d 100644
@@ -1106,6 +1106,8 @@ tap_dev_close(struct rte_eth_dev *dev)
 		return 0;
 	}
 
+	tap_intr_handle_set(dev, 0);
+
 	if (!internals->persist)
 		tap_link_set_down(dev);
 
@@ -1958,6 +1960,9 @@ tap_lsc_intr_handle_set(struct rte_eth_dev *dev, int set)
 
 clean:
 	rte_eal_alarm_cancel(tap_lsc_register_alarm, dev);
+	if (rte_intr_fd_get(pmd->intr_handle) < 0)
+		return 0;
+
 	do {
 		ret = rte_intr_callback_unregister(pmd->intr_handle,
 			tap_dev_intr_handler, dev);
Signature · 1 line
2.56.0
Reviewed-by Tested-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] net/tap: fix use after free on interrupt handle

David Marchand, Nov 03, 2026 at 18:17:

Quoted 8 lines

This driver was not unregistering its interrupt callbacks when closing the port, which now means use after free after the interrupt handle is not duplicated anymore by the interrupt thread.

Fixes c663018eb7f3 ("interrupts: do not duplicate interrupt handles")

---

Reviewed-by Tested-by on v1 1/2
#3 lore raw
In reply to David Marchand

David Marchand, Nov 03, 2026 at 18:17:

Quoted 12 lines

When a tap interface moves to another namespace, tap_netns_change() calls tap_lsc_intr_handle_set(dev, 1) to recreate the LSC interrupt without first tearing down the old one. The previous LSC netlink socket fd is never closed.

Defer the full LSC teardown and re-registration to an alarm callback which runs in the interrupt thread but outside any active source context. The alarm switches to the device's network namespace, tears down the old LSC handle, sets up a new one (opening a fresh netlink socket in the correct namespace), restores the original namespace, and forces carrier state back up.

Quoted 8 lines

Extract tap_dev_ns_switch/tap_dev_ns_restore helpers to share the namespace switch logic between tap_netns_change and the alarm.

Fixes aae3a38e9e03 ("net/tap: detect namespace change")

---

#4 lore raw
In reply to David Marchand
Quoted 12 lines · On Tue, 3 Nov 2026 18:17:29 +0100 David Marchand <[email protected]>…

When a tap interface moves to another namespace, tap_netns_change() calls tap_lsc_intr_handle_set(dev, 1) to recreate the LSC interrupt without first tearing down the old one. The previous LSC netlink socket fd is never closed.

Defer the full LSC teardown and re-registration to an alarm callback which runs in the interrupt thread but outside any active source context. The alarm switches to the device's network namespace, tears down the old LSC handle, sets up a new one (opening a fresh netlink socket in the correct namespace), restores the original namespace, and forces carrier state back up.

Quoted 7 lines

Extract tap_dev_ns_switch/tap_dev_ns_restore helpers to share the namespace switch logic between tap_netns_change and the alarm.

Fixes aae3a38e9e03 ("net/tap: detect namespace change")

Saw some AI review issues:

Review: net/tap: fix FD leak on netns change (2 patches)

Series applies to main (26.11-rc2) and builds with -Dwerror=true at each commit. Fixes: tags are correct: aae3a38e9e03 is in v25.11, so
Cc: stable on 1/2 is right; c663018eb7f3 is 26.11 only, so 2/2 needs none.

Deferring the LSC re-registration to an alarm is the right approach. The alarm runs in the interrupt thread with no source active, so rte_intr_callback_unregister() completes, and the self-cancel in
clean: returns EINPROGRESS instead of spinning.

[PATCH 1/2] net/tap: fix FD leak on netns change

Warning: setns()/open() failure now reported as "interface deleted"

tap_dev_ns_switch() returns -1 for three different cases: no netns (interface gone), open("/proc/self/ns/net") failure, and setns() failure (e.g. EPERM without CAP_SYS_ADMIN). tap_netns_change() treats all of them as deletion, logs at INFO and returns 0. Before the patch the last two logged ERR with strerror(errno) and returned -1. Keep the logging in the helper:

	*orig_netns_fd = open("/proc/self/ns/net", O_RDONLY);
	if (*orig_netns_fd < 0) {
		TAP_LOG(ERR, "%s: failed to open original netns: %s",
			pmd->name, strerror(errno));
		close(netns_fd);
		return -1;
	}

	if (setns(netns_fd, CLONE_NEWNET) < 0) {
		TAP_LOG(ERR, "%s: failed to enter new netns: %s",
			pmd->name, strerror(errno));
		...

and return -ENOENT (no log) for the TUNGETDEVNETNS failure so the caller can tell deletion from error.

Warning: tap_dev_ns_restore() ignores setns() failure

The old code logged ERR when switching back failed. Now it is silent, and if it fails the interrupt thread stays in the device's netns. Every later netlink socket opened from that thread (next LSC re-register, alarms) lands in the wrong namespace. Keep the error:

	static void
	tap_dev_ns_restore(struct pmd_internals *pmd, int orig_netns_fd)
	{
		if (setns(orig_netns_fd, CLONE_NEWNET) < 0)
			TAP_LOG(ERR, "%s: failed to return to original netns: %s",
				pmd->name, strerror(errno));
		close(orig_netns_fd);
	}

Info: rte_eal_alarm_set() return value unchecked

If it fails the LSC socket stays in the old namespace and no further link events arrive, with nothing in the log.

	if (rte_eal_alarm_set(1, tap_lsc_register_alarm, dev) < 0)
		TAP_LOG(ERR, "%s: failed to schedule LSC re-registration",
			pmd->name);

Info: concurrent dev_stop window

clean: cancels the alarm before the -EAGAIN retry loop. If dev_stop runs while tap_dev_intr_handler is active, the handler can arm the alarm after the cancel. In practice the alarm fires during the 100 ms rte_delay_ms() and the retry unregisters what it installed, but if the alarm runs as the stop thread wakes, both threads operate on pmd->intr_handle fd at once. Worth a comment, or a second rte_eal_alarm_cancel() before the do/while retry.

[PATCH 2/2] net/tap: fix use after free on interrupt handle

Info: commit message does not mention the fd < 0 early return

The new check in clean: is needed because close after stop now reaches the unregister loop with fd == -1, where rte_intr_callback_unregister() returns -EINVAL and logs "Unregistering with invalid input parameter" plus the driver's "intr callback unregister failed". Add a sentence saying so.

#5 lore raw
Quoted 64 lines · Stephen Hemminger
Quoted 12 lines · On Tue, 3 Nov 2026 18:17:29 +0100 David Marchand <[email protected]>…

When a tap interface moves to another namespace, tap_netns_change() calls tap_lsc_intr_handle_set(dev, 1) to recreate the LSC interrupt without first tearing down the old one. The previous LSC netlink socket fd is never closed.

Defer the full LSC teardown and re-registration to an alarm callback which runs in the interrupt thread but outside any active source context. The alarm switches to the device's network namespace, tears down the old LSC handle, sets up a new one (opening a fresh netlink socket in the correct namespace), restores the original namespace, and forces carrier state back up.

Quoted 7 lines

Extract tap_dev_ns_switch/tap_dev_ns_restore helpers to share the namespace switch logic between tap_netns_change and the alarm.

Fixes aae3a38e9e03 ("net/tap: detect namespace change")

Saw some AI review issues:

Review: net/tap: fix FD leak on netns change (2 patches)

Series applies to main (26.11-rc2) and builds with -Dwerror=true at each commit. Fixes: tags are correct: aae3a38e9e03 is in v25.11, so
Cc: stable on 1/2 is right; c663018eb7f3 is 26.11 only, so 2/2 needs none.

Deferring the LSC re-registration to an alarm is the right approach. The alarm runs in the interrupt thread with no source active, so rte_intr_callback_unregister() completes, and the self-cancel in
clean: returns EINPROGRESS instead of spinning.

[PATCH 1/2] net/tap: fix FD leak on netns change

Warning: setns()/open() failure now reported as "interface deleted"

tap_dev_ns_switch() returns -1 for three different cases: no netns (interface gone), open("/proc/self/ns/net") failure, and setns() failure (e.g. EPERM without CAP_SYS_ADMIN). tap_netns_change() treats all of them as deletion, logs at INFO and returns 0. Before the patch the last two logged ERR with strerror(errno) and returned -1. Keep the logging in the helper:

        *orig_netns_fd = open("/proc/self/ns/net", O_RDONLY);
        if (*orig_netns_fd < 0) {
                TAP_LOG(ERR, "%s: failed to open original netns: %s",
                        pmd->name, strerror(errno));
                close(netns_fd);
                return -1;
        }

        if (setns(netns_fd, CLONE_NEWNET) < 0) {
                TAP_LOG(ERR, "%s: failed to enter new netns: %s",
                        pmd->name, strerror(errno));
                ...
Quoted 2 lines

and return -ENOENT (no log) for the TUNGETDEVNETNS failure so the caller can tell deletion from error.

Regardless of the error, we are in an inconsistent state and the caller can not recover.
I will add back some logs in the helper, but returning a different error code is pointless.

Quoted 15 lines

Warning: tap_dev_ns_restore() ignores setns() failure

The old code logged ERR when switching back failed. Now it is silent, and if it fails the interrupt thread stays in the device's netns. Every later netlink socket opened from that thread (next LSC re-register, alarms) lands in the wrong namespace. Keep the error:

        static void
        tap_dev_ns_restore(struct pmd_internals *pmd, int orig_netns_fd)
        {
                if (setns(orig_netns_fd, CLONE_NEWNET) < 0)
                        TAP_LOG(ERR, "%s: failed to return to original netns: %s",
                                pmd->name, strerror(errno));
                close(orig_netns_fd);
        }
Quoted 8 lines

Info: rte_eal_alarm_set() return value unchecked

If it fails the LSC socket stays in the old namespace and no further link events arrive, with nothing in the log.

        if (rte_eal_alarm_set(1, tap_lsc_register_alarm, dev) < 0)
                TAP_LOG(ERR, "%s: failed to schedule LSC re-registration",
                        pmd->name);

Original code was light on the logging side.
I will add some error logs.

Quoted 8 lines

Info: concurrent dev_stop window
clean: cancels the alarm before the -EAGAIN retry loop. If dev_stop runs while tap_dev_intr_handler is active, the handler can arm the alarm after the cancel. In practice the alarm fires during the 100 ms rte_delay_ms() and the retry unregisters what it installed, but if the alarm runs as the stop thread wakes, both threads operate on pmd->intr_handle fd at once. Worth a comment, or a second rte_eal_alarm_cancel() before the do/while retry.

Well, that looks like a real issue.
Fixed.

Quoted 9 lines

[PATCH 2/2] net/tap: fix use after free on interrupt handle

Info: commit message does not mention the fd < 0 early return

The new check in clean: is needed because close after stop now reaches the unregister loop with fd == -1, where rte_intr_callback_unregister() returns -EINVAL and logs "Unregistering with invalid input parameter" plus the driver's "intr callback unregister failed". Add a sentence saying so.

This added check is actually misplaced and belongs to first patch.

Signature · 1 line
David Marchand
#6 lore raw

PATCH v2 1/2 net/tap: fix FD leak on netns change

In reply to David Marchand

When a tap interface moves to another namespace, tap_netns_change() calls tap_lsc_intr_handle_set(dev, 1) to recreate the LSC interrupt without first tearing down the old one. The previous LSC netlink socket fd is never closed.

Defer the full LSC teardown and re-registration to an alarm callback which runs in the interrupt thread but outside any active source context. The alarm switches to the device's network namespace, tears down the old LSC handle, sets up a new one (opening a fresh netlink socket in the correct namespace), restores the original namespace, and forces carrier state back up.

Extract tap_dev_ns_switch/tap_dev_ns_restore helpers to share the namespace switch logic between tap_netns_change and the alarm.

Fixes aae3a38e9e03 ("net/tap: detect namespace change")

---
Changes since v1:
- moved pmd->if_index clearing before trying to change netns,
- moved alarm registration after netns changes succeed,
- added second alarm cancel for unregister race with netns change,
- ignored ENOENT suggestion from AI review,
- added error logs to please AI review,

---
 drivers/net/tap/rte_eth_tap.c | 99 +++++++++++++++++++++++++----------
 1 file changed, 72 insertions(+), 27 deletions(-)
drivers/net/tap/rte_eth_tap.c +72 -27
index 1d2b71675a..66a55301e7 100644
@@ -17,6 +17,7 @@
 #include <rte_ip.h>
 #include <rte_ethdev.h>
 #include <rte_errno.h>
+#include <rte_alarm.h>
 #include <rte_cycles.h>
 
 #include <assert.h>
@@ -1796,26 +1797,23 @@ tap_mac_addr_remove(struct rte_eth_dev *dev __rte_unused,
 
 static void tap_dev_intr_handler(void *cb_arg);
 static int tap_lsc_intr_handle_set(struct rte_eth_dev *dev, int set);
+static void tap_lsc_register_alarm(void *arg);
 
+#ifdef TUNGETDEVNETNS
 static int
-tap_netns_change(struct rte_eth_dev *dev)
+tap_dev_ns_switch(struct pmd_internals *pmd, int *orig_netns_fd)
 {
-	struct pmd_internals *pmd = dev->data->dev_private;
-#ifdef TUNGETDEVNETNS
-	int netns_fd, orig_netns_fd, new_nlsk_fd;
+	int netns_fd;
 
 	netns_fd = ioctl(pmd->ka_fd, TUNGETDEVNETNS);
 	if (netns_fd < 0) {
 		TAP_LOG(INFO, "%s: interface deleted", pmd->name);
-		return 0;
+		return -1;
 	}
 
-	/* Interface was moved to another namespace */
-	pmd->if_index = 0;
-
 	/* Save current namespace */
-	orig_netns_fd = open("/proc/self/ns/net", O_RDONLY);
-	if (orig_netns_fd < 0) {
+	*orig_netns_fd = open("/proc/self/ns/net", O_RDONLY);
+	if (*orig_netns_fd < 0) {
 		TAP_LOG(ERR, "%s: failed to open original netns: %s",
 			pmd->name, strerror(errno));
 		close(netns_fd);
@@ -1827,10 +1825,37 @@ tap_netns_change(struct rte_eth_dev *dev)
 		TAP_LOG(ERR, "%s: failed to enter new netns: %s",
 			pmd->name, strerror(errno));
 		close(netns_fd);
-		close(orig_netns_fd);
+		close(*orig_netns_fd);
 		return -1;
 	}
 
+	close(netns_fd);
+	return 0;
+}
+
+static void
+tap_dev_ns_restore(struct pmd_internals *pmd, int orig_netns_fd)
+{
+	if (setns(orig_netns_fd, CLONE_NEWNET) < 0)
+		TAP_LOG(ERR, "%s: failed to restore original netns: %s",
+			pmd->name, strerror(errno));
+	close(orig_netns_fd);
+}
+#endif
+
+static int
+tap_netns_change(struct rte_eth_dev *dev)
+{
+	struct pmd_internals *pmd = dev->data->dev_private;
+#ifdef TUNGETDEVNETNS
+	int orig_netns_fd, new_nlsk_fd;
+
+	/* Interface was moved to another namespace */
+	pmd->if_index = 0;
+
+	if (tap_dev_ns_switch(pmd, &orig_netns_fd) < 0)
+		return 0;
+
 	/*
 	 * Update ifindex by querying interface name.
 	 * The interface now has a new ifindex in the new namespace.
@@ -1840,22 +1865,7 @@ tap_netns_change(struct rte_eth_dev *dev)
 	/* Recreate netlink socket in new namespace */
 	new_nlsk_fd = tap_nl_init(0);
 
-	/* Recreate LSC interrupt netlink socket in new namespace */
-	rte_intr_callback_unregister_pending(pmd->intr_handle, tap_dev_intr_handler, dev, NULL);
-	if (tap_lsc_intr_handle_set(dev, 1) < 0)
-		TAP_LOG(WARNING, "%s: failed to recreate LSC interrupt socket",
-			pmd->name);
-
-	/* Force carrier back after switching netns */
-	tap_carrier_set(pmd, 1);
-
-	/* Switch back to original namespace */
-	if (setns(orig_netns_fd, CLONE_NEWNET) < 0)
-		TAP_LOG(ERR, "%s: failed to return to original netns: %s",
-			pmd->name, strerror(errno));
-
-	close(orig_netns_fd);
-	close(netns_fd);
+	tap_dev_ns_restore(pmd, orig_netns_fd);
 
 	if (pmd->if_index == 0) {
 		TAP_LOG(WARNING, "%s: interface moved to another namespace, "
@@ -1877,6 +1887,11 @@ tap_netns_change(struct rte_eth_dev *dev)
 		tap_nl_final(pmd->nlsk_fd);
 	pmd->nlsk_fd = new_nlsk_fd;
 
+	/* Defer LSC re-registration outside callback context */
+	if (rte_eal_alarm_set(1, tap_lsc_register_alarm, dev) < 0)
+		TAP_LOG(ERR, "%s: failed to schedule LSC re-registration",
+			pmd->name);
+
 	TAP_LOG(INFO, "%s: interface moved to another namespace, new ifindex: %u",
 		pmd->name, pmd->if_index);
 #else
@@ -1952,6 +1967,10 @@ tap_lsc_intr_handle_set(struct rte_eth_dev *dev, int set)
 	}
 
 clean:
+	rte_eal_alarm_cancel(tap_lsc_register_alarm, dev);
+	if (rte_intr_fd_get(pmd->intr_handle) < 0)
+		return 0;
+
 	do {
 		ret = rte_intr_callback_unregister(pmd->intr_handle,
 			tap_dev_intr_handler, dev);
@@ -1966,6 +1985,8 @@ tap_lsc_intr_handle_set(struct rte_eth_dev *dev, int set)
 		}
 	} while (true);
 
+	rte_eal_alarm_cancel(tap_lsc_register_alarm, dev);
+
 	if (rte_intr_fd_get(pmd->intr_handle) >= 0) {
 		tap_nl_final(rte_intr_fd_get(pmd->intr_handle));
 		/* fd has been closed already, only clear reference here. */
@@ -1975,6 +1996,30 @@ tap_lsc_intr_handle_set(struct rte_eth_dev *dev, int set)
 	return 0;
 }
 
+static void
+tap_lsc_register_alarm(void *arg)
+{
+#ifdef TUNGETDEVNETNS
+	struct rte_eth_dev *dev = arg;
+	struct pmd_internals *pmd = dev->data->dev_private;
+	int orig_netns_fd;
+
+	if (tap_dev_ns_switch(pmd, &orig_netns_fd) < 0)
+		return;
+
+	tap_lsc_intr_handle_set(dev, 0);
+	if (tap_lsc_intr_handle_set(dev, 1) < 0)
+		TAP_LOG(ERR, "%s: failed to register LSC interrupt", pmd->name);
+
+	tap_dev_ns_restore(pmd, orig_netns_fd);
+
+	tap_carrier_set(pmd, 1);
+	tap_link_update(dev, 0);
+#else
+	RTE_SET_USED(arg);
+#endif
+}
+
 static int
 tap_intr_handle_set(struct rte_eth_dev *dev, int set)
 {
Signature · 1 line
2.56.0
#7 lore raw

PATCH v2 2/2 net/tap: fix use after free on interrupt handle

This driver was not unregistering its interrupt callbacks when closing the port, which now means use after free after the interrupt handle is not duplicated anymore by the interrupt thread.

Fixes c663018eb7f3 ("interrupts: do not duplicate interrupt handles")

---
Changes since v1:
- moved unrelated change,

---
 drivers/net/tap/rte_eth_tap.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/tap/rte_eth_tap.c +2 -0
index 66a55301e7..f6502c5e56 100644
@@ -1106,6 +1106,8 @@ tap_dev_close(struct rte_eth_dev *dev)
 		return 0;
 	}
 
+	tap_intr_handle_set(dev, 0);
+
 	if (!internals->persist)
 		tap_link_set_down(dev);
 
Signature · 1 line
2.56.0
Tested-by on v2 2/2
#8 lore raw

Re: [PATCH v2 2/2] net/tap: fix use after free on interrupt handle

David Marchand, Nov 06, 2026 at 09:57:

Quoted 8 lines

This driver was not unregistering its interrupt callbacks when closing the port, which now means use after free after the interrupt handle is not duplicated anymore by the interrupt thread.

Fixes c663018eb7f3 ("interrupts: do not duplicate interrupt handles")
Quoted 7 lines

---
Changes since v1:
- moved unrelated change,

---
 drivers/net/tap/rte_eth_tap.c | 2 ++
 1 file changed, 2 insertions(+)
Tested-by on v2 1/2
#9 lore raw
In reply to David Marchand

David Marchand, Nov 06, 2026 at 09:56:

Quoted 28 lines

When a tap interface moves to another namespace, tap_netns_change() calls tap_lsc_intr_handle_set(dev, 1) to recreate the LSC interrupt without first tearing down the old one. The previous LSC netlink socket fd is never closed.

Defer the full LSC teardown and re-registration to an alarm callback which runs in the interrupt thread but outside any active source context. The alarm switches to the device's network namespace, tears down the old LSC handle, sets up a new one (opening a fresh netlink socket in the correct namespace), restores the original namespace, and forces carrier state back up.

Extract tap_dev_ns_switch/tap_dev_ns_restore helpers to share the namespace switch logic between tap_netns_change and the alarm.

Fixes aae3a38e9e03 ("net/tap: detect namespace change")

---
Changes since v1:
- moved pmd->if_index clearing before trying to change netns,
- moved alarm registration after netns changes succeed,
- added second alarm cancel for unregister race with netns change,
- ignored ENOENT suggestion from AI review,
- added error logs to please AI review,

Quoted 3 lines
---
 drivers/net/tap/rte_eth_tap.c | 99 +++++++++++++++++++++++++----------
 1 file changed, 72 insertions(+), 27 deletions(-)
#10 lore raw
In reply to David Marchand
Quoted 15 lines · On Fri, 6 Nov 2026 09:56:59 +0100 David Marchand <[email protected]>…

When a tap interface moves to another namespace, tap_netns_change() calls tap_lsc_intr_handle_set(dev, 1) to recreate the LSC interrupt without first tearing down the old one. The previous LSC netlink socket fd is never closed.

Defer the full LSC teardown and re-registration to an alarm callback which runs in the interrupt thread but outside any active source context. The alarm switches to the device's network namespace, tears down the old LSC handle, sets up a new one (opening a fresh netlink socket in the correct namespace), restores the original namespace, and forces carrier state back up.

Extract tap_dev_ns_switch/tap_dev_ns_restore helpers to share the namespace switch logic between tap_netns_change and the alarm.

Quoted 6 lines
Fixes aae3a38e9e03 ("net/tap: detect namespace change")

---

AI review still seeing errors.

Review of [PATCH v2 0/2] net/tap: FD leak and interrupt UAF fixes

Applied to main (v26.11-rc2), both commits build with -Dwerror=true.

[PATCH v2 1/2] net/tap: fix FD leak on netns change

Error

The deferred alarm can run concurrently with the LSC teardown done by dev_stop/dev_close and re-register the callback after the control thread has removed it. The second rte_eal_alarm_cancel() added in v2 waits for a running alarm but does not undo what it did.

ctrl thread (dev_stop) intr thread
---------------------- -----------
rte_eal_alarm_cancel() /* none */

                                    tap_dev_intr_handler()
                                      tap_netns_change()
                                        rte_eal_alarm_set(1, ...)
  rte_intr_callback_unregister()
    -> success (not active)
                                    tap_lsc_register_alarm()
                                      tap_lsc_intr_handle_set(dev, 0)
                                        unregister -> -ENOENT
                                        close old fd, fd = -1
                                      tap_lsc_intr_handle_set(dev, 1)
                                        new fd, callback registered
  rte_eal_alarm_cancel()
    /* spins until alarm done, returns 0 */

tap_nl_final(new fd); fd = -1

The port is left stopped with tap_dev_intr_handler still registered on pmd->intr_handle with fd -1, and the carrier forced up. A following tap_dev_close() then does rte_intr_instance_free() with the callback still registered, which is the use after free patch 2 fixes.

Repeat the teardown until the fd is stable once no alarm is pending or running, and treat -ENOENT as already unregistered:

clean:

	for (;;) {
		rte_eal_alarm_cancel(tap_lsc_register_alarm, dev);
		fd = rte_intr_fd_get(pmd->intr_handle);
		if (fd < 0)
			return 0;

		do {
			ret = rte_intr_callback_unregister(pmd->intr_handle,
				tap_dev_intr_handler, dev);
			if (ret >= 0 || ret == -ENOENT)
				break;
			if (ret != -EAGAIN) {
				TAP_LOG(ERR, "intr callback unregister failed: %d",
					ret);
				break;
			}
			rte_delay_ms(100);
		} while (true);

		/* alarm may have replaced the fd while unregistering */
		rte_eal_alarm_cancel(tap_lsc_register_alarm, dev);
		if (rte_intr_fd_get(pmd->intr_handle) == fd)
			break;
	}

	tap_nl_final(fd);
	/* fd has been closed already, only clear reference here. */
	rte_intr_fd_set(pmd->intr_handle, -1);
	return 0;

When called from the alarm itself the cancel returns with EINPROGRESS, the fd does not change, and the loop exits on the first pass.

Review-Result: ERROR

#11 lore raw

PATCH v3 1/2 net/tap: fix FD leak on netns change

In reply to David Marchand

When a tap interface moves to another namespace, tap_netns_change() calls tap_lsc_intr_handle_set(dev, 1) to recreate the LSC interrupt without first tearing down the old one. The previous LSC netlink socket fd is never closed.

Defer the full LSC teardown and re-registration to an alarm callback which runs in the interrupt thread but outside any active source context. The alarm switches to the device's network namespace, tears down the old LSC handle, sets up a new one (opening a fresh netlink socket in the correct namespace), restores the original namespace, and forces carrier state back up.

Extract tap_dev_ns_switch/tap_dev_ns_restore helpers to share the namespace switch logic between tap_netns_change and the alarm.

Fixes aae3a38e9e03 ("net/tap: detect namespace change")

---
Changes since v2:
- added retry loop for the unregister retry loop,

Changes since v1:
- moved pmd->if_index clearing before trying to change netns,
- moved alarm registration after netns changes succeed,
- added second alarm cancel for unregister race with netns change,
- ignored ENOENT suggestion from AI review,
- added error logs to please AI review,

---
 drivers/net/tap/rte_eth_tap.c | 130 +++++++++++++++++++++++-----------
 1 file changed, 89 insertions(+), 41 deletions(-)
drivers/net/tap/rte_eth_tap.c +89 -41
index 1d2b71675a..87779710a7 100644
@@ -17,6 +17,7 @@
 #include <rte_ip.h>
 #include <rte_ethdev.h>
 #include <rte_errno.h>
+#include <rte_alarm.h>
 #include <rte_cycles.h>
 
 #include <assert.h>
@@ -1796,26 +1797,23 @@ tap_mac_addr_remove(struct rte_eth_dev *dev __rte_unused,
 
 static void tap_dev_intr_handler(void *cb_arg);
 static int tap_lsc_intr_handle_set(struct rte_eth_dev *dev, int set);
+static void tap_lsc_register_alarm(void *arg);
 
+#ifdef TUNGETDEVNETNS
 static int
-tap_netns_change(struct rte_eth_dev *dev)
+tap_dev_ns_switch(struct pmd_internals *pmd, int *orig_netns_fd)
 {
-	struct pmd_internals *pmd = dev->data->dev_private;
-#ifdef TUNGETDEVNETNS
-	int netns_fd, orig_netns_fd, new_nlsk_fd;
+	int netns_fd;
 
 	netns_fd = ioctl(pmd->ka_fd, TUNGETDEVNETNS);
 	if (netns_fd < 0) {
 		TAP_LOG(INFO, "%s: interface deleted", pmd->name);
-		return 0;
+		return -1;
 	}
 
-	/* Interface was moved to another namespace */
-	pmd->if_index = 0;
-
 	/* Save current namespace */
-	orig_netns_fd = open("/proc/self/ns/net", O_RDONLY);
-	if (orig_netns_fd < 0) {
+	*orig_netns_fd = open("/proc/self/ns/net", O_RDONLY);
+	if (*orig_netns_fd < 0) {
 		TAP_LOG(ERR, "%s: failed to open original netns: %s",
 			pmd->name, strerror(errno));
 		close(netns_fd);
@@ -1827,10 +1825,37 @@ tap_netns_change(struct rte_eth_dev *dev)
 		TAP_LOG(ERR, "%s: failed to enter new netns: %s",
 			pmd->name, strerror(errno));
 		close(netns_fd);
-		close(orig_netns_fd);
+		close(*orig_netns_fd);
 		return -1;
 	}
 
+	close(netns_fd);
+	return 0;
+}
+
+static void
+tap_dev_ns_restore(struct pmd_internals *pmd, int orig_netns_fd)
+{
+	if (setns(orig_netns_fd, CLONE_NEWNET) < 0)
+		TAP_LOG(ERR, "%s: failed to restore original netns: %s",
+			pmd->name, strerror(errno));
+	close(orig_netns_fd);
+}
+#endif
+
+static int
+tap_netns_change(struct rte_eth_dev *dev)
+{
+	struct pmd_internals *pmd = dev->data->dev_private;
+#ifdef TUNGETDEVNETNS
+	int orig_netns_fd, new_nlsk_fd;
+
+	/* Interface was moved to another namespace */
+	pmd->if_index = 0;
+
+	if (tap_dev_ns_switch(pmd, &orig_netns_fd) < 0)
+		return 0;
+
 	/*
 	 * Update ifindex by querying interface name.
 	 * The interface now has a new ifindex in the new namespace.
@@ -1840,22 +1865,7 @@ tap_netns_change(struct rte_eth_dev *dev)
 	/* Recreate netlink socket in new namespace */
 	new_nlsk_fd = tap_nl_init(0);
 
-	/* Recreate LSC interrupt netlink socket in new namespace */
-	rte_intr_callback_unregister_pending(pmd->intr_handle, tap_dev_intr_handler, dev, NULL);
-	if (tap_lsc_intr_handle_set(dev, 1) < 0)
-		TAP_LOG(WARNING, "%s: failed to recreate LSC interrupt socket",
-			pmd->name);
-
-	/* Force carrier back after switching netns */
-	tap_carrier_set(pmd, 1);
-
-	/* Switch back to original namespace */
-	if (setns(orig_netns_fd, CLONE_NEWNET) < 0)
-		TAP_LOG(ERR, "%s: failed to return to original netns: %s",
-			pmd->name, strerror(errno));
-
-	close(orig_netns_fd);
-	close(netns_fd);
+	tap_dev_ns_restore(pmd, orig_netns_fd);
 
 	if (pmd->if_index == 0) {
 		TAP_LOG(WARNING, "%s: interface moved to another namespace, "
@@ -1877,6 +1887,11 @@ tap_netns_change(struct rte_eth_dev *dev)
 		tap_nl_final(pmd->nlsk_fd);
 	pmd->nlsk_fd = new_nlsk_fd;
 
+	/* Defer LSC re-registration outside callback context */
+	if (rte_eal_alarm_set(1, tap_lsc_register_alarm, dev) < 0)
+		TAP_LOG(ERR, "%s: failed to schedule LSC re-registration",
+			pmd->name);
+
 	TAP_LOG(INFO, "%s: interface moved to another namespace, new ifindex: %u",
 		pmd->name, pmd->if_index);
 #else
@@ -1929,6 +1944,7 @@ tap_lsc_intr_handle_set(struct rte_eth_dev *dev, int set)
 {
 	struct pmd_internals *pmd = dev->data->dev_private;
 	int ret;
+	int fd;
 
 	/* In any case, disable interrupt if the conf is no longer there. */
 	if (!dev->data->dev_conf.intr_conf.lsc) {
@@ -1953,28 +1969,60 @@ tap_lsc_intr_handle_set(struct rte_eth_dev *dev, int set)
 
 clean:
 	do {
-		ret = rte_intr_callback_unregister(pmd->intr_handle,
-			tap_dev_intr_handler, dev);
-		if (ret >= 0) {
-			break;
-		} else if (ret == -EAGAIN) {
+		rte_eal_alarm_cancel(tap_lsc_register_alarm, dev);
+		fd = rte_intr_fd_get(pmd->intr_handle);
+		if (fd < 0)
+			return 0;
+
+		do {
+			ret = rte_intr_callback_unregister(pmd->intr_handle, tap_dev_intr_handler,
+				dev);
+			if (ret >= 0 || ret == -ENOENT)
+				break;
+			if (ret != -EAGAIN) {
+				TAP_LOG(ERR, "intr callback unregister failed: %d", ret);
+				break;
+			}
 			rte_delay_ms(100);
-		} else {
-			TAP_LOG(ERR, "intr callback unregister failed: %d",
-				     ret);
+		} while (true);
+
+		/* Alarm may have replaced the fd while unregistering */
+		rte_eal_alarm_cancel(tap_lsc_register_alarm, dev);
+		if (rte_intr_fd_get(pmd->intr_handle) == fd)
 			break;
-		}
 	} while (true);
 
-	if (rte_intr_fd_get(pmd->intr_handle) >= 0) {
-		tap_nl_final(rte_intr_fd_get(pmd->intr_handle));
-		/* fd has been closed already, only clear reference here. */
-		rte_intr_fd_set(pmd->intr_handle, -1);
-	}
+	tap_nl_final(fd);
+	/* fd has been closed already, only clear reference here. */
+	rte_intr_fd_set(pmd->intr_handle, -1);
 
 	return 0;
 }
 
+static void
+tap_lsc_register_alarm(void *arg)
+{
+#ifdef TUNGETDEVNETNS
+	struct rte_eth_dev *dev = arg;
+	struct pmd_internals *pmd = dev->data->dev_private;
+	int orig_netns_fd;
+
+	if (tap_dev_ns_switch(pmd, &orig_netns_fd) < 0)
+		return;
+
+	tap_lsc_intr_handle_set(dev, 0);
+	if (tap_lsc_intr_handle_set(dev, 1) < 0)
+		TAP_LOG(ERR, "%s: failed to register LSC interrupt", pmd->name);
+
+	tap_dev_ns_restore(pmd, orig_netns_fd);
+
+	tap_carrier_set(pmd, 1);
+	tap_link_update(dev, 0);
+#else
+	RTE_SET_USED(arg);
+#endif
+}
+
 static int
 tap_intr_handle_set(struct rte_eth_dev *dev, int set)
 {
Signature · 1 line
2.56.0
#12 lore raw

PATCH v3 2/2 net/tap: fix use after free on interrupt handle

This driver was not unregistering its interrupt callbacks when closing the port, which now means use after free after the interrupt handle is not duplicated anymore by the interrupt thread.

Fixes c663018eb7f3 ("interrupts: do not duplicate interrupt handles")

---
Changes since v1:
- moved unrelated change,

---
 drivers/net/tap/rte_eth_tap.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/tap/rte_eth_tap.c +2 -0
index 87779710a7..0476a327a1 100644
@@ -1106,6 +1106,8 @@ tap_dev_close(struct rte_eth_dev *dev)
 		return 0;
 	}
 
+	tap_intr_handle_set(dev, 0);
+
 	if (!internals->persist)
 		tap_link_set_down(dev);
 
Signature · 1 line
2.56.0
#13 lore raw
In reply to David Marchand
Quoted 15 lines · On Tue, 10 Nov 2026 11:03:07 +0100 David Marchand <[email protected]>…

When a tap interface moves to another namespace, tap_netns_change() calls tap_lsc_intr_handle_set(dev, 1) to recreate the LSC interrupt without first tearing down the old one. The previous LSC netlink socket fd is never closed.

Defer the full LSC teardown and re-registration to an alarm callback which runs in the interrupt thread but outside any active source context. The alarm switches to the device's network namespace, tears down the old LSC handle, sets up a new one (opening a fresh netlink socket in the correct namespace), restores the original namespace, and forces carrier state back up.

Extract tap_dev_ns_switch/tap_dev_ns_restore helpers to share the namespace switch logic between tap_netns_change and the alarm.

Quoted 6 lines
Fixes aae3a38e9e03 ("net/tap: detect namespace change")

---

Looks good applied to next-net