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: 13415365 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 19F1FCD80C0 for ; Tue, 10 Oct 2023 12:47:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Subject:Cc:To: From:Date:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=KeFRRxRWI7S99+bzLwMD/ZwWeGC86AL/1jHYmD8sXLU=; b=YE72uwDMiTI2HA jJbbORH+3Khh2j3/IWM7ddIFcMemdNwon2vuzuqS5BMGTXpLCdbOCycv8aKrXJY0ttvkQxg2z4f2V /1tICifjDnr5ehSguIFoIvk9as0Wj8/xVFMiTY04Znmg4KfJ8eUzgUM/1XS5Qk6U+iE+W12/SVzIn Y1ot5G2CU9Q/2pIRVrx6g0leiQd4zIf0I0IlVG+FhRzrTQz9I/joTnGJGKvtWFDGRuq6jOfMwP/Df 5gIK/ZJIVlIENPzTotHRCnbLB1XbIf0KKMjJr1oh67L7CKxwhI6Q4QBOG2yDuFwEtA3kjTY142Bx0 H4jpQMcc0Rq9fPfen9fw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qqC8e-00DN1y-1A; Tue, 10 Oct 2023 12:47:04 +0000 Received: from ams.source.kernel.org ([145.40.68.75]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qqC8a-00DN1F-1j for linux-arm-kernel@lists.infradead.org; Tue, 10 Oct 2023 12:47:02 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id D0528B81C26; Tue, 10 Oct 2023 12:46:58 +0000 (UTC) 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231010_054700_723161_FE5B71EB X-CRM114-Status: GOOD ( 12.56 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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;