git: bf2a85f4e304 - stable/13 - Fix a memory leak when ip_output_send() returns EAGAIN due to send tag issues

From: Andrew Gallatin <gallatin_at_FreeBSD.org>
Date: Thu, 10 Feb 2022 20:39:42 UTC
The branch stable/13 has been updated by gallatin:

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

commit bf2a85f4e304aa6223ab007231e71b707320600e
Author:     Andrew Gallatin <gallatin@FreeBSD.org>
AuthorDate: 2022-01-27 15:28:15 +0000
Commit:     Andrew Gallatin <gallatin@FreeBSD.org>
CommitDate: 2022-02-10 20:39:22 +0000

    Fix a memory leak when ip_output_send() returns EAGAIN due to send tag issues
    
    When ip_output_send() returns EAGAIN due to issues with send tags (route
    change, lagg failover, etc), it must free the mbuf. This is because
    ip_output_send() was written as a wrapper/replacement for a direct
    call to  if_output(), and the contract with if_output() has
    historically been that it owns the mbufs once called. When
    ip_output_send() failed to free mbufs, it violated this assumption
    and lead to leaked mbufs.
    
    This was noticed when using NIC TLS in combination with hardware
    rate-limited connections. When seeing lots of NIC output drops
    triggered ratelimit send tag changes, we noticed we were leaking
    ktls_sessions, send tags and mbufs. This was due ip_output_send()
    leaking mbufs which held references to ktls_sessions, which in
    turn held references to send tags.
    
    Many thanks to jbh, rrs, hselasky and markj for their help in
    debugging this.
    
    Sponsored by: Netflix
    Differential Revision: https://reviews.freebsd.org/D34054
    Reviewed by: hselasky, jhb, rrs
    MFC after: 2 weeks
    
    (cherry picked from commit 9ba117960e1755a693f9361e4d076630dfe13dba)
---
 sys/netinet/ip_output.c   | 2 ++
 sys/netinet6/ip6_output.c | 2 ++
 2 files changed, 4 insertions(+)

diff --git a/sys/netinet/ip_output.c b/sys/netinet/ip_output.c
index a011c28441bd..a23a38b08fa8 100644
--- a/sys/netinet/ip_output.c
+++ b/sys/netinet/ip_output.c
@@ -239,6 +239,7 @@ ip_output_send(struct inpcb *inp, struct ifnet *ifp, struct mbuf *m,
 		 * packet.
 		 */
 		if (mst == NULL) {
+			m_freem(m);
 			error = EAGAIN;
 			goto done;
 		}
@@ -263,6 +264,7 @@ ip_output_send(struct inpcb *inp, struct ifnet *ifp, struct mbuf *m,
 		KASSERT(m->m_pkthdr.rcvif == NULL,
 		    ("trying to add a send tag to a forwarded packet"));
 		if (mst->ifp != ifp) {
+			m_freem(m);
 			error = EAGAIN;
 			goto done;
 		}
diff --git a/sys/netinet6/ip6_output.c b/sys/netinet6/ip6_output.c
index 209169dbdabd..de9ed673de79 100644
--- a/sys/netinet6/ip6_output.c
+++ b/sys/netinet6/ip6_output.c
@@ -336,6 +336,7 @@ ip6_output_send(struct inpcb *inp, struct ifnet *ifp, struct ifnet *origifp,
 		 * packet.
 		 */
 		if (mst == NULL) {
+			m_freem(m);
 			error = EAGAIN;
 			goto done;
 		}
@@ -360,6 +361,7 @@ ip6_output_send(struct inpcb *inp, struct ifnet *ifp, struct ifnet *origifp,
 		KASSERT(m->m_pkthdr.rcvif == NULL,
 		    ("trying to add a send tag to a forwarded packet"));
 		if (mst->ifp != ifp) {
+			m_freem(m);
 			error = EAGAIN;
 			goto done;
 		}