From nobody Fri Jan 2 15:25:36 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 270A0CD80C0 for ; Tue, 10 Oct 2023 12:47:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231800AbjJJMrB (ORCPT ); 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 @@ =20 struct buffer { size_t size; - char data[]; + char data[] __counted_by(size); }; =20 static ssize_t atags_read(struct file *file, char __user *buf, @@ -54,7 +54,7 @@ static int __init init_atags_procfs(void) =20 WARN_ON(tag->hdr.tag !=3D ATAG_NONE); =20 - b =3D kmalloc(sizeof(*b) + size, GFP_KERNEL); + b =3D kmalloc(struct_size(b, data, size), GFP_KERNEL); if (!b) goto nomem; =20 --=20 2.34.1