git: 8efd2acf07bc - main - pf: improve pf_state_key_attach() error handling
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Mon, 31 Mar 2025 14:57:57 UTC
The branch main has been updated by kp:
URL: https://cgit.FreeBSD.org/src/commit/?id=8efd2acf07bc0e1c3ea1f7390e0f1cfb7cf6f86c
commit 8efd2acf07bc0e1c3ea1f7390e0f1cfb7cf6f86c
Author: Kristof Provost <kp@FreeBSD.org>
AuthorDate: 2025-03-27 14:21:41 +0000
Commit: Kristof Provost <kp@FreeBSD.org>
CommitDate: 2025-03-31 12:55:42 +0000
pf: improve pf_state_key_attach() error handling
If we fail to attach the stack key that means we've already attached the wire
key. That means the state could be found by other cores, and given that we then
free it, be used after free.
Fix this by not releasing the ID hashrow lock and key locks until after we've
removed the inserted key again, ensuring the state cannot be found by other
cores.
Reported by: markj
Submitted by: glebius
Reviewed by: glebius, markj
MFC after: 3 weeks
Sponsored by: Rubicon Communications, LLC ("Netgate")
Differential Revision: https://reviews.freebsd.org/D49550
---
sys/netpfil/pf/pf.c | 25 +++++++++++++++++++------
1 file changed, 19 insertions(+), 6 deletions(-)
diff --git a/sys/netpfil/pf/pf.c b/sys/netpfil/pf/pf.c
index ef86d70db760..ae1ad679d951 100644
--- a/sys/netpfil/pf/pf.c
+++ b/sys/netpfil/pf/pf.c
@@ -1523,15 +1523,28 @@ keyattach:
printf("\n");
}
s->timeout = PFTM_UNLINKED;
+ if (idx == PF_SK_STACK)
+ /*
+ * Remove the wire key from
+ * the hash. Other threads
+ * can't be referencing it
+ * because we still hold the
+ * hash lock.
+ */
+ pf_state_key_detach(s,
+ PF_SK_WIRE);
PF_HASHROW_UNLOCK(ih);
KEYS_UNLOCK();
- if (idx == PF_SK_WIRE) {
+ if (idx == PF_SK_WIRE)
+ /*
+ * We've not inserted either key.
+ * Free both.
+ */
uma_zfree(V_pf_state_key_z, skw);
- if (skw != sks)
- uma_zfree(V_pf_state_key_z, sks);
- } else {
- pf_detach_state(s);
- }
+ if (skw != sks)
+ uma_zfree(
+ V_pf_state_key_z,
+ sks);
return (EEXIST); /* collision! */
}
}