|WARNING| [v3,2/2] net/tap: fix use after free on interrupt handle

dpdklab at iol.unh.edu dpdklab at iol.unh.edu
Tue Nov 10 19:32:32 CET 2026


Test-Label: ai-code-review
Test-Status: WARNING
https://dpdk.org/patch/172794

_AI Code Review_

=== Patch Review: series.patch (via Claude) ===
Review date: 2026-11-10

# DPDK Patch Review

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

### ERRORS

**Error 1: Resource leak on error path**
```c
if (tap_dev_ns_switch(pmd, &orig_netns_fd) < 0)
    return 0;
```
Line 1896 in tap_netns_change(): When `tap_dev_ns_switch()` fails, it may have already opened `orig_netns_fd` (at line 1814) before the `setns()` call fails (line 1824). The error path at line 1826-1828 closes both `netns_fd` and `*orig_netns_fd`, but the error return at line 1856 in `tap_netns_change()` does not close `orig_netns_fd` if the failure happened after line 1814 but returned -1.

Actually, reviewing the code more carefully: `tap_dev_ns_switch()` closes `orig_netns_fd` at line 1828 on the `setns()` failure path, and closes `netns_fd` at line 1832. So if `tap_dev_ns_switch()` returns -1, both fds have been closed. No leak here.

**Error 2: Infinite loop on unexpected error codes**
```c
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);
```
Lines 1978-1986: The inner retry loop breaks on any error other than -EAGAIN, including the log at line 1982. However, the outer `do {} while (true)` at line 1971 continues indefinitely if the alarm keeps resetting the fd. If `rte_intr_callback_unregister()` returns an unexpected error (not -EAGAIN, not -ENOENT, not success), the code logs it once and breaks the inner loop, but then the outer loop re-checks the fd and repeats. If the alarm is rescheduling faster than the loop can run, this could spin forever. 

Wait, let me re-read. The outer loop condition at line 1990 checks `if (rte_intr_fd_get(pmd->intr_handle) == fd)`. If the alarm replaced the fd, the loop breaks. If the fd is still the same, the loop retries. But if unregister returned a fatal error (not -EAGAIN), we already logged and broke the inner loop. Then we cancel the alarm again (line 1989), re-check the fd. If the fd is still the same, we loop back to line 1971, which starts a fresh `do {} while (true)` at line 1977. So we retry indefinitely on fatal errors as long as the fd doesn't change.

Suggested fix: Change the outer loop to track the fatal error case and break:
```c
clean:
    do {
        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);
                rte_intr_fd_set(pmd->intr_handle, -1);
                goto done;
            }
            rte_delay_ms(100);
        } while (true);

        rte_eal_alarm_cancel(tap_lsc_register_alarm, dev);
        if (rte_intr_fd_get(pmd->intr_handle) == fd)
            break;
    } while (true);

done:
    if (fd >= 0) {
        tap_nl_final(fd);
        rte_intr_fd_set(pmd->intr_handle, -1);
    }
    return 0;
```
Or simpler: just return the error after logging instead of breaking, since we've already hit an unrecoverable state.

### WARNINGS

**Warning 1: Missing NULL check on dev parameter**
```c
static void
tap_lsc_register_alarm(void *arg)
{
#ifdef TUNGETDEVNETNS
    struct rte_eth_dev *dev = arg;
    struct pmd_internals *pmd = dev->data->dev_private;
```
Line 2002-2004: The function casts `arg` to `struct rte_eth_dev *` without checking for NULL. While `rte_eal_alarm_set()` is called with `dev` at line 1891, and alarms are canceled before `dev` is freed, defensive NULL checks are good practice for alarm callbacks. However, reviewing the call sites: the only caller is line 1891 where `dev` is the function parameter and cannot be NULL at that point (it's dereferenced earlier in the function). This is Info-level, not a Warning.

### INFO

**Info 1: Pre-existing inconsistency in error logging**
The patch adds error logs for alarm registration failure (line 1892) and LSC registration failure (line 2013), which is good. However, the existing code at line 1864 calls `tap_nl_init()` without logging on failure. Pre-existing, not introduced by this patch.

---

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

### CLEAN

The patch correctly adds cleanup of the interrupt handle before the device is closed. The `tap_intr_handle_set(dev, 0)` call at line 1109 tears down both LSC and Rx interrupt registrations (it calls `tap_lsc_intr_handle_set(dev, 0)` which was added in patch 1/2), preventing use-after-free when the interrupt thread tries to access the handle after `dev` is freed.

No issues found in this patch.

---

## SUMMARY

Patch 1/2 introduces an infinite retry loop on fatal unregister errors due to the nested loop structure. The outer loop retries indefinitely as long as the fd remains unchanged, even when the inner loop has already hit a non-EAGAIN error and logged it. This is a correctness bug.

Patch 2/2 is correct.

Review-Result: ERROR


More information about the test-report mailing list