Message ID | 20230627144339.144478-22-Julia.Lawall@inria.fr |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp8263875vqr; Tue, 27 Jun 2023 08:03:13 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6K8jVgrQ9SNoOfiRwzfSlWXO60WbQWHnx/0WpyKjonuyZCq7DmgJ/AZTQD20TgtwtUsfJI X-Received: by 2002:a17:902:dac7:b0:1b0:3ab6:5143 with SMTP id q7-20020a170902dac700b001b03ab65143mr6329833plx.68.1687878192941; Tue, 27 Jun 2023 08:03:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687878192; cv=none; d=google.com; s=arc-20160816; b=t1uac0KWKhhOKSaiPWqf9WAKZkHmmMrewKAPJYdwuHMEqIQEiYptO80dGIFKWUz4Zq x0fP4SeRAEVKQl80J482Dc7ZPFzTVjtwtEdXWG1hvw7JeLQtP0YrKkv42NsAjuvVPYIb 8SLLwJNUCWC+ujIsGhJ2gd1W8LyG1EQUrfKXe2xwzfPruUHA+8qFwW+ubC1+54JGJHA5 2z4/JkD5F47y5TXB0n8Gzn+KtrPHGgmP6IZ6NLa66uzUmQIqqld6si12b0UwhU5LdPYl sBO0afxDK3YJ5YGEXqHJfnXkydRIsMgc8JFF+2AMck2Qu6EX3lfykutwvdX51NfLHPG8 wdQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=fwBApC6jUrjlR2TIvGLbzPMAAmAGjDhOLjgURNPzPzY=; fh=s2znx5y9PkrupoBVYpjXEyh2OcIIRwmeyfdKzbiZ+kM=; b=omgP5lQY7dewi416Qh9RZk/60UhAEiXMDdXOySvE1SO8HFul5r2jo12gDzEdqwgqw8 S3FCBSKWr7hfjSGa4xItSCXYmZEGREJVKQyHBnQuVxruhpx8/uW8LCgr44EQCMR2v989 8XMaIjQwPWycEdrdmZNd4XM4ufb+X02Ko4PGAyavCTPnpLTTYriMN/8XhiguFEEI6TK6 ISx+PReXXMB9Ez5tJ2rpDi6x8Zd8elq0NmCqEwmcEPdplIomO2Bcs2e6TkONOSk3PpbB 29jwYhvz3kWkm1ewiDoPeUP/zuYfUZ5wuv1xUFEvDeeshpCPZZHxghY1HJzpSspUfjvl HYnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@inria.fr header.s=dc header.b=BHgadewr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=inria.fr Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w10-20020a17090a8a0a00b0025653dc2881si10169223pjn.23.2023.06.27.08.02.54; Tue, 27 Jun 2023 08:03:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@inria.fr header.s=dc header.b=BHgadewr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=inria.fr Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232035AbjF0OrC (ORCPT <rfc822;nicolai.engesland@gmail.com> + 99 others); Tue, 27 Jun 2023 10:47:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231989AbjF0Op5 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 27 Jun 2023 10:45:57 -0400 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94BC72D69; Tue, 27 Jun 2023 07:45:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=inria.fr; s=dc; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fwBApC6jUrjlR2TIvGLbzPMAAmAGjDhOLjgURNPzPzY=; b=BHgadewrKiM5wnoPnQZAWH8o6p5BCcSHwzkn5yYPbk0N0IP7/9sSeR7N sqE5B+1PHNUwj6GGrZBBO0y0feNdNGhOCNhIeNH3u8ZyMFbrOUfyOJO54 B0p1GvJMIraCE0QjixXzsjSnRtyRFnT16pIhfE20xApj+YjoQ+3KwB0vz c=; Authentication-Results: mail2-relais-roc.national.inria.fr; dkim=none (message not signed) header.i=none; spf=SoftFail smtp.mailfrom=Julia.Lawall@inria.fr; dmarc=fail (p=none dis=none) d=inria.fr X-IronPort-AV: E=Sophos;i="6.01,162,1684792800"; d="scan'208";a="114936344" Received: from i80.paris.inria.fr (HELO i80.paris.inria.fr.) ([128.93.90.48]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jun 2023 16:43:53 +0200 From: Julia Lawall <Julia.Lawall@inria.fr> To: Jarkko Sakkinen <jarkko@kernel.org> Cc: kernel-janitors@vger.kernel.org, keescook@chromium.org, christophe.jaillet@wanadoo.fr, kuba@kernel.org, Dave Hansen <dave.hansen@linux.intel.com>, Thomas Gleixner <tglx@linutronix.de>, Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>, x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>, linux-sgx@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 21/24] x86/sgx: use vmalloc_array and vcalloc Date: Tue, 27 Jun 2023 16:43:36 +0200 Message-Id: <20230627144339.144478-22-Julia.Lawall@inria.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230627144339.144478-1-Julia.Lawall@inria.fr> References: <20230627144339.144478-1-Julia.Lawall@inria.fr> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1769868564000114205?= X-GMAIL-MSGID: =?utf-8?q?1769868564000114205?= |
Series |
use vmalloc_array and vcalloc
|
|
Commit Message
Julia Lawall
June 27, 2023, 2:43 p.m. UTC
Use vmalloc_array and vcalloc to protect against
multiplication overflows.
The changes were done using the following Coccinelle
semantic patch:
// <smpl>
@initialize:ocaml@
@@
let rename alloc =
match alloc with
"vmalloc" -> "vmalloc_array"
| "vzalloc" -> "vcalloc"
| _ -> failwith "unknown"
@@
size_t e1,e2;
constant C1, C2;
expression E1, E2, COUNT, x1, x2, x3;
typedef u8;
typedef __u8;
type t = {u8,__u8,char,unsigned char};
identifier alloc = {vmalloc,vzalloc};
fresh identifier realloc = script:ocaml(alloc) { rename alloc };
@@
(
alloc(x1*x2*x3)
|
alloc(C1 * C2)
|
alloc((sizeof(t)) * (COUNT), ...)
|
- alloc((e1) * (e2))
+ realloc(e1, e2)
|
- alloc((e1) * (COUNT))
+ realloc(COUNT, e1)
|
- alloc((E1) * (E2))
+ realloc(E1, E2)
)
// </smpl>
Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>
---
v2: Use vmalloc_array and vcalloc instead of array_size.
This also leaves a multiplication of a constant by a sizeof
as is. Two patches are thus dropped from the series.
arch/x86/kernel/cpu/sgx/main.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
On 6/27/23 07:43, Julia Lawall wrote: > Use vmalloc_array and vcalloc to protect against > multiplication overflows. ... > diff -u -p a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c > --- a/arch/x86/kernel/cpu/sgx/main.c > +++ b/arch/x86/kernel/cpu/sgx/main.c > @@ -628,7 +628,7 @@ static bool __init sgx_setup_epc_section > if (!section->virt_addr) > return false; > > - section->pages = vmalloc(nr_pages * sizeof(struct sgx_epc_page)); > + section->pages = vmalloc_array(nr_pages, sizeof(struct sgx_epc_page)); > if (!section->pages) { I'm not sure that changelog matches the code. 'nr_pages' here is an 'unsigned long' and The sizeof()==32. In practice, the multiplication can be done with a shift, and the ulong is a *LONG* way from overflowing. I'll accept that, as a general rule, vmalloc_array() is the preferred form. It's totally possible that someone could copy and paste the nr_foo*sizeof(struct bar) code over to a place where nr_foo is a more troublesome type. But, if that's the true motivation, could we please say that in the changelog? As it stands, it's a bit silly to be talking about multiplication overflows, unless I'm missing something totally obvious.
On Tue, 27 Jun 2023, Dave Hansen wrote: > On 6/27/23 07:43, Julia Lawall wrote: > > Use vmalloc_array and vcalloc to protect against > > multiplication overflows. > ... > > diff -u -p a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c > > --- a/arch/x86/kernel/cpu/sgx/main.c > > +++ b/arch/x86/kernel/cpu/sgx/main.c > > @@ -628,7 +628,7 @@ static bool __init sgx_setup_epc_section > > if (!section->virt_addr) > > return false; > > > > - section->pages = vmalloc(nr_pages * sizeof(struct sgx_epc_page)); > > + section->pages = vmalloc_array(nr_pages, sizeof(struct sgx_epc_page)); > > if (!section->pages) { > > I'm not sure that changelog matches the code. > > 'nr_pages' here is an 'unsigned long' and The sizeof()==32. In > practice, the multiplication can be done with a shift, and the ulong is > a *LONG* way from overflowing. > > I'll accept that, as a general rule, vmalloc_array() is the preferred > form. It's totally possible that someone could copy and paste the > nr_foo*sizeof(struct bar) code over to a place where nr_foo is a more > troublesome type. > > But, if that's the true motivation, could we please say that in the > changelog? As it stands, it's a bit silly to be talking about > multiplication overflows, unless I'm missing something totally obvious. If it is certain that no overflow is possible, then perhaps it is fine to drop the patch? I didn't change cases where both arguments are constants nor where the result of the sizeof is 1. But I also didn't do a careful analysis to see if an overflow is possible given the possible values involved. Or if it seems better to keep the change, I can also change the log message. julia
On 6/27/23 08:01, Julia Lawall wrote: > If it is certain that no overflow is possible, then perhaps it is fine to > drop the patch? It's impossible in practice in this case because the code is 64-bit only and uses an 'unsigned long'. But, like I said, I can see that same vmalloc() being copied-and-pasted or moved to a 32-bit system and theoretically causing problems in rare scenarios. I'd probably just drop this patch.
diff -u -p a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -628,7 +628,7 @@ static bool __init sgx_setup_epc_section if (!section->virt_addr) return false; - section->pages = vmalloc(nr_pages * sizeof(struct sgx_epc_page)); + section->pages = vmalloc_array(nr_pages, sizeof(struct sgx_epc_page)); if (!section->pages) { memunmap(section->virt_addr); return false;