From patchwork Sun Oct 1 07:13:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe JAILLET X-Patchwork-Id: 147040 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp746007vqb; Sun, 1 Oct 2023 00:21:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE7U6yNcddyzLlgm96lCdflG4BSXAwX35g9225h6uvGi4jZVs/CPaUsZhQ/+QtfkmcyC1kl X-Received: by 2002:a05:6358:4408:b0:133:b42:69ca with SMTP id z8-20020a056358440800b001330b4269camr10533789rwc.18.1696144901527; Sun, 01 Oct 2023 00:21:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696144901; cv=none; d=google.com; s=arc-20160816; b=frqRYCoHFA8g6STMuf9hQh+55x8PWUK299UvsOKY2bi95m5RbifBkUkEeTW31V2SAX lYccLfhHrSrOaujYPf/pORFR7ss4jojPUqepmkffQ1GcU2dWDQFkPPFHp8tgRv0qU5oP NRlWbpxJGPAfQtr1i2P5bYGgq532uxDpIbPr+hywV1tfWT4qmr1oSKL6xzbL4FZbYbMc 6MDpkJr6UU3A53+J+jtMUOITvyPzk7OVViWc473yUEyY8ByQJNUBhnY9Ujas/36pypJ2 mGLzPJPb7ln31p/q+xKq9NY9ZPRk1YiNYhdBjLvz1Yl7TFf4AnwAhHnTz9unu8m9Lhlf nVVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=Dz1UeOwZ8D3gWqhd347/K4+hqBwOzPrkQWzzaVtlMzY=; fh=XmwlumnN935p04hir0wsNAjcPKCxtnFT07m6ri9QVAk=; b=RUmfA2jlzpRTOez3+tggLEO91A8EQLNH4ixDSEyvbV274eZHzaOOuIuuM6pkfikbG1 BVNqw1via7kckMP9JOjgQbmwcqG555G9mR7c0He8tBpmWv19XLPis/yR7SZi17XNTFbk /AVZHbu8NjA2E8f5+4Vfe9X+fW7HH3gPXxaT6a8+iX7k+Ij2XQ8kRSBi1f0WVuoY+W2z Y6isEX/WW23nlCKn1mHl9pTob79DgpzaDlS9wibNAruaflrt5pI2zMCZ5lQ0VOi4EGgY yzWIY1liIcPTfShZuV5DnoYTDEMPX0E5bkdtoRjqR0Zrtkqb5m4ONAYPYlhf9v4kQrzg e0QA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=PQawNpi9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id ch7-20020a056a00288700b006935df3019esi6178382pfb.235.2023.10.01.00.21.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Oct 2023 00:21:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=PQawNpi9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 0CDFE80309EA; Sun, 1 Oct 2023 00:14:17 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234397AbjJAHOO (ORCPT + 18 others); Sun, 1 Oct 2023 03:14:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234371AbjJAHOM (ORCPT ); Sun, 1 Oct 2023 03:14:12 -0400 Received: from smtp.smtpout.orange.fr (smtp-15.smtpout.orange.fr [80.12.242.15]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C227BF for ; Sun, 1 Oct 2023 00:14:08 -0700 (PDT) Received: from pop-os.home ([86.243.2.178]) by smtp.orange.fr with ESMTPA id mqeRqDkx1FUAImqeRqiyTX; Sun, 01 Oct 2023 09:14:06 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1696144446; bh=Dz1UeOwZ8D3gWqhd347/K4+hqBwOzPrkQWzzaVtlMzY=; h=From:To:Cc:Subject:Date; b=PQawNpi9kuc2NtZ7RgyVhcDO425+uGUgiUz6D13ulhKLSwmkQA6wWu5hiT3J7Y8jk yNpNrMK0R4GbtAYoC2a2VMHMzWQ9V6tvquetNHnhGvTEs0SQ5gdTplEfBq1YQS9oSo oBXRPQBNWDuHO8Pxj0lh898dN6EUpyq1V36bV1nORSquzqw9j6G7wVlN4byFgd6Gys 9x8wYJabPmpFjO6ito9ye6c8yerJR22PSZNtmTNz1TtPlUoEFGRxwtTVa5i7i9JRQG nw37iCpgVbnGPr7+1Su7hBZBnhnCPkGur3OSgLSK22yUrj8Vb20JHP3FN/MZNrebVi Kgr34PBPFnnmg== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sun, 01 Oct 2023 09:14:06 +0200 X-ME-IP: 86.243.2.178 From: Christophe JAILLET To: Kent Overstreet , Brian Foster , Kees Cook , "Gustavo A. R. Silva" , Nathan Chancellor , Nick Desaulniers , Tom Rix Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET , linux-bcachefs@vger.kernel.org, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH] bcachefs: Use struct_size() Date: Sun, 1 Oct 2023 09:13:54 +0200 Message-Id: <120b638f99b088f91d5a4491c800463c554e70b8.1696144401.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Sun, 01 Oct 2023 00:14:17 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778536835900386716 X-GMAIL-MSGID: 1778536835900386716 Use struct_size() instead of hand writing it. This is less verbose and more robust. While at it, 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 checking via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions). Signed-off-by: Christophe JAILLET Reviewed-by: Gustavo A. R. Silva --- This patch is part of a work done in parallel of what is currently worked on by Kees Cook. My patches are only related to corner cases that do NOT match the semantic of his Coccinelle script[1]. In this case, struct_size() was not used to compute the size needed for the structure and its flex array. [1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci --- fs/bcachefs/disk_groups.c | 3 +-- fs/bcachefs/super_types.h | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/fs/bcachefs/disk_groups.c b/fs/bcachefs/disk_groups.c index b292dbef7992..224efa917427 100644 --- a/fs/bcachefs/disk_groups.c +++ b/fs/bcachefs/disk_groups.c @@ -166,8 +166,7 @@ int bch2_sb_disk_groups_to_cpu(struct bch_fs *c) if (!groups) return 0; - cpu_g = kzalloc(sizeof(*cpu_g) + - sizeof(cpu_g->entries[0]) * nr_groups, GFP_KERNEL); + cpu_g = kzalloc(struct_size(cpu_g, entries, nr_groups), GFP_KERNEL); if (!cpu_g) return -BCH_ERR_ENOMEM_disk_groups_to_cpu; diff --git a/fs/bcachefs/super_types.h b/fs/bcachefs/super_types.h index 597a8db73585..78d6138db62d 100644 --- a/fs/bcachefs/super_types.h +++ b/fs/bcachefs/super_types.h @@ -46,7 +46,7 @@ struct bch_disk_group_cpu { struct bch_disk_groups_cpu { struct rcu_head rcu; unsigned nr; - struct bch_disk_group_cpu entries[]; + struct bch_disk_group_cpu entries[] __counted_by(nr); }; #endif /* _BCACHEFS_SUPER_TYPES_H */