Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp441716iog; Mon, 13 Jun 2022 06:05:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxLc5smERsS6Kq/Vm+ogb8aCoWIE1tD5iWTb8QTFOjYnhEBadLjLHv2OQu8PCDWQwCMUgVo X-Received: by 2002:a05:6402:750:b0:42d:a765:8637 with SMTP id p16-20020a056402075000b0042da7658637mr64899983edy.342.1655125518460; Mon, 13 Jun 2022 06:05:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655125518; cv=none; d=google.com; s=arc-20160816; b=XIUr/DWt3jU76ro9Mwf61w8T8EeN+a4t7FMEUwdIQ7rWT3zEn71q6Df9aVDxg6/c8i 7sAuEmVfcyIsPzRy1t2J5SnAYa+YUQe+88DJiOLEgHrZm24wBavE6B61O1+hQnb5YFYM 9L1mckrH7kDA7JYKKJ2xOv2flx12Zeldv4xLg3O7aMII2ONEyECBrPYgoNnZFvq2/a0Y SokldC2OC+Hj9/omnH/Jk+xrSNrRVCK815uc7YVvnNQlYcesSUqGBJWBYo+T+PF2ZzG+ VdkrbxyV1xumKnEdYcf5UlNe6ZokJBa5IOs4QlUuUoebIXv731bZ1lDEn+tnLWCuXxK3 BAUQ== 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=lolHn135a1vYwBsOmBFgglcUczG0aRE4SDezKIwJb+A=; b=pt29GBYo/lqJ2jU4Rg0KfFo7GF8lLpAN3K9GfPaJE/KWHediC0vyFdTraNSTwqJL+3 8fcwLceVvV4wuxj+d7nEgzw9jdfb7xLuyuNvRdJR5NTFBCzm6aOP1m47gTsxGdW1Ii61 A1Lo1wGmLJ8OYR3D2Yz8emVPP30Y7gqHzwbifaOK4e4K9w47X4AbHRgufodx14cx4Glg gPtJ2I5fmvVtYY91pRculV3V4dA3m7yKyTDQZujibEvf9iE3AR/gDiZs72mxsIcVOyB0 LsiUXJt3a1rD6EOAomvfY/auuS1u35KI0g2qXb9SpS7XzpBjEOZNj5W76ec7eiZM0a7Z rIhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0QatUJ3c; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nc31-20020a1709071c1f00b00708de654a3esi8568551ejc.995.2022.06.13.06.04.49; Mon, 13 Jun 2022 06:05:18 -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=@linuxfoundation.org header.s=korg header.b=0QatUJ3c; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243325AbiFMKX0 (ORCPT + 99 others); Mon, 13 Jun 2022 06:23:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241989AbiFMKWl (ORCPT ); Mon, 13 Jun 2022 06:22:41 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C0961FCFB; Mon, 13 Jun 2022 03:17:54 -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 6AEBD6066C; Mon, 13 Jun 2022 10:17:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4DB5AC3411C; Mon, 13 Jun 2022 10:17:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655115472; bh=yQaSjnJx43yFK8oDB8LXKvE+rIhRgPudwBeneeFl5Iw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0QatUJ3cdxuaxnu2pM7tjywigLDKE3vg++EPUkAV0ifXL8rUfnbcFQFXRoHyqupjU kERysvpRFOTt8jpoa4NifAOWTzqoGDMHACGUkVPzq2T+Vqps29cFpmq3FJo31aBavj XQ47mvU3n3s2yh9U+lYRLtSSgrYRFvvaAoR8l5cE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xiaomeng Tong , Song Liu Subject: [PATCH 4.9 089/167] md: fix an incorrect NULL check in md_reload_sb Date: Mon, 13 Jun 2022 12:09:23 +0200 Message-Id: <20220613094901.752890280@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220613094840.720778945@linuxfoundation.org> References: <20220613094840.720778945@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=-8.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 @@ -8882,16 +8882,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; }