Module Name:    src
Committed By:   simonb
Date:           Fri Apr  2 06:28:55 UTC 2021

Modified Files:
        src/usr.bin/vmstat: Makefile vmstat.c

Log Message:
Drop setgid kmem, simplify nlist setup.


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/usr.bin/vmstat/Makefile
cvs rdiff -u -r1.245 -r1.246 src/usr.bin/vmstat/vmstat.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/vmstat/Makefile
diff -u src/usr.bin/vmstat/Makefile:1.34 src/usr.bin/vmstat/Makefile:1.35
--- src/usr.bin/vmstat/Makefile:1.34	Thu Dec 29 23:42:39 2016
+++ src/usr.bin/vmstat/Makefile	Fri Apr  2 06:28:55 2021
@@ -1,8 +1,7 @@
-#	$NetBSD: Makefile,v 1.34 2016/12/29 23:42:39 mrg Exp $
+#	$NetBSD: Makefile,v 1.35 2021/04/02 06:28:55 simonb Exp $
 #	@(#)Makefile	8.1 (Berkeley) 6/6/93
 
 .include <bsd.own.mk>
-USE_FORT?= yes	# setgid
 PROG=	vmstat
 
 CPPFLAGS+=-D_KMEMUSER -I${NETBSDSRCDIR}/sys
@@ -10,8 +9,6 @@ SRCS=	drvstats.c vmstat.c
 MAN=	vmstat.1
 DPADD=	${LIBKVM}
 LDADD=	-lkvm
-BINGRP=	kmem
-BINMODE=2555
 
 CWARNFLAGS.clang+=	-Wno-format-extra-args
 COPTS.vmstat.c += -Wno-format-nonliteral
@@ -19,6 +16,4 @@ COPTS.vmstat.c += -Wno-format-nonliteral
 # sparc64 is broken!
 COPTS.vmstat.c += -Wno-error=stack-protector
 
-.include "../../compat/exec.mk"
-
 .include <bsd.prog.mk>

Index: src/usr.bin/vmstat/vmstat.c
diff -u src/usr.bin/vmstat/vmstat.c:1.245 src/usr.bin/vmstat/vmstat.c:1.246
--- src/usr.bin/vmstat/vmstat.c:1.245	Thu Apr  1 06:23:14 2021
+++ src/usr.bin/vmstat/vmstat.c	Fri Apr  2 06:28:55 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: vmstat.c,v 1.245 2021/04/01 06:23:14 simonb Exp $ */
+/* $NetBSD: vmstat.c,v 1.246 2021/04/02 06:28:55 simonb Exp $ */
 
 /*-
  * Copyright (c) 1998, 2000, 2001, 2007, 2019, 2020
@@ -71,7 +71,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 19
 #if 0
 static char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 3/1/95";
 #else
-__RCSID("$NetBSD: vmstat.c,v 1.245 2021/04/01 06:23:14 simonb Exp $");
+__RCSID("$NetBSD: vmstat.c,v 1.246 2021/04/02 06:28:55 simonb Exp $");
 #endif
 #endif /* not lint */
 
@@ -328,7 +328,6 @@ static const int clockrate_mib[] = { CTL
 static const int vmmeter_mib[] = { CTL_VM, VM_METER };
 static const int uvmexp2_mib[] = { CTL_VM, VM_UVMEXP2 };
 static const int boottime_mib[] = { CTL_KERN, KERN_BOOTTIME };
-static char kvm_errbuf[_POSIX2_LINE_MAX];
 
 int
 main(int argc, char *argv[])
@@ -336,11 +335,10 @@ main(int argc, char *argv[])
 	int c, todo, verbose, wide;
 	struct timespec interval;
 	int reps;
-	gid_t egid = getegid();
 	const char *histname, *hashname;
+	char errbuf[_POSIX2_LINE_MAX];
 
 	histname = hashname = NULL;
-	(void)setegid(getgid());
 	memf = nlistf = NULL;
 	reps = todo = verbose = wide = 0;
 	interval.tv_sec = 0;
@@ -415,35 +413,21 @@ main(int argc, char *argv[])
 	if (todo == 0)
 		todo = VMSTAT;
 
-	/*
-	 * Discard setgid privileges.  If not the running kernel, we toss
-	 * them away totally so that bad guys can't print interesting stuff
-	 * from kernel memory, otherwise switch back to kmem for the
-	 * duration of the kvm_openfiles() call.
-	 */
-	if (nlistf != NULL || memf != NULL)
-		(void)setgid(getgid());
-	else
-		(void)setegid(egid);
-
-	kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, kvm_errbuf);
-	if (kd == NULL) {
-		if (nlistf != NULL || memf != NULL) {
-			errx(1, "kvm_openfiles: %s", kvm_errbuf);
-		}
+	if (memf == NULL) {
+		kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
+	} else {
+		kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
+		getnlist(todo);
 	}
 
-	if (nlistf == NULL && memf == NULL)
-		(void)setgid(getgid());
-
+	if (kd == NULL)
+		errx(EXIT_FAILURE, "%s", errbuf);
 
 	if (todo & VMSTAT) {
 		struct winsize winsize;
 
 		(void)drvinit(0);/* Initialize disk stats, no disks selected. */
 
-		(void)setgid(getgid()); /* don't need privs anymore */
-
 		argv = choosedrives(argv);	/* Select disks. */
 		winsize.ws_row = 0;
 		(void)ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize);
@@ -466,8 +450,6 @@ main(int argc, char *argv[])
 	} else if (reps)
 		interval.tv_sec = 1;
 
-
-	getnlist(todo);
 	/*
 	 * Statistics dumping is incompatible with the default
 	 * VMSTAT/dovmstat() output. So perform the interval/reps handling
@@ -539,36 +521,30 @@ main(int argc, char *argv[])
 void
 getnlist(int todo)
 {
-	static int namelist_done = 0;
 	static int done = 0;
 	int c;
 	size_t i;
 
-	if (kd == NULL)
-		errx(1, "kvm_openfiles: %s", kvm_errbuf);
-
-	if (!namelist_done) {
-		namelist_done = 1;
-		if ((c = kvm_nlist(kd, namelist)) != 0) {
-			int doexit = 0;
-			if (c == -1)
-				errx(1, "kvm_nlist: %s %s",
-				    "namelist", kvm_geterr(kd));
-			for (i = 0; i < __arraycount(namelist)-1; i++)
-				if (namelist[i].n_type == 0) {
-					if (doexit++ == 0)
-						(void)fprintf(stderr,
-						    "%s: undefined symbols:",
-						    getprogname());
-					(void)fprintf(stderr, " %s",
-					    namelist[i].n_name);
-				}
-			if (doexit) {
-				(void)fputc('\n', stderr);
-				exit(1);
+	if ((c = kvm_nlist(kd, namelist)) != 0) {
+		int doexit = 0;
+		if (c == -1)
+			errx(1, "kvm_nlist: %s %s",
+			    "namelist", kvm_geterr(kd));
+		for (i = 0; i < __arraycount(namelist)-1; i++)
+			if (namelist[i].n_type == 0) {
+				if (doexit++ == 0)
+					(void)fprintf(stderr,
+					    "%s: undefined symbols:",
+					    getprogname());
+				(void)fprintf(stderr, " %s",
+				    namelist[i].n_name);
 			}
+		if (doexit) {
+			(void)fputc('\n', stderr);
+			exit(1);
 		}
 	}
+
 	if ((todo & (VMSTAT|INTRSTAT)) && !(done & (VMSTAT))) {
 		done |= VMSTAT;
 		if ((c = kvm_nlist(kd, timenl)) == -1 || c == X_TIMENL_SIZE)

Reply via email to