From nobody Wed Nov 05 00:28:12 2025 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4d1R4X6rgYz6Fs9W; Wed, 05 Nov 2025 00:28:12 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4d1R4X6Gd9z3dck; Wed, 05 Nov 2025 00:28:12 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1762302492; 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=Sbmr1ddeLM30ink1YKJZUzUU4eBP7nRuN7Z+MxJsDs4=; b=nd2sbAUozhddARCSNuxclzY+8AKKsPErTpBxGM+c/Tft/edVM8VcYaRNdsUgWoH3S/db5r UOs82k80Cvt2lHnGg7PjaBOkvvrKYATt49xPsBAQCAZTs4S9DsTeWxNGHDdFEAks1ytWn3 4decIcPweDrU0icj7JOmQvOmefVWpIeYU+yBSgH6aucglE5FEFZGFXRRduSAOsgOOw1IdW Nq1tqXwLTtgYNDD/zXK0he34WntC/EoANlOolP4R3Fj79YTWgQrss8GiBglpoRsh/dz/GA vX7NfKzEoWjt24hzG2WFpRIv2e0fUhQ93MpFqzqrrKu4v44/XvL+MC//rk5u0Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1762302492; 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=Sbmr1ddeLM30ink1YKJZUzUU4eBP7nRuN7Z+MxJsDs4=; b=i7NYLjg6zMsPFvLaVIVR/FispSz1kUfP/KNIiB7fGYjPMDt8cIBF4XYWWDjXMfUkT2rF1h AjectJhhcKmafD7UUycninGAAIZXBHcxJAuubTZg+LHMcZ7akCDN05Xn1/IFs5SWCxPxhq B71BagFx4yO70GzHmmVMHJrynUG6/lq1/QjC0uNdYVGMIX2QsjiLZtKh6M81WF1mPfqGPH LdfBkeNDaQ1+jpHJm/l7gksuUpBmUbxDia/9fOceMbqNylrLK0DQ7jr2fMJcrkNqaNvidX ApCKvm49TVHgAqJfYrvHVJOJnsOWCtsLDza93RaBtGnf8lFWMe6RQ1ORox4alQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1762302492; a=rsa-sha256; cv=none; b=YCl/bmafEj630hgXTmIyllEXzVsQcriu2gRMfDN/+bmGPUsuhRclwHj+za9CK/ztSReWHk mlrsdj8LSwPAKLpZpkJfXwYHvmOEElFmP3PA1xMnN1wvqKuVmxo7OoGFCapM3t20lJClln 85YHUILj4agUzUr6Z/hY/2crO6prlywx8FV2k16lwUl5UJsAOe/vBtLytV/4RqcZDP5C2h nUYW0PIC3K4WjDAOiK68+9+c4BcKFU/qWmY9sJKergtp058PgZe007hkLNtLPEfIiVmRSV 1xyKqyzYzOBBzxaE+k8EgJFLUToDMR8CcbKEPZdtWxft+AHFiyiYW5+roop5fA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4d1R4X5py0z4pZ; Wed, 05 Nov 2025 00:28:12 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 5A50SCnU017276; Wed, 5 Nov 2025 00:28:12 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 5A50SCDI017273; Wed, 5 Nov 2025 00:28:12 GMT (envelope-from git) Date: Wed, 5 Nov 2025 00:28:12 GMT Message-Id: <202511050028.5A50SCDI017273@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kyle Evans Subject: git: 96c1d8db39df - main - if_tuntap: defer transient destroy_dev() to a taskqueue List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 96c1d8db39dfeea78ea3f27d67649252a39bbf2e Auto-Submitted: auto-generated The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=96c1d8db39dfeea78ea3f27d67649252a39bbf2e commit 96c1d8db39dfeea78ea3f27d67649252a39bbf2e Author: Kyle Evans AuthorDate: 2025-11-05 00:28:02 +0000 Commit: Kyle Evans CommitDate: 2025-11-05 00:28:02 +0000 if_tuntap: defer transient destroy_dev() to a taskqueue We're in the dtor, so we can't destroy it now without deadlocking after recent changes to make destroy_dev() provide a barrier. However, we know there isn't any other dtor to run, so we can go ahead and clean up our state and just prevent a use-after-free if someone races to open the device while we're trying to destroy it. tunopen() now uses the net epoch to protect against softc release by a concurrent tun_destroy(). While we're here, allow a destroy operation to proceed if we caught a signal in cv_wait_sig() but tun_busy dropped to 0 while we were waiting to acquire the lock. This was more of an inherent design flaw, rather than a bug in the below-refed commit. PR: 290575 Fixes: 4dbe6628179d ("devfs: make destroy_dev() a release [...]") Reviewed by: kib, markj Differential Revision: https://reviews.freebsd.org/D53438 --- sys/net/if_tuntap.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 6 deletions(-) diff --git a/sys/net/if_tuntap.c b/sys/net/if_tuntap.c index 56bb90cce9bc..0dc3a58f6ae6 100644 --- a/sys/net/if_tuntap.c +++ b/sys/net/if_tuntap.c @@ -138,6 +138,7 @@ struct tuntap_softc { #define TUN_READY (TUN_OPEN | TUN_INITED) pid_t tun_pid; /* owning pid */ + struct epoch_context tun_epoch_ctx; struct ifnet *tun_ifp; /* the interface */ struct sigio *tun_sigio; /* async I/O info */ struct tuntap_driver *tun_drv; /* appropriate driver */ @@ -630,6 +631,18 @@ out: CURVNET_RESTORE(); } +static void +tunfree(struct epoch_context *ctx) +{ + struct tuntap_softc *tp; + + tp = __containerof(ctx, struct tuntap_softc, tun_epoch_ctx); + + /* Any remaining resources that would be needed by a concurrent open. */ + mtx_destroy(&tp->tun_mtx); + free(tp, M_TUN); +} + static int tun_destroy(struct tuntap_softc *tp, bool may_intr) { @@ -649,7 +662,7 @@ tun_destroy(struct tuntap_softc *tp, bool may_intr) error = cv_wait_sig(&tp->tun_cv, &tp->tun_mtx); else cv_wait(&tp->tun_cv, &tp->tun_mtx); - if (error != 0) { + if (error != 0 && tp->tun_busy != 0) { tp->tun_flags &= ~TUN_DYING; TUN_UNLOCK(tp); return (error); @@ -663,8 +676,18 @@ tun_destroy(struct tuntap_softc *tp, bool may_intr) TAILQ_REMOVE(&tunhead, tp, tun_list); mtx_unlock(&tunmtx); - /* destroy_dev will take care of any alias. */ - destroy_dev(tp->tun_dev); + /* + * destroy_dev will take care of any alias. For transient tunnels, + * we're being called from close(2) so we can't destroy it ourselves + * without deadlocking, but we already know that we can cleanup + * everything else and just continue to prevent it from being reopened. + */ + if ((tp->tun_flags & TUN_TRANSIENT) != 0) { + atomic_store_ptr(&tp->tun_dev->si_drv1, tp->tun_dev); + destroy_dev_sched(tp->tun_dev); + } else { + destroy_dev(tp->tun_dev); + } seldrain(&tp->tun_rsel); knlist_clear(&tp->tun_rsel.si_note, 0); knlist_destroy(&tp->tun_rsel.si_note); @@ -679,9 +702,8 @@ tun_destroy(struct tuntap_softc *tp, bool may_intr) sx_xunlock(&tun_ioctl_sx); free_unr(tp->tun_drv->unrhdr, TUN2IFP(tp)->if_dunit); if_free(TUN2IFP(tp)); - mtx_destroy(&tp->tun_mtx); cv_destroy(&tp->tun_cv); - free(tp, M_TUN); + NET_EPOCH_CALL(tunfree, &tp->tun_epoch_ctx); CURVNET_RESTORE(); return (0); @@ -742,9 +764,11 @@ tun_uninit(const void *unused __unused) mtx_unlock(&tunmtx); for (i = 0; i < nitems(tuntap_drivers); ++i) { drv = &tuntap_drivers[i]; + destroy_dev_drain(&drv->cdevsw); delete_unrhdr(drv->unrhdr); clone_cleanup(&drv->clones); } + NET_EPOCH_DRAIN_CALLBACKS(); mtx_destroy(&tunmtx); } SYSUNINIT(tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, tun_uninit, NULL); @@ -1104,19 +1128,43 @@ out: static int tunopen(struct cdev *dev, int flag, int mode, struct thread *td) { + struct epoch_tracker et; struct ifnet *ifp; struct tuntap_softc *tp; + void *p; int error __diagused, tunflags; + /* + * Transient tunnels do deferred destroy of the tun device but want + * to immediately cleanup state, so they clobber si_drv1 to avoid a + * use-after-free in case someone does happen to open it in the interim. + * We avoid using NULL to be able to distinguish from an uninitialized + * cdev. + * + * We use the net epoch here to let a concurrent tun_destroy() schedule + * freeing our tuntap_softc, in case we entered here and loaded si_drv1 + * before it was swapped out. If we managed to load this while it was + * still a softc, then the concurrent tun_destroy() hasn't yet scheduled + * it to be free- that will take place sometime after the epoch we just + * entered, so we can safely use it. + */ + NET_EPOCH_ENTER(et); + p = atomic_load_ptr(&dev->si_drv1); + if (p == dev) { + NET_EPOCH_EXIT(et); + return (ENXIO); + } + tunflags = 0; CURVNET_SET(TD_TO_VNET(td)); error = tuntap_name2info(dev->si_name, NULL, &tunflags); if (error != 0) { CURVNET_RESTORE(); + NET_EPOCH_EXIT(et); return (error); /* Shouldn't happen */ } - tp = dev->si_drv1; + tp = p; KASSERT(tp != NULL, ("si_drv1 should have been initialized at creation")); @@ -1124,14 +1172,17 @@ tunopen(struct cdev *dev, int flag, int mode, struct thread *td) if ((tp->tun_flags & TUN_INITED) == 0) { TUN_UNLOCK(tp); CURVNET_RESTORE(); + NET_EPOCH_EXIT(et); return (ENXIO); } if ((tp->tun_flags & (TUN_OPEN | TUN_DYING)) != 0) { TUN_UNLOCK(tp); CURVNET_RESTORE(); + NET_EPOCH_EXIT(et); return (EBUSY); } + NET_EPOCH_EXIT(et); error = tun_busy_locked(tp); KASSERT(error == 0, ("Must be able to busy an unopen tunnel")); ifp = TUN2IFP(tp);