From patchwork Mon Oct 9 20:44:54 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: 150321 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a888:0:b0:403:3b70:6f57 with SMTP id x8csp2111877vqo; Mon, 9 Oct 2023 13:45:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHCLGXd1kiZCIw+uoR+4z6+1fWOqZUd7y1/ow181kQ7QIRt3io3ME8HDFNQP/5kfbyGhhes X-Received: by 2002:a05:6a21:2723:b0:16b:f8f0:1c0c with SMTP id rm35-20020a056a21272300b0016bf8f01c0cmr6898011pzb.0.1696884338127; Mon, 09 Oct 2023 13:45:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696884338; cv=none; d=google.com; s=arc-20160816; b=UYRY1hf3YNShY+X9T6FAbC8Ijcc46VR04mnYhn2K7oHMRVmUI1NOxUpAhcfZMwWos9 p9qusyZqOoiADJ0Z3WeFxx2q8Sp1L+byl5hJC/yJtKXGg44V1uZJjWh8o/XqVzt2K43y D/4ChIw4V27HWuOwI7DjcYMckd4b7OlCYntcAxglbik3g6OPU2fOSgTiDbp5hpAgb9Rd TYZbByY6xRjmwt0gQZovAGxdNKoBHiaWctmLhThLcTQmEAc92YkbLzZkw1OD4d54QmFt wU0eWae84OYE+U+71AqQbKcGo1YTCHgJ+p0N2XacKTaMOTTSb2uPORcXopWJGIpNSss6 tpRQ== 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=3D4YPsnjnF045rqiyeVOVh8MkhuaP2uTNT0SWtNOFGo=; fh=H/c59XmP5gfibSQY7xyG1tRLY3T+AqCPws8si1zsimM=; b=OBUIJou4m4z872W6cLRnjsgL6lG4tbiOCbCjaH6L09ZUm0+9Hpq6keCHxq1i0u4WHN CWME4wzagWARYJXLmAvqKway3x3HTfuXv8r7DtZY4eV6P5I/9ByBwBYvlS7oS9iHG4wp PRYt1KTwMQe2eSiDBCMI6Mp/VNRzxDAxsvqXPo5OYmdLESPheV8vMxytbJcAAvCFOFsV MpCItg/ehSuUTZb7qVDHwbUOTGKd5ofR/VtTw2G4Nu3edzytGZQeLAhyvXbdxar4C7iV J0xb+oWjSncSITSW9vZouyMzxVLREvZDVvpXNqxL41Wkqnq4Kmq/a5D/yRj0ejJsTGks TA1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZEZGlqku; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id u7-20020a170903124700b001c589ba4a12si10905662plh.140.2023.10.09.13.45.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 13:45:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZEZGlqku; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id 3C052807C85F; Mon, 9 Oct 2023 13:45:11 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377975AbjJIUpA (ORCPT + 19 others); Mon, 9 Oct 2023 16:45:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234598AbjJIUo7 (ORCPT ); Mon, 9 Oct 2023 16:44:59 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0D2792; Mon, 9 Oct 2023 13:44:58 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 46609C433C8; Mon, 9 Oct 2023 20:44:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696884298; bh=xqPPlteo+/irwg1A8Nmsg7I8TpquWpmsFmUvKGKyQHo=; h=Date:From:To:Cc:Subject:From; b=ZEZGlqkuZvJF2yg5oonzytBzx9hYOgBwyLTgVN0/S7cA1HKqyN7f84BevW/PYUGjZ jm+fg16/Eoqtk5lixQKPZ1rb3J4iK1kAHnHvdi6G8QezxsgYveuaxfWqx9OwnvqiYI aQGXJ8B+DRU9+0zi2Aa/G//zaR+6hzBdIZbsqevBzeBgCWE4h56/16MJHw8z5LqO4X QdyEhvT8Fg/n9I4q5cLU0fSwzw6sJPWRc+vpRkqjLl1uZlLzMwCibT1ZPvklAcmTsi D+u7jNb0ZgmaruYQcPbHlVcReX40dDp2VQhJFJZ5dRN47OZdKB7m6AVVbOvRw5shLm qKhW28wJqYPSQ== Date: Mon, 9 Oct 2023 14:44:54 -0600 From: "Gustavo A. R. Silva" To: Chris Mason , Josef Bacik , David Sterba Cc: linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org Subject: [PATCH][next] btrfs: Add __counted_by for struct btrfs_delayed_item 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 pete.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 (pete.vger.email [0.0.0.0]); Mon, 09 Oct 2023 13:45:11 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779312191428830411 X-GMAIL-MSGID: 1779312191428830411 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 --- fs/btrfs/delayed-inode.c | 2 +- fs/btrfs/delayed-inode.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/btrfs/delayed-inode.c b/fs/btrfs/delayed-inode.c index 35d7616615c1..4f364e242341 100644 --- a/fs/btrfs/delayed-inode.c +++ b/fs/btrfs/delayed-inode.c @@ -313,7 +313,7 @@ static struct btrfs_delayed_item *btrfs_alloc_delayed_item(u16 data_len, { struct btrfs_delayed_item *item; - item = kmalloc(sizeof(*item) + data_len, GFP_NOFS); + item = kmalloc(struct_size(item, data, data_len), GFP_NOFS); if (item) { item->data_len = data_len; item->type = type; diff --git a/fs/btrfs/delayed-inode.h b/fs/btrfs/delayed-inode.h index d050e572c7f9..5cceb31bbd16 100644 --- a/fs/btrfs/delayed-inode.h +++ b/fs/btrfs/delayed-inode.h @@ -95,7 +95,7 @@ struct btrfs_delayed_item { bool logged; /* The maximum leaf size is 64K, so u16 is more than enough. */ u16 data_len; - char data[]; + char data[] __counted_by(data_len); }; static inline void btrfs_init_delayed_root(