Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/kvm_mkdb Fix a long standing bug.



details:   https://anonhg.NetBSD.org/src/rev/307ebd1305ec
branches:  trunk
changeset: 507294:307ebd1305ec
user:      msaitoh <msaitoh%NetBSD.org@localhost>
date:      Mon Mar 19 15:18:59 2001 +0000

description:
Fix a long standing bug.

Old kvm_mkdb had a possibility to check old kernel's version
string retained at a hole between text segment and data segment.
It might happen a misunderstanding that the running kernel's version
matches kvm.db's version.

This had been caused by checking current kernel's version string
via /dev/kmem. The version string can get via sysctl, so use it!

diffstat:

 usr.sbin/kvm_mkdb/testdb.c |  41 +++++++++++++++++++----------------------
 1 files changed, 19 insertions(+), 22 deletions(-)

diffs (81 lines):

diff -r 2ff7bed1a98c -r 307ebd1305ec usr.sbin/kvm_mkdb/testdb.c
--- a/usr.sbin/kvm_mkdb/testdb.c        Mon Mar 19 11:18:42 2001 +0000
+++ b/usr.sbin/kvm_mkdb/testdb.c        Mon Mar 19 15:18:59 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: testdb.c,v 1.7 1997/10/18 08:49:36 lukem Exp $ */
+/*     $NetBSD: testdb.c,v 1.8 2001/03/19 15:18:59 msaitoh Exp $       */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -38,17 +38,20 @@
 #if 0
 static char sccsid[] = "from: @(#)testdb.c     8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: testdb.c,v 1.7 1997/10/18 08:49:36 lukem Exp $");
+__RCSID("$NetBSD: testdb.c,v 1.8 2001/03/19 15:18:59 msaitoh Exp $");
 #endif
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/file.h>
+#include <sys/sysctl.h>
+#include <err.h>
 #include <errno.h>
 #include <limits.h>
 #include <kvm.h>
 #include <db.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <paths.h>
@@ -60,11 +63,12 @@
 testdb()
 {
        DB *db;
-       int cc, kd, ret, dbversionlen;
+       int kd, ret, dbversionlen;
        DBT rec;
-       struct nlist nitem;
        char dbversion[_POSIX2_LINE_MAX];
-       char kversion[_POSIX2_LINE_MAX];
+       char *kversion;
+       int mib[2];
+       size_t size;
 
        ret = 0;
        db = NULL;
@@ -86,23 +90,16 @@
        dbversionlen = rec.size;
 
        /* Read version string from kernel memory */
-       rec.data = VRS_SYM;
-       rec.size = sizeof(VRS_SYM) - 1;
-       if ((db->get)(db, &rec, &rec, 0))
-               goto close;
-       if (rec.data == 0 || rec.size != sizeof(struct nlist))
-               goto close;
-       memmove(&nitem, rec.data, sizeof(nitem));
-       /*
-        * Theoretically possible for lseek to be seeking to -1.  Not
-        * that it's something to lie awake nights about, however.
-        */
-       errno = 0;
-       if (lseek(kd, (off_t)nitem.n_value, SEEK_SET) == -1 && errno != 0)
-               goto close;
-       cc = read(kd, kversion, sizeof(kversion));
-       if (cc < 0 || cc != sizeof(kversion))
-               goto close;
+       mib[0] = CTL_KERN;
+       mib[1] = KERN_VERSION;
+       if (sysctl(mib, 2, NULL, &size, NULL, 0) == -1)
+               errx(1, "can't get size of kernel version string");
+
+       if ((kversion = malloc(size)) == NULL)
+               err(1, "couldn't allocate space for buffer data");
+
+       if (sysctl(mib, 2, kversion, &size, NULL, 0) == -1)
+               errx(1, "can't get kernel version string");
 
        /* If they match, we win */
        ret = memcmp(dbversion, kversion, dbversionlen) == 0;



Home | Main Index | Thread Index | Old Index