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: 13245985 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 62582C7EE23 for ; Wed, 17 May 2023 23:33:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229702AbjEQXdT (ORCPT ); Wed, 17 May 2023 19:33:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229468AbjEQXdS (ORCPT ); Wed, 17 May 2023 19:33:18 -0400 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2614B40EB for ; Wed, 17 May 2023 16:33:17 -0700 (PDT) Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1ae3a5dfa42so11170435ad.0 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=Mi2d1JaW73bu21WCnzHDxaE7pbW86GJy41w5d9suUn4hdNx2PTr+vq2mrmmRRcAx9M qAN3CwL2AAUaLYrixdswX922/U7+6P9q48ic0/lmrcZXA/3+zQ9QY4Ybfjfypghi8F+E uvCWVIJ0f4V/q85hf19QLaXzZ1CWIBHL/4B2GgXFqIWZ5X4/eQCRLkfZ5yP6meUtTMHP k/79YJXwMmcAU8LVO2lVYdiyNRchdjlp9iCbVnNFU7wZbWhUHvA95dvxCkx1SYnr3MgU IrbtHfS2h/zD6L8+Hw26pY+XHjLZhgU1X4yIFIvIZs717fl/5QZF6JzxP1o8yKjjwY0L OnWQ== X-Gm-Message-State: AC+VfDzghJLvbneN4YkMVWBV5zoYYde3cRc4PMDqY5pAuhbAXS69ynKE gMin22ONykagZ3ZmUhLvrf4mmQ== X-Google-Smtp-Source: ACHHUZ6E2Rw/PI0VAhjazUgu2RxKRx7eteRLCIGlE1gszdmLtBUw2S018ls9GRz9WQsxYdPoqGDQIQ== 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 Precedence: bulk List-ID: X-Mailing-List: linux-raid@vger.kernel.org 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