From 794c57d3c59959ece2d99547c0566e5cadab9e63 Mon Sep 17 00:00:00 2001 From: Mark Johnston Date: Tue, 21 Jul 2015 23:57:38 +0000 Subject: [PATCH] Fix counter reads on platforms where sizeof(uint64_t) != sizeof(uint64_t *). In the kernel, structs such as tcpstat are manipulated as an array of counter_u64_t (uint64_t *), but made visible to userland as an array of uint64_t. kread_counters() was previously copying the counter array into user space and sequentially overwriting each counter with its value. This mostly affects IPsec counters, as other counters are exported via sysctl. PR: 201700 Tested by: Jason Unovitch MFC after: 1 week --- usr.bin/netstat/main.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/usr.bin/netstat/main.c b/usr.bin/netstat/main.c index acc27f773878..477add6fe06b 100644 --- a/usr.bin/netstat/main.c +++ b/usr.bin/netstat/main.c @@ -776,19 +776,31 @@ kread_counter(u_long addr) int kread_counters(u_long addr, void *buf, size_t size) { - uint64_t *c = buf; + uint64_t *c; + u_long *counters; + size_t i, n; if (kvmd_init() < 0) return (-1); - if (kread(addr, buf, size) < 0) + if (size % sizeof(uint64_t) != 0) { + xo_warnx("kread_counters: invalid counter set size"); return (-1); - - while (size != 0) { - *c = kvm_counter_u64_fetch(kvmd, *c); - size -= sizeof(*c); - c++; } + + n = size / sizeof(uint64_t); + if ((counters = malloc(n * sizeof(u_long))) == NULL) + xo_err(-1, "malloc"); + if (kread(addr, counters, n * sizeof(u_long)) < 0) { + free(counters); + return (-1); + } + + c = buf; + for (i = 0; i < n; i++) + c[i] = kvm_counter_u64_fetch(kvmd, counters[i]); + + free(counters); return (0); }