From patchwork Tue Jun 27 14:43:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julia Lawall X-Patchwork-Id: 113449 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp8252402vqr; Tue, 27 Jun 2023 07:46:00 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4bd36Lljb2bVFTgT+UFLy7e8rVlRCIGAncPxZAnVtu25b5KZPfU31HqjkOYcIyCmrd/9J+ X-Received: by 2002:a17:90a:74c5:b0:262:e912:270f with SMTP id p5-20020a17090a74c500b00262e912270fmr6069455pjl.29.1687877160527; Tue, 27 Jun 2023 07:46:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687877160; cv=none; d=google.com; s=arc-20160816; b=pPRmYHHovY7g2e27hUxWLVtZPMRQvBPOtPLORsUUOTh5TvjErdjyBRu9PFVUa8gd/t StquQWJGxGB7bsz+C0XRe1Xvu0ScQJSGwNbmV7PjiAq3n99sLxl4sjDrX21hPjoVDO4n a5LKwwuVAlH+hmg9Slojq7HChvcPX6R2E6ao54xsWwpBi1koDQDSoEfx5wqdLibGWG15 yBf/Sx+Q6UzSw1td6tvxs5jSTh7DS+PeUp/sPqhvHt9WPr96Q6lBSuWDvMgnvc0PbUUZ 5JD2A9fraoCBo4uiQSvaD+5NKpd6Hb9ogDK7mIDMkv+2oVoASZ2yZ8PmA7B3hAYj8nz/ aVyA== 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=cW6FsUc372OgjCXWGlVTOpJDBn/sKhwbAV7MvUlsfDU=; fh=lH17MxD66mEZA0q9ApMflL6110os5JJlJCkBGCyBnjQ=; b=zaiIB/1P5wd0Wlh84PlF1twT2LhzXG/TLbx9IXgEtB6UoGhZRZSJcNGjtnEY0hmsTc hfT9PAwlof4L9cZasm9zn36gUp5FeHqvJTnDhdssOU12OlJFKD/1UmQT0V7ok87nE0bQ HhwWk9cksuKkyVxKW8QMD66WqrkjYQjVUq++zdmMOuXVDqIWh5adTt9gC01tflXUE8Na IC3i+Ea/E/qsnwBnqIKEKIPO9oaefIfikdtmdhCIuaQfCUVYZ/9DApgnxCQC4DLElL2h rcZMLfIeIihUkidQxNZ4IRp9oCJoq2V3P4d5C+VDjUN7aGAN27d7SlBnmHEX746tMBAn EUJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@inria.fr header.s=dc header.b=jWt7EReJ; 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 c14-20020a631c4e000000b00543ae703b97si7545769pgm.209.2023.06.27.07.45.47; Tue, 27 Jun 2023 07:46:00 -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=jWt7EReJ; 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 S231771AbjF0Op0 (ORCPT + 99 others); Tue, 27 Jun 2023 10:45:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43494 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231684AbjF0Oop (ORCPT ); Tue, 27 Jun 2023 10:44:45 -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 7654F12C; Tue, 27 Jun 2023 07:44:12 -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=cW6FsUc372OgjCXWGlVTOpJDBn/sKhwbAV7MvUlsfDU=; b=jWt7EReJ5kDtaESX69GV8d4liYZONaNjYK6YwdVqiDhOrR+UO9w9kKRo +ywuVIwSPjmV/l3kagzTPaaXWEA08ystOtiHsidk5aXckj/9gzF9Rpoj3 I1ITEQ51Rbp6KM/FiTv0VHqRI8yNXSJ9Lc78ZzS16NIIda7XjaWrQUZT3 A=; 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="114936318" 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:51 +0200 From: Julia Lawall To: linux-kernel@vger.kernel.org Cc: kernel-janitors@vger.kernel.org, keescook@chromium.org, christophe.jaillet@wanadoo.fr, kuba@kernel.org Subject: [PATCH v2 01/24] lib/test_vmalloc.c: use vmalloc_array and vcalloc Date: Tue, 27 Jun 2023 16:43:16 +0200 Message-Id: <20230627144339.144478-2-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 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1769867481705279300?= X-GMAIL-MSGID: =?utf-8?q?1769867481705279300?= Use vmalloc_array and vcalloc to protect against multiplication overflows. The changes were done using the following Coccinelle semantic patch: // @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) ) // Signed-off-by: Julia Lawall --- 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. lib/test_vmalloc.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff -u -p a/lib/test_vmalloc.c b/lib/test_vmalloc.c --- a/lib/test_vmalloc.c +++ b/lib/test_vmalloc.c @@ -156,7 +156,7 @@ static int random_size_alloc_test(void) for (i = 0; i < test_loop_count; i++) { n = get_random_u32_inclusive(1, 100); - p = vmalloc(n * PAGE_SIZE); + p = vmalloc_array(n, PAGE_SIZE); if (!p) return -1; @@ -221,11 +221,11 @@ static int full_fit_alloc_test(void) junk_length = fls(num_online_cpus()); junk_length *= (32 * 1024 * 1024 / PAGE_SIZE); - ptr = vmalloc(sizeof(void *) * junk_length); + ptr = vmalloc_array(junk_length, sizeof(void *)); if (!ptr) return rv; - junk_ptr = vmalloc(sizeof(void *) * junk_length); + junk_ptr = vmalloc_array(junk_length, sizeof(void *)); if (!junk_ptr) { vfree(ptr); return rv; @@ -271,7 +271,8 @@ static int fix_size_alloc_test(void) if (use_huge) ptr = vmalloc_huge((nr_pages > 0 ? nr_pages:1) * PAGE_SIZE, GFP_KERNEL); else - ptr = vmalloc((nr_pages > 0 ? nr_pages:1) * PAGE_SIZE); + ptr = vmalloc_array(nr_pages > 0 ? nr_pages : 1, + PAGE_SIZE); if (!ptr) return -1;