🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: muc.lists.freebsd.bugs
Article #733

[Bug 301093] wg(4): wg_timers_disable() fails to drain peer callouts

#733
From: bugzilla-noreply
Date: Tue, 24 Nov 2026 22:03
108 lines
6410 bytes
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=301093

            Bug ID: 301093
           Summary: wg(4): wg_timers_disable() fails to drain peer
                    callouts
           Product: Base System
           Version: 16.0-CURRENT
          Hardware: Any
                OS: Any
            Status: New
          Severity: Affects Only Me
          Priority: ---
         Component: kern
          Assignee: bugs@FreeBSD.org
          Reporter: laamalif@proton.me

wg_timers_disable() calls callout_stop() on five wg(4) peer callouts
and can return while a handler is still using the peer.
NET_EPOCH_WAIT() does not cover a handler that has started, and every
handler uses the peer before or without an epoch section. The peer is
later freed by an epoch callback. Stock 15.1 showed wg_peer_destroy()
overlapping a running handler without an uninstrumented access after free.

At main 72d7d152c2ff, sys/dev/wg/if_wg.c lines 1045-1049 are:

        callout_stop(&peer->p_new_handshake);
        callout_stop(&peer->p_send_keepalive);
        callout_stop(&peer->p_retry_handshake);
        callout_stop(&peer->p_persistent_keepalive);
        callout_stop(&peer->p_zero_key_material);

Import 744bfb213144 contains the same stops; the functions used ck_pr_*
then and use atomic_* now.
The callouts are MPSAFE, so c_lock is NULL and the argument is
the raw peer. The last noise_remote_put() schedules
noise_remote_smr_free(), which calls wg_peer_free_deferred()
(if_wg.c:447) and then frees the remote.

On 15.1-RELEASE-p7 GENERIC amd64
(releng/15.1-n283696-bb03631fe3b7), DTrace matched both pointer and peer ID;
pointer-only matching produced 3793 false hits. In 4096- and 3072-destroy
runs, it recorded five wg_peer_destroy() entries while the
corresponding retry handler was running. Four measured tails were 56.1,
7.7, 60.6, and 14.3 microseconds. The free landed after
the handler. The host remained up through those 7168 destroys and the
other counted remove runs of 256, 384, 400, and 768.

wg_busy adds peers with persistent-keepalive 1 and removes them
at 5167 ms. The interface must already be up, with a private
key; pubs.bin holds raw 32-byte keys. On
16.0-CURRENT GENERIC-KASAN amd64 (main-6edecf52def6-dirty),
the same remove with no added delay completed:

    $ cc -Wall -Wextra -Werror -o wg_busy wg_busy.c -lnv
    $ cpuset -l 7 ./wg_busy wg0 pubs.bin 32 1 5167
    round=0 remove_off_ms=5167.0
    round=0 remove_done_ms=5167.9 ioctl_ms=0.9
    DONE

chill_fault.d holds noise_create_initiation() for 50 ms only when the retry
handler calls it. This widens the observed overlap enough for the deferred
free to finish before the handler's next access. In another terminal:

    # dtrace -w -s chill_fault.d

With dtrace running, round 0 completed and round 1 panicked.
The SSH connection terminated before the probe printed.
/var/crash/vmcore.3:

    panic: ASan: Invalid access, 8-byte read at 0xfffffe015d8eb540,
UMAUseAfterFree(fd)
    cpuid = 0
    noise_create_initiation() at noise_create_initiation+0x138
    wg_timers_run_send_initiation() at wg_timers_run_send_initiation+0x11d
    wg_timers_run_retry_handshake() at wg_timers_run_retry_handshake+0x1a3
    softclock_call_cc() at softclock_call_cc+0x33c

The faulting load was r->r_local from the freed noise_remote.
As a control, chill_verify.d delays the return after the peer accesses;
the free landed during that delay without fault.

The patch replaces the five stops with callout_drain(). Built as
if_wg.ko (sha256
db98411e38b9ba5a7d3ed5b14b549aeddf834cf4c179ad546308d0444ff94333)
against that KASAN kernel, the same 50 ms delay ran four rounds
with no panic. The remove ioctls took 55.6, 213.8, 101.0, and
243.9 ms, and all 128 frees ran after the handler returned. No handler
takes sc_lock, which wg_peer_destroy holds across the disable.

Peer removal requires local privilege. A configured peer can
schedule a timer but cannot remove itself.
On 15.1-RELEASE-p7 a root in a vnet jail created wg5, set
persistent-keepalive 1, sent 296 bytes, and removed the peer.
The remove ran in the host kernel and completed without a jail escape
or a host panic.

The patch was exercised as the if_wg module on the CURRENT KASAN
kernel only; 15.1-RELEASE, 14.4, arm64, and a full kernel build
are untested.

-- 
You are receiving this mail because:
You are the assignee for the bug.

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Technical Details
Message-ID: <bug-301093-227@https.bugs.freebsd.org/bugzilla/>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.karotte.org!news.muc.de!.POSTED.news.muc.de!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.karotte.org!news.muc.de!.POSTED.news.muc.de!not-for-mail
From: bugzilla-noreply@freebsd.org
Newsgroups: muc.lists.freebsd.bugs
Subject: [Bug 301093] wg(4): wg_timers_disable() fails to drain peer callouts
Date: Tue, 24 Nov 2026 22:03:02 +0000
Organization: Newsgate at muc.de e.V.
Approved: news-admin@muc.de
Distribution: world
Message-ID: <bug-301093-227@https.bugs.freebsd.org/bugzilla/>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Info: news.muc.de; posting-host="news.muc.de:193.149.48.2";
	logging-data="61408"; mail-complaints-to="news-admin@muc.de"
X-Original-To: bugs@mlmmj.nyi.freebsd.org
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim;
	t=1795557783;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:mime-version:mime-version:content-type:content-type:
	 content-transfer-encoding:content-transfer-encoding;
	bh=wkzypofU+dI5kzlCWTqJWojnSYZ96bvmJtQeDyL1HbQ=;
	b=xH7tf9NSl2+u0xKf7KlIYLQopxtYInHzkYYtLFb96A4UN74Iv7H8aHAU65AV9rtZmLg8qk
	KV3o9CSdcfQme7MzcXzihB2SgJo2ucL9mV1ehzCED7Is8k+oJyWpUMq6CC6oHM9O1a0EP9
	fHJxiGLN0iMf8CJxhdurVYA50rSoLntNsrKF61Hnbqrx2G/j58Rh4w22se/GPw8s2WooBp
	CkTgilNpWDBtQm+Ib8qns5TyPOrdJoaEWZpDcTfQXM8IEth7FyPaAlcKa6UYlV1+msc6/i
	59kEOb6IY15CLHnkNoj73myuxwHaIu++xjCl5m2qwC6xxkprbXCO1YDGC3ACeQ==
X-Authentication-Warning: kenobi.freebsd.org: www set sender to bugzilla-noreply@freebsd.org using -f
Auto-Submitted: auto-generated
Precedence: list
X-Newsgate-Id: f02c70b8849f+
X-No-Archive: Yes