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 27C45C4363A for ; Tue, 27 Oct 2020 17:08:13 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8082A2224A for ; Tue, 27 Oct 2020 17:08:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8082A2224A Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=perches.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id BE0516B0072; Tue, 27 Oct 2020 13:08:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BB5FC6B0073; Tue, 27 Oct 2020 13:08:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ACC8F6B0074; Tue, 27 Oct 2020 13:08:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0141.hostedemail.com [216.40.44.141]) by kanga.kvack.org (Postfix) with ESMTP id 7FCAF6B0072 for ; Tue, 27 Oct 2020 13:08:11 -0400 (EDT) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 2C401180AD806 for ; Tue, 27 Oct 2020 17:08:11 +0000 (UTC) X-FDA: 77418338382.24.table91_3e0956d2727d Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin24.hostedemail.com (Postfix) with ESMTP id 0B3B91A4A5 for ; Tue, 27 Oct 2020 17:08:11 +0000 (UTC) X-HE-Tag: table91_3e0956d2727d X-Filterd-Recvd-Size: 6138 Received: from smtprelay.hostedemail.com (smtprelay0152.hostedemail.com [216.40.44.152]) by imf08.hostedemail.com (Postfix) with ESMTP for ; Tue, 27 Oct 2020 17:08:10 +0000 (UTC) Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay08.hostedemail.com (Postfix) with ESMTP id 45489182CED2A; Tue, 27 Oct 2020 17:08:10 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-HE-Tag: rake71_590ddfb2727d X-Filterd-Recvd-Size: 4916 Received: from XPS-9350.home (unknown [47.151.133.149]) (Authenticated sender: joe@perches.com) by omf05.hostedemail.com (Postfix) with ESMTPA; Tue, 27 Oct 2020 17:08:04 +0000 (UTC) Message-ID: <2767969b94fd66db1fb0fc13b5783ae65b7deb2f.camel@perches.com> Subject: Re: [PATCH 3/8] vhost: vringh: use krealloc_array() From: Joe Perches To: Bartosz Golaszewski Cc: "Michael S. Tsirkin" , Bartosz Golaszewski , Andy Shevchenko , Sumit Semwal , Gustavo Padovan , Christian =?ISO-8859-1?Q?K=F6nig?= , Mauro Carvalho Chehab , Borislav Petkov , Tony Luck , James Morse , Robert Richter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Alexander Shishkin , Linus Walleij , Jason Wang , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Jaroslav Kysela , Takashi Iwai , linux-media , linux-drm , linaro-mm-sig@lists.linaro.org, LKML , linux-edac@vger.kernel.org, linux-gpio , kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev , linux-mm@kvack.org, Linux-ALSA Date: Tue, 27 Oct 2020 10:08:02 -0700 In-Reply-To: References: <20201027121725.24660-1-brgl@bgdev.pl> <20201027121725.24660-4-brgl@bgdev.pl> <20201027112607-mutt-send-email-mst@kernel.org> <685d850347a1191bba8ba7766fc409b140d18f03.camel@perches.com> Content-Type: text/plain; charset="ISO-8859-1" User-Agent: Evolution 3.38.1-1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Tue, 2020-10-27 at 17:58 +0100, Bartosz Golaszewski wrote: > On Tue, Oct 27, 2020 at 5:50 PM Joe Perches wrote: > >=20 > > 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 > > > >=20 > > > > Use the helper that checks for overflows internally instead of ma= nually > > > > calculating the size of the new array. > > > >=20 > > > > Signed-off-by: Bartosz Golaszewski > > >=20 > > > No problem with the patch, it does introduce some symmetry in the c= ode. > >=20 > > Perhaps more symmetry by using kmemdup > > --- > > =A0drivers/vhost/vringh.c | 23 ++++++++++------------- > > =A01 file changed, 10 insertions(+), 13 deletions(-) > >=20 > > 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, > > =A0static int resize_iovec(struct vringh_kiov *iov, gfp_t gfp) > > =A0{ > > =A0=A0=A0=A0=A0=A0=A0=A0struct kvec *new; > > - unsigned int flag, new_num =3D (iov->max_num & ~VRINGH_IOV_AL= LOCATED) * 2; > > + size_t new_num =3D (iov->max_num & ~VRINGH_IOV_ALLOCATED) * 2= ; > > + size_t size; > >=20 > > =A0=A0=A0=A0=A0=A0=A0=A0if (new_num < 8) > > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0new_num =3D 8; > >=20 > > - flag =3D (iov->max_num & VRINGH_IOV_ALLOCATED); > > - if (flag) > > - new =3D krealloc(iov->iov, new_num * sizeof(struct io= vec), gfp); > > - else { > > - new =3D kmalloc_array(new_num, sizeof(struct iovec), = gfp); > > - if (new) { > > - memcpy(new, iov->iov, > > - iov->max_num * sizeof(struct iovec)); > > - flag =3D VRINGH_IOV_ALLOCATED; > > - } > > - } > > + if (unlikely(check_mul_overflow(new_num, sizeof(struct iovec)= , &size))) > > + return -ENOMEM; > > + >=20 > The whole point of using helpers such as kmalloc_array() is not doing > these checks manually. Tradeoffs for in readability for overflow and not mistyping or doing the multiplication of iov->max_num * sizeof(struct iovec) twice. Just fyi: the realloc doesn't do a multiplication overflow test as written so the suggestion is slightly more resistant to defect. =20