From owner-freebsd-bugs@freebsd.org Fri Sep 16 19:18:06 2016 Return-Path: Delivered-To: freebsd-bugs@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 31E38BDDDF8 for ; Fri, 16 Sep 2016 19:18:06 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from kenobi.freebsd.org (kenobi.freebsd.org [IPv6:2001:1900:2254:206a::16:76]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 21A76855 for ; Fri, 16 Sep 2016 19:18:06 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from bugs.freebsd.org ([127.0.1.118]) by kenobi.freebsd.org (8.15.2/8.15.2) with ESMTP id u8GJI5Mt008727 for ; Fri, 16 Sep 2016 19:18:05 GMT (envelope-from bugzilla-noreply@freebsd.org) From: bugzilla-noreply@freebsd.org To: freebsd-bugs@FreeBSD.org Subject: [Bug 212749] bridge fragment can leak mbufs Date: Fri, 16 Sep 2016 19:18:06 +0000 X-Bugzilla-Reason: AssignedTo X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: Base System X-Bugzilla-Component: kern X-Bugzilla-Version: CURRENT X-Bugzilla-Keywords: X-Bugzilla-Severity: Affects Only Me X-Bugzilla-Who: fodillemlinkarim@gmail.com X-Bugzilla-Status: New X-Bugzilla-Resolution: X-Bugzilla-Priority: --- X-Bugzilla-Assigned-To: freebsd-bugs@FreeBSD.org X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version rep_platform op_sys bug_status bug_severity priority component assigned_to reporter attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: https://bugs.freebsd.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 16 Sep 2016 19:18:06 -0000 https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D212749 Bug ID: 212749 Summary: bridge fragment can leak mbufs Product: Base System Version: CURRENT Hardware: Any OS: Any Status: New Severity: Affects Only Me Priority: --- Component: kern Assignee: freebsd-bugs@FreeBSD.org Reporter: fodillemlinkarim@gmail.com Created attachment 174858 --> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=3D174858&action= =3Dedit patch to the bridge code that solves the problem Hello, It has come to my attention that when bridge_fragment() has to fragment an = IP packet it can cause mbuf leaks if, for example m_pullup() ends up allocatin= g a new buffer at the front of the mbuf chain or if the calls to M_PREPEND would fail due to the mbuf pool being depleted. One can easily convince himself that something is wrong by observing that, = in bridge_pfil(), bridge_fragment() gets passed an mbuf pointer and if the cal= l to m_pullup() in bridge_fragment ends up allocating a new mbuf at the front of= the chain, then whatever happens next isn't going to give bridge_pfil() the upd= ated mbuf pointer. You can see below this is why I had to change the arguments to bridge_fragment to take a double pointer to the mbuf. The problem actually goes a lot deeper since, after having called ip_fragment(), bridge_fragment() simply goes over the list of packets it got and carelessly calls into M_PREPEND() to make space for the ethernet header. Now this isn't a problem if M_PREPEND() can find the space at the beginning= of the mbuf but will wreak havoc if it must take the least used code path and prepend an mbuf to the chain. In this case we have m0 pointing to a newly allocated mbuf but the list (walked through m_nextpkt) will never get updat= ed to point to m0 (the previous mbuf's m_nextpkt pointer should now point to t= he new m0). And it goes on, the error case isn't' handled properly since if MPREPEND deleted m0 we not have a list of packets that is potentially pointed to an already freed mbuf. Nowhere can we see that m_nextpkt is updated to take ca= re of that case. This is why in my patch below I detach each m0 from the packet list so if a failure occurs in M_PREPEND of if a new mbuf is added at the beginning of the chain I can keep the list of packets updated. Same thing with the goto out; at the end. If we lost a fragment while adding ethernet header to it we must free the entire chain of packets or we will l= eak mbufs. There is no point in sending fragments if we aren't going to send th= em all. To convince yourself of this problem, one can simple create, on his favorite bridge one 'reass' rule in ipfw. I use the following rule: 00080 253 27878 reass ip from any to any { proto udp or proto gre } Then send (assuming your MTU is 1500), a big udp packet using iperf. I use = this command: iperf -c 10.10.73.2 -u -b16Mb -t 240 -i 10 -l 5000 After iperf has finished, look at your mbuf count with netstat -m. I have made a patch to fix this problem (attached). The patch was tested un= der load when we actually do run out of mbuf to trigger the error cases as well= as with all sorts of number of fragments (varying the initial packet size). I see this seems to affect all modern versions of FreeBSD, please feel free= to test or contest. I would like to see this added to FreeBSD eventually in one shape or another. Best regards, Karim. --=20 You are receiving this mail because: You are the assignee for the bug.=