Received: by 2002:a05:6520:4211:b029:f4:110d:56bc with SMTP id o17csp2110620lkv; Thu, 20 May 2021 03:39:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJywCMm8w7pbgYw4WyB+pp7Srbh9047GkvdprTuDGptd314LgKGOnFMG5WUNUUZGnJj6I9Yo X-Received: by 2002:a02:866a:: with SMTP id e97mr3375246jai.136.1621507190904; Thu, 20 May 2021 03:39:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621507190; cv=none; d=google.com; s=arc-20160816; b=0IhWdcf7gyLpJcBWYaaRvBkZC1YlvZg6vYZTsp1DdZ3vmqRiZJK+LB2d2ZjLRXbFVg WwGrT0WG/UWx4RoWZRu8PP737rPejnVViBqjvm0UQP+mjAxF/Nt2soEysMDRSL0tLYLG xzUh6NODR07DKhfC/iFfurpi66SMS0oQ1BUkV9ubh3YGrBvQqLe5LL3Okn0d0dLw6bod RqXG63wZjGhK6QfABX7UwNZ0AsI+9N+YKlnkhp2RZ1hyftARQUaAifSnMrwoUiDmUWBN 9zthV9hcBI+t24fnEW9vmUIYFiRKLjQuKXIeVqsGFciniQhEpAD56ceRTI3KVima04lG Qz0w== 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=YPn6sic/6ogjgQXFClOIJTENcWcJjXNrlmRQpBFRBgM=; b=TlnpSYTsSMCKxnsmE+6GS1U9vsTxf61SsV5U9pvLfVEQE4Bd4PAyTiU3mkLzzfIsBh mV7fvWpXo3jIpL32fuTWPHZp3fSt8UMHrMcyFM5GEhu8PmPzweS8mlDZAwC4ScuC20xP tsnHlv9lsJG12d493MLtDyWF6c6nf27Bz8gV2j7IBk6juv6t65Jweqbt7+o2F/lPUK7u qxhRdVn6EruNnhkgRcXRH8yDNGTHp+h1Mm/yiQEr7ONMNI51b239/PnuBCuHOCmOC8Ta x94blXff2jiLeGWfc8L3rIA1F4S4s/KuWNWQgjs3HXyAXurBDvwaXQdcu6SqNBm2oF/1 Y2OA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UMGO+6yo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f2si2223792ioz.41.2021.05.20.03.39.21; Thu, 20 May 2021 03:39:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UMGO+6yo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S238213AbhETKjl (ORCPT + 99 others); Thu, 20 May 2021 06:39:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:54246 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236801AbhETKYc (ORCPT ); Thu, 20 May 2021 06:24:32 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7DF2F61A36; Thu, 20 May 2021 09:49:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621504166; bh=J5MH54xNeuQyCdX7JeYoUeQSCQSEwkPu2DURWMb8VIc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UMGO+6yoZ/auG1E4d4xFqDdzExzd/72HG2ZwuLtOP4/v63cZhhef8XYN71C3WzqRq w09wkZprGs5P845TaTbTvUNSo+LPxmO91dojrYaKiuvbugDeUuHFWJkrPmh2NbXRKv f70f5+8KwwB4/tjj5cwXzavpkUY8Uu2wli8kMsbY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Gang He , Heming Zhao , Song Liu Subject: [PATCH 4.14 121/323] md-cluster: fix use-after-free issue when removing rdev Date: Thu, 20 May 2021 11:20:13 +0200 Message-Id: <20210520092124.258236324@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520092120.115153432@linuxfoundation.org> References: <20210520092120.115153432@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Heming Zhao commit f7c7a2f9a23e5b6e0f5251f29648d0238bb7757e upstream. md_kick_rdev_from_array will remove rdev, so we should use rdev_for_each_safe to search list. How to trigger: env: Two nodes on kvm-qemu x86_64 VMs (2C2G with 2 iscsi luns). ``` node2=192.168.0.3 for i in {1..20}; do echo ==== $i `date` ====; mdadm -Ss && ssh ${node2} "mdadm -Ss" wipefs -a /dev/sda /dev/sdb mdadm -CR /dev/md0 -b clustered -e 1.2 -n 2 -l 1 /dev/sda \ /dev/sdb --assume-clean ssh ${node2} "mdadm -A /dev/md0 /dev/sda /dev/sdb" mdadm --wait /dev/md0 ssh ${node2} "mdadm --wait /dev/md0" mdadm --manage /dev/md0 --fail /dev/sda --remove /dev/sda sleep 1 done ``` Crash stack: ``` stack segment: 0000 [#1] SMP ... ... RIP: 0010:md_check_recovery+0x1e8/0x570 [md_mod] ... ... RSP: 0018:ffffb149807a7d68 EFLAGS: 00010207 RAX: 0000000000000000 RBX: ffff9d494c180800 RCX: ffff9d490fc01e50 RDX: fffff047c0ed8308 RSI: 0000000000000246 RDI: 0000000000000246 RBP: 6b6b6b6b6b6b6b6b R08: ffff9d490fc01e40 R09: 0000000000000000 R10: 0000000000000001 R11: 0000000000000001 R12: 0000000000000000 R13: ffff9d494c180818 R14: ffff9d493399ef38 R15: ffff9d4933a1d800 FS: 0000000000000000(0000) GS:ffff9d494f700000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007fe68cab9010 CR3: 000000004c6be001 CR4: 00000000003706e0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 Call Trace: raid1d+0x5c/0xd40 [raid1] ? finish_task_switch+0x75/0x2a0 ? lock_timer_base+0x67/0x80 ? try_to_del_timer_sync+0x4d/0x80 ? del_timer_sync+0x41/0x50 ? schedule_timeout+0x254/0x2d0 ? md_start_sync+0xe0/0xe0 [md_mod] ? md_thread+0x127/0x160 [md_mod] md_thread+0x127/0x160 [md_mod] ? wait_woken+0x80/0x80 kthread+0x10d/0x130 ? kthread_park+0xa0/0xa0 ret_from_fork+0x1f/0x40 ``` Fixes: dbb64f8635f5d ("md-cluster: Fix adding of new disk with new reload code") Fixes: 659b254fa7392 ("md-cluster: remove a disk asynchronously from cluster environment") Cc: stable@vger.kernel.org Reviewed-by: Gang He Signed-off-by: Heming Zhao Signed-off-by: Song Liu Signed-off-by: Greg Kroah-Hartman --- drivers/md/md.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) --- a/drivers/md/md.c +++ b/drivers/md/md.c @@ -8831,11 +8831,11 @@ void md_check_recovery(struct mddev *mdd } if (mddev_is_clustered(mddev)) { - struct md_rdev *rdev; + struct md_rdev *rdev, *tmp; /* kick the device if another node issued a * remove disk. */ - rdev_for_each(rdev, mddev) { + rdev_for_each_safe(rdev, tmp, mddev) { if (test_and_clear_bit(ClusterRemove, &rdev->flags) && rdev->raid_disk < 0) md_kick_rdev_from_array(rdev); @@ -9135,7 +9135,7 @@ err_wq: static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev) { struct mdp_superblock_1 *sb = page_address(rdev->sb_page); - struct md_rdev *rdev2; + struct md_rdev *rdev2, *tmp; int role, ret; char b[BDEVNAME_SIZE]; @@ -9152,7 +9152,7 @@ static void check_sb_changes(struct mdde } /* Check for change of roles in the active devices */ - rdev_for_each(rdev2, mddev) { + rdev_for_each_safe(rdev2, tmp, mddev) { if (test_bit(Faulty, &rdev2->flags)) continue;