From patchwork Tue Oct 10 12:59:44 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: 150760 Return-Path: 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 + 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 ); 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 ; 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" To: David Howells , Marc Dionne Cc: linux-afs@lists.infradead.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org Subject: [PATCH][next] afs: Add __counted_by for struct afs_acl and use struct_size() Message-ID: MIME-Version: 1.0 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: 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 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 --- 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); }; 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;