⬢ DragonFlyBSD Kernel Audit
DF-5160 / fix.diff
← back to finding ↓ download raw
--- a/sys/dev/misc/aperf/aperf.c
+++ b/sys/dev/misc/aperf/aperf.c
@@ -25,6 +25,7 @@
 
 #include <machine/clock.h>
 #include <machine/cpufunc.h>
+#include <machine/md_var.h>
 
 #include "cpu_if.h"
 
@@ -84,6 +85,10 @@
 	if (device_find_child(parent, "aperf", -1) != NULL)
 		return;
 
+	/* Guard against out-of-range leaves echoing the highest leaf. */
+	if (cpu_high < 6)
+		return;
+
 	/* CPUID Fn0000_0006_ECX Effective Processor Frequency Interface */
 	do_cpuid(0x00000006, regs);
 	if ((regs[2] & 1) == 0)
@@ -156,8 +161,14 @@
 
 	crit_enter();
 	tsc = rdtsc_ordered();
-	aperf = rdmsr(MSR_APERF);
-	mperf = rdmsr(MSR_MPERF);
+	if (rdmsr_safe(MSR_APERF, &aperf)) {
+		crit_exit();
+		return;
+	}
+	if (rdmsr_safe(MSR_MPERF, &mperf)) {
+		crit_exit();
+		return;
+	}
 	crit_exit();
 
 	if ((sc->sc_flags & APERF_FLAG_PREV_VALID) == 0) {