Subject: lib/36649: ps -M causes core in libkvm
To: None <lib-bug-people@netbsd.org, gnats-admin@netbsd.org,>
From: None <gary@duzan.org>
List: netbsd-bugs
Date: 07/15/2007 14:35:00
>Number:         36649
>Category:       lib
>Synopsis:       ps -M causes core in libkvm
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sun Jul 15 14:35:00 +0000 2007
>Originator:     Gary Duzan
>Release:        NetBSD 4.99.22
>Organization:
	None
>Environment:
System: NetBSD capo 4.99.22 NetBSD 4.99.22 (CAPO64) #36: Sat Jul 14 18:16:53 EDT 2007 gary@capo:/usr/obj/sys/arch/amd64/compile.amd64/CAPO64 amd64
Architecture: x86_64
Machine: amd64
>Description:
	Running "ps -M /var/crash/netbsd.0.core" causes a segmentation
	fault in kvm_getproc2. The call to kvm_getlwps() returns NULL
	for pid 0 because the read of the back pointer fails.
>How-To-Repeat:
	ps -M /var/crash/netbsd.0.core
>Fix:
	The following allows the ps to complete successfully, though I'm
	not sure it is the correct fix.

Index: lib/libkvm/kvm_proc.c
===================================================================
RCS file: /usr2/netbsd-cvs/src/lib/libkvm/kvm_proc.c,v
retrieving revision 1.73
diff -b -u -p -r1.73 kvm_proc.c
--- lib/libkvm/kvm_proc.c	9 Jul 2007 22:28:13 -0000	1.73
+++ lib/libkvm/kvm_proc.c	15 Jul 2007 13:25:56 -0000
@@ -813,10 +813,15 @@ again:
 			laddr = (u_long)PTRTOUINT64(l.l_runq.tqe_prev);
 			st = kvm_read(kd, laddr, &back, sizeof(back));
 			if (st == -1) {
+#ifdef GDD_NOTDEF_XXX
 				_kvm_syserr(kd, kd->program, "kvm_getlwps");
 				return (NULL);
-			}
+#else
+				kl->l_back = PTRTOUINT64(NULL);
+#endif
+			} else {
 			kl->l_back = PTRTOUINT64(back);
+			}
 			kl->l_addr = PTRTOUINT64(l.l_addr);
 			kl->l_lid = l.l_lid;
 			kl->l_flag = l.l_flag;