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 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AD86CC43217 for ; Thu, 30 Sep 2021 22:42:01 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 57E5261261 for ; Thu, 30 Sep 2021 22:42:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 57E5261261 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id D46CA9400E6; Thu, 30 Sep 2021 18:42:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CCF4D9400E4; Thu, 30 Sep 2021 18:42:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B70969400E6; Thu, 30 Sep 2021 18:42:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0214.hostedemail.com [216.40.44.214]) by kanga.kvack.org (Postfix) with ESMTP id A5C7E9400E4 for ; Thu, 30 Sep 2021 18:42:00 -0400 (EDT) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 69BBB1845738A for ; Thu, 30 Sep 2021 22:42:00 +0000 (UTC) X-FDA: 78645714000.17.2FFCDD7 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf26.hostedemail.com (Postfix) with ESMTP id 092E62001706 for ; Thu, 30 Sep 2021 22:41:59 +0000 (UTC) Received: by mail.kernel.org (Postfix) with ESMTPSA id 2FA9F6109E; Thu, 30 Sep 2021 22:41:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1633041719; bh=FxgCKikKkujaOhD0GKVlpovh2JSLoa6S3E3sVjKqxC0=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=dGDIR4AzX+Adf/hTKvFpvU2ysWfztQWbBPb450/GzHL/nYyM8pG7uXYQYO12ToIq2 xMKbS51LnW3DeCWgI0ym0sVJmtBukbSZD+0dBOOasLaRHQJIA7FaNiMuACQ5wYwB8V 8j+HRERUIGt2XhvbB1QlWrIoPTq2o0lx20nz3rgnLIZLVRu7+JwQji2xmeMYCTzgI9 UxEfz0qKXBWOLn7s7naVQhz4rSnaxOFOBqeZfHb5lH6nrR7K6Oh92eTs/XD9zZDGxn 6UTM7U0bFFjrV5VD3HDtQfKq2IrpJw1Yak+ptLBONhPTdK+IYmMKbAbE+BZV2t5ovW MKarUjHBFvcog== Date: Thu, 30 Sep 2021 17:46:04 -0500 From: "Gustavo A. R. Silva" To: Kees Cook Cc: Andrew Morton , kernel test robot , Matt Porter , Alexandre Bounine , Jing Xiangfeng , Ira Weiny , Souptick Joarder , John Hubbard , Joe Perches , Miguel Ojeda , Nathan Chancellor , Nick Desaulniers , Andy Whitcroft , Dwaipayan Ray , Lukas Bulwahn , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Vlastimil Babka , Daniel Micay , Dennis Zhou , Tejun Heo , Masahiro Yamada , Michal Marek , clang-built-linux@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH v3 1/8] rapidio: Avoid bogus __alloc_size warning Message-ID: <20210930224604.GA906353@embeddedor> References: <20210930222704.2631604-1-keescook@chromium.org> <20210930222704.2631604-2-keescook@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210930222704.2631604-2-keescook@chromium.org> X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 092E62001706 X-Stat-Signature: q3txxn9bdzwtyrfgoh4nrgwhb8pjfnqr Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=dGDIR4Az; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of gustavoars@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=gustavoars@kernel.org X-HE-Tag: 1633041719-57283 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 Thu, Sep 30, 2021 at 03:26:57PM -0700, Kees Cook wrote: > After adding __alloc_size attributes to the allocators, GCC 9.3 (but not > later) may incorrectly evaluate the arguments to check_copy_size(), > getting seemingly confused by the size being returned from array_size(). > Instead, perform the calculation once, which both makes the code more > readable and avoids the bug in GCC. > > In file included from arch/x86/include/asm/preempt.h:7, > from include/linux/preempt.h:78, > from include/linux/spinlock.h:55, > from include/linux/mm_types.h:9, > from include/linux/buildid.h:5, > from include/linux/module.h:14, > from drivers/rapidio/devices/rio_mport_cdev.c:13: > In function 'check_copy_size', > inlined from 'copy_from_user' at include/linux/uaccess.h:191:6, > inlined from 'rio_mport_transfer_ioctl' at drivers/rapidio/devices/rio_mport_cdev.c:983:6: > include/linux/thread_info.h:213:4: error: call to '__bad_copy_to' declared with attribute error: copy destination size is too small > 213 | __bad_copy_to(); > | ^~~~~~~~~~~~~~~ > > But the allocation size and the copy size are identical: > > transfer = vmalloc(array_size(sizeof(*transfer), transaction.count)); > if (!transfer) > return -ENOMEM; > > if (unlikely(copy_from_user(transfer, > (void __user *)(uintptr_t)transaction.block, > array_size(sizeof(*transfer), transaction.count)))) { > > Reported-by: kernel test robot > Link: https://lore.kernel.org/linux-mm/202109091134.FHnRmRxu-lkp@intel.com/ > Cc: Matt Porter > Cc: Alexandre Bounine > Cc: Jing Xiangfeng > Cc: Ira Weiny > Cc: Souptick Joarder > Cc: Gustavo A. R. Silva > Signed-off-by: Kees Cook > Reviewed-by: John Hubbard Reviewed-by: Gustavo A. R. Silva Thanks -- Gustavo > --- > drivers/rapidio/devices/rio_mport_cdev.c | 9 +++++---- > 1 file changed, 5 insertions(+), 4 deletions(-) > > diff --git a/drivers/rapidio/devices/rio_mport_cdev.c b/drivers/rapidio/devices/rio_mport_cdev.c > index 94331d999d27..7df466e22282 100644 > --- a/drivers/rapidio/devices/rio_mport_cdev.c > +++ b/drivers/rapidio/devices/rio_mport_cdev.c > @@ -965,6 +965,7 @@ static int rio_mport_transfer_ioctl(struct file *filp, void __user *arg) > struct rio_transfer_io *transfer; > enum dma_data_direction dir; > int i, ret = 0; > + size_t size; > > if (unlikely(copy_from_user(&transaction, arg, sizeof(transaction)))) > return -EFAULT; > @@ -976,13 +977,14 @@ static int rio_mport_transfer_ioctl(struct file *filp, void __user *arg) > priv->md->properties.transfer_mode) == 0) > return -ENODEV; > > - transfer = vmalloc(array_size(sizeof(*transfer), transaction.count)); > + size = array_size(sizeof(*transfer), transaction.count); > + transfer = vmalloc(size); > if (!transfer) > return -ENOMEM; > > if (unlikely(copy_from_user(transfer, > (void __user *)(uintptr_t)transaction.block, > - array_size(sizeof(*transfer), transaction.count)))) { > + size))) { > ret = -EFAULT; > goto out_free; > } > @@ -994,8 +996,7 @@ static int rio_mport_transfer_ioctl(struct file *filp, void __user *arg) > transaction.sync, dir, &transfer[i]); > > if (unlikely(copy_to_user((void __user *)(uintptr_t)transaction.block, > - transfer, > - array_size(sizeof(*transfer), transaction.count)))) > + transfer, size))) > ret = -EFAULT; > > out_free: > -- > 2.30.2 >