From patchwork Tue Oct 10 12:46:50 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: 150751 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp170280vqb; Tue, 10 Oct 2023 05:47:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEob4sa4+gOafZx4RBTGhBDYZorPu5aqgsejI2yh6beCCla7PVNJA4pQlzudxnavO4e30dj X-Received: by 2002:a05:6808:18:b0:3a8:ccf0:103f with SMTP id u24-20020a056808001800b003a8ccf0103fmr18888190oic.3.1696942028898; Tue, 10 Oct 2023 05:47:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696942028; cv=none; d=google.com; s=arc-20160816; b=NvU0QEXCy+Z8hYHfPtDazfZtAHoi71gQT5qKW2QwcgQ/Xx7WTjh0cKzHE+LWwWjzGi 0/usAfvyyGR2U1yoxrdQl4pKPF0Alcb4Y8ryEg53Uj/T7zMZxt/lOELrA5G/K6YkIimU cC3yis1d6P/QnLvpFKFc2cOjV8DMG8ZffaQBe7Mm5E7r4hCtTG30qxXgrP5bBpK4s3fx D9AjMs59rvSKtjZdn6ROzWPZadt5DulEZEtL8ubJUCKQfFYXaHbOR+tKUeErZ6cWClAP kszxGHkAjxOyb06VJAErpbBf/CbjncayHSsBFvk/0dIb7PVZbGctkdA0tzZa02usjxQX VDxQ== 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=0W+NVk1HtgMkuOmuI6FYel4mZ8AsjOkxkrVojCUFms8=; fh=6zqWW1ROCRAuQe1BMtdMaxUJ7WyhP8zMvQQIczDBZjU=; b=LH6GI2Pljcc/ymJEp3R+SkoFUCKJKhre5PeG1aVb/HPzBG5fRoTw83ns+tWA9s6HFT g8MP70bsIcb3/7dSPyDj46SN/MnDrFWvEvCu3NBWszL/Go6BrqEieA5De3Fq5zpvnThq 0GQPpJslJfBr82q/Z7gdM34Pu5tp/RDwCXqb1oOCN8lCPdbj6oqjaO87p/hiZz31g7Ms Kq3KitxjPYqUkPdSOFTKLuQshTsvObLl5Y0gNWdZC2fJIVlpGRjwHNB0f9ICujNTjPHG ZaK6lc0bz5j5U/uK7a5vxJDAyvtMZa+3wWgJ81+1bS9eRrKcnRMYu+MGJ02SPakBLJWr UJ+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dC0DHgjw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id k198-20020a636fcf000000b005855424fc2dsi11303671pgc.611.2023.10.10.05.47.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 05:47:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dC0DHgjw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id 0E4B2801CB6A; Tue, 10 Oct 2023 05:47:07 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231650AbjJJMrB (ORCPT + 20 others); Tue, 10 Oct 2023 08:47:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229508AbjJJMq7 (ORCPT ); Tue, 10 Oct 2023 08:46:59 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8992299 for ; Tue, 10 Oct 2023 05:46:58 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B4903C433C8; Tue, 10 Oct 2023 12:46:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696942018; bh=knh9lxB2dNgPZ1euXXn82WdFpZKIB9XIHk6ythKlouc=; h=Date:From:To:Cc:Subject:From; b=dC0DHgjw3VtLh8cQ3kYe/L78FbRWlOvMsX9oohjbkBB9XPOui28VOa9BETiUJRM3P IOkj/tgMvv/GpKQIixGpE+CjJ49llPZWCwsfvJiNrr/uiAPFAhfjYbMHsmxpdYPPT5 +3iG8hKPjY7TnD8e3DGGjSMwKQ3D4q1zc/Y7YYwdhrGg9teqbSVEUaQL0a/IyXiRx3 Md2MYGD9nR7kNWd3XHcS9uiZsksC9Pt7SyreonpU/rWUruXeyZ4Jh+v3r38R99of+g NaMR28hHJAHMgwyUGFfkhqJ+TiSSMvGVT8eRzyRyr/tdgSOcwqCU6uIuRY5Xyt06se jUMWz6hDK4JUg== Date: Tue, 10 Oct 2023 06:46:50 -0600 From: "Gustavo A. R. Silva" To: Russell King Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org Subject: [PATCH][next] atags_proc: Add __counted_by for struct buffer and use struct_size() Message-ID: MIME-Version: 1.0 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 agentk.vger.email 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 (agentk.vger.email [0.0.0.0]); Tue, 10 Oct 2023 05:47:07 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779372685107989037 X-GMAIL-MSGID: 1779372685107989037 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: Justin Stitt Reviewed-by: Kees Cook --- arch/arm/kernel/atags_proc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/arm/kernel/atags_proc.c b/arch/arm/kernel/atags_proc.c index 3ec2afe78423..cd09f8ab93e3 100644 --- a/arch/arm/kernel/atags_proc.c +++ b/arch/arm/kernel/atags_proc.c @@ -7,7 +7,7 @@ struct buffer { size_t size; - char data[]; + char data[] __counted_by(size); }; static ssize_t atags_read(struct file *file, char __user *buf, @@ -54,7 +54,7 @@ static int __init init_atags_procfs(void) WARN_ON(tag->hdr.tag != ATAG_NONE); - b = kmalloc(sizeof(*b) + size, GFP_KERNEL); + b = kmalloc(struct_size(b, data, size), GFP_KERNEL); if (!b) goto nomem;