Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp58204rdb; Thu, 21 Dec 2023 02:45:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IFuzsNuwkbBKEKHL0vf1yJiqvQLYhiQoVthFpi+2ktwQ9mq5uTlV3UP1Rl0R05thjCc7lCL X-Received: by 2002:a17:902:f68c:b0:1d3:e5a7:b2fd with SMTP id l12-20020a170902f68c00b001d3e5a7b2fdmr3771320plg.12.1703155516304; Thu, 21 Dec 2023 02:45:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703155516; cv=none; d=google.com; s=arc-20160816; b=U04VS0Zg1NLucSbGA4Betk3XozEkFPgRM4652sPToF9WZlrstzxaFRFUxH9AdXBupI aVvkq2iacccREKnQeaa0vD2ul2V9IN2J+UQPn/6S2FkCokJ9JRgtauvqyqS8eKmDFrM8 7sdZcmrIJZtPPlDxF5xYyTkeuVwRGqBk5iV7QiWwwZlUt3/DjrGYHnA65BTnPeGwBhNp mnxQy/g3pABLNQCPF+BoOsMxu/b4WW1keU9Vl8W/W7ivjvtjhalP0OBGix/ZkA2SO9IO O/2waSAM4xvRZ/n1kKz0vXYQEardCjSTJxMW2eNLHohymMAuJ9UMNfXqDBoGdBOWPpFm rr8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=Bo5HJl27sw1CMSOKy+oOqaIrDQeJX/kJbyC1rLu2v4w=; fh=bbOOGpO/4N3vNTC5d7rk7VAh8CeLA50sg63pegRtLBk=; b=xgk135DSSV8jkJ4H3KWAasCh7j51eKjJul5ILz1jDVEv3jv/NagvaS+2kOQRncaC0T b+fKaOjIt8WlDwpkqpZ9S9xH4IIOJUY08i33lvJPVVqY2onWuK/a38QJt5CPQZcH2DsX Pj2eyD5rzvxho1/SgN7Fx/AwCJY/9BJmk13q9nE0pQCYSwXRcCpLO5G31EZcqTC+P2aX 2mIGojbmed0k+6flsw1lFXD4qn7dPsB2nPLx42eAWpaPXKrsZmnnada+Wdp4RVot2Jmo 8AKbVGmV37/P6PF/leY3jx9ZVCw3aWxGfZTSejyS9QXFC+w9DB1BSJcLciVmPBSnQbmD DAmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="dMUtexx/"; spf=pass (google.com: domain of linux-kernel+bounces-8234-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-8234-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id w3-20020a170902e88300b001d3b22f5784si1338673plg.438.2023.12.21.02.45.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Dec 2023 02:45:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-8234-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="dMUtexx/"; spf=pass (google.com: domain of linux-kernel+bounces-8234-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-8234-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id B02512827BC for ; Thu, 21 Dec 2023 10:45:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 23DA66E59A; Thu, 21 Dec 2023 10:44:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dMUtexx/" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0BA8A6E2A0; Thu, 21 Dec 2023 10:44:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-5cd54e5fbb2so255266a12.2; Thu, 21 Dec 2023 02:44:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703155443; x=1703760243; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Bo5HJl27sw1CMSOKy+oOqaIrDQeJX/kJbyC1rLu2v4w=; b=dMUtexx/KE1PzAoch6XRtlFsEksjvIreexOf1j08l8igJb7XXWXWLGSLfgxV6ELLIn V51k/xfwcgDz8vgS31vWu5bUapYULUuPOuA6ojdESopb3efm3pPlQw7ZYLQlOCxZP+Rx r4+xPIQQ8UShwmRB/ju/Bb9Zs0dDvO3Wzd2Pf4Ev73V+xyAAOfjiOGsndt+rFC+QqlNx IgS6NOEKqiOG8AM2GKt02QP6sPjL8dgPsiGPEMzzP3oajhEjF2LGKHahTDbq7mEKDWbR lZoT5nJWrVD+vUlYFIJjevIuV3CbERSNZ3ZLBbefXdXBOzd6qg9qBEMX3wu4fSZRnzbK rf4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703155443; x=1703760243; 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=Bo5HJl27sw1CMSOKy+oOqaIrDQeJX/kJbyC1rLu2v4w=; b=jp7mEc235Y6mnjMfdIhyHBpc2cgYpdxJaGPJkYYMMGbCTamDVgi3RTdJkdq5FQ5Mqc b4yawj1HPh1LE8dhYBKhMgXaf8X2cS2Ajiwc7x+KmGnUaY//+G5sq+PNjk9AQ+W2rNf4 aDKTI2DpMVkY0Ec2DD5zaEYvacOfcDzOuzNDWePOMBogfXx50hOR77G5lTdzMJhwGle6 GWbWhv9dZRbkYP3pxOhhZzCssSeTZYp6j2GP/ZuA4oeLD5wzYf/8fOjDhJYjs8sG/upr WukgxtQx84ZMAuJOGox7dyQa0PTM8MusXNM4HTpsiqiYw9bHsUo9m+yEKt2bW3LUakcW Bqfg== X-Gm-Message-State: AOJu0YyjcapEPF3CLrstS7r5zsajXGulEOqlwx9UDxWb3ZpHTNtRyrxb kk8IUPKJOvViRk6n+IJz6Pc= X-Received: by 2002:a05:6a20:a10e:b0:190:8c90:c310 with SMTP id q14-20020a056a20a10e00b001908c90c310mr795053pzk.42.1703155443225; Thu, 21 Dec 2023 02:44:03 -0800 (PST) Received: from g2039B650.. ([106.39.42.144]) by smtp.gmail.com with ESMTPSA id ei8-20020a17090ae54800b0028bd9f88576sm1470111pjb.26.2023.12.21.02.43.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Dec 2023 02:44:02 -0800 (PST) From: Gui-Dong Han <2045gemini@gmail.com> To: song@kernel.org, yukuai3@huawei.com Cc: linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org, baijiaju1990@outlook.com, Gui-Dong Han <2045gemini@gmail.com>, BassCheck Subject: [PATCH] md/raid5: fix atomicity violation in raid5_cache_count Date: Thu, 21 Dec 2023 18:43:43 +0800 Message-Id: <20231221104343.5557-1-2045gemini@gmail.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In raid5_cache_count(): if (conf->max_nr_stripes < conf->min_nr_stripes) return 0; return conf->max_nr_stripes - conf->min_nr_stripes; The current check is ineffective, as the values could change immediately after being checked. In raid5_set_cache_size(): ... conf->min_nr_stripes = size; ... while (size > conf->max_nr_stripes) conf->min_nr_stripes = conf->max_nr_stripes; ... Due to intermediate value updates in raid5_set_cache_size(), concurrent execution of raid5_cache_count() and raid5_set_cache_size() may lead to inconsistent reads of conf->max_nr_stripes and conf->min_nr_stripes. The current checks are ineffective as values could change immediately after being checked, raising the risk of conf->min_nr_stripes exceeding conf->max_nr_stripes and potentially causing an integer overflow. This possible bug is found by an experimental static analysis tool developed by our team. This tool analyzes the locking APIs to extract function pairs that can be concurrently executed, and then analyzes the instructions in the paired functions to identify possible concurrency bugs including data races and atomicity violations. The above possible bug is reported when our tool analyzes the source code of Linux 6.2. To resolve this issue, it is suggested to introduce local variables 'min_stripes' and 'max_stripes' in raid5_cache_count() to ensure the values remain stable throughout the check. Adding locks in raid5_cache_count() fails to resolve atomicity violations, as raid5_set_cache_size() may hold intermediate values of conf->min_nr_stripes while unlocked. With this patch applied, our tool no longer reports the bug, with the kernel configuration allyesconfig for x86_64. Due to the lack of associated hardware, we cannot test the patch in runtime testing, and just verify it according to the code logic. Fixes: edbe83ab4c27e ("md/raid5: allow the stripe_cache to grow and ...") Reported-by: BassCheck Signed-off-by: Gui-Dong Han <2045gemini@gmail.com> --- drivers/md/raid5.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c index 8497880135ee..62ebf33402cc 100644 --- a/drivers/md/raid5.c +++ b/drivers/md/raid5.c @@ -7390,11 +7390,12 @@ static unsigned long raid5_cache_count(struct shrinker *shrink, struct shrink_control *sc) { struct r5conf *conf = shrink->private_data; - - if (conf->max_nr_stripes < conf->min_nr_stripes) + int max_stripes = conf->max_nr_stripes; + int min_stripes = conf->min_nr_stripes; + if (max_stripes < min_stripes) /* unlikely, but not impossible */ return 0; - return conf->max_nr_stripes - conf->min_nr_stripes; + return max_stripes - min_stripes; } static struct r5conf *setup_conf(struct mddev *mddev) -- 2.34.1