From nobody Sat May 18 09:23:00 2024 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 2032DCD6128 for ; Mon, 9 Oct 2023 18:55:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378245AbjJISzg (ORCPT ); Mon, 9 Oct 2023 14:55:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377493AbjJISze (ORCPT ); Mon, 9 Oct 2023 14:55:34 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3C7FA4 for ; Mon, 9 Oct 2023 11:55:33 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F04BC433C7; Mon, 9 Oct 2023 18:55:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696877733; bh=GFc4Q5qaXcf/ZjZp9N+g0hF8nyG1r4y/dZRvyq8Du9A=; h=Date:From:To:Cc:Subject:From; b=E5t4oZoZGUwok3/PFxqTFm3bx+BQDZJJ+vyjeQKx5qfH3NW3T8avl6qgl+bnyUYJC zO1yvhHO+sP4fFX8+Jb4cqmWJslQJwmigAelDrmyPqCA58TgriU+tkTzbfJb5SuovZ jmqfndJSghzEQXm51nn5/kzyOz9T/Y1tXPAfnCjNoUTr4bTsLW0LPDmZeCVakppUDH jEcJR2BKfR9ECTm+7FG2ekhXkNGfJJsXybno9Pv1LFe6AsPro4qWuKH7WRyXxsIk1O o7ESnWSiQu2Nh1It7cAwnSiFhLfLU8drWQ2MQTfz+NjJJgjGI/aYtvnD6rJ7+HzeyS ydCrTBcQ7J1Mg== Date: Mon, 9 Oct 2023 12:55:30 -0600 From: "Gustavo A. R. Silva" To: Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko Cc: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org Subject: [PATCH][next] xen/xenbus: Add __counted_by for struct read_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: Jason Andryuk Reviewed-by: Kees Cook --- drivers/xen/xenbus/xenbus_dev_frontend.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/xen/xenbus/xenbus_dev_frontend.c b/drivers/xen/xenbus/= xenbus_dev_frontend.c index 0792fda49a15..6f56640092a9 100644 --- a/drivers/xen/xenbus/xenbus_dev_frontend.c +++ b/drivers/xen/xenbus/xenbus_dev_frontend.c @@ -82,7 +82,7 @@ struct read_buffer { struct list_head list; unsigned int cons; unsigned int len; - char msg[]; + char msg[] __counted_by(len); }; =20 struct xenbus_file_priv { @@ -195,7 +195,7 @@ static int queue_reply(struct list_head *queue, const v= oid *data, size_t len) if (len > XENSTORE_PAYLOAD_MAX) return -EINVAL; =20 - rb =3D kmalloc(sizeof(*rb) + len, GFP_KERNEL); + rb =3D kmalloc(struct_size(rb, msg, len), GFP_KERNEL); if (rb =3D=3D NULL) return -ENOMEM; =20 --=20 2.34.1