From nobody@FreeBSD.org  Sun Dec  2 09:59:24 2007
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 43C9916A417
	for <freebsd-gnats-submit@FreeBSD.org>; Sun,  2 Dec 2007 09:59:24 +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 324CF13C455
	for <freebsd-gnats-submit@FreeBSD.org>; Sun,  2 Dec 2007 09:59:24 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.2/8.14.2) with ESMTP id lB29x8vM055250
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 2 Dec 2007 09:59:08 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.2/8.14.1/Submit) id lB29x7xo055249;
	Sun, 2 Dec 2007 09:59:07 GMT
	(envelope-from nobody)
Message-Id: <200712020959.lB29x7xo055249@www.freebsd.org>
Date: Sun, 2 Dec 2007 09:59:07 GMT
From: Michal Vranek <michal.vranek@seznam.cz>
To: freebsd-gnats-submit@FreeBSD.org
Subject: libkvm - kvm_close doesn't dealocate all used memory
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         118380
>Category:       bin
>Synopsis:       [libkvm] kvm_close doesn't dealocate all used memory
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    delphij
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Dec 02 10:00:03 UTC 2007
>Closed-Date:    Mon Feb 11 01:25:29 UTC 2008
>Last-Modified:  Mon Feb 11 01:25:29 UTC 2008
>Originator:     Michal Vranek
>Release:        6.2, 6.3
>Organization:
>Environment:
FreeBSD xerius.felk.cvut.cz 6.3-PRERELEASE FreeBSD 6.3-PRERELEASE #3: Sun Nov  4                                           22:19:33 CET 2007     root@xerius.felk.cvut.cz:/usr/obj/usr/src/sys/XERIUS  i38                                          6
>Description:
Using more times sequencely kvm-library functions to get environment
variables of other running proceses causes memory consume in the
questioning process.

I think the reason is kd->argspc and kd->argbuf nowhere dealocated.


>How-To-Repeat:
Run this code and watch its size in memory.

void main()
{
pid_t pid;
scanf("%d",&pid); // input pid of another running proces
while(1) 
{
  int count;

  char errbuf[_POSIX2_LINE_MAX];

  struct kinfo_proc *process_identity;

  kvm_t *kd;

  char **environment_variables;

  kd = kvm_openfiles( NULL , NULL, NULL, O_RDONLY, errbuf );

  if ( kd == NULL ) exit(1);

  process_identity = kvm_getprocs( kd, KERN_PROC_PID, pid, &count );

  if ( process_identity == NULL )
  {
   kvm_close( kd );

   exit(1);
  }

  environment_variables = kvm_getenvv( kd, process_identity, 0 );

  if( environment_variables == NULL )
  {
   kvm_close( kd );

   exit(1);
  }

  /* here could be variables listing */

  kvm_close( kd );
}
}
>Fix:
Most probably the solution is to modify (add two times free()) kvm_close() in /usr/src/lib/libkvm/kvm.c .

int
kvm_close(kd)
        kvm_t *kd;
{
        int error = 0;

        if (kd->pmfd >= 0)
                error |= close(kd->pmfd);
        if (kd->vmfd >= 0)
                error |= close(kd->vmfd);
        if (kd->nlfd >= 0)
                error |= close(kd->nlfd);
        if (kd->vmst)
                _kvm_freevtop(kd);
        if (kd->procbase != 0)
                free((void *)kd->procbase);
        if (kd->argv != 0)
                free((void *)kd->argv);
/////////////////////////////////////////////
        if (kd->argspc != 0)
                free((void *) kd->argspc);
        if (kd->argbuf != 0)
                free((void *) kd->argbuf);
/////////////////////////////////////////////
        free((void *)kd);

        return (0);
}


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->delphij 
Responsible-Changed-By: delphij 
Responsible-Changed-When: Sat Jan 12 00:51:20 UTC 2008 
Responsible-Changed-Why:  
Take. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=118380 
State-Changed-From-To: open->patched 
State-Changed-By: delphij 
State-Changed-When: Sat Jan 12 00:54:50 UTC 2008 
State-Changed-Why:  
Patch applied against -HEAD, MFC reminder (1 month). 

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

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/118380: commit references a PR
Date: Sat, 12 Jan 2008 00:54:52 +0000 (UTC)

 delphij     2008-01-12 00:54:47 UTC
 
   FreeBSD src repository
 
   Modified files:
     lib/libkvm           kvm.c 
   Log:
   Plug memory leaks that is observed when argbuf or argspc is used in the
   context.
   
   Submitted by:   Michal Vranek <michal.vranek seznam cz>
   PR:             bin/118380
   MFC after:      1 month
   
   Revision  Changes    Path
   1.32      +4 -0      src/lib/libkvm/kvm.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: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/118380: commit references a PR
Date: Mon, 11 Feb 2008 00:31:12 +0000 (UTC)

 delphij     2008-02-11 00:31:04 UTC
 
   FreeBSD src repository
 
   Modified files:        (Branch: RELENG_6)
     lib/libkvm           kvm.c 
   Log:
   MFC revision 1.32
   date: 2008/01/12 00:54:47;  author: delphij;  state: Exp;  lines: +4 -0
   Plug memory leaks that is observed when argbuf or argspc is used in the
   context.
   
   Submitted by:   Michal Vranek <michal.vranek seznam cz>
   PR:             bin/118380
   
   Revision  Changes    Path
   1.27.2.2  +4 -0      src/lib/libkvm/kvm.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: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/118380: commit references a PR
Date: Mon, 11 Feb 2008 00:31:28 +0000 (UTC)

 delphij     2008-02-11 00:31:23 UTC
 
   FreeBSD src repository
 
   Modified files:        (Branch: RELENG_5)
     lib/libkvm           kvm.c 
   Log:
   MFC revision 1.32
   date: 2008/01/12 00:54:47;  author: delphij;  state: Exp;  lines: +4 -0
   Plug memory leaks that is observed when argbuf or argspc is used in the
   context.
   
   Submitted by:   Michal Vranek <michal.vranek seznam cz>
   PR:             bin/118380
   
   Revision  Changes    Path
   1.26.2.1  +4 -0      src/lib/libkvm/kvm.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"
 
State-Changed-From-To: patched->closed 
State-Changed-By: delphij 
State-Changed-When: Mon Feb 11 01:25:12 UTC 2008 
State-Changed-Why:  
Patch applied against RELENG_[567]. 

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