[next] staging: greybus: Add __counted_by for struct apr_rx_buf and use struct_size()
Message ID | ZSR2O6zGyT/VX6ve@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 x8csp2138640vqo; Mon, 9 Oct 2023 14:53:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGidGIIv2eFrXP44nqhMBozxDhe1O/tv//6lAONssrcQSwwEuMhowd6OuyMkckLIvJH0xcI X-Received: by 2002:a17:90b:3e8c:b0:276:ae8f:2456 with SMTP id rj12-20020a17090b3e8c00b00276ae8f2456mr12400983pjb.3.1696888405671; Mon, 09 Oct 2023 14:53:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696888405; cv=none; d=google.com; s=arc-20160816; b=iSkJivPDZWCr2n4Wp9JiiJ/X1bCobXgIAQatYuaUwzklMgFyVxr7VEV9aKITBBq2Qf vO/TZAoh8Szt8wDan9NfVdwdjsIN0b047//KHDN8khLQxL1zaOE+1mjqpPuqlIldqDla cupo57hkcf0GJ/PzJXtztbxKzZTj+5K6IIIpZj2XrI1C8EwwGnVRA5O5fFnie278J65z AwEmu4KM+WHZj+C6Tzqh4eTB8F0zq0+uOKTjd8YBkvt0rlK+4CeVFQ+OYuLN97pHIZYY hmhC+a4WOTjcWoLZ+db18H2cc/SZK8hq63vN4flqeoLeCAZtZWwUMr6UCo36iG0dIsM/ dnfw== 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=Lpf978w+OIjOOLdgZpu7oTMgicPgjQWuNNOvHYIngSU=; fh=89pE/LxKGH3iOgaIQefHo0YUNbyHvQK9BmfHRFaHMSs=; b=XLN6vWh63T2u/8K0LuzwzmBoJ9AT3BGwH1c3OU+s3KGm/G3uia3R6HNLeDkuxdOeJQ +KwKyrwHaipMc9xF05C8RdcJFtshSmGjJnylTC+ZMBpWI4tWhfk0FeIK3O0XEZ7S0Taa 4k4OV5BSUP+3Q/MhFrfTwKP4/+v5UU9xoGGoTXFd1H+/DNUeIaGVycixEETEmphb+ATZ y7KXHRPUr4cPp4tqNBGHg2BLHnsr54z5sPPLuIlZwUSKOMu0ycJoPm3aeBetDwmDS+1g hd83cpUVH7r13Icg1Hv8Ss/7jQevTjn9q4vp4uq+ufIXXfVgsIAsIq4VXtEi85AVwnb6 1TUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=IyYdAZ9E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id e4-20020a17090ab38400b0027652885184si12109138pjr.91.2023.10.09.14.53.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 14:53:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=IyYdAZ9E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 BC0AF80309AA; Mon, 9 Oct 2023 14:53:23 -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 S1378792AbjJIVxF (ORCPT <rfc822;makky5685@gmail.com> + 19 others); Mon, 9 Oct 2023 17:53:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377858AbjJIVxE (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 9 Oct 2023 17:53:04 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0AD8199 for <linux-kernel@vger.kernel.org>; Mon, 9 Oct 2023 14:53:04 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7CBB9C433C7; Mon, 9 Oct 2023 21:53:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696888383; bh=W+VZfakyDjRAObO1dDQmRahEdfhe6IqKNZRo1tRwkuk=; h=Date:From:To:Cc:Subject:From; b=IyYdAZ9EdWVH9jfcqTIBghy4+Muh2EwEWNScrz4b6/aGOUsd4mtqMfoBGTvK0lO6c onA3n0Ti125FvNmSQK8G0nHnkQuQfLHPIw94GdKqymW9RjYlnO5rr8K59gXix6rYDv K+smp/Ptnv2nHzZokKGH0fagbuF1UXumVrzSuy0S+2dpj6k0oYedewNK8JDJCA4UBh 2kaFeZIXRSskXp46DKazFRFkJSWh0LQUEl4WA4ZKmoYOfpOlfrz6Q1kGaCyWSDkKHV R+pJO6bQVK5kh2aPVWX0fFUVo6ErAoyTDyV3dCkv9qD4GYbDHRsqsj4TsHtEtYQEZn AaCet59ZGYm5w== Date: Mon, 9 Oct 2023 15:52:59 -0600 From: "Gustavo A. R. Silva" <gustavoars@kernel.org> To: Johan Hovold <johan@kernel.org>, Alex Elder <elder@kernel.org>, Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: greybus-dev@lists.linaro.org, linux-staging@lists.linux.dev, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" <gustavoars@kernel.org>, linux-hardening@vger.kernel.org Subject: [PATCH][next] staging: greybus: Add __counted_by for struct apr_rx_buf and use struct_size() Message-ID: <ZSR2O6zGyT/VX6ve@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:53:23 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779316456967760650 X-GMAIL-MSGID: 1779316456967760650 |
Series |
[next] staging: greybus: Add __counted_by for struct apr_rx_buf and use struct_size()
|
|
Commit Message
Gustavo A. R. Silva
Oct. 9, 2023, 9:52 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/staging/greybus/raw.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
Comments
On Mon, Oct 09, 2023 at 03:52:59PM -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> Looks right, "len" is updated before accessing "data[]". Reviewed-by: Kees Cook <keescook@chromium.org>
On 10/9/23 4:52 PM, 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. Looks good to me, and I like the use of struct_size(). Reviewed-by: Alex Elder <elder@linaro.org> > > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> > --- > drivers/staging/greybus/raw.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/drivers/staging/greybus/raw.c b/drivers/staging/greybus/raw.c > index a00978c8e1d2..b9c6eff7cdc1 100644 > --- a/drivers/staging/greybus/raw.c > +++ b/drivers/staging/greybus/raw.c > @@ -29,7 +29,7 @@ struct gb_raw { > struct raw_data { > struct list_head entry; > u32 len; > - u8 data[]; > + u8 data[] __counted_by(len); > }; > > static const struct class raw_class = { > @@ -73,7 +73,7 @@ static int receive_data(struct gb_raw *raw, u32 len, u8 *data) > goto exit; > } > > - raw_data = kmalloc(sizeof(*raw_data) + len, GFP_KERNEL); > + raw_data = kmalloc(struct_size(raw_data, data, len), GFP_KERNEL); > if (!raw_data) { > retval = -ENOMEM; > goto exit;
diff --git a/drivers/staging/greybus/raw.c b/drivers/staging/greybus/raw.c index a00978c8e1d2..b9c6eff7cdc1 100644 --- a/drivers/staging/greybus/raw.c +++ b/drivers/staging/greybus/raw.c @@ -29,7 +29,7 @@ struct gb_raw { struct raw_data { struct list_head entry; u32 len; - u8 data[]; + u8 data[] __counted_by(len); }; static const struct class raw_class = { @@ -73,7 +73,7 @@ static int receive_data(struct gb_raw *raw, u32 len, u8 *data) goto exit; } - raw_data = kmalloc(sizeof(*raw_data) + len, GFP_KERNEL); + raw_data = kmalloc(struct_size(raw_data, data, len), GFP_KERNEL); if (!raw_data) { retval = -ENOMEM; goto exit;