From a7d9e1f93d278184a9804d27439056fc70cda3a4 Mon Sep 17 00:00:00 2001 From: simonb Date: Fri, 16 Jun 2000 00:20:23 +0000 Subject: [PATCH] Use new KERN_MSGBUF sysctl if running on a live kernel, and remove setgid kmem bit from executable. --- sbin/dmesg/dmesg.c | 98 +++++++++++++++++++++++++++++----------------- 1 file changed, 61 insertions(+), 37 deletions(-) diff --git a/sbin/dmesg/dmesg.c b/sbin/dmesg/dmesg.c index 55465416b02a..268a3d3e7b20 100644 --- a/sbin/dmesg/dmesg.c +++ b/sbin/dmesg/dmesg.c @@ -1,4 +1,4 @@ -/* $NetBSD: dmesg.c,v 1.16 1998/07/05 08:56:54 mrg Exp $ */ +/* $NetBSD: dmesg.c,v 1.17 2000/06/16 00:20:23 simonb Exp $ */ /*- * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. @@ -42,11 +42,13 @@ __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\ #if 0 static char sccsid[] = "@(#)dmesg.c 8.1 (Berkeley) 6/5/93"; #else -__RCSID("$NetBSD: dmesg.c,v 1.16 1998/07/05 08:56:54 mrg Exp $"); +__RCSID("$NetBSD: dmesg.c,v 1.17 2000/06/16 00:20:23 simonb Exp $"); #endif #endif /* not lint */ +#include #include +#include #include #include @@ -54,7 +56,9 @@ __RCSID("$NetBSD: dmesg.c,v 1.16 1998/07/05 08:56:54 mrg Exp $"); #include #include #include +#include #include +#include #include #include #include @@ -76,11 +80,9 @@ main(argc, argv) int argc; char *argv[]; { + struct kern_msgbuf cur; int ch, newl, skip, i; - char *p; - struct kern_msgbuf *bufp, cur; - char *memf, *nlistf, *bufdata; - kvm_t *kd; + char *p, *memf, *nlistf, *bufdata; char buf[5]; memf = nlistf = NULL; @@ -99,38 +101,60 @@ main(argc, argv) argc -= optind; argv += optind; - /* - * Discard setgid privileges if not the running kernel so that bad - * guys can't print interesting stuff from kernel memory. - */ - if (memf != NULL || nlistf != NULL) - setgid(getgid()); + if (memf == NULL && nlistf == NULL) { + size_t size; + int mib[2]; - /* Read in message buffer header and data, and do sanity checks. */ - if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg")) == NULL) - exit (1); - setgid(getgid()); /* do now anyway */ - if (kvm_nlist(kd, nl) == -1) - errx(1, "kvm_nlist: %s", kvm_geterr(kd)); - if (nl[X_MSGBUF].n_type == 0) - errx(1, "%s: msgbufp not found", nlistf ? nlistf : "namelist"); - if (KREAD(nl[X_MSGBUF].n_value, bufp)) - errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd), - nl[X_MSGBUF].n_value); - if (KREAD((long)bufp, cur)) - errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd), - (unsigned long)bufp); - if (cur.msg_magic != MSG_MAGIC) - errx(1, "magic number incorrect"); - bufdata = malloc(cur.msg_bufs); - if (bufdata == NULL) - errx(1, "couldn't allocate space for buffer data"); - if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata, - cur.msg_bufs) != cur.msg_bufs) - errx(1, "kvm_read: %s", kvm_geterr(kd)); - kvm_close(kd); - if (cur.msg_bufx >= cur.msg_bufs) - cur.msg_bufx = 0; + mib[0] = CTL_KERN; + mib[1] = KERN_MSGBUF; + + if (sysctl(mib, 2, NULL, &size, NULL, 0) == -1) + errx(1, "can't get size of msgbuf"); + + if ((bufdata = malloc(size)) == NULL) + err(1, "couldn't allocate space for buffer data"); + + if (sysctl(mib, 2, bufdata, &size, NULL, 0) == -1) + err(1, "can't get msgbuf"); + + /* make a dummy struct msgbuf for the display logic */ + cur.msg_bufx = cur.msg_bufs = size; + } else { + kvm_t *kd; + struct kern_msgbuf *bufp; + + /* + * Read in message buffer header and data, and do sanity + * checks. + */ + kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg"); + if (kd == NULL) + exit (1); + if (kvm_nlist(kd, nl) == -1) + errx(1, "kvm_nlist: %s", kvm_geterr(kd)); + if (nl[X_MSGBUF].n_type == 0) + errx(1, "%s: msgbufp not found", nlistf ? nlistf : + "namelist"); + if (KREAD(nl[X_MSGBUF].n_value, bufp)) + errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd), + nl[X_MSGBUF].n_value); + if (kvm_read(kd, (long)bufp, &cur, + offsetof(struct kern_msgbuf, msg_bufc)) != + offsetof(struct kern_msgbuf, msg_bufc)) + errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd), + (unsigned long)bufp); + if (cur.msg_magic != MSG_MAGIC) + errx(1, "magic number incorrect"); + bufdata = malloc(cur.msg_bufs); + if (bufdata == NULL) + errx(1, "couldn't allocate space for buffer data"); + if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata, + cur.msg_bufs) != cur.msg_bufs) + errx(1, "kvm_read: %s", kvm_geterr(kd)); + kvm_close(kd); + if (cur.msg_bufx >= cur.msg_bufs) + cur.msg_bufx = 0; + } /* * The message buffer is circular; start at the write pointer