Subject: lib/347: minor bug in libkvm
To: None <gnats-admin>
From: Alasdair Baird <>
List: netbsd-bugs
Date: 07/19/1994 14:50:06
>Number:         347
>Category:       lib
>Synopsis:       libkvm returns garbage for null process environments
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    gnats-admin (Library Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Jul 19 14:50:05 1994
>Originator:     & Baird
absolutely none whatsoever
>Release:        current of 18/7/94
System: NetBSD 1.0-ALPHA NetBSD 1.0-ALPHA (WILDCAT) #20: Tue Jul 19 21:31:48 BST 1994 i386


	Commands such as /bin/ps that return information about other
	processes environments via a call to libkvm return bogus
	information for those processes with null (non-existent)
	environments (also for those with null argument lists, though
	these are not so common).

	In libkvm/kvm_proc.c the code that obtains argv/env goop
	does noes not check to see if there are zero items in either
	the argument or environment vectors; it assumes there must
	be something and guddles around to return something.  (This
	could might even cause a crash on architectures that are
	more sensitive to dereferencing bad pointers than that which
	I use.)


	Run "/bin/ps axe" and observe garbage for the environment for
	commands such as "/sbin/init" that do not have any environment.
	(/usr/libexec/getty does the same, but the garbage is subtly


	The fix is very simple, requiring a minor modification to one
	line of kvm_proc.c.  This change checks the number of elements
	in the argv/env and only goes looking for stuff if there is
	something to look for.  Context diff follows:

*** kvm_proc.c.old	Wed May 25 12:37:47 1994
--- kvm_proc.c	Tue Jul 19 21:55:32 1994
*** 657,663 ****
  		return (0);
  	(*info)(&arginfo, &addr, &cnt);
! 	ap = kvm_argv(kd, p, addr, cnt, nchr);
  	 * For live kernels, make sure this process didn't go away.
--- 657,663 ----
  		return (0);
  	(*info)(&arginfo, &addr, &cnt);
! 	ap = (cnt == 0) ? 0 : kvm_argv(kd, p, addr, cnt, nchr);
  	 * For live kernels, make sure this process didn't go away.

	I have compiled this and checked it against a recompiled /bin/ps
	and it seems to work correctly there at least.  I have not checked
	it against any other libkvm users, but don't forsee any grief with
	other utilities.