Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2195582iof; Tue, 7 Jun 2022 22:52:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxrYjv72Ifk0vWWgpZ1LEhPt8IU4pqjDCsEzlNTCyS1lOZLRkMpnjMKU+WhFpnbDXm9Jwnm X-Received: by 2002:a17:90a:4413:b0:1cd:2d00:9d0b with SMTP id s19-20020a17090a441300b001cd2d009d0bmr35785438pjg.81.1654667524345; Tue, 07 Jun 2022 22:52:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654667524; cv=none; d=google.com; s=arc-20160816; b=DZ0QORPKFXFIbSyrHJ3yimTHWYoPDSUJTT8jXFi8Q9862cTFBhoZOqDnzq6MEyRCk6 CW8+WY2YPc1Ep9l+dxIOehh9c0ndAAuXh/wcI68+wmShox/J6CTQVzYWCAKKlPFDJU/A e87M9oRANyDXEA0DbxETEh/XPHENsWzLRPU1C7fb/dTaKrE47ZHrqX/fV2/oy7uwv7j7 JfXyjbkg+Saezho0CQTAPorc1qQPwVigNSl3Yd6lSAA/TYze/3VwWrBm4AYA/C4Cxjf1 SMAassPk/V3FCwxpLYTPHlDpxIpeJf6ID8AredvRGcI6scOmkVaGU0t6HVONlcVIyCxh ucrg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=GA9X4a51sD748ThwpT4kjEGTfycLFiL3dLv3pTv7LyI=; b=jLlayxcJ7yRI+mtAn9N7d4owePbBHmdCNLnvmFf8z2UnmuZ8sPklDcJWsg041WQGcz BVA8/OODTM31ERd7PD8PLLrzGY6YEO3eyyjWRevxOZKGdu024Di0sXGqvos1KQ9LiOvl DXrKvVAYPyBYGjed/ewoF53/zjUu9TNCtDEKiEzIxjNglLBkWS3ImHz+PW4NhQZljNMM tlLV63A4V52NbjiSGZ0N5ny0mPv2VJMr1v0G05n+KhCT1ETjIOzFV2ghHT4AhG09Ge0Q fdTtWfNQsWe4941fK13S9SOftVs4kxAvDjCv/++/ofQBVp1OJLhxL87byrxHlvzSTBpT w+sQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rKXE6eGZ; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id i10-20020a170902e48a00b00153bc4c8388si24043390ple.499.2022.06.07.22.52.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 22:52:04 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rKXE6eGZ; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 64CA64A9DEF; Tue, 7 Jun 2022 22:18:51 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1392105AbiFHAvo (ORCPT + 99 others); Tue, 7 Jun 2022 20:51:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380568AbiFGWWU (ORCPT ); Tue, 7 Jun 2022 18:22:20 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57EEF26D34A; Tue, 7 Jun 2022 12:22:33 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 053BCB823CA; Tue, 7 Jun 2022 19:22:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 76887C385A2; Tue, 7 Jun 2022 19:22:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654629741; bh=UOKdETRuR5DXM21XqefVhp20EYHK9K/myDZldj1142s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rKXE6eGZ5nHiJtSXnVaGiNSqaIHrYtWLO+e7HB8KUxMY/UHYr4ln0n8AYDmoEDbP0 RQlDxR8MHsMiCT64hujXACfUEyWxAV3usCV7tkBK92FHF2WA+FZ0g4tKiyHQQqb2uy nH55nWi83MbYS8r9Z08nvKbza91wR+6/gCLM5m7s= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xiaomeng Tong , Song Liu Subject: [PATCH 5.18 799/879] md: fix an incorrect NULL check in md_reload_sb Date: Tue, 7 Jun 2022 19:05:17 +0200 Message-Id: <20220607165026.051730455@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607165002.659942637@linuxfoundation.org> References: <20220607165002.659942637@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE 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 From: Xiaomeng Tong commit 64c54d9244a4efe9bc6e9c98e13c4bbb8bb39083 upstream. The bug is here: if (!rdev || rdev->desc_nr != nr) { The list iterator value 'rdev' will *always* be set and non-NULL by rdev_for_each_rcu(), so it is incorrect to assume that the iterator value will be NULL if the list is empty or no element found (In fact, it will be a bogus pointer to an invalid struct object containing the HEAD). Otherwise it will bypass the check and lead to invalid memory access passing the check. To fix the bug, use a new variable 'iter' as the list iterator, while using the original variable 'pdev' as a dedicated pointer to point to the found element. Cc: stable@vger.kernel.org Fixes: 70bcecdb1534 ("md-cluster: Improve md_reload_sb to be less error prone") Signed-off-by: Xiaomeng Tong Signed-off-by: Song Liu Signed-off-by: Greg Kroah-Hartman --- drivers/md/md.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) --- a/drivers/md/md.c +++ b/drivers/md/md.c @@ -9793,16 +9793,18 @@ static int read_rdev(struct mddev *mddev void md_reload_sb(struct mddev *mddev, int nr) { - struct md_rdev *rdev; + struct md_rdev *rdev = NULL, *iter; int err; /* Find the rdev */ - rdev_for_each_rcu(rdev, mddev) { - if (rdev->desc_nr == nr) + rdev_for_each_rcu(iter, mddev) { + if (iter->desc_nr == nr) { + rdev = iter; break; + } } - if (!rdev || rdev->desc_nr != nr) { + if (!rdev) { pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr); return; }