From patchwork Mon Oct 9 19:41:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gustavo A. R. Silva" X-Patchwork-Id: 150307 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a888:0:b0:403:3b70:6f57 with SMTP id x8csp2084894vqo; Mon, 9 Oct 2023 12:41:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGvngR+b0Tlcdf0R+8ZAkvK1Bw2lTHDMb6VurfBnjrXHnQwo+tGGrSOBiSyMm6L8bluYkuZ X-Received: by 2002:a05:6a20:8e29:b0:15d:684d:f514 with SMTP id y41-20020a056a208e2900b0015d684df514mr16729780pzj.6.1696880517490; Mon, 09 Oct 2023 12:41:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696880517; cv=none; d=google.com; s=arc-20160816; b=Y4xkZ8bNgGJzwLHJ88PxeshVP/jr027gSTDhh88sEloEVlNFPO2MJ0WCAixOpGPIuo D5/Cv58oqpgwQaq5lvR3L6xlKtOg/dNnq55Bp9da/zjiCeOFy7mS9U/yebf93WTpM9oU 20kiOsbSloiBt/ik2HGfaRf/B+UkD3BAROTIQ/1Gc+OTESQTUhD8dRYuBdCAuXTRHIPM fUEQYS3EN3EkJQ1Jz6JGdotSoNJOsmgUH2gtgXs1Q23uCum+dqMFU7hryoBVwQbWyTog DlYOrXN7o5CImwQmUtvGEPESiAwkTJpNofsTHZYt5/Lb+C/NMNr2pqDzbj2llUnS2Vok dd/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=pi378Wr/cbhjQzGUziB8omxSpuf1Y/bdgaG1EwVHo9I=; fh=Kxi+pXmXusNl3GFSAfoeZ4biV4zdvCvLwD6n/Wb6lA4=; b=QM2zAQvcURhRoW57UvMJp316mwkfFif2Tt6fAroNg3GATL5o3HJ4m03AtaLiXalB8c Wc6wo7AQPtAbYHgfWU5qYdwAjhrKfaFjXurJDBLaz7oOrnzZgkXJApIZhMnXge2tZO0L AXH7gZpDp5cgquRJb4Ls/SPN+rUpgqAs27oq2ciuNTzSEDB8JYgTUwOHuC/K6UFgy8ks YHVv1GfG5ZglQQ8ox/Ppv2LlMJiaHwiBE/7HJ6QTOmK2ZUoQwNKq6VytlNO4Sfgyegw2 U8F01Cbj6k0l20lgC/vyxkD2hJkUDcAjJ5CCY6/aVUYM5wWxVrxeQbUnNfU+2d9eBG4O VaFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uhC6t7VJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id f3-20020a170902ce8300b001c1e4f9c63esi680419plg.491.2023.10.09.12.41.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 12:41:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uhC6t7VJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id CE4988056C44; Mon, 9 Oct 2023 12:41:54 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378439AbjJITlY (ORCPT + 18 others); Mon, 9 Oct 2023 15:41:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346674AbjJITlX (ORCPT ); Mon, 9 Oct 2023 15:41:23 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B6D793; Mon, 9 Oct 2023 12:41:22 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 01C09C433C7; Mon, 9 Oct 2023 19:41:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696880481; bh=3m8vfJotEQ3gbSMgx24f32JgP5HmYW7wJZrzxltQSBs=; h=Date:From:To:Cc:Subject:From; b=uhC6t7VJO+HL06uaAHUWAJUdKbYI1U0I4lHAaKZd4bTG5TO0SzI4JO02sEqNYsuDE ZT5ovm+BxxZ/PabeZEwHa5iWpZ7qTf0S7GezI4mDDGLsZ8KxhENtVfi7xXIMiXj9yu NB5aARhXR239GMlhoDAunPLa/kPwWmrjOfGUiQ1xW1nHrAv1j445CAlIYuhhHMTwub TmgB3wSrGsv/yyDQyCgjpfJ9+CNla5nIkaRna/JnEnLkt9+Ezyy2DK6wNr4KxU36fS /3xkom5ZGapfi75jW1iWdXlGkjIkss4HPI92i5JKnWHM/Fstt0+Hv1y0qcIQV64cvV qmnl8EpzozcQw== Date: Mon, 9 Oct 2023 13:41:18 -0600 From: "Gustavo A. R. Silva" To: Kalle Valo Cc: linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org Subject: [PATCH][next] hostap: Add __counted_by for struct prism2_download_data and use struct_size() Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=2.4 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Mon, 09 Oct 2023 12:41:54 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779308185482573613 X-GMAIL-MSGID: 1779308185482573613 Prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions). While there, use struct_size() helper, instead of the open-coded version, to calculate the size for the allocation of the whole flexible structure, including of course, the flexible-array member. This code was found with the help of Coccinelle, and audited and fixed manually. Signed-off-by: Gustavo A. R. Silva Reviewed-by: Kees Cook --- drivers/net/wireless/intersil/hostap/hostap_download.c | 3 +-- drivers/net/wireless/intersil/hostap/hostap_wlan.h | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/drivers/net/wireless/intersil/hostap/hostap_download.c b/drivers/net/wireless/intersil/hostap/hostap_download.c index 3672291ced5c..5e5bada28b5b 100644 --- a/drivers/net/wireless/intersil/hostap/hostap_download.c +++ b/drivers/net/wireless/intersil/hostap/hostap_download.c @@ -732,8 +732,7 @@ static int prism2_download(local_info_t *local, goto out; } - dl = kzalloc(sizeof(*dl) + param->num_areas * - sizeof(struct prism2_download_data_area), GFP_KERNEL); + dl = kzalloc(struct_size(dl, data, param->num_areas), GFP_KERNEL); if (dl == NULL) { ret = -ENOMEM; goto out; diff --git a/drivers/net/wireless/intersil/hostap/hostap_wlan.h b/drivers/net/wireless/intersil/hostap/hostap_wlan.h index c25cd21d18bd..f71c0545c0be 100644 --- a/drivers/net/wireless/intersil/hostap/hostap_wlan.h +++ b/drivers/net/wireless/intersil/hostap/hostap_wlan.h @@ -617,7 +617,7 @@ struct prism2_download_data { u32 addr; /* wlan card address */ u32 len; u8 *data; /* allocated data */ - } data[]; + } data[] __counted_by(num_areas); };