git: 53d8733b3dd7 - main - ipsec: Do not reference the default policy for packets without a PCB

From: R. Christian McDonald <rcm_at_FreeBSD.org>
Date: Thu, 12 Nov 2026 12:57:43 UTC
The branch main has been updated by rcm:

URL: https://cgit.FreeBSD.org/src/commit/?id=53d8733b3dd754852ec7a2c22b01c82906b8130e

commit 53d8733b3dd754852ec7a2c22b01c82906b8130e
Author:     R. Christian McDonald <rcm@FreeBSD.org>
AuthorDate: 2026-11-12 12:53:25 +0000
Commit:     R. Christian McDonald <rcm@FreeBSD.org>
CommitDate: 2026-11-12 12:57:21 +0000

    ipsec: Do not reference the default policy for packets without a PCB
    
    When no security policy matches a packet, ipsec[46]_getpolicy() returns
    the default policy with a new reference, and the caller drops it right
    after looking at the policy type.  Once any security policy is
    configured, every forwarded packet goes through ip_forward() and
    ipsec4_forward(), which checks the inbound and the outbound policy.
    That is four atomic operations per packet on one reference count that
    all CPUs write.
    
    The default policy is allocated with its vnet and freed only when the
    vnet goes away, and the sysctl limits it to NONE or DISCARD.  A packet
    without a PCB has no policy cache to keep it in.  In that case, check
    the policy type directly and take no reference, for NONE as well as for
    DISCARD.  Packets with a PCB still take the reference, because the PCB
    caches the policy.
    
    No functional change intended.  DISCARD still drops the packet, and the
    policy violation counters are incremented as before.
    
    Reviewed by:            kib
    MFC after:              1 week
    Sponsored by:           Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D61384
---
 sys/netipsec/ipsec.c | 36 ++++++++++++++++++++++++++++++------
 1 file changed, 30 insertions(+), 6 deletions(-)

diff --git a/sys/netipsec/ipsec.c b/sys/netipsec/ipsec.c
index 12d1b7945e64..d7e72c630f1f 100644
--- a/sys/netipsec/ipsec.c
+++ b/sys/netipsec/ipsec.c
@@ -568,6 +568,13 @@ ipsec4_setspidx_ipaddr(const struct mbuf *m, struct ip *ip1,
 	spidx->prefd = sizeof(struct in_addr) << 3;
 }
 
+/*
+ * Return a held reference to the SP for the packet.  Return NULL when no
+ * SP matches and there is no PCB: the default policy applies then, and
+ * the caller checks its type, NONE or DISCARD.  The default SP lives as
+ * long as its vnet, so that needs no reference, and the reference count
+ * would be one cache line written by every CPU for each forwarded packet.
+ */
 static struct secpolicy *
 ipsec4_getpolicy(const struct mbuf *m, struct inpcb *inp, struct ip *ip1,
     u_int dir, int needport)
@@ -583,7 +590,8 @@ ipsec4_getpolicy(const struct mbuf *m, struct inpcb *inp, struct ip *ip1,
 		spidx.dir = dir;
 		sp = key_allocsp(&spidx, dir);
 	}
-	if (sp == NULL)		/* No SP found, use system default. */
+	/* No SP found, the PCB caches the system default. */
+	if (sp == NULL && inp != NULL)
 		sp = key_allocsp_default();
 	return (sp);
 }
@@ -601,6 +609,8 @@ ipsec4_checkpolicy(const struct mbuf *m, struct inpcb *inp, struct ip *ip1,
 	sp = ipsec4_getpolicy(m, inp, ip1, IPSEC_DIR_OUTBOUND, needport);
 	if (sp != NULL)
 		sp = ipsec_checkpolicy(sp, inp, error);
+	else if (V_def_policy->policy == IPSEC_POLICY_DISCARD)
+		*error = -EINVAL;	/* Packet is discarded by caller. */
 	if (sp == NULL) {
 		switch (*error) {
 		case 0: /* No IPsec required: BYPASS or NONE */
@@ -648,8 +658,11 @@ ipsec4_in_reject1(const struct mbuf *m, struct ip *ip1, struct inpcb *inp)
 	}
 
 	sp = ipsec4_getpolicy(m, inp, ip1, IPSEC_DIR_INBOUND, 0);
-	result = ipsec_in_reject(sp, inp, m);
-	key_freesp(&sp);
+	if (sp != NULL) {
+		result = ipsec_in_reject(sp, inp, m);
+		key_freesp(&sp);
+	} else
+		result = (V_def_policy->policy == IPSEC_POLICY_DISCARD);
 	if (result != 0)
 		IPSECSTAT_INC(ips_in_polvio);
 	return (result);
@@ -757,6 +770,11 @@ ipsec6_setspidx_ipaddr(const struct mbuf *m, struct secpolicyindex *spidx)
 	spidx->prefd = sizeof(struct in6_addr) << 3;
 }
 
+/*
+ * Return a held reference to the SP for the packet.  Return NULL when no
+ * SP matches and there is no PCB: the default policy applies then, and
+ * the caller checks its type without a reference, see ipsec4_getpolicy().
+ */
 static struct secpolicy *
 ipsec6_getpolicy(const struct mbuf *m, struct inpcb *inp, u_int dir,
     int needport)
@@ -772,7 +790,8 @@ ipsec6_getpolicy(const struct mbuf *m, struct inpcb *inp, u_int dir,
 		spidx.dir = dir;
 		sp = key_allocsp(&spidx, dir);
 	}
-	if (sp == NULL)		/* No SP found, use system default. */
+	/* No SP found, the PCB caches the system default. */
+	if (sp == NULL && inp != NULL)
 		sp = key_allocsp_default();
 	return (sp);
 }
@@ -790,6 +809,8 @@ ipsec6_checkpolicy(const struct mbuf *m, struct inpcb *inp, int *error,
 	sp = ipsec6_getpolicy(m, inp, IPSEC_DIR_OUTBOUND, needport);
 	if (sp != NULL)
 		sp = ipsec_checkpolicy(sp, inp, error);
+	else if (V_def_policy->policy == IPSEC_POLICY_DISCARD)
+		*error = -EINVAL;	/* Packet is discarded by caller. */
 	if (sp == NULL) {
 		switch (*error) {
 		case 0: /* No IPsec required: BYPASS or NONE */
@@ -828,8 +849,11 @@ ipsec6_in_reject(const struct mbuf *m, struct inpcb *inp)
 		return (0);
 #endif
 	sp = ipsec6_getpolicy(m, inp, IPSEC_DIR_INBOUND, 0);
-	result = ipsec_in_reject(sp, inp, m);
-	key_freesp(&sp);
+	if (sp != NULL) {
+		result = ipsec_in_reject(sp, inp, m);
+		key_freesp(&sp);
+	} else
+		result = (V_def_policy->policy == IPSEC_POLICY_DISCARD);
 	if (result)
 		IPSEC6STAT_INC(ips_in_polvio);
 	return (result);