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
This commit is contained in:
Mark Johnston 2015-07-21 23:57:38 +00:00
parent d258fd1d98
commit 794c57d3c5
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=285782

View File

@ -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);
}