git: 3f42750ec157 - main - netipsec: Do not unlock the sahtree twice in ipsec6_common_input_cb()

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

URL: https://cgit.FreeBSD.org/src/commit/?id=3f42750ec157b84477cccb5175e7c7cd5794924a

commit 3f42750ec157b84477cccb5175e7c7cd5794924a
Author:     R. Christian McDonald <rcm@FreeBSD.org>
AuthorDate: 2026-11-26 19:42:03 +0000
Commit:     R. Christian McDonald <rcm@FreeBSD.org>
CommitDate: 2026-11-26 19:42:03 +0000

    netipsec: Do not unlock the sahtree twice in ipsec6_common_input_cb()
    
    For transport mode, ipsec6_common_input_cb() releases the sahtree read
    lock before it walks the remaining header chain.  The error exits of
    that walk jumped to the bad label, which released the lock a second
    time.  A packet that exceeds net.inet6.ip6.hdrnestlimit, is truncated,
    or fails the inbound policy check on its last header is enough to get
    there.
    
    The second rm_runlock() panics kernels with INVARIANTS.  Without
    INVARIANTS it unbalances td_pinned and removes a tracker that is no
    longer queued, which may corrupt the per-CPU tracker list.
    
    Give the header chain walk its own exit label that skips the unlock.
    
    Reported by:            llm-postcr
    Reviewed by:            markj, kib
    Fixes:                  183513d15f22 ("sys/netipsec: ensure sah stability...")
    MFC after:              1 week
    Sponsored by:           Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D62098
---
 sys/netipsec/ipsec_input.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/sys/netipsec/ipsec_input.c b/sys/netipsec/ipsec_input.c
index 43e01a3e9895..9b7f06fe6d49 100644
--- a/sys/netipsec/ipsec_input.c
+++ b/sys/netipsec/ipsec_input.c
@@ -765,7 +765,7 @@ ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
 		if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
 			IP6STAT_INC(ip6s_toomanyhdr);
 			error = EINVAL;
-			goto bad;
+			goto bad_unlocked;
 		}
 
 		/*
@@ -776,7 +776,7 @@ ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
 			IP6STAT_INC(ip6s_tooshort);
 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
 			error = EINVAL;
-			goto bad;
+			goto bad_unlocked;
 		}
 		/*
 		 * Enforce IPsec policy checking if we are seeing last header.
@@ -785,7 +785,7 @@ ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
 		 */
 		if (ipsec6_lasthdr(nxt) && ipsec6_in_reject(m, NULL)) {
 			error = EINVAL;
-			goto bad;
+			goto bad_unlocked;
 		}
 		/*
 		 * AH, ESP and IPComp are not in ip6_protox[]; as in
@@ -804,8 +804,9 @@ ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
 	key_freesav(&sav);
 	return (error);
 bad:
-	NET_EPOCH_EXIT(et);
 	ipsec_sahtree_runlock(sahtree_tracker);
+bad_unlocked:
+	NET_EPOCH_EXIT(et);
 	key_freesav(&sav);
 	if (m)
 		m_freem(m);