From owner-svn-src-stable-12@freebsd.org Thu Feb 27 15:22:20 2020 Return-Path: Delivered-To: svn-src-stable-12@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 36DF7244E43; Thu, 27 Feb 2020 15:22:20 +0000 (UTC) (envelope-from avg@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 48SxHG6y36z4BNs; Thu, 27 Feb 2020 15:22:18 +0000 (UTC) (envelope-from avg@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 983F61ACDA; Thu, 27 Feb 2020 15:22:18 +0000 (UTC) (envelope-from avg@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 01RFMIcY083724; Thu, 27 Feb 2020 15:22:18 GMT (envelope-from avg@FreeBSD.org) Received: (from avg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 01RFMIST083723; Thu, 27 Feb 2020 15:22:18 GMT (envelope-from avg@FreeBSD.org) Message-Id: <202002271522.01RFMIST083723@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: avg set sender to avg@FreeBSD.org using -f From: Andriy Gapon Date: Thu, 27 Feb 2020 15:22:18 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r358388 - stable/12/lib/libdevstat X-SVN-Group: stable-12 X-SVN-Commit-Author: avg X-SVN-Commit-Paths: stable/12/lib/libdevstat X-SVN-Commit-Revision: 358388 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable-12@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for only the 12-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 27 Feb 2020 15:22:20 -0000 Author: avg Date: Thu Feb 27 15:22:18 2020 New Revision: 358388 URL: https://svnweb.freebsd.org/changeset/base/358388 Log: MFC r355325: devstat_selectdevs: resize dev_select only after copying data The resizing could be a downsizing so some data would be lost and we could attempt to read past the end of the new memory allocation. Sponsored by: Panzura Modified: stable/12/lib/libdevstat/devstat.c Directory Properties: stable/12/ (props changed) Modified: stable/12/lib/libdevstat/devstat.c ============================================================================== --- stable/12/lib/libdevstat/devstat.c Thu Feb 27 15:21:05 2020 (r358387) +++ stable/12/lib/libdevstat/devstat.c Thu Feb 27 15:22:18 2020 (r358388) @@ -584,10 +584,10 @@ devstat_selectdevs(struct device_selection **dev_selec * In this case, we have selected devices before, but the device * list has changed since we last selected devices, so we need to * either enlarge or reduce the size of the device selection list. + * But delay the resizing until after copying the data to old_dev_select + * as to not lose any data in the case of reducing the size. */ } else if (*num_selections != numdevs) { - *dev_select = (struct device_selection *)reallocf(*dev_select, - numdevs * sizeof(struct device_selection)); *select_generation = current_generation; init_selections = 1; /* @@ -643,6 +643,11 @@ devstat_selectdevs(struct device_selection **dev_selec old_num_selections = *num_selections; bcopy(*dev_select, old_dev_select, sizeof(struct device_selection) * *num_selections); + } + + if (!changed && *num_selections != numdevs) { + *dev_select = (struct device_selection *)reallocf(*dev_select, + numdevs * sizeof(struct device_selection)); } if (init_selections != 0) {