From nobody Wed Nov 29 18:04:29 2023 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 4SgRzf20Tlz532bm; Wed, 29 Nov 2023 18:04:30 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4SgRzf1Zqhz4GJv; Wed, 29 Nov 2023 18:04:30 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1701281070; 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=U5KXDZpURdalv+i+jusjKLsedk5OkSOmxcSItYHAGHA=; b=Op597yPgk8s/BWuLK2h6lKHH5RUCtGIBv4Y5LFdjVMN7RlHxpdAY9vApJo31yMqyv/eyUC NuZqouPCi3cvy/dBtg3Ccjhzk7YsvHSoGf2/v4fj56nVekXTsfaTAJALvVxo955o5J0cme 6zwylldKqNkZf/jQuhekOsEiyLt4XGqIhiPvN61qbe8RP58zwvzgDKrMI1nLisaVXr4Dd4 HXp7+/JvmnHmj57tjEZ6VVCOgt4Xz46jIajwEVoeSko6UbttZOkSfoVWWKUEuVOCWCyqbH meE56IP8hdlWfd+YaGvPUrdLn/9enDfW6iJlDSpXo+nRyR2pd9LXRkuoM68Tmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1701281070; 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=U5KXDZpURdalv+i+jusjKLsedk5OkSOmxcSItYHAGHA=; b=ucEPucQeS21RhAxA5ryEJlLzc3i7B/7Gh/kshcsLwVrPPkCPNG/PaEQ0uhZdyBVcOtfj17 5ewXqqTnuKm0WqEaFAhHxDhWhS0RBTUfNSjMT8wTiKHJ91XUxI5L9P7N4Lb+vrSORiBeAx x8DXALm5RwQ33ExHAhw7TjJMglHvkpw069YBpqdjcj+ztHPLqT6s3nZcTIDensvJsm55qp H+nizPUiDV9AVapv22w++I5Aej3NOzJamW0F7tdFBskZ1VJsE6qyNd5hExPPamocppa/+j dwaI3dgPHkTXHmzqEUIOKTvh3CsbSHSQjTGx4b8mTkLbtXIIDjApHfwUu3yYYQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1701281070; a=rsa-sha256; cv=none; b=C1MtNCi1JLvXtR6AKz0qeOF6iGw5kIsv6zx56JL0dYHu7xPoK081Lc/cT1yArROnTkI/C1 YGyLeAOFra6RHnhmLw4dSwWIVxkcVjz5zMNBpGs4+KSvkOSmJqlNSlctkTL1PG84cLPUBF Hk1tBk3AqXStzYoVDmDpKP7E52SKhfSb+EqSS6+aWM2opHI1cyfiFZSBVlEQ7Sdvfn+nhh NjkvYgOnoSyT2NMMP+ngo0jNZyhpCN2fJ6v3xxEJCtKM436XqrQiILNWxPqbLdRGCeJr10 pq1Gs/Vmb+hCsgWj/kyjqW72j0m/dmrdpxkV3lV9XFVCALxYWXcF144jbOraSA== 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 4SgRzf0fxLz23W; Wed, 29 Nov 2023 18:04:30 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3ATI4Tcn098341; Wed, 29 Nov 2023 18:04:30 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3ATI4TmM098338; Wed, 29 Nov 2023 18:04:29 GMT (envelope-from git) Date: Wed, 29 Nov 2023 18:04:29 GMT Message-Id: <202311291804.3ATI4TmM098338@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: 0626d30e41cb - main - pf: fix mem leaks upon vnet destroy 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: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: 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: 0626d30e41cba64b41667314c3a4f7611f0eb685 Auto-Submitted: auto-generated The branch main has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=0626d30e41cba64b41667314c3a4f7611f0eb685 commit 0626d30e41cba64b41667314c3a4f7611f0eb685 Author: Igor Ostapenko AuthorDate: 2023-11-29 12:35:41 +0000 Commit: Kristof Provost CommitDate: 2023-11-29 16:59:28 +0000 pf: fix mem leaks upon vnet destroy Add missing cleanup actions: - remove user defined anchor rulesets - remove user defined ether anchor rulesets - remove tables linked to user defined anchors - deal with wildcard anchor peculiarities to get them removed correctly PR: 274310 Reviewed by: kp MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D42747 --- sys/netpfil/pf/pf_ioctl.c | 67 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 7 deletions(-) diff --git a/sys/netpfil/pf/pf_ioctl.c b/sys/netpfil/pf/pf_ioctl.c index ec55d43d3800..007327b1932d 100644 --- a/sys/netpfil/pf/pf_ioctl.c +++ b/sys/netpfil/pf/pf_ioctl.c @@ -5809,6 +5809,7 @@ pf_clear_tables(void) int error; bzero(&io, sizeof(io)); + io.pfrio_flags |= PFR_FLAG_ALLRSETS; error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel, io.pfrio_flags); @@ -6219,8 +6220,54 @@ shutdown_pf(void) int error = 0; u_int32_t t[5]; char nn = '\0'; + struct pf_kanchor *anchor; + struct pf_keth_anchor *eth_anchor; + int rs_num; do { + /* Unlink rules of all user defined anchors */ + RB_FOREACH(anchor, pf_kanchor_global, &V_pf_anchors) { + /* Wildcard based anchors may not have a respective + * explicit anchor rule or they may be left empty + * without rules. It leads to anchor.refcnt=0, and the + * rest of the logic does not expect it. */ + if (anchor->refcnt == 0) + anchor->refcnt = 1; + for (rs_num = 0; rs_num < PF_RULESET_MAX; ++rs_num) { + if ((error = pf_begin_rules(&t[rs_num], rs_num, + anchor->path)) != 0) { + DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: " + "anchor.path=%s rs_num=%d\n", + anchor->path, rs_num)); + goto error; /* XXX: rollback? */ + } + } + for (rs_num = 0; rs_num < PF_RULESET_MAX; ++rs_num) { + error = pf_commit_rules(t[rs_num], rs_num, + anchor->path); + MPASS(error == 0); + } + } + + /* Unlink rules of all user defined ether anchors */ + RB_FOREACH(eth_anchor, pf_keth_anchor_global, + &V_pf_keth_anchors) { + /* Wildcard based anchors may not have a respective + * explicit anchor rule or they may be left empty + * without rules. It leads to anchor.refcnt=0, and the + * rest of the logic does not expect it. */ + if (eth_anchor->refcnt == 0) + eth_anchor->refcnt = 1; + if ((error = pf_begin_eth(&t[0], eth_anchor->path)) + != 0) { + DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: eth " + "anchor.path=%s\n", eth_anchor->path)); + goto error; + } + error = pf_commit_eth(t[0], eth_anchor->path); + MPASS(error == 0); + } + if ((error = pf_begin_rules(&t[0], PF_RULESET_SCRUB, &nn)) != 0) { DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: SCRUB\n")); @@ -6247,12 +6294,16 @@ shutdown_pf(void) break; /* XXX: rollback? */ } - /* XXX: these should always succeed here */ - pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn); - pf_commit_rules(t[1], PF_RULESET_FILTER, &nn); - pf_commit_rules(t[2], PF_RULESET_NAT, &nn); - pf_commit_rules(t[3], PF_RULESET_BINAT, &nn); - pf_commit_rules(t[4], PF_RULESET_RDR, &nn); + error = pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn); + MPASS(error == 0); + error = pf_commit_rules(t[1], PF_RULESET_FILTER, &nn); + MPASS(error == 0); + error = pf_commit_rules(t[2], PF_RULESET_NAT, &nn); + MPASS(error == 0); + error = pf_commit_rules(t[3], PF_RULESET_BINAT, &nn); + MPASS(error == 0); + error = pf_commit_rules(t[4], PF_RULESET_RDR, &nn); + MPASS(error == 0); if ((error = pf_clear_tables()) != 0) break; @@ -6261,7 +6312,8 @@ shutdown_pf(void) DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: eth\n")); break; } - pf_commit_eth(t[0], &nn); + error = pf_commit_eth(t[0], &nn); + MPASS(error == 0); #ifdef ALTQ if ((error = pf_begin_altq(&t[0])) != 0) { @@ -6279,6 +6331,7 @@ shutdown_pf(void) /* fingerprints and interfaces have their own cleanup code */ } while(0); +error: return (error); }