From patchwork Wed May 17 23:33:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 95597 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp135693vqo; Wed, 17 May 2023 17:13:16 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5nRnsSwgSJ8zrgFBefwFT6+8j8cuasA5QPPbV4ZjEL1Zkz8SyMhDoxb7bPAPsmPRIJCIxM X-Received: by 2002:a17:903:32c1:b0:1aa:e5cd:647a with SMTP id i1-20020a17090332c100b001aae5cd647amr814349plr.23.1684368796022; Wed, 17 May 2023 17:13:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684368796; cv=none; d=google.com; s=arc-20160816; b=i3ZekeKrZdwQdRX7X14WCXlbRJSWWgQ4mPD/6NEUGQ6Mm/AfrpXnmzZITiaxOY4tg1 lz/kF46768vxYuKrLlx0bASBdWdcp/mGkv4MZQwlKjz7alVJILPctgTWTR/bSziEf/MT c2+FJAyJxzPcYN2dbalMHV6WHhrS43jrCKd1aXOoHI8wdns8qDCxu4q48jaj99eH2Dkw fpJkHMEQfZ9pUdTrvaVgMxnuVEbhCTd33/6dzWg66ncljiiE8ruhtgHRrUtAgHKv1UOq /n0WR12dkJncTwtGAXthdXaHV87DqTq0ZXEotttqGA8AIWOmQHeok1CcORlZh4gqzFBu MoMw== 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=ZsdBaiO+SLRmvoF1oxELdQlQ303GgQ3he6ImyAqilwM=; b=fiZxn743iwYXFnnVv9gL1pH0VvbX/yjOnlGLRejlJhh4HXSkRsoVKy98J+BWNadOqp JcFrEtc7NA8KRrXxKMLoTIRfYdXnD88GmNIelaa5LzDIe1dbpdutoAl0J3SNMMA24aPH 39jV22VK3WdNuEEBo6EjV5PzUfTdiGSuzHn5SlQ5YE5bzykiplIGA2bBChRpuxvsuby2 FJMh5IvaVwcKuJkYZtYjULy0lD2PzOnQvYbpdaKY5vMNhVU2d0k7CTPlig6SORhATJYS +gqjBCUfky4GYvgRgiAdmA3XqlrSv5JThZf6jz8qzUJnIi6Utju7RFg/9mPtVC3sYEZP Fncg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="J/MaOic+"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j2-20020a170902758200b001a9581bfb91si20248548pll.543.2023.05.17.17.13.03; Wed, 17 May 2023 17:13:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="J/MaOic+"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229708AbjEQXdV (ORCPT + 99 others); Wed, 17 May 2023 19:33:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57322 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229700AbjEQXdT (ORCPT ); Wed, 17 May 2023 19:33:19 -0400 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2627840EF for ; Wed, 17 May 2023 16:33:17 -0700 (PDT) Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1ae4f28454bso10997985ad.3 for ; Wed, 17 May 2023 16:33:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684366396; x=1686958396; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ZsdBaiO+SLRmvoF1oxELdQlQ303GgQ3he6ImyAqilwM=; b=J/MaOic+T6lZSX9z6Crj6gInRjMD4E+E+bGF8IeK/gvj8+kVm52/pHWph1u3U31zYE jIREYD7YA8vdvmTFseMWbpy5Nv0+FJ9aDZeIyNdd6yH1d6RIjSOmxQv7oWoVirv0X/E3 +iEcRGwC9ZGFbWfLGxA7xjhE6A9XCca1Op/D8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684366396; x=1686958396; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ZsdBaiO+SLRmvoF1oxELdQlQ303GgQ3he6ImyAqilwM=; b=M5Zsg7bMpUrFq2fNnXjKycKxrfq1n0pzqn+Rup56OfMUjBTmjNaF5Lwf6NmwzQtZF5 C+cTRAyOgkH5/zvBW3gmnzusey2dUIsBDRssPxzg/ebzjZAx+s4TcDKaUMK1fQkBEc2B ZjoF3cDbMDMkFfcQv450XBxhN2UNxHnc3IQZ98lXWKLwl9VpCFDCkF/8qfYxXzcRAhL9 aVEqG+kaCdJmegTiE7ZO14BsANRXBBFsTzWGjYHaxV6i6YmEpK3lMHC+/Pa0oZM/qgCB mAtFI4TqmWowHA29R/SzCkuLLQn+vNkpBENCXW5K52gcUQ9tBToYZIcb0wxOwmlOWmyS 3L9A== X-Gm-Message-State: AC+VfDzL6QhR+CjFOZrE/iT25tH9uRwfqEUP0t4cI1o9zgywkcofHsev Vc7Pz7ekCD/EzLSqJnuaypNv+A== X-Received: by 2002:a17:903:441:b0:19d:1bc1:ce22 with SMTP id iw1-20020a170903044100b0019d1bc1ce22mr456930plb.5.1684366396668; Wed, 17 May 2023 16:33:16 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id j12-20020a170902690c00b001a04d27ee92sm18103534plk.241.2023.05.17.16.33.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 May 2023 16:33:16 -0700 (PDT) From: Kees Cook To: Song Liu Cc: Kees Cook , linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH] md/raid5: Convert stripe_head's "dev" to flexible array member Date: Wed, 17 May 2023 16:33:14 -0700 Message-Id: <20230517233313.never.130-kees@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2199; h=from:subject:message-id; bh=dQaDdwwfJIKVOpP+xhSN1JdgX+awrlBT4qD4Mzl0Qq4=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBkZWQ6fyNNtbsV/hrySf2Chk5e0QLsK0bNKO8z4CGK W/FZl+6JAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZGVkOgAKCRCJcvTf3G3AJkBVD/ 0VnC+3+XtytL1FbA2D8cGLXcifNdjNC2tFU+YvanxrXpbAXCMZMNbbLI9x/wA+DpnMgDCIorVvmzxp 5qOg6fKbI521D3qFRGLs8+zvjo97zAUnamMh5P10oVxDuR8+/mTpLJqOriXMDpbWQ59PPiue9d7y4i A2Ypz4LBiPgfwTy28j9oMEjIFrJdLJ8f6fDIk/+ylA13ye5HybcjkqMPnw0GFcaqSw9QKPY04SZwdI V3gofJWC4TrHAvO8UlrBXlHn/85ONM/WVetDN2CbBnAtnbhVM5Dvl4q6w/EIEmzpx0qLW2/K+XS/hc TBfQwNA3x/qN4Q05uJL8MOots/9ubwpvVAkP04R2GWnYra7dSuQ/qx9n7o31zZRAxNpLp4sZdCxg/j OPRzEvMElGft2bf2uZBvOhVkAmLIF3g4S9miE4hN112NTumjDidb3fDdAD6QOxN4WmV52AoNbYl9Wn vGAjU36QjmZHLEwcZT6tjXBIN2lZJhV5At1cWpn7wETRK2cf3/9hcvabLdoGem+0uSUPL/5dUtkE06 wgRJw/BNYXNanjMEU7MwSQH3+q/ZO8gs7KWh5tW9ugSylw28UAFJq9ZrkoX553GNAyC3q3cCGxLlEc jpl3o64ZLcZvNX0/+vF8mJpYtCDj+CmiWU6+qpL+68medw17eMHWEIKs1fGQ== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766188694668788248?= X-GMAIL-MSGID: =?utf-8?q?1766188694668788248?= Replace old-style 1-element array of "dev" in struct stripe_head with modern C99 flexible array. In the future, we can additionally annotate it with the run-time size, found in the "disks" member. Cc: Song Liu Cc: linux-raid@vger.kernel.org Signed-off-by: Kees Cook --- It looks like this memory calculation: memory = conf->min_nr_stripes * (sizeof(struct stripe_head) + max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; ... was already buggy (i.e. it included the single "dev" bytes in the result). However, I'm not entirely sure if that is the right analysis, since "dev" is not related to struct bio nor PAGE_SIZE? --- drivers/md/raid5.c | 4 ++-- drivers/md/raid5.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c index 4739ed891e75..95aa74aea076 100644 --- a/drivers/md/raid5.c +++ b/drivers/md/raid5.c @@ -2433,7 +2433,7 @@ static int grow_stripes(struct r5conf *conf, int num) conf->active_name = 0; sc = kmem_cache_create(conf->cache_name[conf->active_name], - sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), + struct_size((struct stripe_head *)0, dev, devs), 0, 0, NULL); if (!sc) return 1; @@ -2559,7 +2559,7 @@ static int resize_stripes(struct r5conf *conf, int newsize) /* Step 1 */ sc = kmem_cache_create(conf->cache_name[1-conf->active_name], - sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), + struct_size((struct stripe_head *)0, dev, newsize), 0, 0, NULL); if (!sc) return -ENOMEM; diff --git a/drivers/md/raid5.h b/drivers/md/raid5.h index e873938a6125..6cfc74162b41 100644 --- a/drivers/md/raid5.h +++ b/drivers/md/raid5.h @@ -268,7 +268,7 @@ struct stripe_head { unsigned long flags; u32 log_checksum; unsigned short write_hint; - } dev[1]; /* allocated with extra space depending of RAID geometry */ + } dev[]; /* allocated with extra space depending of RAID geometry */ }; /* stripe_head_state - collects and tracks the dynamic state of a stripe_head