From nobody Sun Sep 01 15:06:12 2024 X-Original-To: dev-commits-src-main@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 4WxZw5575tz5VLK1; Sun, 01 Sep 2024 15:06:13 +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 4WxZw50SJgz4YTD; Sun, 1 Sep 2024 15:06:13 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1725203173; 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=c8PUBRWtgknrqsMnuifK7zKFm5+DPWkr0Fx9Vqf5JIw=; b=wF+Sjgfy7xbNHjluTBtALJVwzM6zTv7Onlri8uYsWtpXUAqpaMKDuiApLGG4QR+DJkdCZX mapl3zE+hNUUNegSPSEP0P3l5nHge3QD95tMsLZYqbY6Pxtd7hggVF4xdlWgjoH0gfhxjf pWbIqLW1AJZdfVY+3lSQB+B9N/KsLI6O3FBz7nobdzAvGBR/GxuSJfbia1ROdedIadZy2p He4JPaMaSmge7WtZ6gYSmP0IwjFpXTtjWegUl+qsQ6bVYqqUjCHu6Kjf5Ujw20yIVoF2lf JqU03iorCZZtVFbSmmYImvDr77tr4xkklsjxD/3g0ggZQtdSlpMzT0KlPai2EA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1725203173; a=rsa-sha256; cv=none; b=c16q95Z6LrA1asek4FmavNiTe5m5o5yPJzr+3Pd/rEPuL1AjEBzT0Py0H1GG+jP01qBRmI kyGC5j+uG+WEm/patRP4UFTLiceXI6H1ub6yUPoxl2ZeZG7QruBGvACf1Ra9+u/ty9XD9y 4zL3aqzBYlwFYfaUqlh7RCPd2asSqRbdqnEl6Yhwyut0IqUXwYXlfZCWZZRXlu4zMtqdJo E6eD/4QjmMXJoUWJR2ogY8uuGYiz+GSoOy3gTxVDm1ssrwvlrf/lA/XI7DfA2E3jv/eGeM nLzy2Hc+TAj7Wc7/Gsr+sB+jc0g3vA7qYvkCekDnNb+8ypls5esN7EUe3iGerw== 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=1725203173; 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=c8PUBRWtgknrqsMnuifK7zKFm5+DPWkr0Fx9Vqf5JIw=; b=u5SlJUcLNDWhi57zGBMqINWnEZiPkZ5+mBYdVWo9kgfK8AggnnlWZxPETbZLfwGUtboFWb 3vhisu9QUSEVvfmAdOqkT3fH4Ao04PeUvZhgwZNzAKVTdVrglsifJ1Xy3JuXxHIp4tKwdm ocKblHyoCvRPtKrUNL5v1aTX9giVC9gm0bDxuK+j0DGEwCD0W45pVC88kA3FCep5LhOKWP cWGjTt6oJf5yki3YCs8fYROv/3+yx5GGZIKstFM80rtuUkCX8CAF2T0I7hDfl9Zr2s5jQ1 Xakx7VSixwQ/ZYAOIglAoU8Nt7rsLbW/mUjcA1qyXVvtlvzgcBKIB5FFCdOswQ== 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 4WxZw501QBzZ5B; Sun, 1 Sep 2024 15:06:13 +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 481F6CC5002710; Sun, 1 Sep 2024 15:06:12 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 481F6CtG002707; Sun, 1 Sep 2024 15:06:12 GMT (envelope-from git) Date: Sun, 1 Sep 2024 15:06:12 GMT Message-Id: <202409011506.481F6CtG002707@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: 0578fe492284 - main - pf: rework pf_icmp_state_lookup() failure mode List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@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: 0578fe492284ded4745167060be794032e6e22f0 Auto-Submitted: auto-generated The branch main has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=0578fe492284ded4745167060be794032e6e22f0 commit 0578fe492284ded4745167060be794032e6e22f0 Author: Kristof Provost AuthorDate: 2024-08-30 11:36:39 +0000 Commit: Kristof Provost CommitDate: 2024-09-01 15:05:29 +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") --- 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 43d4b908a407..7b3c1c49696a 100644 --- a/sys/net/pfvar.h +++ b/sys/net/pfvar.h @@ -374,8 +374,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 e28bad8750f9..f7fe75184efd 100644 --- a/sys/netpfil/pf/pf.c +++ b/sys/netpfil/pf/pf.c @@ -6754,6 +6754,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); @@ -6806,15 +6808,16 @@ pf_test_state_icmp(struct pf_kstate **state, 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); } @@ -7221,8 +7224,10 @@ pf_test_state_icmp(struct pf_kstate **state, 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] != @@ -7277,16 +7282,17 @@ pf_test_state_icmp(struct pf_kstate **state, 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); }