git: 5d2e81f29a8a - main - netipsec: Release the SA inside the vnet context in AH and IPComp callbacks

From: R. Christian McDonald <rcm_at_FreeBSD.org>
Date: Tue, 10 Nov 2026 13:41:44 UTC
The branch main has been updated by rcm:

URL: https://cgit.FreeBSD.org/src/commit/?id=5d2e81f29a8aa5b093cd2343c604466332eca2d4

commit 5d2e81f29a8aa5b093cd2343c604466332eca2d4
Author:     R. Christian McDonald <rcm@FreeBSD.org>
AuthorDate: 2026-11-10 13:40:53 +0000
Commit:     R. Christian McDonald <rcm@FreeBSD.org>
CommitDate: 2026-11-10 13:40:53 +0000

    netipsec: Release the SA inside the vnet context in AH and IPComp callbacks
    
    The error paths of ah_input_cb(), ah_output_cb(), ipcomp_input_cb() and
    ipcomp_output_cb() called CURVNET_RESTORE() before key_freesav() and
    key_freesp().  When the callback runs on a crypto return thread, no
    vnet is set after the restore, and key_freesav() panics with "vnet is
    not set" on INVARIANTS kernels.  Without INVARIANTS, the last reference
    would be released without a vnet.
    
    Restore the vnet last, as the ESP callbacks do.
    
    Reviewed by:            mjg
    Fixes:                  8a9269edd71a ("ipsec: sprinkle CURVNET_ASSERT_SET")
    MFC after:              1 week
    Sponsored by:           Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D61386
---
 sys/netipsec/xform_ah.c     | 4 ++--
 sys/netipsec/xform_ipcomp.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/sys/netipsec/xform_ah.c b/sys/netipsec/xform_ah.c
index 06d3b7923409..7b913bfcce08 100644
--- a/sys/netipsec/xform_ah.c
+++ b/sys/netipsec/xform_ah.c
@@ -832,7 +832,6 @@ ah_input_cb(struct cryptop *crp)
 	return error;
 bad:
 	ipsec_sahtree_runlock(&sahtree_tracker);
-	CURVNET_RESTORE();
 	if (sav)
 		key_freesav(&sav);
 	if (m != NULL)
@@ -841,6 +840,7 @@ bad:
 		free(xd, M_AH);
 	if (crp != NULL)
 		crypto_freereq(crp);
+	CURVNET_RESTORE();
 	return error;
 }
 
@@ -1166,11 +1166,11 @@ ah_output_cb(struct cryptop *crp)
 	CURVNET_RESTORE();
 	return (error);
 bad:
-	CURVNET_RESTORE();
 	free(xd, M_AH);
 	crypto_freereq(crp);
 	key_freesav(&sav);
 	key_freesp(&sp);
+	CURVNET_RESTORE();
 	return (error);
 }
 
diff --git a/sys/netipsec/xform_ipcomp.c b/sys/netipsec/xform_ipcomp.c
index 933be3c57424..f7d4f08ffae9 100644
--- a/sys/netipsec/xform_ipcomp.c
+++ b/sys/netipsec/xform_ipcomp.c
@@ -392,7 +392,6 @@ ipcomp_input_cb(struct cryptop *crp)
 	return error;
 bad:
 	ipsec_sahtree_runlock(&sahtree_tracker);
-	CURVNET_RESTORE();
 	if (sav != NULL)
 		key_freesav(&sav);
 	if (m != NULL)
@@ -401,6 +400,7 @@ bad:
 		free(xd, M_IPCOMP);
 	if (crp != NULL)
 		crypto_freereq(crp);
+	CURVNET_RESTORE();
 	return error;
 }
 
@@ -669,11 +669,11 @@ ipcomp_output_cb(struct cryptop *crp)
 bad:
 	if (m)
 		m_freem(m);
-	CURVNET_RESTORE();
 	free(xd, M_IPCOMP);
 	crypto_freereq(crp);
 	key_freesav(&sav);
 	key_freesp(&sp);
+	CURVNET_RESTORE();
 	return (error);
 }