From patchwork Mon Oct 9 18:55:30 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: 150291 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a888:0:b0:403:3b70:6f57 with SMTP id x8csp2062634vqo; Mon, 9 Oct 2023 11:55:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHZG3t7L0BiaBRgxUG0/c2OTrtyw8uyMjZFtE39k8lfkTNicyV+oZs78h3rpVpkmXSRQclD X-Received: by 2002:a54:480d:0:b0:3a3:640d:ed71 with SMTP id j13-20020a54480d000000b003a3640ded71mr16508781oij.10.1696877753607; Mon, 09 Oct 2023 11:55:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696877753; cv=none; d=google.com; s=arc-20160816; b=CIaCpYL4xIZ20npmwwRTuSpD91Y5uh5ghbG56q5kMpwcOYhML1tYeDrGohIh1Lu8Ko 0Nw2Stxb44/1vSdAYRh7dhWiVjNUZ5uyiC2vcIKA/I2xG5XoRuCCfDG+WgDUMqYe9m5Q HbT+dpdqBLTTkrcQZMB/MqWdORrha0+KBqYJVeqNfz+CxQ31BCyOP1OVn0DP400xmPu8 +coRDNIvOEapq/s6gzX0jazkqLQ+DfCM/ihMvHvlIBFuR/TcG76Yv+DlRZg5OW51a/vu Q5cR5LfT5xczwS+l4I33CL4Gg7XAseiMxGwf1XZsnxBjgKvzp7BLmEs/RHfya3WiHnZ1 yzXg== 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=GSYY2Srdg/52Gvfst7CcuGnyOWExqVx0ZsG644gQBW0=; fh=grvoduVzbCjvQHNuY8TNqZyALqbAi7v/eKkAflEVkYI=; b=ZxhgdMEPUfJ1nAKghvNGhjXUkC4mLUSvd4pUCTbdotapoBPQciR7al2ej7yu7aUMd/ TrcX9lEhOoxhmb0i20GZByCl4SUNlAFZ2geX5b39PYOT4cokmmlQbov8sfau5jhL2flu 6a1QDoYhA5om55qw5TXAmJizyq0khP3bzS2Nvv6JHyMKxzuNw4GGhMY4zhZpkNy9Y3Jr Hrmb0/fjUNS831pwMYYiAV5JrYrYDASn4NTHf7AverLIKjYWn5jgZqYqrZk99I66EPR8 SQ828BQqmuP7oJR8AnF03VhmiW7l5NuL2OWfnomOu2ge6wSUQD4hDwHcLBZ/73U0zLYQ G6sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=E5t4oZoZ; 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 z16-20020a656650000000b0057823b96685si9954409pgv.681.2023.10.09.11.55.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 11:55:53 -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=E5t4oZoZ; 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 9CE2A80B12D1; Mon, 9 Oct 2023 11:55:51 -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 S1378218AbjJISzg (ORCPT + 18 others); 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 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]); Mon, 09 Oct 2023 11:55:51 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779305287362907515 X-GMAIL-MSGID: 1779305287362907515 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 Reviewed-by: Jason Andryuk --- 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); }; struct xenbus_file_priv { @@ -195,7 +195,7 @@ static int queue_reply(struct list_head *queue, const void *data, size_t len) if (len > XENSTORE_PAYLOAD_MAX) return -EINVAL; - rb = kmalloc(sizeof(*rb) + len, GFP_KERNEL); + rb = kmalloc(struct_size(rb, msg, len), GFP_KERNEL); if (rb == NULL) return -ENOMEM;