From nobody@FreeBSD.org  Thu Apr 15 23:15:56 2010
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 016941065673
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 15 Apr 2010 23:15:56 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id E61178FC13
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 15 Apr 2010 23:15:55 +0000 (UTC)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.3/8.14.3) with ESMTP id o3FNFtAG095953
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 15 Apr 2010 23:15:55 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id o3FNFtCe095952;
	Thu, 15 Apr 2010 23:15:55 GMT
	(envelope-from nobody)
Message-Id: <201004152315.o3FNFtCe095952@www.freebsd.org>
Date: Thu, 15 Apr 2010 23:15:55 GMT
From: Peter Molnar <peter@molnar.cc>
To: freebsd-gnats-submit@FreeBSD.org
Subject: Access to freed mbuf in ip_forward with IPSEC enabled
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         145736
>Category:       kern
>Synopsis:       [netinet] [patch] Access to freed mbuf in ip_forward with IPSEC enabled
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    bz
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Apr 15 23:20:01 UTC 2010
>Closed-Date:    Thu Dec 02 16:29:04 UTC 2010
>Last-Modified:  Thu Dec  2 16:30:11 UTC 2010
>Originator:     Peter Molnar
>Release:        RELENG_7_0_0_RELEASE
>Organization:
>Environment:
>Description:
Test setup:

A FreeBSD machine acts as a router and is connected to a WAN and a LAN.
The traffic routed into the WAN is IPSEC encrypted. The TCP traffic
originating from a machine in the LAN does not fit the MTU on the WAN
link and PMTU discovery comes to action.

Detailed problem description:

 * At some point the routed packet reaches ip_forward (sys/netinet/ip_input.c).
 * The packet is passed in the paramter m to the routine ip_output
   (sys/netinet/ip_output.c).
 * The packet does not fit the MTU, so EMSGSIZE is returned and the
   routine is exited via the label bad.
 * The mbuf m is freed using m_free.
 * Control flow returns to ip_forward.
 * ip_ipsec_mtu is called on the mbuf m.
>How-To-Repeat:

>Fix:


Patch attached with submission follows:

diff --git a/sys/netinet/ip_input.c b/sys/netinet/ip_input.c
index acd1e17..d296299 100644
--- a/sys/netinet/ip_input.c
+++ b/sys/netinet/ip_input.c
@@ -1403,7 +1403,7 @@ ip_forward(struct mbuf *m, int srcrt)
 		code = ICMP_UNREACH_NEEDFRAG;
 
 #ifdef IPSEC
-		mtu = ip_ipsec_mtu(m);
+		mtu = ip_ipsec_mtu(mcopy);
 #endif /* IPSEC */
 		/*
 		 * If the MTU wasn't set before use the interface mtu or


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->freebsd-net 
Responsible-Changed-By: linimon 
Responsible-Changed-When: Sat Apr 17 06:20:25 UTC 2010 
Responsible-Changed-Why:  
Over to maintainer(s). 

http://www.freebsd.org/cgi/query-pr.cgi?pr=145736 
State-Changed-From-To: open->analyzed 
State-Changed-By: bz 
State-Changed-When: Sat Apr 17 15:46:55 UTC 2010 
State-Changed-Why:  
This seems to be the correct conclusion and it seems to be 
still true in HEAD. 


Responsible-Changed-From-To: freebsd-net->bz 
Responsible-Changed-By: bz 
Responsible-Changed-When: Sat Apr 17 15:46:55 UTC 2010 
Responsible-Changed-Why:  
Take. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=145736 
State-Changed-From-To: analyzed->patched 
State-Changed-By: bz 
State-Changed-When: Wed Apr 21 10:22:04 UTC 2010 
State-Changed-Why:  
Change comitted to HEAD with r206989. MFC in three days. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=145736 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/145736: commit references a PR
Date: Wed, 21 Apr 2010 10:21:44 +0000 (UTC)

 Author: bz
 Date: Wed Apr 21 10:21:34 2010
 New Revision: 206989
 URL: http://svn.freebsd.org/changeset/base/206989
 
 Log:
   Avoid memory access after free.  Use the (shortend) copy for the
   ipsec mtu lookup as well.
   
   PR:		kern/145736
   Submitted by:	Peter Molnar (peter molnar.cc)
   MFC after:	3 days
 
 Modified:
   head/sys/netinet/ip_input.c
 
 Modified: head/sys/netinet/ip_input.c
 ==============================================================================
 --- head/sys/netinet/ip_input.c	Wed Apr 21 08:18:06 2010	(r206988)
 +++ head/sys/netinet/ip_input.c	Wed Apr 21 10:21:34 2010	(r206989)
 @@ -1590,7 +1590,7 @@ ip_forward(struct mbuf *m, int srcrt)
  		 * If IPsec is configured for this path,
  		 * override any possibly mtu value set by ip_output.
  		 */ 
 -		mtu = ip_ipsec_mtu(m, mtu);
 +		mtu = ip_ipsec_mtu(mcopy, mtu);
  #endif /* IPSEC */
  		/*
  		 * If the MTU was set before make sure we are below the
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/145736: commit references a PR
Date: Sun,  2 May 2010 15:55:54 +0000 (UTC)

 Author: bz
 Date: Sun May  2 15:55:29 2010
 New Revision: 207512
 URL: http://svn.freebsd.org/changeset/base/207512
 
 Log:
   MFC r206989:
     Avoid memory access after free.  Use the (shortend) copy for the
     ipsec mtu lookup as well.
   
   PR:		kern/145736
   Submitted by:	Peter Molnar (peter molnar.cc)
 
 Modified:
   stable/8/sys/netinet/ip_input.c
 Directory Properties:
   stable/8/sys/   (props changed)
   stable/8/sys/amd64/include/xen/   (props changed)
   stable/8/sys/cddl/contrib/opensolaris/   (props changed)
   stable/8/sys/contrib/dev/acpica/   (props changed)
   stable/8/sys/contrib/pf/   (props changed)
   stable/8/sys/dev/xen/xenpci/   (props changed)
   stable/8/sys/geom/sched/   (props changed)
 
 Modified: stable/8/sys/netinet/ip_input.c
 ==============================================================================
 --- stable/8/sys/netinet/ip_input.c	Sun May  2 14:46:05 2010	(r207511)
 +++ stable/8/sys/netinet/ip_input.c	Sun May  2 15:55:29 2010	(r207512)
 @@ -1590,7 +1590,7 @@ ip_forward(struct mbuf *m, int srcrt)
  		 * If IPsec is configured for this path,
  		 * override any possibly mtu value set by ip_output.
  		 */ 
 -		mtu = ip_ipsec_mtu(m, mtu);
 +		mtu = ip_ipsec_mtu(mcopy, mtu);
  #endif /* IPSEC */
  		/*
  		 * If the MTU was set before make sure we are below the
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: patched->closed 
State-Changed-By: bz 
State-Changed-When: Thu Dec 2 16:28:06 UTC 2010 
State-Changed-Why:  
Merged to stable/7 as well. Sorry I had missed that. It'll 
be part of 7.4-R. 
Thanks a lot for the report and patch! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=145736 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/145736: commit references a PR
Date: Thu,  2 Dec 2010 16:27:36 +0000 (UTC)

 Author: bz
 Date: Thu Dec  2 16:27:28 2010
 New Revision: 216125
 URL: http://svn.freebsd.org/changeset/base/216125
 
 Log:
   MFC r206989:
   
     Avoid memory access after free. Use the (shortend) copy for the
     ipsec mtu lookup as well.
   
     Submitted by:	Peter Molnar (peter molnar.cc)
   PR:		kern/145736
   Approved by:	re (kib)
 
 Modified:
   stable/7/sys/netinet/ip_input.c
 Directory Properties:
   stable/7/sys/   (props changed)
   stable/7/sys/cddl/contrib/opensolaris/   (props changed)
   stable/7/sys/contrib/dev/acpica/   (props changed)
   stable/7/sys/contrib/pf/   (props changed)
 
 Modified: stable/7/sys/netinet/ip_input.c
 ==============================================================================
 --- stable/7/sys/netinet/ip_input.c	Thu Dec  2 15:59:41 2010	(r216124)
 +++ stable/7/sys/netinet/ip_input.c	Thu Dec  2 16:27:28 2010	(r216125)
 @@ -1404,7 +1404,7 @@ ip_forward(struct mbuf *m, int srcrt)
  		 * If IPsec is configured for this path,
  		 * override any possibly mtu value set by ip_output.
  		 */ 
 -		mtu = ip_ipsec_mtu(m, mtu);
 +		mtu = ip_ipsec_mtu(mcopy, mtu);
  #endif /* IPSEC */
  		/*
  		 * If the MTU was set before make sure we are below the
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
>Unformatted:
