Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2250666iof; Wed, 8 Jun 2022 00:30:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzgXCTRYpluLuvDj3wigtk3zi69yrjiIZmeMgvZ7E523KwY718FKMxEtWw9Gd/YvCnZrsjD X-Received: by 2002:a63:171a:0:b0:3fc:1970:804a with SMTP id x26-20020a63171a000000b003fc1970804amr29055377pgl.299.1654673417431; Wed, 08 Jun 2022 00:30:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654673417; cv=none; d=google.com; s=arc-20160816; b=XcPv9VrfiFhpiMclre2/KrAGcbmVnADfilYp2bg6T6IP74Bprz/0nXEaiN0n6pPLKk MyLYkV3EcudNkN0B64n3vpVL9kgIxsFjIOC7MgQGTtPPmW7K7nU2XdwC8Qk2+vu+XnA1 YesgsJxWM8yXDuqiMPZypF97vbFHmhLbitDJlNOQJCRoREJE4+JCpn6YGy06c6vNlb0s mk07tmPxNMhsgGPcV01DDFUugdqhz41UjKeJmw08z01Monu+f1MEnTKrrBlVv+pnpFkA 4WFyIM0B8izolbsCqyL7nOiNiBdW+YVQL/2K/VFBv9iKtD96mh9ESukFPDvyda8P+2Ys mEzw== 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=wqgQXa7AfoARYP7lW/hdzgFZTho2qNp3lHlkZmAa6h4=; b=X1g0WQRDDmNIPgfuB7oio6ubjLM2EJEgwMpPYa6CqqVkkHEMirJAJ69+++EiSpWWaR GGyvJPpBXyzktNm1CT2dSrANFS2AS1i4LEyarvP2osFK9dquwGKuhdMXS7/UD2hcePHM f3rq43z6T1ujRiFSzUUPM5yyLO+QobPxmi5Jo0NLebIgITseUJlrf7qOrJJU6+x+0npJ svjUj6itfxruW2AyDJFMXZh4iQ5uOwmD3AQDgdlGPnWl90cKa60KgC6+cZ5G5r/Gdjim P0KuJZRcImVb2XMvPzsSd8Cy2SdCZ0TX3e/RrcFE3NhtWpk+j+DXyZ3FVg727SYMnyyK N80w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=FkHx9gDz; 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 y19-20020aa79af3000000b00518c3e572e3si24393233pfp.295.2022.06.08.00.30.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 00:30:17 -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=FkHx9gDz; 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 D3BAF1AA3DF; Wed, 8 Jun 2022 00:02:27 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348796AbiFGSEb (ORCPT + 99 others); Tue, 7 Jun 2022 14:04:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348825AbiFGRqZ (ORCPT ); Tue, 7 Jun 2022 13:46:25 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B3E1189; Tue, 7 Jun 2022 10:35:47 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 16B4861529; Tue, 7 Jun 2022 17:35:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 24FB5C385A5; Tue, 7 Jun 2022 17:35:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654623346; bh=Icm0M15oLjLJ7eMipClD8rKyrsCX6j4KF2YBbrMzUbc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FkHx9gDzdAMhbZ/Wh+d1azdaZTSEkH+35VJQTh4OLGzMz1XbM6H+R9wzwZcriMyRP bHp2hKRwF7sH1GOy9uO5VAQzmixzT+B12t2m3rtNZAsZ6X6fKd5gAbjjRWGk2vxDOU nfcDCO9yTwnkmkyPMK34EcMDwIRSWVmBhCCcATzg= 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.10 385/452] md: fix an incorrect NULL check in md_reload_sb Date: Tue, 7 Jun 2022 19:04:02 +0200 Message-Id: <20220607164920.037853859@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607164908.521895282@linuxfoundation.org> References: <20220607164908.521895282@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 @@ -9730,16 +9730,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; }