Bug 289017 - [lagg] A time-of-check to time-of-use (TOCTOU) race exists in the Link Aggregation (LAGG) network subsystem
Summary: [lagg] A time-of-check to time-of-use (TOCTOU) race exists in the Link Aggreg...
Status: In Progress
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 14.3-RELEASE
Hardware: Any Any
: --- Affects Some People
Assignee: Zhenlei Huang
URL: https://reviews.freebsd.org/D55123
Keywords:
: 292493 (view as bug list)
Depends on:
Blocks:
 
Reported: 2025-08-23 08:54 UTC by Qiu-ji Chen
Modified: 2026-05-04 08:28 UTC (History)
4 users (show)

See Also:
zlei: mfc-stable14?


Attachments
setup_and_trigger.sh (1.28 KB, text/plain)
2025-12-14 19:15 UTC, Gui-Dong Han
no flags Details
poc.c (2.90 KB, text/plain)
2025-12-14 19:17 UTC, Gui-Dong Han
no flags Details
Patch to fix lagg NULL dereferencing (2.70 KB, patch)
2026-02-05 05:03 UTC, Zhenlei Huang
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Qiu-ji Chen 2025-08-23 08:54:51 UTC
A time-of-check to time-of-use (TOCTOU) race exists in the net
subsystem at several call sites of lagg_proto_start(),
lagg_proto_input(), and lagg_proto_portreq().

In the SIOCSLAGG branch of lagg_ioctl(), updating sc is performed
by calling lagg_proto_detach() followed by lagg_proto_attach(). In
the middle of this sequence, sc->sc_proto is set to
LAGG_PROTO_NONE. This change is not mutually exclusive with
NET_EPOCH read-side critical sections, so sc->sc_proto may still
change while a reader is executing under NET_EPOCH.

For example, in lagg_transmit_ethernet(), the code checks:
if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0)

Because this check is protected only by NET_EPOCH, after it passes,
sc->sc_proto may still be changed to LAGG_PROTO_NONE. As a result,
lagg_proto_start(sc, m) will index
lagg_protos[sc->sc_proto].pr_start(sc, m) with
LAGG_PROTO_NONE. Since the LAGG_PROTO_NONE entry in lagg_protos
does not provide any function pointers, this leads to a NULL function
pointer dereference. The same issue exists at the call site in
lagg_transmit_infiniband().

Similarly, the call sites of lagg_proto_input() in
lagg_input_ethernet() and lagg_input_infiniband(), and the call
sites of lagg_proto_portreq() via lagg_port2req() in
lagg_port_ioctl() and lagg_ioctl(), are subject to the same race.

We also examined call sites of the related helpers
lagg_proto_addport(), lagg_proto_delport(), lagg_proto_linkstate(),
lagg_proto_init(), lagg_proto_stop(), lagg_proto_lladdr(), and
lagg_proto_request(). Their invocations are protected by the
appropriate locks, and we did not find a TOCTOU issue there.

Suggested fix: Before invoking the above helpers, snapshot
sc->sc_proto into a local variable, validate it, and use that single
value for both the check and the subsequent function-pointer call.
Comment 1 Zhenlei Huang freebsd_committer freebsd_triage 2025-08-27 16:37:57 UTC
(In reply to Qiu-ji Chen from comment #0)
Thanks for the reporting !

I think that is a nice finding.

As for the fix, FreeBSD's net stack is moving towards to lockless fast path, so probably the right fix should be flagging the interface down and waiting until the fast paths finish their work and then changing the proto.

Do you have any test cases so that it will make it easier to repeat the issue and verify the fix ?
Comment 2 Qiu-ji Chen 2025-12-14 03:34:40 UTC
(In reply to Zhenlei Huang from comment #1)

Thank you for the response.

I have successfully verified this TOCTOU issue dynamically, moving beyond the initial static analysis.

To reproduce the race condition reliably, I set up a QEMU environment with a custom FreeBSD 14.3 kernel. I injected a busy loop (DELAY) in lagg_transmit_ethernet specifically between the protocol check (sc_proto != NONE) and the function pointer call (lagg_proto_start). I then developed a multi-threaded PoC where one thread repeatedly toggles the lagg protocol via SIOCSLAGG while multiple victim threads flood the interface with packets.

This setup successfully triggered a Kernel Panic (Fatal trap 12: page fault with instruction pointer 0x0), proving that the protocol can indeed be switched to NONE after the check passes but before usage.

Panic Log:
Fatal trap 12: page fault while in kernel mode
cpuid = 1; apic id = 01
fault virtual address   = 0x0
fault code              = supervisor read instruction, page not present
instruction pointer     = 0x20:0x0
stack pointer           = 0x28:0xfffffe0068ff2948
frame pointer           = 0x28:0xfffffe0068ff2970
code segment            = base rx0, limit 0xfffff, type 0x1b
                        = DPL 0, pres 1, long 1, def32 0, gran 1
processor eflags        = interrupt enabled, IOPL = 0
current process         = 873 (poc)
rdi: fffff8000360a200 rsi: fffff800043c8700 rdx: 0000000000000000
rcx: 000000000000005a  r8: fffffe000937c060  r9: fffff800043c8760
rax: 0000000000000000 rbx: fffff80003663000 rbp: fffffe0068ff2970
r10: 00000000000000a0 r11: fffff800046f2740 r12: 000000000000000e
r13: 0000000000000008 r14: fffffe0068ff2ac0 r15: fffff80003663000
trap number             = 12
panic: page fault
cpuid = 1
time = 1765682403
KDB: stack backtrace:
#0 0xffffffff80ba8f1d at kdb_backtrace+0x5d
#1 0xffffffff80b5aa11 at vpanic+0x161
#2 0xffffffff80b5a8a3 at panic+0x43
#3 0xffffffff8104dbfa at trap_pfault+0x3da
#4 0xffffffff81023dd8 at calltrap+0x8
#5 0xffffffff80c85a50 at ether_output+0x6b0
#6 0xffffffff80d21998 at ip_output+0x13a8
#7 0xffffffff80d52c40 at udp_send+0xb60
#8 0xffffffff80c0145c at sosend_dgram+0x31c
#9 0xffffffff80c0242f at sousrsend+0x5f
#10 0xffffffff80c0aec0 at kern_sendit+0x1c0
#11 0xffffffff80c0b1f2 at sendit+0x1b2
#12 0xffffffff80c0b02d at sys_sendto+0x4d
#13 0xffffffff8104e547 at amd64_syscall+0x117
#14 0xffffffff810246eb at fast_syscall_common+0xf8
Uptime: 1m13s
Automatic reboot in 15 seconds - press a key on the console to abort
--> Press a key on the console to reboot,
--> or switch off the system now.


I verified that this vulnerable logic also persists in 15.0. While I plan to attempt a reproduction without artificial delays using pure concurrency to further demonstrate the impact, I believe the current result with the widened window definitively proves the bug's existence and mechanism.

I suggest prioritizing a fix for this race condition.
Comment 3 Gui-Dong Han 2025-12-14 19:13:05 UTC
(In reply to Zhenlei Huang from comment #1)

Update: I have successfully reproduced the race condition on a completely unmodified GENERIC kernel. By maximizing the race window through high-concurrency packet spraying and continuous protocol switching, I can reliably trigger the panic within seconds. Please find the reproduction scripts and the corresponding crash log below.

Crash log:
Fatal trap 12: page fault while in kernel mode
cpuid = 0; apic id = 00
fault virtual address   = 0x0
fault code              = supervisor read instruction, page not present
instruction pointer     = 0x20:0x0
stack pointer           = 0x28:0xfffffe0068f55948
frame pointer           = 0x28:0xfffffe0068f55970
code segment            = base rx0, limit 0xfffff, type 0x1b
                        = DPL 0, pres 1, long 1, def32 0, gran 1
processor eflags        = interrupt enabled, IOPL = 0
current process         = 1040 (poc)
rdi: fffff80004444200 rsi: fffff80004f5d900 rdx: 9c58554433221100
rcx: fffffe0068f55ac0  r8: 0008a2ff10fc9c58  r9: fffff80004f5d960
rax: 0000000000000000 rbx: fffff800036fc000 rbp: fffffe0068f55970
r10: 0000000000000090 r11: fffff80004f74000 r12: 000000000000000e
r13: 0000000000000008 r14: fffffe0068f55ac0 r15: fffff800036fc000
trap number             = 12
panic: page fault
cpuid = 0
time = 1765738480
KDB: stack backtrace:
#0 0xffffffff80ba8f1d at kdb_backtrace+0x5d
#1 0xffffffff80b5aa11 at vpanic+0x161
#2 0xffffffff80b5a8a3 at panic+0x43
#3 0xffffffff8104dbfa at trap_pfault+0x3da
#4 0xffffffff81023dd8 at calltrap+0x8
#5 0xffffffff80c85a50 at ether_output+0x6b0
#6 0xffffffff80d21998 at ip_output+0x13a8
#7 0xffffffff80d52c40 at udp_send+0xb60
#8 0xffffffff80c0145c at sosend_dgram+0x31c
#9 0xffffffff80c0242f at sousrsend+0x5f
#10 0xffffffff80c0aec0 at kern_sendit+0x1c0
#11 0xffffffff80c0b1f2 at sendit+0x1b2
#12 0xffffffff80c0b02d at sys_sendto+0x4d
#13 0xffffffff8104e547 at amd64_syscall+0x117
#14 0xffffffff810246eb at fast_syscall_common+0xf8
Comment 4 Gui-Dong Han 2025-12-14 19:15:10 UTC
Created attachment 266193 [details]
setup_and_trigger.sh

setup_and_trigger.sh: A shell script that initializes the virtual network environment (tap0 attached to lagg0) and configures static ARP to ensure immediate packet delivery. It runs an infinite loop that rapidly switches the lagg protocol state to trigger the race condition.
Comment 5 Gui-Dong Han 2025-12-14 19:17:00 UTC
Created attachment 266194 [details]
poc.c

poc.c: A multi-threaded C program that floods the interface with UDP packets. It utilizes CPU affinity to maximize contention and is designed to persist through network errors, ensuring continuous pressure on the lagg driver during state transitions.
Comment 6 Gui-Dong Han 2025-12-14 19:18:26 UTC
To reproduce the issue, compile poc.c and run it simultaneously with setup_and_trigger.sh. This combination reliably triggers the panic within seconds.
Comment 7 Gui-Dong Han 2025-12-14 19:28:08 UTC
(In reply to Zhenlei Huang from comment #1)

I can reliably reproduce the panic on an unmodified GENERIC kernel within seconds using the scripts provided.

However, by inserting artificial delays to widen the race window, I captured the specific stack trace below.

Crash log:
Fatal trap 12: page fault while in kernel mode
cpuid = 2; apic id = 02
fault virtual address   = 0x40
fault code              = supervisor read data, page not present
instruction pointer     = 0x20:0xffffffff82825bdd
stack pointer           = 0x28:0xfffffe0068fc58c0
frame pointer           = 0x28:0xfffffe0068fc58d0
code segment            = base rx0, limit 0xfffff, type 0x1b
[TOCTOU_DEBUG] SIOCSLAGG: Change!
                        = DPL 0, pres 1, long 1, def32 0, gran 1
processor eflags        = interrupt enabled, IOPL = 0
current process         = 860 (poc)
rdi: fffff8000363c000 rsi: 00000000d1f2023f rdx: 00000000000000ff
rcx: fffffe0068fc58ec  r8: 0000000000000800  r9: 0000000000000008
rax: 0000000000000000 rbx: fffff80004a08300 rbp: fffffe0068fc58d0
r10: fffffe0068fc5800 r11: 00fff58b8d9e8b8c r12: 000000000000000e
[TOCTOU_DEBUG] SIOCSLAGG: Change!
r13: 0000000000000008 r14: fffff8000363c000 r15: fffff80003624800
trap number             = 12
panic: page fault
cpuid = 2
time = 1765735007
KDB: stack backtrace:
#0 0xffffffff80ba8f1d at kdb_backtrace+0x5d
#1 0xffffffff80b5aa11 at vpanic+0x161
#2 0xffffffff80b5a8a3 at panic+0x43
#3 0xffffffff8104dbfa at trap_pfault+0x3da
#4 0xffffffff81023e88 at calltrap+0x8
#5 0xffffffff82821f7a at lagg_lacp_start+0x1a
#6 0xffffffff8281fa25 at lagg_transmit_ethernet+0xb5
#7 0xffffffff80c85c5c at ether_output_frame+0xcc
#8 0xffffffff80c85a50 at ether_output+0x6b0
#9 0xffffffff80d21a48 at ip_output+0x13a8
#10 0xffffffff80d52cf0 at udp_send+0xb60
#11 0xffffffff80c0145c at sosend_dgram+0x31c
#12 0xffffffff80c0242f at sousrsend+0x5f
#13 0xffffffff80c0aec0 at kern_sendit+0x1c0
#14 0xffffffff80c0b1f2 at sendit+0x1b2
#15 0xffffffff80c0b02d at sys_sendto+0x4d
#16 0xffffffff8104e547 at amd64_syscall+0x117
#17 0xffffffff8102479b at fast_syscall_common+0xf8

This crash indicates that lagg_lacp_start was executing after the protocol resources had already been cleared by the detach routine.

This confirms a severe lack of synchronization between the data path and the control path, which can lead to various race conditions.

I strongly recommend validating any proposed fix by running the attached stress-test scripts for an extended period.
Comment 8 Gui-Dong Han 2025-12-14 19:44:14 UTC
Also, I want to highlight that compiler optimizations (such as inlining and tail call optimization) can make pinpointing the exact crash location in a stock kernel extremely difficult.

I observed that crashes on the unmodified kernel often appear to stall at ether_output+0x6b0, masking the deeper call chain. The detailed stack trace provided above (showing ether_output_frame, lagg_transmit_ethernet, and lagg_lacp_start) was only obtained after I explicitly added __noinline and __attribute__((optnone)) to the relevant functions to disable these optimizations.
Comment 9 Gui-Dong Han 2025-12-14 19:48:24 UTC
I believe this race condition can manifest in multiple ways depending on the exact timing. Potential scenarios include a NULL pointer dereference within lagg_proto_start (specifically when the protocol is LAGG_PROTO_NONE) and the lagg_lacp_start crash shown above. These are likely just examples of many potential failure points caused by this synchronization issue.
Comment 10 Gui-Dong Han 2025-12-15 02:58:34 UTC
(In reply to Gui-Dong Han from comment #7)

Given that the transmit path can continue into the protocol's start function after lagg_proto_detach has executed (as evidenced by the crash above), and considering that detach explicitly frees protocol resources, I believe triggering a Use-After-Free (UAF) scenario is also highly probable with the specific timing.
Comment 11 Gui-Dong Han 2026-01-15 18:06:37 UTC
(In reply to Zhenlei Huang from comment #1)

Hi Zhenlei Huang,

I hope you had a relaxing holiday season.

I am writing to briefly follow up on this issue. As detailed in my previous comments from mid-December, I have attached a complete reproduction suite (setup_and_trigger.sh and poc.c) that can reliably trigger this race condition on an unmodified kernel within seconds.

Have you had a chance to verify the issue using these scripts? Please let me know if you encounter any difficulties with the reproduction steps.
Comment 12 Zhenlei Huang freebsd_committer freebsd_triage 2026-01-16 03:58:47 UTC
(In reply to Gui-Dong Han from comment #11)
Thanks for your working on the scripts to repeat this issue. That is great and I think it is sufficient.

I'm taking this.
Comment 13 Gui-Dong Han 2026-01-16 04:31:24 UTC
*** Bug 292493 has been marked as a duplicate of this bug. ***
Comment 14 Zhenlei Huang freebsd_committer freebsd_triage 2026-02-05 05:03:20 UTC
Created attachment 267797 [details]
Patch to fix lagg NULL dereferencing

(In reply to Qiu-ji Chen from comment #0)
(In reply to Gui-Dong Han from comment #11)
> Have you had a chance to verify the issue using these scripts? Please let me
> know if you encounter any difficulties with the reproduction steps.

```
static int
lagg_transmit_ethernet(struct ifnet *ifp, struct mbuf *m)
{
        struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;

        NET_EPOCH_ASSERT();
#if defined(KERN_TLS) || defined(RATELIMIT)
        if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
                MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
#endif
        /* We need a Tx algorithm and at least one port */
        if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
                m_freem(m);
                if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
                return (ENXIO);
        }

        ETHER_BPF_MTAP(ifp, m);

        return (lagg_proto_start(sc, m));
}
```
The assembly shows clearly that lagg_proto_start() does re-read sc->sc_proto and does not reuse the snapshot read prior to ETHER_BPF_MTAP(). I think obviously compiler can not conclude ETHER_BPF_MTAP() has no side effect, so no optimization could be done.

So there're various way to fix it. Finally I decide to add start and input routines ( pr_start and pr_input ) to the none lagg protocol, to eliminates the need to check protocol in the fast path.

Can you please give it a try with the patch I uploaded ?
Comment 15 Zhenlei Huang freebsd_committer freebsd_triage 2026-02-05 05:25:05 UTC
The patch is open for review. See https://reviews.freebsd.org/D55123 .
Comment 16 Gui-Dong Han 2026-02-05 09:49:04 UTC
(In reply to Zhenlei Huang from comment #14)

Thanks for the patch. I verified it on FreeBSD 14.3 (GENERIC and GENERIC-KASAN).

One small catch: the patch is missing a comma after ".pr_num = LAGG_PROTO_NONE", causing a build failure. I added it manually for the test.

With that fixed, the panic is resolved. The system survived the PoC and KASAN reported no issues.

However, I noticed lagg_port2req is not covered. While 15.0 seems safe (callers are all locked), 14.3 appears to have unprotected call sites that invoke lagg_port2req (and subsequently lagg_proto_portreq). These paths likely need lock backportings for stable branches.

I will be away on holiday until late February, so I'm afraid I won't be able to assist with further testing for a while. Since the PoC is reliable and works well in QEMU, hopefully reproducing the issue won't be too difficult in my absence.

Feel free to add my Tested-by and Reviewed-by tags.
Comment 17 Gui-Dong Han 2026-02-05 10:03:24 UTC
(In reply to Zhenlei Huang from comment #14)

Just a side note: I observed that packet transmission occasionally stalls during the stress test, as shown below:

Target: 192.168.100.2:12345
[Thread 3] Ready to fire.
[Thread 2] Ready to fire.
[Thread 0] Ready to fire.
[Thread 1] Ready to fire.
[Status] Total Sent: 393995 | Speed: 393995 pkts/sec
[Status] Total Sent: 663843 | Speed: 269848 pkts/sec
[Status] Total Sent: 894535 | Speed: 230692 pkts/sec
[Status] Total Sent: 966494 | Speed: 71959 pkts/sec
[Status] Total Sent: 966494 | Speed: 0 pkts/sec
[Status] Total Sent: 966494 | Speed: 0 pkts/sec
[Status] Total Sent: 966494 | Speed: 0 pkts/sec
[Status] Total Sent: 966494 | Speed: 0 pkts/sec
[Status] Total Sent: 966494 | Speed: 0 pkts/sec
[Status] Total Sent: 966494 | Speed: 0 pkts/sec
...

The throughput drops to zero, recovers after a while, and then repeats.

I suspect this is a normal artifact of the test setup (using cat /dev/tap0 > /dev/null to drain the interface), likely causing buffer saturation or triggering flow control on the tap device. I assume this is expected behavior for this specific environment, but I wanted to mention it in case you find it abnormal.
Comment 18 Gui-Dong Han 2026-02-05 10:14:31 UTC
(In reply to Zhenlei Huang from comment #14)

Just a quick side question: Is there any chance this issue qualifies for a CVE?

I noticed that Linux often assigns CVEs for reproducible crashes in network packet processing. Since this bug leads to a panic and acts like a Denial of Service (potentially remotely triggerable if traffic hits the race window), I was wondering if it fits FreeBSD's criteria.

Getting a CVE is always a nice motivation for kernel hackers like me, but if not, no worries at all!
Comment 19 Gui-Dong Han 2026-02-05 11:00:46 UTC
(In reply to Zhenlei Huang from comment #14)

While the patch successfully fixes the LAGG_PROTO_NONE NULL dereference, further investigation suggests the fundamental race condition between the data path and control path persists.

As noted in Comment 7 (where I used delays to widen the window), I previously triggered a panic in lagg_lacp_start. The current patch does not prevent lagg_proto_detach from running concurrently with lagg_transmit, leading to other failures.

Consider the following execution sequence:

Thread 1 (Data Path):
1. lagg_transmit_ethernet() passes the protocol check.
2. Calls lagg_proto_start() -> lagg_lacp_start().
3. Calls lacp_select_tx_port().
4. Macros LACP_SOFTC(sc) attempts to access sc->sc_psc.

Thread 2 (Control Path):
1. lagg_ioctl() -> lagg_proto_detach() -> lagg_lacp_detach().
2. Sets sc->sc_psc = NULL.
3. Calls lacp_detach(psc) and frees memory.

If Thread 2 executes step 2 before Thread 1 executes step 4, Thread 1 crashes (NULL dereference). This aligns with the stack trace I reported in Comment 7.

To mitigate this, the cleanup logic (specifically callout_drain) should be moved out of lacp_detach and executed inside lagg_lacp_detach *before* sc->sc_psc is set to NULL.

I will attempt to reproduce this specific race on an unmodified kernel when I have more time.

Summary: The current patch is necessary as it fixes the most easily triggered crash, but further fixes are required to address the remaining synchronization gaps, including this race and the lagg_port2req issue mentioned earlier.
Comment 20 Gui-Dong Han 2026-02-05 11:26:17 UTC
(In reply to Zhenlei Huang from comment #14)

Before I sign off, I performed a quick audit of other protocols and found a critical issue that confirms the Use-After-Free risk is real.

1. LAGG_PROTO_LOADBALANCE (UAF):
In lagg_lb_detach, the code directly frees the softc:
	lb = (struct lagg_lb *)sc->sc_psc;
	if (lb != NULL)
		free(lb, M_LAGG);

If lagg_transmit (executing lagg_lb_start) races with this detach routine, it will access the freed lb structure. This is a definitive Use-After-Free.
Comment 21 Zhenlei Huang freebsd_committer freebsd_triage 2026-02-05 16:33:33 UTC
(In reply to Gui-Dong Han from comment #16)
> Thanks for the patch. I verified it on FreeBSD 14.3 (GENERIC and GENERIC-KASAN).

> One small catch: the patch is missing a comma after ".pr_num = LAGG_PROTO_NONE", 
> causing a build failure. I added it manually for the test.

Ah, I see the problem. I tested the uploaded patch along with https://reviews.freebsd.org/D55124, which incidentally **FIXED** the compiling error.

> With that fixed, the panic is resolved. The system survived the PoC and KASAN reported no issues.
Good news!

> However, I noticed lagg_port2req is not covered. While 15.0 seems safe (callers are 
> all locked), 14.3 appears to have unprotected call sites that invoke lagg_port2req 
> (and subsequently lagg_proto_portreq). These paths likely need lock backportings for
> stable branches.

Thanks for finding those! I'll review the design.

> I will be away on holiday until late February, so I'm afraid I won't be able to 
> assist with further testing for a while. Since the PoC is reliable and works well in
> QEMU, hopefully reproducing the issue won't be too difficult in my absence.

You're welcome ! Wish you a nice holiday !

Feel free to add my Tested-by and Reviewed-by tags.
Comment 22 Zhenlei Huang freebsd_committer freebsd_triage 2026-02-05 16:36:19 UTC
(In reply to Gui-Dong Han from comment #17)

> The throughput drops to zero, recovers after a while, and then repeats.

I set up tests with real hardwares. The throughput drops but not to zero. This needs more investigation.
Comment 23 Zhenlei Huang freebsd_committer freebsd_triage 2026-02-05 16:47:02 UTC
(In reply to Gui-Dong Han from comment #18)

> Just a quick side question: Is there any chance this issue qualifies for a CVE?

I'm not an expert on security issues, so I can not speak too much, really.

> I noticed that Linux often assigns CVEs for reproducible crashes in network packet 
> processing. Since this bug leads to a panic and acts like a Denial of Service 
> (potentially remotely triggerable if traffic hits the race window), I was wondering 
> if it fits FreeBSD's criteria.

That really depends. For this case, a NULL pointer dereference, happens when the server have busy traffic over the lagg(4) interface, meanwhile the system administrator changes the protocol. IMO that is rare case. Most servers are set up and run until a reboot is required, e.g. updating / upgrading the server. A workaround can be, taking down the lagg(4) interface, changes the protocol, and then bring it up.

> Getting a CVE is always a nice motivation for kernel hackers like me, but if not,
> no worries at all!

I'm CCing the security team, and let them decide whether a CVE or an EN is favored.
Comment 24 commit-hook freebsd_committer freebsd_triage 2026-02-06 03:39:51 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=a622030b4baec2136984cea7bd25c2985a2ae9b3

commit a622030b4baec2136984cea7bd25c2985a2ae9b3
Author:     Zhenlei Huang <zlei@FreeBSD.org>
AuthorDate: 2026-02-06 03:37:43 +0000
Commit:     Zhenlei Huang <zlei@FreeBSD.org>
CommitDate: 2026-02-06 03:37:43 +0000

    lagg: Make the none protocol a first-class citizen

    All the other protocols have corresponding start and input routines,
    which are used in the fast path. Currently the none protocol is
    treated specially. In the fast path it is checked to indicate whether
    a working protocol is configured. There are two issues raised by this
    design:

    1. In production, other protocols are commonly used, but not the
    none protocol. It smells like an overkill to always check it in the
    fast path. It is unfair to other commonly used protocols.

    2. PR 289017 reveals that there's a small window between checking the
    protocol and calling lagg_proto_start(). lagg_proto_start() is possible
    to see the none protocol and do NULL deferencing.

    Fix them by making the none protocol a first-class citizen so that it
    has start and input routines just the same as other protocols. Then we
    can stop checking it in the fast path, since lagg_proto_start() and
    lagg_proto_input() will never fail to work.

    The error ENETDOWN is chosen for the start routine. Obviously no active
    ports are available, and the packets will go nowhere. It is also a
    better error than ENXIO, since indeed the interface is configured and
    has a TX algorithm (the none protocol).

    PR:             289017
    Diagnosed by:   Qiu-ji Chen <chenqiuji666@gmail.com>
    Tested by:      Gui-Dong Han <hanguidong02@gmail.com>
    Reviewed by:    glebius
    MFC after:      5 days
    Differential Revision:  https://reviews.freebsd.org/D55123

 sys/net/if_lagg.c | 42 +++++++++++++++++++++++++++++++++---------
 1 file changed, 33 insertions(+), 9 deletions(-)
Comment 25 Gui-Dong Han 2026-02-06 09:48:53 UTC
(In reply to Zhenlei Huang from comment #22)

> I set up tests with real hardwares. The throughput drops but not to zero. This needs more investigation.

Regarding the throughput drops on real hardware: As discussed in Comment 19 and Comment 20, I suspect this is a symptom of lagg_transmit racing with resource reclamation (detach) or new initialization (attach).

The current logic checks sc->sc_proto once, but execution continues without preventing concurrent detach/attach operations. We likely need a global mechanism—similar to a correctly placed callout_drain (as noted in Comment 19)—to ensure the data path is quiescent before allowing detach or new attach to proceed.

I currently lack access to my test servers due to the holiday, but I suspect that injecting delays into pr_start and similar packet transmission callbacks would confirm this hypothesis and reveal further instabilities.
Comment 26 Gui-Dong Han 2026-02-06 10:51:22 UTC
(In reply to Gui-Dong Han from comment #25)

I re-examined the code and realized I misunderstood callout_drain. It only ensures the specific timer callback has finished, but does not wait for the lagg_transmit data path.

So, moving callout_drain (as suggested in Comment 19) won't prevent the race. To safely handle the concurrency between the data path and detach, we need a mechanism ensure all active data path executions have finished before we free the resources.
Comment 27 Zhenlei Huang freebsd_committer freebsd_triage 2026-02-06 19:49:36 UTC
(In reply to Gui-Dong Han from comment #26)
> I re-examined the code and realized I misunderstood callout_drain. It only ensures 
> the specific timer callback has finished, but does not wait for the lagg_transmit 
> data path.

> So, moving callout_drain (as suggested in Comment 19) won't prevent the race. To 
> safely handle the concurrency between the data path and detach, we need a mechanism 
> ensure all active data path executions have finished before we free the resources.

FreeBSD's net stack utilize epoch(9) to safely free memory after the data path ( the fast path ) reaches a **safe** point. Well synchronization is still required when using epoch(9).
Comment 28 Gui-Dong Han 2026-02-07 01:39:12 UTC
(In reply to Zhenlei Huang from comment #27)

I noticed NET_EPOCH_CALL is used for ports (lagg_port_destroy_cb) and so on, but protocol detach routines appear to free resources (like sc_psc) immediately without explicit epoch deferral.

Please refer to the race sequence in Comment 19 and the backtrace in Comment 7. The current patch does not seem to cover this specific synchronization gap.
Comment 29 Zhenlei Huang freebsd_committer freebsd_triage 2026-02-08 01:10:24 UTC
(In reply to Gui-Dong Han from comment #28)
> I noticed NET_EPOCH_CALL is used for ports (lagg_port_destroy_cb) and so on, but 
> protocol detach routines appear to free resources (like sc_psc) immediately without
> explicit epoch deferral.

I think you're right. I managed to catch the UAF with KASAN enabled.
```
Unread portion of the kernel message buffer:
panic: ASan: Invalid access, 8-byte read at 0xfffffe00f2a88488, UMAUseAfterFree(fd)
cpuid = 1
time = 1770481930
KDB: stack backtrace:
db_trace_self_wrapper() at db_trace_self_wrapper+0xa5/frame 0xfffffe00ec103df0
kdb_backtrace() at kdb_backtrace+0xc6/frame 0xfffffe00ec103f50
vpanic() at vpanic+0x214/frame 0xfffffe00ec1040f0
panic() at panic+0xb5/frame 0xfffffe00ec1041b0
kasan_report() at kasan_report+0xdf/frame 0xfffffe00ec104280
lagg_lb_start() at lagg_lb_start+0xbd/frame 0xfffffe00ec1042b0
ether_output_frame() at ether_output_frame+0x25f/frame 0xfffffe00ec104390
ether_output() at ether_output+0xd20/frame 0xfffffe00ec1044d0
ip_output() at ip_output+0x253d/frame 0xfffffe00ec104710
udp_send() at udp_send+0x144e/frame 0xfffffe00ec104920
sosend_dgram() at sosend_dgram+0x469/frame 0xfffffe00ec1049a0
sousrsend() at sousrsend+0xf9/frame 0xfffffe00ec104a30
kern_sendit() at kern_sendit+0x465/frame 0xfffffe00ec104b90
sendit() at sendit+0x121/frame 0xfffffe00ec104bf0
sys_sendto() at sys_sendto+0x181/frame 0xfffffe00ec104d10
amd64_syscall() at amd64_syscall+0x3d5/frame 0xfffffe00ec104f30
fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe00ec104f30
--- syscall (133, FreeBSD ELF64, sendto), rip = 0x82467557a, rsp = 0x825909ea8, rbp = 0x825909ef0 ---
Uptime: 7m41s
Dumping 1207 out of 16138 MB:..2%..11%..22%..31%..42%..51%..61%..71%..81%..91%


(kgdb) frame 6
#6  0xffffffff8616fb6d in lagg_lb_start (sc=0xfffffe00f2aee800, m=0xfffffe01576b8300) at /home/zlei/freebsd-src/sys/net/if_lagg.c:2622
2622		lp = lb->lb_ports[p];
(kgdb) p lb
$1 = (struct lagg_lb *) 0xfffffe00f2a88480
(kgdb)

```

The KASAN reports clearly the UAF.


> So 
> 
> Please refer to the race sequence in Comment 19 and the backtrace in Comment 7. 
> The current patch does not seem to cover this specific synchronization gap.

This is a different path.
Comment 30 Gui-Dong Han 2026-02-08 03:26:24 UTC
(In reply to Zhenlei Huang from comment #29)

> The KASAN reports clearly the UAF.

Nice catch! This confirms the analysis in Comment 20 perfectly. LAGG_PROTO_LOADBALANCE is indeed the most susceptible to UAF due to the immediate free().

This definitively confirms that further fixes are required. The confirmed UAF significantly elevates the severity and potential exploitability of this issue.

> This is a different path.

While the specific protocol function differs, the root cause is identical—the lack of synchronization between detach and the fast/data path. LACP triggers NULL dereference more easily because it zeroes the pointer early, but it is fundamentally vulnerable to UAF as well given the right timing.

This race condition likely also explains the throughput instability observed in Comment 17 and Comment 22.
Comment 31 commit-hook freebsd_committer freebsd_triage 2026-02-11 10:03:35 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=18c70c737cb3e488e2658add19134f455add294d

commit 18c70c737cb3e488e2658add19134f455add294d
Author:     Zhenlei Huang <zlei@FreeBSD.org>
AuthorDate: 2026-02-06 03:37:43 +0000
Commit:     Zhenlei Huang <zlei@FreeBSD.org>
CommitDate: 2026-02-11 10:00:44 +0000

    lagg: Make the none protocol a first-class citizen

    All the other protocols have corresponding start and input routines,
    which are used in the fast path. Currently the none protocol is
    treated specially. In the fast path it is checked to indicate whether
    a working protocol is configured. There are two issues raised by this
    design:

    1. In production, other protocols are commonly used, but not the
    none protocol. It smells like an overkill to always check it in the
    fast path. It is unfair to other commonly used protocols.

    2. PR 289017 reveals that there's a small window between checking the
    protocol and calling lagg_proto_start(). lagg_proto_start() is possible
    to see the none protocol and do NULL deferencing.

    Fix them by making the none protocol a first-class citizen so that it
    has start and input routines just the same as other protocols. Then we
    can stop checking it in the fast path, since lagg_proto_start() and
    lagg_proto_input() will never fail to work.

    The error ENETDOWN is chosen for the start routine. Obviously no active
    ports are available, and the packets will go nowhere. It is also a
    better error than ENXIO, since indeed the interface is configured and
    has a TX algorithm (the none protocol).

    PR:             289017
    Diagnosed by:   Qiu-ji Chen <chenqiuji666@gmail.com>
    Tested by:      Gui-Dong Han <hanguidong02@gmail.com>
    Reviewed by:    glebius
    MFC after:      5 days
    Differential Revision:  https://reviews.freebsd.org/D55123

    (cherry picked from commit a622030b4baec2136984cea7bd25c2985a2ae9b3)

 sys/net/if_lagg.c | 42 +++++++++++++++++++++++++++++++++---------
 1 file changed, 33 insertions(+), 9 deletions(-)