>Synopsis: trunk(4) protocol switch frees trunk_lb but keeps using it
>Category: kernel
>Severity: serious
>Class: sw-bug
>Release: OpenBSD 8.0-current (source as of 2026-11-03)
>Environment:
System : OpenBSD 8.0-current
Details : OpenBSD 8.0-current (GENERIC.MP), WITNESS + DIAGNOSTIC
Architecture: OpenBSD.amd64
Machine : amd64 (QEMU/KVM guest, 2 vcpu)
Source : sys/net/if_trunk.c rev 1.161, openbsd/src as of
2026-11-03, git d5b06e73
>Description:
SIOCSTRUNK ("ifconfig trunkN trunkproto X", trunk_ioctl(), line 640)
can leave the trunk calling into its freed loadbalance state, and in
one case it also leaves every member port unpublished:
1. tr_detach() runs before the new protocol is validated.
ra_proto is only checked against TRUNK_PROTO_MAX.
TRUNK_PROTO_LACP (5) is below that limit, but trunk_protos[] has
no entry for it. So "trunkproto lacp" clears ac_trport on every
port, refcnt_finalize()s them, calls tr->tr_detach(tr), and only
then fails with EPROTONOSUPPORT (line 695). That path:
- never republishes ac_trport or refcnt_init()s tp_refs. Every
member keeps trunk(4)'s hooks and ioctl/output redirection,
but its ac_trport is NULL. ether_input() stops handing its
packets to trunk_input(), and the next trunk_port_destroy()
(removing the port, destroying the member or the trunk) trips
KASSERT(ac_trport == &tp->tp_ether_port) at line 419;
- keeps tr_proto == TRUNK_PROTO_LOADBALANCE and the trunk_lb_*
hooks, with tr_psc pointing at the freed struct trunk_lb. The
next protocol change calls trunk_lb_detach() again (a double
free).
2. The detach routines do not clear the protocol hooks.
trunk_lb_detach() frees tr_psc (line 1453) but leaves tr_psc,
tr_start, tr_input, tr_port_create, tr_port_destroy, etc. in
place. TRUNK_PROTO_NONE has no attach routine, so after
"trunkproto loadbalance" -> "trunkproto none" the trunk still
has tr_port_create = trunk_lb_port_create and
tr_port_destroy = trunk_lb_port_destroy. Both are called
without checking tr_proto (trunk_port_create() line 380,
trunk_port_destroy() line 430).
Either way, the next "trunkport" add, or any member's detach (for
example destroying a vether that is a port), runs
trunk_lb_porttable() on the freed struct trunk_lb. That function
bzero()s lb->lb_ports (line 1464) and refills it (line 1473).
struct trunk_lb is 272 bytes (16-byte SIPHASH key, then 32 port
pointers), so it comes from the 512-byte malloc bucket, and
lb_ports[0] is at byte 16. The stray stores are therefore NULL or
trunk_port pointers (208-byte objects, 256-aligned) starting at
word 4 of a freed 512-byte chunk. The next malloc that recycles the
chunk, typically an allocation made while exec(2)ing, then panics
(How-To-Repeat "none" case below, GENERIC.MP + WITNESS + DIAGNOSTIC):
panic: Data modified on freelist: word 4 of object 0xffff800000e23200
size 0x110 previous type devbuf (0xdf7900 != 0xdeaf4152)
malloc() <- exec_elf_makecmds() <- check_exec() <- sys_execve()
The "size" and "previous type" in that message describe the NEW
allocation and the chunk's last legitimate owner, not the object
that was written after being freed.
The writer was confirmed with a debug-only guard allocator (not part
of this diff): every 257..512-byte malloc gets its own page, and on
free the page is made read-only and quarantined, so a stray store
faults at the writer:
mguard: FAULT at 0xffff800013d10010 = zone 512 slot 3298 +0x10
state 2 (FREED, parked read-only) gen 1 size 272 atype devbuf
mguard: malloc stack: malloc <- trunk_lb_attach+0x33 <- trunk_ioctl
mguard: free stack: free <- trunk_lb_detach+0x2e <- trunk_ioctl
fatal page fault in supervisor mode
panic: trap type 6, code=3, pc=ffffffff8125a522 (memset+0x52)
memset() <- trunk_port_create+0x4a7 (the (*tr->tr_port_create)(tp)
call; trunk_lb_port_create tail-calls trunk_lb_porttable, whose
bzero is the store)
<- trunk_ioctl+0x521 (SIOCSTRUNKPORT) <- ifioctl <- sys_ioctl
A randomized interface-configuration fuzzer, run on the same
instrumented kernel, faulted on the identical object and stack.
>How-To-Repeat:
Tom (me ) mangaged to repeat these issues belwo (1,3,4 )
As root, one of:
1. ifconfig vether0 create up; ifconfig vether1 create up
ifconfig trunk0 create
ifconfig trunk0 trunkproto loadbalance trunkport vether0 \
trunkproto none trunkport vether1 # write into freed trunk_lb
ifconfig vether0 destroy # again, via the detach hook
for i in $(jot 50); do /usr/bin/true; done # malloc notices
2. the same with "trunkproto lacp" in place of "trunkproto none"
(fails with EPROTONOSUPPORT after freeing the state and
unpublishing the ports); the vether0 destroy then trips the
trunk_port_destroy() KASSERT.
3. ifconfig vether0 create up; ifconfig vether1 create up
ifconfig trunk0 create
ifconfig trunk0 trunkproto loadbalance trunkport vether0
ifconfig trunk0 trunkproto lacp # fails, ports unpublished
ifconfig trunk0 destroy # KASSERT in trunk_port_destroy()
4. as 3., but before destroying the trunk:
ifconfig aggr0 create; ifconfig aggr0 trunkport vether0
ifconfig aggr0 destroy; ifconfig trunk0 destroy
>Fix:
- Look the new protocol up in trunk_protos[] before anything is torn
down, so "lacp" (or any unimplemented value) fails with
EPROTONOSUPPORT and has no side effects.
- New trunk_proto_clear(): after tr_detach() succeeds, NULL every
protocol hook and tr_psc. An attach failure also leaves the trunk
at TRUNK_PROTO_NONE with no hooks.
- trunk_start() runs from the send queue without NET_LOCK and only
looks at tr_start while tr_proto != TRUNK_PROTO_NONE. So tr_proto
is set to NONE and ifq_barrier(&ifp->if_snd) runs before the old
state is freed, and the new tr_proto is published (after a
membar_producer()) only once ti_attach() has succeeded. Without
this, a concurrent trunk_start() could still run trunk_lb_start()
on the state being freed, or call a cleared tr_start.
- The ports' ac_trport / tp_refs are republished on every path,
including a failed detach or attach.
Index: sys/net/if_trunk.c
===================================================================
RCS file: /cvs/src/sys/net/if_trunk.c,v
retrieving revision 1.161
diff -u -p -r1.161 if_trunk.c
--- sys/net/if_trunk.c 27 Jun 2026 06:29:15 -0000 1.161
+++ sys/net/if_trunk.c 5 Nov 2026 07:20:00 -0000
@@ -136,6 +136,27 @@ static const struct {
{ TRUNK_PROTO_NONE, NULL }
};
+/*
+ * Forget the current protocol. The tr_detach routine may have freed the
+ * private state (trunk_lb_detach() frees tr_psc), so none of the old hooks
+ * may be called once it has run.
+ */
+static void
+trunk_proto_clear(struct trunk_softc *tr)
+{
+ tr->tr_detach = NULL;
+ tr->tr_start = NULL;
+ tr->tr_input = NULL;
+ tr->tr_port_create = NULL;
+ tr->tr_port_destroy = NULL;
+ tr->tr_linkstate = NULL;
+ tr->tr_init = NULL;
+ tr->tr_stop = NULL;
+ tr->tr_req = NULL;
+ tr->tr_portreq = NULL;
+ tr->tr_psc = NULL;
+}
+
void
trunkattach(int count)
{
@@ -615,6 +636,7 @@ trunk_ioctl(struct ifnet *ifp, u_long cm
struct trunk_port *tp;
struct ifnet *tpif;
int i, error = 0;
+ enum trunk_proto oproto;
bzero(&rpbuf, sizeof(rpbuf));
@@ -648,6 +670,21 @@ trunk_ioctl(struct ifnet *ifp, u_long cm
}
/*
+ * Look the new protocol up before the current one is torn
+ * down: TRUNK_PROTO_LACP is below TRUNK_PROTO_MAX but has no
+ * implementation, and failing after tr_detach() would leave
+ * the trunk with freed protocol state and its ports unpublished.
+ */
+ for (i = 0; i < nitems(trunk_protos); i++) {
+ if (trunk_protos[i].ti_proto == ra->ra_proto)
+ break;
+ }
+ if (i == nitems(trunk_protos)) {
+ error = EPROTONOSUPPORT;
+ break;
+ }
+
+ /*
* Use of ifp->if_input and ac->ac_trunkport is
* protected by NET_LOCK, but that may not be true
* in the future. The below comment and code flow is
@@ -660,6 +697,19 @@ trunk_ioctl(struct ifnet *ifp, u_long cm
* granting us an exclusive access to it.
*/
NET_ASSERT_LOCKED();
+
+ /*
+ * trunk_start() runs from the send queue without NET_LOCK and
+ * only calls tr_start while tr_proto != TRUNK_PROTO_NONE.
+ * Unpublish the protocol and wait for a running trunk_start()
+ * before its state is freed; the new protocol is published
+ * only once it is fully attached.
+ */
+ oproto = tr->tr_proto;
+ tr->tr_proto = TRUNK_PROTO_NONE;
+ membar_producer();
+ ifq_barrier(&ifp->if_snd);
+
SLIST_FOREACH(tp, &tr->tr_ports, tp_entries) {
struct arpcom *ac = (struct arpcom *)tp->tp_if;
SMR_PTR_SET_LOCKED(&ac->ac_trport, NULL);
@@ -667,33 +717,34 @@ trunk_ioctl(struct ifnet *ifp, u_long cm
SLIST_FOREACH(tp, &tr->tr_ports, tp_entries)
refcnt_finalize(&tp->tp_refs, "trunkset");
smr_barrier();
- if (tr->tr_proto != TRUNK_PROTO_NONE)
+ if (oproto != TRUNK_PROTO_NONE)
error = tr->tr_detach(tr);
- if (error != 0)
- break;
- for (i = 0; i < nitems(trunk_protos); i++) {
- if (trunk_protos[i].ti_proto == ra->ra_proto) {
- if (tr->tr_ifflags & IFF_DEBUG)
- printf("%s: using proto %u\n",
- tr->tr_ifname,
- trunk_protos[i].ti_proto);
- tr->tr_proto = trunk_protos[i].ti_proto;
- if (tr->tr_proto != TRUNK_PROTO_NONE)
- error = trunk_protos[i].ti_attach(tr);
- SLIST_FOREACH(tp, &tr->tr_ports, tp_entries) {
- struct arpcom *ac =
- (struct arpcom *)tp->tp_if;
- refcnt_init(&tp->tp_refs);
- SMR_PTR_SET_LOCKED(&ac->ac_trport,
- &tp->tp_ether_port);
- }
- /* Update trunk capabilities */
- tr->tr_capabilities = trunk_capabilities(tr);
- goto out;
+ if (error != 0) {
+ /* the old protocol is still attached */
+ membar_producer();
+ tr->tr_proto = oproto;
+ } else {
+ trunk_proto_clear(tr);
+ if (tr->tr_ifflags & IFF_DEBUG)
+ printf("%s: using proto %u\n", tr->tr_ifname,
+ trunk_protos[i].ti_proto);
+ if (trunk_protos[i].ti_proto != TRUNK_PROTO_NONE) {
+ error = trunk_protos[i].ti_attach(tr);
+ if (error == 0) {
+ membar_producer();
+ tr->tr_proto = trunk_protos[i].ti_proto;
+ } else
+ trunk_proto_clear(tr);
}
}
- error = EPROTONOSUPPORT;
- break;
+ SLIST_FOREACH(tp, &tr->tr_ports, tp_entries) {
+ struct arpcom *ac = (struct arpcom *)tp->tp_if;
+ refcnt_init(&tp->tp_refs);
+ SMR_PTR_SET_LOCKED(&ac->ac_trport, &tp->tp_ether_port);
+ }
+ /* Update trunk capabilities */
+ tr->tr_capabilities = trunk_capabilities(tr);
+ goto out;
case SIOCGTRUNKOPTS:
/* Only LACP trunks have options atm */
error = EPROTONOSUPPORT;
>Verification:
Diff applied as shown with patch -p0 to sys/net/if_trunk.c r1.161
(openbsd/src as of 2026-11-03), no fuzz or offset; kernel
GENERIC.MP + WITNESS + DIAGNOSTIC, amd64, 2 vcpu, built without
warnings. Unpatched = the same tree without the diff, same config.
Each How-To-Repeat case run three times on each kernel:
unpatched: case 1 3/3 "Data modified on freelist: word 4 ... size
0x110 previous type devbuf" from malloc() in exec;
cases 2, 3 and 4 3/3 each 'kernel diagnostic assertion
"SMR_PTR_GET_LOCKED(&ac0->ac_trport) == &tp->tp_ether_port"
failed: file ".../if_trunk.c", line 419' in
trunk_port_destroy() (from the member detach hook in
case 2, from trunk_clone_destroy() in cases 3 and 4).
patched: 12/12 no panic, assertion or WITNESS output; "trunkproto
lacp" fails with "SIOCSTRUNK: Protocol not supported"
and leaves the trunk as it was. In each run a second
trunk with two ports was switched loadbalance ->
roundrobin -> failover -> broadcast -> none ->
loadbalance (both ports kept), then lacp (refused, still
loadbalance with both ports), and torn down cleanly.