From dan@kulesh.obluda.cz  Tue Jul 12 18:56:26 2005
Return-Path: <dan@kulesh.obluda.cz>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 9087416A41C
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 12 Jul 2005 18:56:26 +0000 (GMT)
	(envelope-from dan@kulesh.obluda.cz)
Received: from kulesh.obluda.cz (kulesh.obluda.cz [193.179.22.243])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 5A76543D48
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 12 Jul 2005 18:56:25 +0000 (GMT)
	(envelope-from dan@kulesh.obluda.cz)
Received: from kulesh.obluda.cz (localhost.eunet.cz [127.0.0.1])
	by kulesh.obluda.cz (8.13.3/8.13.3) with ESMTP id j6CIuNAi016504
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 12 Jul 2005 20:56:23 +0200 (CEST)
	(envelope-from dan@kulesh.obluda.cz)
Received: (from root@localhost)
	by kulesh.obluda.cz (8.13.3/8.13.1/Submit) id j6CIuMml016503;
	Tue, 12 Jul 2005 20:56:22 +0200 (CEST)
	(envelope-from dan)
Message-Id: <200507121856.j6CIuMml016503@kulesh.obluda.cz>
Date: Tue, 12 Jul 2005 20:56:22 +0200 (CEST)
From: Dan Lukes <dan@obluda.cz>
Reply-To: Dan Lukes <dan@obluda.cz>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [ PATCH ] Improper handling of malloc failures within libc's RPC functions
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         83344
>Category:       bin
>Synopsis:       [patch] Improper handling of malloc failures within libc's RPC functions
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    matteo
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Jul 12 19:00:10 GMT 2005
>Closed-Date:    Sun Apr 25 15:05:34 UTC 2010
>Last-Modified:  Sun Apr 25 15:05:34 UTC 2010
>Originator:     Dan Lukes
>Release:        FreeBSD 5.4-STABLE i386
>Organization:
Obludarium
>Environment:
System: FreeBSD 5.4-STABLE #8: Sat Jul 9 16:31:08 CEST 2005 i386
lib/libc/rpc/rpcb_clnt.c,v 1.13 2003/10/29 09:22:49 mbr

>Description:
	Improper handling of malloc failures cause the memory leak.

	Improper checking of targaddr parameter within getclnthandle() may
cause dereferencing of NULL

>How-To-Repeat:
>Fix:

--- patch begins here ---
--- lib/libc/rpc/rpcb_clnt.c.ORIG	Fri Nov 14 03:22:48 2003
+++ lib/libc/rpc/rpcb_clnt.c	Tue Jul 12 20:26:30 2005
@@ -239,11 +239,21 @@
 	ad_cache->ac_taddr = (struct netbuf *)malloc(sizeof (struct netbuf));
 	if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
 		(uaddr && !ad_cache->ac_uaddr)) {
+		free(ad_cache->ac_host);
+		free(ad_cache->ac_netid);
+		free(ad_cache->ac_uaddr);
+		free(ad_cache->ac_taddr);
+		free(ad_cache);
 		return;
 	}
 	ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
 	ad_cache->ac_taddr->buf = (char *) malloc(taddr->len);
 	if (ad_cache->ac_taddr->buf == NULL) {
+		free(ad_cache->ac_host);
+		free(ad_cache->ac_netid);
+		free(ad_cache->ac_uaddr);
+		free(ad_cache->ac_taddr);
+		free(ad_cache);
 		return;
 	}
 	memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
@@ -375,9 +385,15 @@
 		} else {
 			struct sockaddr_un sun;
 
-			*targaddr = malloc(sizeof(sun.sun_path));
-			strncpy(*targaddr, _PATH_RPCBINDSOCK,
-			    sizeof(sun.sun_path));
+			if (targaddr) {
+				*targaddr = malloc(sizeof(sun.sun_path));
+				if (*targaddr == NULL) {
+					CLNT_DESTROY(client);
+					return(NULL);
+				}
+				strncpy(*targaddr, _PATH_RPCBINDSOCK,
+				    sizeof(sun.sun_path));
+			}
 			return (client);
 		}
 	} else {
--- patch ends here ---

	Remember the free(NULL) is correct construction, so it's not
necesarry to do something like if (x) free(x);
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->matteo 
Responsible-Changed-By: matteo 
Responsible-Changed-When: Mon Sep 17 20:44:11 UTC 2007 
Responsible-Changed-Why:  
I'm working on RPC related programs, so this PR interests me. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=83344 
State-Changed-From-To: open->patched 
State-Changed-By: matteo 
State-Changed-When: Thu Sep 20 22:39:32 UTC 2007 
State-Changed-Why:  
Comited to HEAD, will MFC in 1 week. 

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

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/83344: commit references a PR
Date: Thu, 20 Sep 2007 22:35:31 +0000 (UTC)

 matteo      2007-09-20 22:35:24 UTC
 
   FreeBSD src repository
 
   Modified files:
     lib/libc/rpc         auth_time.c getnetconfig.c getnetpath.c 
                          rpc_generic.c rpcb_clnt.c 
   Log:
   Fix some improper handling of malloc failures
   
   PR:             bin/83344 , kern/81987
   Reviewed by:    alfred
   Approved by:    re (kensmith)
   MFC after:      1 week
   
   Revision  Changes    Path
   1.12      +18 -9     src/lib/libc/rpc/auth_time.c
   1.14      +10 -5     src/lib/libc/rpc/getnetconfig.c
   1.8       +8 -5      src/lib/libc/rpc/getnetpath.c
   1.14      +8 -7      src/lib/libc/rpc/rpc_generic.c
   1.17      +9 -4      src/lib/libc/rpc/rpcb_clnt.c
 _______________________________________________
 cvs-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/cvs-all
 To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
 

From: Dan Lukes <dan@obluda.cz>
To: bug-followup@FreeBSD.org, dan@obluda.cz
Cc:  
Subject: Re: bin/83344: [patch] Improper handling of malloc failures within
 libc's RPC functions
Date: Sun, 30 Aug 2009 22:12:05 +0200

 The 1 week is over.
 
 I assume it's already MFCed, so this PR should be closed ...
 
 						Dan
State-Changed-From-To: patched->closed 
State-Changed-By: brucec 
State-Changed-When: Sun Apr 25 15:05:07 UTC 2010 
State-Changed-Why:  
This has been MFC'd. 

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