[next] wifi: brcmfmac: fweh: Add __counted_by for struct brcmf_fweh_queue_item and use struct_size()
Message ID | ZSRzrIe0345eymk2@work |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a888:0:b0:403:3b70:6f57 with SMTP id x8csp2135276vqo; Mon, 9 Oct 2023 14:42:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFUd5/ED2CcsNkHUBabAr1nBWgbzsyBjFDAw8TyyVc5iWUcCMFqtZqu5lydiVah/ORWxSds X-Received: by 2002:a05:6a00:1943:b0:690:d4fa:d43d with SMTP id s3-20020a056a00194300b00690d4fad43dmr13894203pfk.6.1696887762714; Mon, 09 Oct 2023 14:42:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696887762; cv=none; d=google.com; s=arc-20160816; b=bBK/khwKXdG8+mlBLK0qgdKYgKN+hflFrZRDUfO2jKcfPORqKpI7Kn21fOjWm3fLbq T/hU9iklxad20tisVOud3dz7DwK6n7hVo+7u06qmTU/ud8JuUAnvXvQwGfititYXk4mJ /wFPwhkKFB50VbwwrgEEe3ljJqwWXKDqJa2jv65BLGIS9zbLI6z1fFErSinU1/RQiqFT qisJnQWKEMb2xajMcIRK6TURXAODSjelKCLRov1TOVAoJTC3+yROPlR1ztU9G5JqDxQF Hrfhgg08u86Qc/2vUuubajz61Yvgl25Sjfw+ygbC//BH6u2S5HiiR63BDafv0iz4SnGp EQsw== 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=rhNkFoulAQ5KUGDDBpYhlLHwa9LM08taRIoPYUFKthU=; fh=TGcdJfqbne5PRlTBJrv131c/v9qZA/+rHC4Ubukd7sc=; b=ZnPVlF8gzI9hsUxxc/Im/Xkayw+qt+rbYJZNrpmPChcXw+HL77HII+ccd6lEWK3Ylr vOxdVebAbTXCpAmkbwNiYJgA3AEDoWEpSPzpaD5r46/LAEwWf7ZvkvbNbNU0ZEMrS7z6 Mm9c5DTekoSeS1jWDTq3WoX2zMe563BmDfpOTDHal8zrYseln6x2kS3lTLE6I7I5MiMB oLIxiLNmQqsLxel81PN6UnItigfv8VAs+HLbu5hmtNnpVY80T7lkG2SxWy3Omm+aHd5i XXDdW4AoLC4fGAeIWpzlVFegJ+sC6zHU1UnaUh2nN1BQscLMeEX9oYkfMCop0YC3F5Jq 1+9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LhxZG3fG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id br4-20020a056a00440400b0068fd7cb5864si8320749pfb.263.2023.10.09.14.42.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 14:42:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LhxZG3fG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (Postfix) with ESMTP id 87B928030984; Mon, 9 Oct 2023 14:42:39 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378818AbjJIVmR (ORCPT <rfc822;makky5685@gmail.com> + 19 others); Mon, 9 Oct 2023 17:42:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39914 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378790AbjJIVmN (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 9 Oct 2023 17:42:13 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ECA9C127; Mon, 9 Oct 2023 14:42:08 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D458AC433C7; Mon, 9 Oct 2023 21:42:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696887728; bh=kq7cApyaT7hqQOO78PhiQUyzEI02824CzKv278QATpY=; h=Date:From:To:Cc:Subject:From; b=LhxZG3fGzzNxCc8RPo3e5Apt/vaJKOUso5HbDl0esgi/4BcjGd46MWi6EjUemPJM0 eC3Yo/QQtFvvtIshkn3s5CzSQveQ1RLlh+VJpdtrnMhz1+dhL2I0IKkaueyS2krcvZ UtdFf9SjO5lWp5TdS5Gorhp/lJjHWhFmHn8fGkxrpYnMk2s6hLkgG2Hn58bh3ZSN6v k1pAj8vT1qSI1jn2cXNdhOLb7XBnKUAgVf1E9D7N63CgW/1h+ISrufFpCghZkCbfDZ Le1a/d7Fa1uvsVA4+TXZEKboWAEBshbN91oiFmrkUD3myeRkg5ulbbNJ/pW/q/EJxH 5FBDRwAxf+zig== Date: Mon, 9 Oct 2023 15:42:04 -0600 From: "Gustavo A. R. Silva" <gustavoars@kernel.org> To: Arend van Spriel <aspriel@gmail.com>, Franky Lin <franky.lin@broadcom.com>, Hante Meuleman <hante.meuleman@broadcom.com>, Kalle Valo <kvalo@kernel.org> Cc: linux-wireless@vger.kernel.org, brcm80211-dev-list.pdl@broadcom.com, SHA-cyfmac-dev-list@infineon.com, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" <gustavoars@kernel.org>, linux-hardening@vger.kernel.org Subject: [PATCH][next] wifi: brcmfmac: fweh: Add __counted_by for struct brcmf_fweh_queue_item and use struct_size() Message-ID: <ZSRzrIe0345eymk2@work> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii 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 lipwig.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 09 Oct 2023 14:42:39 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779315782416871080 X-GMAIL-MSGID: 1779315782416871080 |
Series |
[next] wifi: brcmfmac: fweh: Add __counted_by for struct brcmf_fweh_queue_item and use struct_size()
|
|
Commit Message
Gustavo A. R. Silva
Oct. 9, 2023, 9:42 p.m. UTC
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).
Also, relocate `event->datalen = datalen;` to before calling
`memcpy(event->data, data, datalen);`, so that the __counted_by
annotation has effect, and flex-array member `data` can be properly
bounds-checked at run-time.
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 <gustavoars@kernel.org>
---
drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
Comments
On Mon, Oct 09, 2023 at 03:42:04PM -0600, Gustavo A. R. Silva wrote: > 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). > > Also, relocate `event->datalen = datalen;` to before calling > `memcpy(event->data, data, datalen);`, so that the __counted_by > annotation has effect, and flex-array member `data` can be properly > bounds-checked at run-time. > > 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 <gustavoars@kernel.org> Yeah, looks right. Thanks for moving the count assignment. Reviewed-by: Kees Cook <keescook@chromium.org> -Kees > --- > drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c | 6 +++--- > 1 file changed, 3 insertions(+), 3 deletions(-) > > diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c > index dac7eb77799b..68960ae98987 100644 > --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c > +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c > @@ -33,7 +33,7 @@ struct brcmf_fweh_queue_item { > u8 ifaddr[ETH_ALEN]; > struct brcmf_event_msg_be emsg; > u32 datalen; > - u8 data[]; > + u8 data[] __counted_by(datalen); > }; > > /* > @@ -418,17 +418,17 @@ void brcmf_fweh_process_event(struct brcmf_pub *drvr, > datalen + sizeof(*event_packet) > packet_len) > return; > > - event = kzalloc(sizeof(*event) + datalen, gfp); > + event = kzalloc(struct_size(event, data, datalen), gfp); > if (!event) > return; > > + event->datalen = datalen; > event->code = code; > event->ifidx = event_packet->msg.ifidx; > > /* use memcpy to get aligned event message */ > memcpy(&event->emsg, &event_packet->msg, sizeof(event->emsg)); > memcpy(event->data, data, datalen); > - event->datalen = datalen; > memcpy(event->ifaddr, event_packet->eth.h_dest, ETH_ALEN); > > brcmf_fweh_queue_event(fweh, event); > -- > 2.34.1 > >
On October 9, 2023 11:42:11 PM "Gustavo A. R. Silva" <gustavoars@kernel.org> wrote: > 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). > > Also, relocate `event->datalen = datalen;` to before calling > `memcpy(event->data, data, datalen);`, so that the __counted_by > annotation has effect, and flex-array member `data` can be properly > bounds-checked at run-time. > > 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. From the description above I think this change looks sane so... Acked-by: Arend van Spriel <arend.vanspriel@broadcom.com> > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> > --- > drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c | 6 +++--- > 1 file changed, 3 insertions(+), 3 deletions(-)
"Gustavo A. R. Silva" <gustavoars@kernel.org> wrote: > 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). > > Also, relocate `event->datalen = datalen;` to before calling > `memcpy(event->data, data, datalen);`, so that the __counted_by > annotation has effect, and flex-array member `data` can be properly > bounds-checked at run-time. > > 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 <gustavoars@kernel.org> > Reviewed-by: Kees Cook <keescook@chromium.org> > Acked-by: Arend van Spriel <arend.vanspriel@broadcom.com> Patch applied to wireless-next.git, thanks. 62d19b358088 wifi: brcmfmac: fweh: Add __counted_by for struct brcmf_fweh_queue_item and use struct_size()
diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c index dac7eb77799b..68960ae98987 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c @@ -33,7 +33,7 @@ struct brcmf_fweh_queue_item { u8 ifaddr[ETH_ALEN]; struct brcmf_event_msg_be emsg; u32 datalen; - u8 data[]; + u8 data[] __counted_by(datalen); }; /* @@ -418,17 +418,17 @@ void brcmf_fweh_process_event(struct brcmf_pub *drvr, datalen + sizeof(*event_packet) > packet_len) return; - event = kzalloc(sizeof(*event) + datalen, gfp); + event = kzalloc(struct_size(event, data, datalen), gfp); if (!event) return; + event->datalen = datalen; event->code = code; event->ifidx = event_packet->msg.ifidx; /* use memcpy to get aligned event message */ memcpy(&event->emsg, &event_packet->msg, sizeof(event->emsg)); memcpy(event->data, data, datalen); - event->datalen = datalen; memcpy(event->ifaddr, event_packet->eth.h_dest, ETH_ALEN); brcmf_fweh_queue_event(fweh, event);