From nobody Wed Sep 04 08:53:34 2024 X-Original-To: dev-commits-src-branches@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 4WzGVk6fJ5z5TSWL; Wed, 04 Sep 2024 08:53:34 +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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4WzGVk5FBVz4XSD; Wed, 4 Sep 2024 08:53:34 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1725440014; 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=3uxULkJCBLo1WOuGZ0+TAukEhFYnPAf7/7JhuE57gzs=; b=MCO4xISXsJ1NhpuJmZV0f4YhyTro14XXuNBTKWl0A/MElm/jsMf2rODij9rAioR3Nv0L5R ecMrsciNNGg3V7isHMt5vhHCgJBiChiaR8lOHfyGqX5bLOkK6qdrKGr1KV8KCUsPpWEVp1 JYo5THjc6eO1E09n0YpFehTb3XVvb8H2JLKbOYibmeNlY7Q7QkUd1QfdhjipdhGejeRyRq 2wuLU+6F+II/TMFv1GivBBUyCKJB+mYPMA0vmw10vf8jL/9G+ZR95Fb1wFlpOan5toGSVe 55lEo9/oompdy1wQu23yO/XuFx41gSXM4HElmhthfTzWvMAcuP7yqQpJqs94ZQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1725440014; a=rsa-sha256; cv=none; b=qPmAvHyn6NC1gyIYzr6/goZtYx+gRPeZKNLzbAg0RCwAcUZT4i50aeqh/mczhqgBNWoya6 zSTEC+8pIhjXaw56xca/KSiy7WtuBMGr9lDLoOaWjuEEo1i8WCYVKptPW6mscJaz5Fm/fv JcztScTAc5GULUKgzHXCeiGr7pehPU6g54QiFL8p7qwuKcdV0cbBmCHpF0XcuquArebN+4 DMDqJIgD5pGlBhHZNvd/nPE91cjQ+SLFbC4bZtc7Kc6YysbN60yKL1mXUzOAfEBKueG2Im Y/iuSokdFjYh7dtevnVNj6rzoii9yCIYkWyf2i9VRAqon0MtoJAdd1YzMSxrJw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1725440014; 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=3uxULkJCBLo1WOuGZ0+TAukEhFYnPAf7/7JhuE57gzs=; b=HyGx4LenE4QMq+Ea1ZLH0RHX1m9a/FAnTnatDP95zFm2pVLP0xagy0B0zacbQ37l+BWa3Y rIxtMfh9vPJHATV6ZEXDJctVbAkVsBx7QKUzcjx6FnbfVyTk2KS38OJ26bAVJt1O/uejvw 94DyEIq8OoO3uZmx0BDsZuPkPzERoVWvXhyRtv95iUI65OpE4BgT8pK5mDB6USlpWCS5sH WqKjlHVwJdsUqJ/HAY70VDY3Sky7EQvQX96sJ/e8AbVo7sORDPtXAl9H3mvYRbyOB3J0tq +meadbRq8BzxYpQC3BpZ9hazcVs0z+At87Hdudb7HCp7fSsxUWA3aC5DZuqKIA== 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 4WzGVk4qlvzHrh; Wed, 4 Sep 2024 08:53:34 +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 4848rYPC005913; Wed, 4 Sep 2024 08:53:34 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4848rYbe005910; Wed, 4 Sep 2024 08:53:34 GMT (envelope-from git) Date: Wed, 4 Sep 2024 08:53:34 GMT Message-Id: <202409040853.4848rYbe005910@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kristof Provost Subject: git: d6e5f8643d37 - stable/13 - pf: rework pf_icmp_state_lookup() failure mode List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@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/stable/13 X-Git-Reftype: branch X-Git-Commit: d6e5f8643d37e925aa51fc8224cfc05aba0813f7 Auto-Submitted: auto-generated The branch stable/13 has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=d6e5f8643d37e925aa51fc8224cfc05aba0813f7 commit d6e5f8643d37e925aa51fc8224cfc05aba0813f7 Author: Kristof Provost AuthorDate: 2024-08-30 11:36:39 +0000 Commit: Kristof Provost CommitDate: 2024-09-04 08:53:07 +0000 pf: rework pf_icmp_state_lookup() failure mode If pf_icmp_state_lookup() finds a state but rejects it for not matching the expected direction we should unlock the state (and NULL out *state). This simplifies life for callers, and also ensures there's no confusion about what a non-NULL returned state means. Previously it could have been left in there by the caller, resulting in callers unlocking the same state twice. MFC after: 1 week Sponsored by: Rubicon Communications, LLC ("Netgate") (cherry picked from commit 0578fe492284ded4745167060be794032e6e22f0) --- sys/net/pfvar.h | 4 ++-- sys/netpfil/pf/pf.c | 20 +++++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/sys/net/pfvar.h b/sys/net/pfvar.h index e6c2d1f290cf..66bdbf43e212 100644 --- a/sys/net/pfvar.h +++ b/sys/net/pfvar.h @@ -330,8 +330,8 @@ struct pfi_dynaddr { mtx_unlock(_s->lock); \ } while (0) #else -#define PF_STATE_LOCK(s) mtx_lock(s->lock) -#define PF_STATE_UNLOCK(s) mtx_unlock(s->lock) +#define PF_STATE_LOCK(s) mtx_lock((s)->lock) +#define PF_STATE_UNLOCK(s) mtx_unlock((s)->lock) #endif #ifdef INVARIANTS diff --git a/sys/netpfil/pf/pf.c b/sys/netpfil/pf/pf.c index db7d8659baec..b95ef59a3250 100644 --- a/sys/netpfil/pf/pf.c +++ b/sys/netpfil/pf/pf.c @@ -6112,6 +6112,8 @@ pf_icmp_state_lookup(struct pf_state_key_cmp *key, struct pf_pdesc *pd, pf_print_state(*state); printf("\n"); } + PF_STATE_UNLOCK(*state); + *state = NULL; return (PF_DROP); } return (-1); @@ -6164,15 +6166,16 @@ pf_test_state_icmp(struct pf_kstate **state, int direction, struct pfi_kkif *kif kif, virtual_id, virtual_type, icmp_dir, &iidx, PF_ICMP_MULTI_NONE, 0); if (ret >= 0) { + MPASS(*state == NULL); if (ret == PF_DROP && pd->af == AF_INET6 && icmp_dir == PF_OUT) { - if (*state != NULL) - PF_STATE_UNLOCK((*state)); ret = pf_icmp_state_lookup(&key, pd, state, m, off, pd->dir, kif, virtual_id, virtual_type, icmp_dir, &iidx, multi, 0); - if (ret >= 0) + if (ret >= 0) { + MPASS(*state == NULL); return (ret); + } } else return (ret); } @@ -6579,8 +6582,10 @@ pf_test_state_icmp(struct pf_kstate **state, int direction, struct pfi_kkif *kif ret = pf_icmp_state_lookup(&key, &pd2, state, m, off, pd2.dir, kif, virtual_id, virtual_type, icmp_dir, &iidx, PF_ICMP_MULTI_NONE, 1); - if (ret >= 0) + if (ret >= 0) { + MPASS(*state == NULL); return (ret); + } /* translate source/destination address, if necessary */ if ((*state)->key[PF_SK_WIRE] != @@ -6635,16 +6640,17 @@ pf_test_state_icmp(struct pf_kstate **state, int direction, struct pfi_kkif *kif pd->dir, kif, virtual_id, virtual_type, icmp_dir, &iidx, PF_ICMP_MULTI_NONE, 1); if (ret >= 0) { + MPASS(*state == NULL); if (ret == PF_DROP && pd2.af == AF_INET6 && icmp_dir == PF_OUT) { - if (*state != NULL) - PF_STATE_UNLOCK((*state)); ret = pf_icmp_state_lookup(&key, &pd2, state, m, off, pd->dir, kif, virtual_id, virtual_type, icmp_dir, &iidx, multi, 1); - if (ret >= 0) + if (ret >= 0) { + MPASS(*state == NULL); return (ret); + } } else return (ret); }