From nobody@FreeBSD.org  Sun Oct 31 09:34:21 2004
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id A5C8B16A4CE
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 31 Oct 2004 09:34:21 +0000 (GMT)
Received: from www.freebsd.org (www.freebsd.org [216.136.204.117])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 9679243D46
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 31 Oct 2004 09:34:21 +0000 (GMT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.12.11/8.12.11) with ESMTP id i9V9YLlU048125
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 31 Oct 2004 09:34:21 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.12.11/8.12.11/Submit) id i9V9YLjG048124;
	Sun, 31 Oct 2004 09:34:21 GMT
	(envelope-from nobody)
Message-Id: <200410310934.i9V9YLjG048124@www.freebsd.org>
Date: Sun, 31 Oct 2004 09:34:21 GMT
From: Jacques Vidrine <nectar@FreeBSD.org>
To: freebsd-gnats-submit@FreeBSD.org
Subject: nsswitch: potential invalid free
X-Send-Pr-Version: www-2.3

>Number:         73337
>Category:       conf
>Synopsis:       [nsswitch.conf] [patch] potential invalid free
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    delphij
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Oct 31 09:40:32 GMT 2004
>Closed-Date:    Tue Aug 23 22:29:33 UTC 2011
>Last-Modified:  Tue Aug 23 22:29:33 UTC 2011
>Originator:     Jacques Vidrine
>Release:        5.3-STABLE
>Organization:
FreeBSD
>Environment:
FreeBSD madman.celabo.org 5.3-STABLE FreeBSD 5.3-STABLE #5: Mon Oct 18 20:11:13 CDT 2004     root@madman.celabo.org:/usr/obj/usr/src/sys/MADMAN  i386
>Description:
Date: Wed, 27 Oct 2004 09:33:33 +0200                                                            
From: Danny Braniss <danny@cs.huji.ac.il>                                                        
To: bushman@rsu.ru                                                                               
Cc: hackers@freebsd.org, "Jacques A. Vidrine" <nectar@freebsd.org>,                                      "Christian S.J. Peron" <csjp@freebsd.org>                                                
Subject: Re: nsdispatch services patch + lookupd                                                 
Message-Id: <20041027073335.8B74343D55@mx1.FreeBSD.org>                                             

while trying to add hesiod/dns support, i've noticed, what looks as a problem:

in nss_tls.h, the function name##_getstate(...) can return a static pointer,
which gets freed in name##_endstate(...), as far as i know, freeing a non
malloced memory is asking for trouble.
proposed fix, instead of static, also do a calloc(...).

danny
>How-To-Repeat:
      
>Fix:
make jacques work on it
>Release-Note:
>Audit-Trail:

From: Bruce Cran <bruce@cran.org.uk>
To: bug-followup@FreeBSD.org, nectar@FreeBSD.org
Cc:  
Subject: Re: bin/73337: nsswitch: potential invalid free
Date: Sat, 12 Jan 2008 11:01:48 +0000

 This still appears to be a problem on 7.0-PRERELEASE: single-threaded 
 applications get returned a statically-allocated [name]_state structure, 
 but all of the [name]_endstate functions such as dns_endstate assume 
 that the memory has been dynamically allocated - and so attempt to 
 free() a pointer which wasn't obtained through malloc().  I think the 
 patch below would fix the problem.
 
 --- nss_tls.h.old       2008-01-12 00:21:20.000000000 +0000
 +++ nss_tls.h   2008-01-12 10:54:17.000000000 +0000
 @@ -50,12 +50,18 @@
  static int                                                     \
  name##_getstate(struct name##_state **p)                       \
  {                                                              \
 -       static struct name##_state st;                          \
 +       static struct name##_state *st = NULL;                          \
         static pthread_once_t   keyinit = PTHREAD_ONCE_INIT;    \
         int                     rv;                             \
                                                                 \
         if (!__isthreaded || _pthread_main_np() != 0) {         \
 -               *p = &st;                                       \
 +               if (st == NULL) {               \
 +                       st = calloc(1, sizeof(*st));            \
 +                       if (st == NULL)         \
 +                               return (ENOMEM);    \
 +               }                           \
 +                                          \
 +               *p = st;                                        \
                 return (0);                                     \
         }                                                       \
         rv = _pthread_once(&keyinit, name##_keyinit);           \
 
 
 --
 Bruce Cran

From: Bruce Cran <bruce@cran.org.uk>
To: bug-followup@FreeBSD.org, nectar@FreeBSD.org
Cc:  
Subject: Re: bin/73337: [nsswitch] [patch] potential invalid free
Date: Sun, 10 Feb 2008 11:39:38 +0000

 This is a multi-part message in MIME format.
 --------------080704050506000507090004
 Content-Type: text/plain; charset=ISO-8859-1; format=flowed
 Content-Transfer-Encoding: 7bit
 
 Attaching the patch as an attachment.
 
 --------------080704050506000507090004
 Content-Type: text/plain;
  name="nss_tls.h.diff"
 Content-Transfer-Encoding: 7bit
 Content-Disposition: inline;
  filename="nss_tls.h.diff"
 
 --- nss_tls.h.old	2008-01-12 00:21:20.000000000 +0000
 +++ nss_tls.h	2008-01-12 10:54:17.000000000 +0000
 @@ -50,12 +50,18 @@
  static int							\
  name##_getstate(struct name##_state **p)			\
  {								\
 -	static struct name##_state st;				\
 +	static struct name##_state *st = NULL;				\
  	static pthread_once_t	keyinit = PTHREAD_ONCE_INIT;	\
  	int			rv;				\
  								\
  	if (!__isthreaded || _pthread_main_np() != 0) {		\
 -		*p = &st;					\
 +		if (st == NULL) {               \
 +			st = calloc(1, sizeof(*st));		\
 +			if (st == NULL)         \
 +				return (ENOMEM);    \
 +		}                           \
 +		                           \
 +		*p = st;					\
  		return (0);					\
  	}							\
  	rv = _pthread_once(&keyinit, name##_keyinit);		\
 
 --------------080704050506000507090004--
State-Changed-From-To: open->feedback 
State-Changed-By: delphij 
State-Changed-When: Thu Oct 1 00:59:45 UTC 2009 
State-Changed-Why:  
I don't think this is a real issue and proposes to close the PR. 

The reasoning is: 

- _endstate are destructors called by thread key destructor; 
- when the destructor is registered, the program is threaded 
and the thread specific storage points to a dynamically 
allocated memory. 
- Thus, there is problem with the current implementation. 


Responsible-Changed-From-To: freebsd-bugs->delphij 
Responsible-Changed-By: delphij 
Responsible-Changed-When: Thu Oct 1 00:59:45 UTC 2009 
Responsible-Changed-Why:  
Take. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=73337 
State-Changed-From-To: feedback->closed 
State-Changed-By: delphij 
State-Changed-When: Tue Aug 23 22:28:24 UTC 2011 
State-Changed-Why:  


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