bin/91411: [PATCH] kvm(3) should handle empty files properly

Wojciech A. Koszek dunstan at freebsd.czest.pl
Fri Jan 6 10:40:06 PST 2006


>Number:         91411
>Category:       bin
>Synopsis:       [PATCH] kvm(3) should handle empty files properly
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Jan 06 18:40:04 GMT 2006
>Closed-Date:
>Last-Modified:
>Originator:     Wojciech A. Koszek
>Release:        FreeBSD 7.0-CURRENT i386
>Organization:
>Environment:
System: FreeBSD dunstan.freebsd.czest.pl 7.0-CURRENT FreeBSD 7.0-CURRENT #4: Mon Jan 2 21:27:11 CET 2006 root at dunstan.freebsd.czest.pl:/usr/obj/usr/src/sys/LAPTOP i386

>Description:

Right now you will get segmentation fault with almost every application
linked with kvm(3) if the file which name was passed in kvm_open.. has 0
bytes:


root at dunstan:(/usr/src/lib/libkvm)# touch /tmp/Zzz
root at dunstan:(/usr/src/lib/libkvm)# ps -M /tmp/Zzz
zsh: segmentation fault  ps -M /tmp/Zzz


Besides ps(1), every program tries to properly handle error value returned
from kvm_open*, which should be NULL (and this behaviour is documented in
manual page).

>How-To-Repeat:

root at dunstan:(/usr/src/lib/libkvm)# touch /tmp/Zzz
root at dunstan:(/usr/src/lib/libkvm)# ps -M /tmp/Zzz
zsh: segmentation fault  ps -M /tmp/Zzz

>Fix:

Patch is here:
	http://freebsd.czest.pl/dunstan/FreeBSD/libkvm.0.patch

--- libkvm.0.patch begins here ---
(c) 2006 Wojciech A. Koszek <dunstan%FreeBSD.czest.pl>

Patch against FreeBSD 7.0-CURRENT, kern.osreldate: 700011.

diff --exclude=CVS -upr /usr/src/bin/ps/ps.c src/bin/ps/ps.c
--- /usr/src/bin/ps/ps.c	Wed Feb  9 18:37:38 2005
+++ src/bin/ps/ps.c	Wed Jan  4 23:00:47 2006
@@ -432,7 +432,7 @@ main(int argc, char *argv[])
 		xkeep = xkeep_implied;
 
 	kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
-	if (kd == 0)
+	if (kd == NULL)
 		errx(1, "%s", errbuf);
 
 	if (!_fmt)
diff --exclude=CVS -upr /usr/src/lib/libkvm/kvm.c src/lib/libkvm/kvm.c
--- /usr/src/lib/libkvm/kvm.c	Tue Jul 19 16:48:58 2005
+++ src/lib/libkvm/kvm.c	Wed Jan  4 22:59:11 2006
@@ -179,6 +179,15 @@ _kvm_open(kd, uf, mf, flag, errout)
 		_kvm_syserr(kd, kd->program, "%s", mf);
 		goto failed;
 	}
+	/*
+	 * Disallow access to 0-length files, since further initialization
+	 * will cause segmentation fault.
+	 */
+	if (S_ISREG(st.st_mode) && st.st_size <= 0) {
+		errno = EINVAL;
+		_kvm_syserr(kd, kd->program, "empty file");
+		goto failed;
+	}
 	if (fcntl(kd->pmfd, F_SETFD, FD_CLOEXEC) < 0) {
 		_kvm_syserr(kd, kd->program, "%s", mf);
 		goto failed;
@@ -225,10 +234,10 @@ failed:
 	/*
 	 * Copy out the error if doing sane error semantics.
 	 */
-	if (errout != 0)
+	if (errout != NULL)
 		strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
 	(void)kvm_close(kd);
-	return (0);
+	return (NULL);
 }
 
 kvm_t *
--- libkvm.0.patch ends here ---
>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-bugs mailing list