[next] soc: qcom: apr: Add __counted_by for struct apr_rx_buf and use struct_size()
Message ID | ZSRw6RNi3Crhd32H@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 x8csp2130836vqo; Mon, 9 Oct 2023 14:30:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEmgvP6DrgASjJRxDSr59YPOoYB6/iLB0dYRL1wcmuHgCOfsc2csoxaC9b4UPpjWFySO2Af X-Received: by 2002:a05:6808:ab6:b0:3a7:5cc1:69b0 with SMTP id r22-20020a0568080ab600b003a75cc169b0mr17901193oij.7.1696887052548; Mon, 09 Oct 2023 14:30:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696887052; cv=none; d=google.com; s=arc-20160816; b=yZiJOkwBC2Otpx/xvt1A0gt8Z3KhaC6tCjkWg7mJCh4DFb1/W5UL9MlYKlyUd/Hi33 zLXVholT0JnlwUBG4sHGIne8XNMDsM5PfyCDgekxMNlc4oFg+keaEOiEuFvOW9IgIwMs p7PwBfh6lM4Z0YNrpqZ1lqCp2xcnfJNyOxR4OVjd3dXqJZMmbtBITZCE25B/46WVMhu1 qcORZ+V3BgpY3olDBKLWiOikTapYGI8h+5CJRJR0LqkDLoVNz0kx5isnM89l7+x1JH2E HyKrkMUH/2oFw9suZPYj29T1TqeUXKqKb73zMxw/KPd8pjHsiefQGu/7AcoSHmLnS2dE Hwfg== 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=bZgIpFs1cvlSs8wxxuT2gYnYizO0+6nV0Fzg3pchGbc=; fh=H/sfbYbtM1nqnHS5dmMIKiZH+o+RAIa2eS1MzqoDL3E=; b=kpCW36nEZzWkqZjNh8NOW0gqyD4bDMIxX6Cxn3fpVPa+6teSEoB7naZMfdXJ0KPjBP 2b1b39+m06dn9O1gFIJkxcJigkT1erKEg8rN+ltffm4yA1MeyCRacLYyujE9orW/Hb5j 1CaTcfj/6SoPsO5bNX/HkH4KKoHaGxfD7HqbyJqrh4rqDZa0vb8ZX4bWjMOPPTaVxP42 kx363PeDmGfot7HD5FDBFiD343gr1+NZz1/vW9a6fr1XCI5GOh/XNxjZP/vUAAophXbE psXt26pIcfxaTyni7M1P4MHIcoNsUwf3jg3bo0BaofyKE9XLXNHxibZJ4z/vj1eYHfMq lOQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rT1qhpFp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id bx36-20020a056a02052400b00578a30162c8si11085765pgb.537.2023.10.09.14.30.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 14:30:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rT1qhpFp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 CCED580BB55B; Mon, 9 Oct 2023 14:30:37 -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 S1378702AbjJIVaZ (ORCPT <rfc822;makky5685@gmail.com> + 19 others); Mon, 9 Oct 2023 17:30:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378542AbjJIVaW (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 9 Oct 2023 17:30:22 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92D4592; Mon, 9 Oct 2023 14:30:21 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E2D10C433C7; Mon, 9 Oct 2023 21:30:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696887021; bh=hKpw9LZwyKaVS0FWXHmJymdA4SjptOQOm2bWj75OgPs=; h=Date:From:To:Cc:Subject:From; b=rT1qhpFpqRiVqgpTZwUGexp2A+nUsN2ViOQjRqnwpV0V0OEDPncJ7UNSgaYVBGHsH qKxM3kOH+f9qJrEyWZPcguD95u3rmDjaCIF5AVOLgk5fP2yAOEmMMJtYd77KoXZrFw Wq6qTfZatu8Mlf81/v9INrgEXKTDHDPi5aCI+OO2PEAo9gx3ZRj1LmHA3FEQ4cIP+N YwNrvNLakxwSDssvtvd2pvR/7MhoZLyFiiR95n8xwK6qH/kqt1oL05VW5cl6ROxs9x fIomWW8LKuLgLPRQpFao87ba4zFOX2cCBX/2F8Q35XWeJEhC05ayqb8/jR9GpH/LE2 BSFrwwIN8Vd5g== Date: Mon, 9 Oct 2023 15:30:17 -0600 From: "Gustavo A. R. Silva" <gustavoars@kernel.org> To: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>, Banajit Goswami <bgoswami@quicinc.com>, Andy Gross <agross@kernel.org>, Bjorn Andersson <andersson@kernel.org>, Konrad Dybcio <konrad.dybcio@linaro.org> Cc: alsa-devel@alsa-project.org, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" <gustavoars@kernel.org>, linux-hardening@vger.kernel.org Subject: [PATCH][next] soc: qcom: apr: Add __counted_by for struct apr_rx_buf and use struct_size() Message-ID: <ZSRw6RNi3Crhd32H@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 groat.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 (groat.vger.email [0.0.0.0]); Mon, 09 Oct 2023 14:30:37 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779315037889514204 X-GMAIL-MSGID: 1779315037889514204 |
Series |
[next] soc: qcom: apr: Add __counted_by for struct apr_rx_buf and use struct_size()
|
|
Commit Message
Gustavo A. R. Silva
Oct. 9, 2023, 9:30 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).
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/soc/qcom/apr.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
Comments
On Mon, Oct 09, 2023 at 03:30:17PM -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). > > 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> Yup, looks right. Reviewed-by: Kees Cook <keescook@chromium.org>
On Mon, Oct 9, 2023 at 2:30 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). > > 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> Excited for __counted_by to land! This looks right. Reviewed-by: Justin Stitt <justinstitt@google.com> > --- > drivers/soc/qcom/apr.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/drivers/soc/qcom/apr.c b/drivers/soc/qcom/apr.c > index 30f81d6d9d9d..1f8b315576a4 100644 > --- a/drivers/soc/qcom/apr.c > +++ b/drivers/soc/qcom/apr.c > @@ -41,7 +41,7 @@ struct packet_router { > struct apr_rx_buf { > struct list_head node; > int len; > - uint8_t buf[]; > + uint8_t buf[] __counted_by(len); > }; > > /** > @@ -171,7 +171,7 @@ static int apr_callback(struct rpmsg_device *rpdev, void *buf, > return -EINVAL; > } > > - abuf = kzalloc(sizeof(*abuf) + len, GFP_ATOMIC); > + abuf = kzalloc(struct_size(abuf, buf, len), GFP_ATOMIC); > if (!abuf) > return -ENOMEM; > > -- > 2.34.1 > >
On Mon, Oct 09, 2023 at 03:30:17PM -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). > > 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> Applied, thanks. > --- > drivers/soc/qcom/apr.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/drivers/soc/qcom/apr.c b/drivers/soc/qcom/apr.c > index 30f81d6d9d9d..1f8b315576a4 100644 > --- a/drivers/soc/qcom/apr.c > +++ b/drivers/soc/qcom/apr.c > @@ -41,7 +41,7 @@ struct packet_router { > struct apr_rx_buf { > struct list_head node; > int len; > - uint8_t buf[]; > + uint8_t buf[] __counted_by(len); > }; > > /** > @@ -171,7 +171,7 @@ static int apr_callback(struct rpmsg_device *rpdev, void *buf, > return -EINVAL; > } > > - abuf = kzalloc(sizeof(*abuf) + len, GFP_ATOMIC); > + abuf = kzalloc(struct_size(abuf, buf, len), GFP_ATOMIC); > if (!abuf) > return -ENOMEM; > > -- > 2.34.1 >
diff --git a/drivers/soc/qcom/apr.c b/drivers/soc/qcom/apr.c index 30f81d6d9d9d..1f8b315576a4 100644 --- a/drivers/soc/qcom/apr.c +++ b/drivers/soc/qcom/apr.c @@ -41,7 +41,7 @@ struct packet_router { struct apr_rx_buf { struct list_head node; int len; - uint8_t buf[]; + uint8_t buf[] __counted_by(len); }; /** @@ -171,7 +171,7 @@ static int apr_callback(struct rpmsg_device *rpdev, void *buf, return -EINVAL; } - abuf = kzalloc(sizeof(*abuf) + len, GFP_ATOMIC); + abuf = kzalloc(struct_size(abuf, buf, len), GFP_ATOMIC); if (!abuf) return -ENOMEM;