From nobody Mon Dec 16 22:34:12 2024 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 4YBvr43zCHz5gcgt; Mon, 16 Dec 2024 22:34: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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4YBvr43C7jz47DF; Mon, 16 Dec 2024 22:34:12 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1734388452; 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=mSjGo42f8IpMLyXlLGcBU2t/7zAlc4Vya+/m1RF7o5Q=; b=e84twkhZnATHQR1I1n3A+Nx5fYLOUI6d8qMszknVifuCyMe5TIEj+KEX7GvnNvYU2pDxMK ekYovWB0RV48ez3NzjfoqhvP7NAApyO8u29RZOnGkP/ebExtjkx1shbYBLcBfbm0FmbnHP U/OOqhpSmAowFoTFeHG0ZmyPL9buHqQ/4Pzw4GPQ50b8J7hW+OAVjX7y3vAJOK0TCYNwpb rk8bGZIoFR8rpQqvWBjZcYypeqiBzdnl6MzJg8r0196YM0bcocCaiPcw5S7Rs6KddMrIYM 0GIyVPChOuH4nEbDSBE92SC5I8Z6Aqq0tJ4J4q1DqoSSC7jHnVMQ/c7F00xTcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1734388452; 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=mSjGo42f8IpMLyXlLGcBU2t/7zAlc4Vya+/m1RF7o5Q=; b=kyZauBTdoxwT1NRuxTAUeCEiC0r+4PUolFaTCfAYysPNgrzKtOM0eQ4S/n2u952hMVqjaW yHArlsLGD0KJhLmckWNp+NkXHdG0jPTm7Igj2YZ8aZ/7X/nQVGd/2vtVrtqRmwGy/CUUsR rg8GRWTcwD0eD9N5lKoHk306kkIO+Z56Vs8L7bjL76sc1CuXholQ0cRxTT2hrT4vhppWKu 3z3TyNOzrnxD8dcH8/Lvvkfd1NfQrOQ2/cm2n6hkN8WXws6q3jkE01yvRBRmEinnQJh4O+ 5vmUMIgEu8kQbleJX/hAgx1Z+XE+pic8HypeFf9/sjSc6XE+BySAkCaKySfC0Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1734388452; a=rsa-sha256; cv=none; b=NPr8uQ9+Pw5I+ywPwWHiOWbmeBo1uUOuEnrvaTVR69ctwFmol2x2O61JV33M1jx4xJg9JQ QnZ0opnrGC02DIz50LxqUWD/nB1RcyBG9qQsI2F9m3mZbGllAs9luSCwt8+wvVV2kEtBdx 8a6UkGqzrfZXNa31uMxMLZA7PhnEiaM+55bVI+rIxD8VEyqULFyhSyJKqK43qNb04lm5U9 XzP12F0IKZm1gin5FkS2fdL04aO9bVFxytPMk52S6c3zY6pCrXN4H0bmCK/eRunttE1mXl 5Iul12Y6LKK4e/zliD98pYkhAWFk5MakQ7GCIvEEhrhsNHq065WNSrwjYbq/Gw== 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 4YBvr42pSqz1C36; Mon, 16 Dec 2024 22:34: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 4BGMYCQv031617; Mon, 16 Dec 2024 22:34:12 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4BGMYCGF031614; Mon, 16 Dec 2024 22:34:12 GMT (envelope-from git) Date: Mon, 16 Dec 2024 22:34:12 GMT Message-Id: <202412162234.4BGMYCGF031614@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kristof Provost Subject: git: 358c5f5c0899 - main - pf: fix cleanup deadlock 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: kp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 358c5f5c0899339a005ef2c68ef166601bb9dca9 Auto-Submitted: auto-generated The branch main has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=358c5f5c0899339a005ef2c68ef166601bb9dca9 commit 358c5f5c0899339a005ef2c68ef166601bb9dca9 Author: Kristof Provost AuthorDate: 2024-12-10 14:02:47 +0000 Commit: Kristof Provost CommitDate: 2024-12-16 22:33:55 +0000 pf: fix cleanup deadlock We can get to pfi_kkif_remove_if_unref() via at least two distinct paths: - when the struct ifnet is removed, via pfi_detach_ifnet_event() - when a rule referencing us is removed, via pfi_kkif_unref(). These two events can race against each other, leading us to free this kif twice. That leads to loop in V_pfi_unlinked_kifs, and an eventual deadlock. Avoid this by making sure we only ever insert the kif into V_pfi_unlinked_kifs once. If we don't find it in V_pfi_ifs it's already been removed. Check that it exists in V_pfi_unlinked_kifs (for INVARIANTS). Sponsored by: Rubicon Communications, LLC ("Netgate") Differential Revision: https://reviews.freebsd.org/D48082 --- sys/netpfil/pf/pf_if.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/sys/netpfil/pf/pf_if.c b/sys/netpfil/pf/pf_if.c index 650a7e4db799..d2b1b6a781f4 100644 --- a/sys/netpfil/pf/pf_if.c +++ b/sys/netpfil/pf/pf_if.c @@ -274,6 +274,13 @@ pf_kkif_free(struct pfi_kkif *kif) if (! kif) return; +#ifdef INVARIANTS + if (kif->pfik_ifp) { + struct ifnet *ifp = kif->pfik_ifp; + MPASS(ifp->if_pf_kif == NULL || ifp->if_pf_kif == kif); + } +#endif + #ifdef PF_WANT_32_TO_64_COUNTER wowned = PF_RULES_WOWNED(); if (!wowned) @@ -378,6 +385,35 @@ pfi_kkif_remove_if_unref(struct pfi_kkif *kif) kif == V_pfi_all || kif->pfik_flags != 0) return; + /* + * We can get here in at least two distinct paths: + * - when the struct ifnet is removed, via pfi_detach_ifnet_event() + * - when a rule referencing us is removed, via pfi_kkif_unref(). + * These two events can race against each other, leading us to free this kif + * twice. That leads to a loop in V_pfi_unlinked_kifs, and an eventual + * deadlock. + * + * Avoid this by making sure we only ever insert the kif into + * V_pfi_unlinked_kifs once. + * If we don't find it in V_pfi_ifs it's already been removed. Check that it + * exists in V_pfi_unlinked_kifs. + */ + if (! RB_FIND(pfi_ifhead, &V_pfi_ifs, kif)) { +#ifdef INVARIANTS + struct pfi_kkif *tmp; + bool found = false; + mtx_lock(&pfi_unlnkdkifs_mtx); + LIST_FOREACH(tmp, &V_pfi_unlinked_kifs, pfik_list) { + if (tmp == kif) { + found = true; + break; + } + } + mtx_unlock(&pfi_unlnkdkifs_mtx); + MPASS(found); +#endif + return; + } RB_REMOVE(pfi_ifhead, &V_pfi_ifs, kif); kif->pfik_flags |= PFI_IFLAG_REFS;