From andrew@fubar.geek.nz  Thu Sep 22 10:07:58 2005
Return-Path: <andrew@fubar.geek.nz>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id DBE1016A43D;
	Thu, 22 Sep 2005 10:07:58 +0000 (GMT)
	(envelope-from andrew@fubar.geek.nz)
Received: from avmta3-rme.xtra.co.nz (avmta3-rme.xtra.co.nz [210.86.15.158])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 0021443D4C;
	Thu, 22 Sep 2005 10:07:57 +0000 (GMT)
	(envelope-from andrew@fubar.geek.nz)
Received: from mta2-rme.xtra.co.nz ([210.86.15.194])
          by avmta3-rme.xtra.co.nz with ESMTP
          id <20050922100756.OUR670.avmta3-rme.xtra.co.nz@mta2-rme.xtra.co.nz>;
          Thu, 22 Sep 2005 22:07:56 +1200
Received: from serv.int.fubar.geek.nz ([210.86.110.41])
          by mta2-rme.xtra.co.nz with ESMTP
          id <20050922100756.CASC1606.mta2-rme.xtra.co.nz@serv.int.fubar.geek.nz>;
          Thu, 22 Sep 2005 22:07:56 +1200
Received: by serv.int.fubar.geek.nz (Postfix, from userid 1001)
	id 9807B6163; Thu, 22 Sep 2005 22:07:54 +1200 (NZST)
Message-Id: <20050922100754.9807B6163@serv.int.fubar.geek.nz>
Date: Thu, 22 Sep 2005 22:07:54 +1200 (NZST)
From: Andrew Turner <andrew@fubar.geek.nz>
Reply-To: Andrew Turner <andrew@fubar.geek.nz>
To: FreeBSD-gnats-submit@freebsd.org
Cc: Tim Kientzle <kientzle@freebsd.org>
Subject: libarchive memory leak with bad fd
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         86453
>Category:       kern
>Synopsis:       [patch] libarchive memory leak with bad fd
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    kientzle
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Sep 22 10:10:11 GMT 2005
>Closed-Date:    Wed Jan 18 04:06:48 GMT 2006
>Last-Modified:  Wed Jan 18 04:06:48 GMT 2006
>Originator:     Andrew Turner
>Release:        FreeBSD 5.4-STABLE i386
>Organization:
>Environment:
System: FreeBSD serv.int.fubar.geek.nz 5.4-STABLE FreeBSD 5.4-STABLE #7: Wed May 11 23:57:29 NZST 2005 root@serv.int.fubar.geek.nz:/usr/obj/srctrees/RELENG_5/sys/GENERIC i386


	
>Description:
archive_read_open_fd dosn't free the client data if fstat fails.
This can cause a memory leak as file_close is not called later to free the data.
	
>How-To-Repeat:
The following code leaks the client data as file_close from archive_read_open_fd.c is never called.

#include <archive.h>

int main (int argc, char *argv[]) {
	struct archive *a;
	int i;

	a = archive_read_new();

	archive_read_support_compression_all(a);
	archive_read_open_fd(a, -1, 1024);

	archive_read_finish(a);
}
	
>Fix:
This patch fixed the leak by freeing the client data when fstat fails.
	

--- libarchive_ngtve_fd.diff begins here ---
Index: archive_read_open_fd.c
===================================================================
RCS file: /cvsroot/src/lib/libarchive/archive_read_open_fd.c,v
retrieving revision 1.4
diff -u -3 -p -u -r1.4 archive_read_open_fd.c
--- archive_read_open_fd.c	21 Sep 2005 04:25:05 -0000	1.4
+++ archive_read_open_fd.c	22 Sep 2005 09:57:30 -0000
@@ -75,6 +75,8 @@ file_open(struct archive *a, void *clien
 
 	if (fstat(mine->fd, &st) != 0) {
 		archive_set_error(a, errno, "Can't stat fd %d", mine->fd);
+		free(mine->buffer);
+		free(mine);
 		return (ARCHIVE_FATAL);
 	}
 
--- libarchive_ngtve_fd.diff ends here ---


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->kientzle 
Responsible-Changed-By: linimon 
Responsible-Changed-When: Thu Sep 22 19:15:32 GMT 2005 
Responsible-Changed-Why:  
Tim is probably the right person to look at this. 

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

From: Tim Kientzle <tim@kientzle.com>
To: bug-followup@FreeBSD.org, andrew@fubar.geek.nz
Cc:  
Subject: Re: kern/86453: [patch] libarchive memory leak with bad fd
Date: Mon, 16 Jan 2006 20:50:45 -0800

 I've fixed this in -CURRENT.  My fix changes archive_read_open()
 to call the client close routine after any open error.  This
 fixes both this problem and a similar problem for
 archive_read_open_file() (and similarly for any other custom
 archive read package).
 
 
State-Changed-From-To: open->closed 
State-Changed-By: linimon 
State-Changed-When: Wed Jan 18 04:05:36 UTC 2006 
State-Changed-Why:  
Fixed by kientzle 01/16/2006. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=86453 
>Unformatted:
