From owner-svn-src-all@freebsd.org Tue Jul 28 14:31:10 2015 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 4911F9ADBA1; Tue, 28 Jul 2015 14:31:10 +0000 (UTC) (envelope-from glebius@FreeBSD.org) Received: from cell.glebius.int.ru (glebius.int.ru [81.19.69.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "cell.glebius.int.ru", Issuer "cell.glebius.int.ru" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id B13AA9AA; Tue, 28 Jul 2015 14:31:08 +0000 (UTC) (envelope-from glebius@FreeBSD.org) Received: from cell.glebius.int.ru (localhost [127.0.0.1]) by cell.glebius.int.ru (8.14.9/8.14.9) with ESMTP id t6SEV6oX085981 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Tue, 28 Jul 2015 17:31:06 +0300 (MSK) (envelope-from glebius@FreeBSD.org) Received: (from glebius@localhost) by cell.glebius.int.ru (8.14.9/8.14.9/Submit) id t6SEV69l085980; Tue, 28 Jul 2015 17:31:06 +0300 (MSK) (envelope-from glebius@FreeBSD.org) X-Authentication-Warning: cell.glebius.int.ru: glebius set sender to glebius@FreeBSD.org using -f Date: Tue, 28 Jul 2015 17:31:06 +0300 From: Gleb Smirnoff To: Mark Johnston , Jason Unovitch Cc: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: Re: svn commit: r285782 - head/usr.bin/netstat Message-ID: <20150728143106.GY72729@FreeBSD.org> References: <201507212357.t6LNvd7K023794@repo.freebsd.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <201507212357.t6LNvd7K023794@repo.freebsd.org> User-Agent: Mutt/1.5.23 (2014-03-12) X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 28 Jul 2015 14:31:10 -0000 Mark, Jason, On Tue, Jul 21, 2015 at 11:57:39PM +0000, Mark Johnston wrote: M> Fix counter reads on platforms where sizeof(uint64_t) != sizeof(uint64_t *). M> M> In the kernel, structs such as tcpstat are manipulated as an array of M> counter_u64_t (uint64_t *), but made visible to userland as an array of M> uint64_t. kread_counters() was previously copying the counter array into M> user space and sequentially overwriting each counter with its value. This M> mostly affects IPsec counters, as other counters are exported via sysctl. M> M> PR: 201700 M> Tested by: Jason Unovitch Thanks for fixing the bug after me. One question, though: why do you use sizeof(u_long) instead of size of a pointer? M> ============================================================================== M> --- head/usr.bin/netstat/main.c Tue Jul 21 23:44:36 2015 (r285781) M> +++ head/usr.bin/netstat/main.c Tue Jul 21 23:57:38 2015 (r285782) M> @@ -776,19 +776,31 @@ kread_counter(u_long addr) M> int M> kread_counters(u_long addr, void *buf, size_t size) M> { M> - uint64_t *c = buf; M> + uint64_t *c; M> + u_long *counters; M> + size_t i, n; M> M> if (kvmd_init() < 0) M> return (-1); M> M> - if (kread(addr, buf, size) < 0) M> + if (size % sizeof(uint64_t) != 0) { M> + xo_warnx("kread_counters: invalid counter set size"); M> return (-1); M> + } M> M> - while (size != 0) { M> - *c = kvm_counter_u64_fetch(kvmd, *c); M> - size -= sizeof(*c); M> - c++; M> + n = size / sizeof(uint64_t); M> + if ((counters = malloc(n * sizeof(u_long))) == NULL) M> + xo_err(-1, "malloc"); M> + if (kread(addr, counters, n * sizeof(u_long)) < 0) { M> + free(counters); M> + return (-1); M> } M> + M> + c = buf; M> + for (i = 0; i < n; i++) M> + c[i] = kvm_counter_u64_fetch(kvmd, counters[i]); M> + M> + free(counters); M> return (0); M> } -- Totus tuus, Glebius.