From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C4449C55179 for ; Tue, 27 Oct 2020 16:50:50 +0000 (UTC) Received: from hemlock.osuosl.org (smtp2.osuosl.org [140.211.166.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5256422202 for ; Tue, 27 Oct 2020 16:50:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5256422202 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=perches.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=virtualization-bounces@lists.linux-foundation.org Received: from localhost (localhost [127.0.0.1]) by hemlock.osuosl.org (Postfix) with ESMTP id 9C75D872D8; Tue, 27 Oct 2020 16:50:49 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from hemlock.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id YWrfaGMoxahx; Tue, 27 Oct 2020 16:50:48 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by hemlock.osuosl.org (Postfix) with ESMTP id C2388872CE; Tue, 27 Oct 2020 16:50:48 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id AC1DBC088B; Tue, 27 Oct 2020 16:50:48 +0000 (UTC) Received: from whitealder.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 79574C0051 for ; Tue, 27 Oct 2020 16:50:47 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by whitealder.osuosl.org (Postfix) with ESMTP id 71C41860A8 for ; Tue, 27 Oct 2020 16:50:47 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from whitealder.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id MPvlogptL-n1 for ; Tue, 27 Oct 2020 16:50:46 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtprelay.hostedemail.com (smtprelay0236.hostedemail.com [216.40.44.236]) by whitealder.osuosl.org (Postfix) with ESMTPS id 8080986064 for ; Tue, 27 Oct 2020 16:50:46 +0000 (UTC) Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay04.hostedemail.com (Postfix) with ESMTP id 86139180A7FE0; Tue, 27 Oct 2020 16:50:43 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-HE-Tag: bag02_2a11e012727d X-Filterd-Recvd-Size: 4083 Received: from XPS-9350.home (unknown [47.151.133.149]) (Authenticated sender: joe@perches.com) by omf19.hostedemail.com (Postfix) with ESMTPA; Tue, 27 Oct 2020 16:50:38 +0000 (UTC) Message-ID: <685d850347a1191bba8ba7766fc409b140d18f03.camel@perches.com> Subject: Re: [PATCH 3/8] vhost: vringh: use krealloc_array() From: Joe Perches To: "Michael S. Tsirkin" , Bartosz Golaszewski Date: Tue, 27 Oct 2020 09:50:36 -0700 In-Reply-To: <20201027112607-mutt-send-email-mst@kernel.org> References: <20201027121725.24660-1-brgl@bgdev.pl> <20201027121725.24660-4-brgl@bgdev.pl> <20201027112607-mutt-send-email-mst@kernel.org> User-Agent: Evolution 3.38.1-1 MIME-Version: 1.0 Cc: alsa-devel@alsa-project.org, kvm@vger.kernel.org, David Airlie , Gustavo Padovan , Linus Walleij , dri-devel@lists.freedesktop.org, Jaroslav Kysela , linux-mm@kvack.org, Christoph Lameter , Sumit Semwal , Mauro Carvalho Chehab , Bartosz Golaszewski , Alexander Shishkin , David Rientjes , virtualization@lists.linux-foundation.org, linux-media@vger.kernel.org, Robert Richter , Thomas Zimmermann , Maarten Lankhorst , Maxime Ripard , linaro-mm-sig@lists.linaro.org, linux-gpio@vger.kernel.org, Borislav Petkov , Andy Shevchenko , Joonsoo Kim , linux-edac@vger.kernel.org, Tony Luck , netdev@vger.kernel.org, Takashi Iwai , linux-kernel@vger.kernel.org, Pekka Enberg , James Morse , Daniel Vetter , Andrew Morton , Christian =?ISO-8859-1?Q?K=F6nig?= X-BeenThere: virtualization@lists.linux-foundation.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Linux virtualization List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: virtualization-bounces@lists.linux-foundation.org Sender: "Virtualization" On Tue, 2020-10-27 at 11:28 -0400, Michael S. Tsirkin wrote: > On Tue, Oct 27, 2020 at 01:17:20PM +0100, Bartosz Golaszewski wrote: > > From: Bartosz Golaszewski > > > > Use the helper that checks for overflows internally instead of manually > > calculating the size of the new array. > > > > Signed-off-by: Bartosz Golaszewski > > No problem with the patch, it does introduce some symmetry in the code. Perhaps more symmetry by using kmemdup --- drivers/vhost/vringh.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/drivers/vhost/vringh.c b/drivers/vhost/vringh.c index 8bd8b403f087..99222a3651cd 100644 --- a/drivers/vhost/vringh.c +++ b/drivers/vhost/vringh.c @@ -191,26 +191,23 @@ static int move_to_indirect(const struct vringh *vrh, static int resize_iovec(struct vringh_kiov *iov, gfp_t gfp) { struct kvec *new; - unsigned int flag, new_num = (iov->max_num & ~VRINGH_IOV_ALLOCATED) * 2; + size_t new_num = (iov->max_num & ~VRINGH_IOV_ALLOCATED) * 2; + size_t size; if (new_num < 8) new_num = 8; - flag = (iov->max_num & VRINGH_IOV_ALLOCATED); - if (flag) - new = krealloc(iov->iov, new_num * sizeof(struct iovec), gfp); - else { - new = kmalloc_array(new_num, sizeof(struct iovec), gfp); - if (new) { - memcpy(new, iov->iov, - iov->max_num * sizeof(struct iovec)); - flag = VRINGH_IOV_ALLOCATED; - } - } + if (unlikely(check_mul_overflow(new_num, sizeof(struct iovec), &size))) + return -ENOMEM; + + if (iov->max_num & VRINGH_IOV_ALLOCATED) + new = krealloc(iov->iov, size, gfp); + else + new = kmemdup(iov->iov, size, gfp); if (!new) return -ENOMEM; iov->iov = new; - iov->max_num = (new_num | flag); + iov->max_num = new_num | VRINGH_IOV_ALLOCATED; return 0; } _______________________________________________ Virtualization mailing list Virtualization@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/virtualization