Message ID | ZSVKwBmxQ1amv47E@work |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp177690vqb; Tue, 10 Oct 2023 06:00:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF435pSnAdGkOTkdojD0dGPFPE8xILmSNWp8RTVxg8fkJviVnSIE1fbEoqLECwfM26AZCem X-Received: by 2002:a17:90a:6343:b0:279:104d:1473 with SMTP id v3-20020a17090a634300b00279104d1473mr14754583pjs.45.1696942839497; Tue, 10 Oct 2023 06:00:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696942839; cv=none; d=google.com; s=arc-20160816; b=TMROdIG5MwJqHy0wuMC43jGVvLB7MYAyriY+gxH/xjFN74vbZzgTPfCmg/M4sv5scA 7lQxPe405IPFw7TL3bzIcPnTp3uOuhDb8PUUf1YOXPgCrv9IL75v3Yv7kw9fgmbc1w8v hvftqHHs7I1KizQIQW1fTBoygxARfVG4W6q9eAeFN1V6/70kwkyOx6JSO3TEqQnyIkSB Tfgq52ttRrV9OWYD3DdLNe+pj4RjpuFXQCg5ZBtgRRzyaY/C2QysQbTDlgihXbxZZ7Og /CwaVJMAm4gtGoMq36cUaJCzIfsEPYtrn+fG3eSHcqVAkeawsFwmEh12jJLUbs/uFWWp nYtA== 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=1G+vAwB+xZAC+qbVPnhD4T66gVm//BFwlckgf+pr5Rw=; fh=KCaPAGlV9ooJEsJpu2O3eq6qYnfAv7lbcWOTjHR/Md8=; b=jnHOYmzsdJge3DXkxgazJaeXv0tEND6Td7JScde2VroQEDk9fYXl5sOcdfv8mbyasM bC52rDbNRi4Bxdpdzu3QA1/ObA0bXPQXPJf5oiZ2Qh7wzJugkBK4xooFiTkjswoACSZs YsYgrQt4dHheERGyaEXCgdcDM016YiDzkDieUiaZYScj25a4ryVB62xpDNwUXWeJKEp+ iowzqIePB1z8qO2IEGrYJIG7pERMQi2REUZ/x5oBlfRkng87qjMgVSxQiDF2O7/PxvFR 4OL9QwRbwISJtC+K6bGATrucgv/gEWoydy7xFQgr9rzv+MO6hVGefdXJZj+zlV6k/vg8 WlZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=fG2qWorU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id n7-20020a17090a9f0700b00276f10d241fsi14028689pjp.37.2023.10.10.06.00.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 06:00:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=fG2qWorU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id DE7AB8189097; Tue, 10 Oct 2023 06:00:38 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232377AbjJJNAf (ORCPT <rfc822;rua109.linux@gmail.com> + 20 others); Tue, 10 Oct 2023 09:00:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232469AbjJJNAX (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 10 Oct 2023 09:00:23 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 798BD1BC for <linux-kernel@vger.kernel.org>; Tue, 10 Oct 2023 06:00:01 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 73107C433C9; Tue, 10 Oct 2023 12:59:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696942801; bh=Xi+Z6sMFlQ/asWuwXVb1mQI8aYoKAasoVIf0fri5JUU=; h=Date:From:To:Cc:Subject:From; b=fG2qWorUWCfvQAcOdg2BtGbv9Ro/K0B2CpaODoS/VnH9d1Bet0rngO4Io+dDpjVP+ xWEeIBTjpNic9yaaI0woBajR4QdJvM7htlnYU95Xy+b+dWPiDRtOzdFoWFNbHGJmGX jmTIBA72fAJPmxZmwZ6Nxx2gFhjRkcOaKLXGDedAo6tWBD/JxLZ0NR9aEkEjJX5HOw pIa+ZKnY0cG2xrcPx6nOz/ZuRjwXweE0onrNwFHyXr/8Goo2WFFtFHdmdJFUnlP/2D 8EVzEEDBVdK45ixmVGgQvtNo5YFccXrQ0B3xmoUrBzrmyF7B225nBkSc8Pd2jaXkqA Drz3Xk1UTpdiA== Date: Tue, 10 Oct 2023 06:59:44 -0600 From: "Gustavo A. R. Silva" <gustavoars@kernel.org> To: David Howells <dhowells@redhat.com>, Marc Dionne <marc.dionne@auristor.com> Cc: linux-afs@lists.infradead.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" <gustavoars@kernel.org>, linux-hardening@vger.kernel.org Subject: [PATCH][next] afs: Add __counted_by for struct afs_acl and use struct_size() Message-ID: <ZSVKwBmxQ1amv47E@work> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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: <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 (snail.vger.email [0.0.0.0]); Tue, 10 Oct 2023 06:00:38 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779373535161489771 X-GMAIL-MSGID: 1779373535161489771 |
Series |
[next] afs: Add __counted_by for struct afs_acl and use struct_size()
|
|
Commit Message
Gustavo A. R. Silva
Oct. 10, 2023, 12:59 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>
---
fs/afs/internal.h | 2 +-
fs/afs/xattr.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
Comments
On Tue, Oct 10, 2023 at 06:59:44AM -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> > --- > fs/afs/internal.h | 2 +- > fs/afs/xattr.c | 2 +- > 2 files changed, 2 insertions(+), 2 deletions(-) > > diff --git a/fs/afs/internal.h b/fs/afs/internal.h > index 469a717467a4..e263a58b82ba 100644 > --- a/fs/afs/internal.h > +++ b/fs/afs/internal.h > @@ -1116,7 +1116,7 @@ extern void afs_fs_inline_bulk_status(struct afs_operation *); > > struct afs_acl { > u32 size; > - u8 data[]; > + u8 data[] __counted_by(size); > }; It's surprising how many of these 2-member structs we have in the kernel that do the same basic thing. :) And I see that "size" is assigned before using "data", good. Reviewed-by: Kees Cook <keescook@chromium.org> -Kees
On Tue, 10 Oct 2023 06:59:44 -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. > > [...] Applied to for-next/hardening, thanks! [1/1] afs: Add __counted_by for struct afs_acl and use struct_size() https://git.kernel.org/kees/c/7403e688f0c0 Take care,
diff --git a/fs/afs/internal.h b/fs/afs/internal.h index 469a717467a4..e263a58b82ba 100644 --- a/fs/afs/internal.h +++ b/fs/afs/internal.h @@ -1116,7 +1116,7 @@ extern void afs_fs_inline_bulk_status(struct afs_operation *); struct afs_acl { u32 size; - u8 data[]; + u8 data[] __counted_by(size); }; extern void afs_fs_fetch_acl(struct afs_operation *); diff --git a/fs/afs/xattr.c b/fs/afs/xattr.c index 9048d8ccc715..8b42cac51660 100644 --- a/fs/afs/xattr.c +++ b/fs/afs/xattr.c @@ -75,7 +75,7 @@ static bool afs_make_acl(struct afs_operation *op, { struct afs_acl *acl; - acl = kmalloc(sizeof(*acl) + size, GFP_KERNEL); + acl = kmalloc(struct_size(acl, data, size), GFP_KERNEL); if (!acl) { afs_op_nomem(op); return false;