mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yu Kuai <yukuai1@huaweicloud.com>
To: Logan Gunthorpe <logang@deltatee.com>,
	Yu Kuai <yukuai1@huaweicloud.com>,
	dm-devel@redhat.com, song@kernel.org
Cc: linux-kernel@vger.kernel.org, linux-raid@vger.kernel.org,
	yi.zhang@huawei.com, yangerkun@huawei.com,
	"yukuai (C)" <yukuai3@huawei.com>
Subject: Re: [PATCH -next v6 5/5] md: protect md_thread with rcu
Date: Fri, 14 Apr 2023 09:26:35 +0800	[thread overview]
Message-ID: <720261bf-2dfd-1298-4664-853a8e5cf132@huaweicloud.com> (raw)
In-Reply-To: <961691d0-7224-caad-6c19-d9c8ca07a801@deltatee.com>

Hi,

在 2023/04/13 23:10, Logan Gunthorpe 写道:
> 
> 
> On 2023-04-13 05:32, Yu Kuai wrote:
>> From: Yu Kuai <yukuai3@huawei.com>
>>
>> Our test reports a uaf for 'mddev->sync_thread':
>>
>> T1                      T2
>> md_start_sync
>>   md_register_thread
>>   // mddev->sync_thread is set
>> 			raid1d
>> 			 md_check_recovery
>> 			  md_reap_sync_thread
>> 			   md_unregister_thread
>> 			    kfree
>>
>>   md_wakeup_thread
>>    wake_up
>>    ->sync_thread was freed
>>
>> Root cause is that there is a small windown between register thread and
>> wake up thread, where the thread can be freed concurrently.
>>
>> Currently, a global spinlock 'pers_lock' is borrowed to protect
>> 'mddev->thread', this problem can be fixed likewise, however, there are
>> similar problems elsewhere, and use a global lock for all the cases is
>> not good.
>>
>> This patch protect all md_thread with rcu.
>>
>> Signed-off-by: Yu Kuai <yukuai3@huawei.com>
>> ---
>>   drivers/md/md-bitmap.c    | 10 ++++--
>>   drivers/md/md-cluster.c   |  7 ++--
>>   drivers/md/md-multipath.c |  4 +--
>>   drivers/md/md.c           | 69 ++++++++++++++++++---------------------
>>   drivers/md/md.h           |  8 ++---
>>   drivers/md/raid1.c        |  7 ++--
>>   drivers/md/raid1.h        |  2 +-
>>   drivers/md/raid10.c       | 21 +++++++-----
>>   drivers/md/raid10.h       |  2 +-
>>   drivers/md/raid5-cache.c  | 22 ++++++++-----
>>   drivers/md/raid5.c        | 15 +++++----
>>   drivers/md/raid5.h        |  2 +-
>>   12 files changed, 91 insertions(+), 78 deletions(-)
>>
>> diff --git a/drivers/md/md-bitmap.c b/drivers/md/md-bitmap.c
>> index 29fd41ef55a6..ab27f66dbb1f 100644
>> --- a/drivers/md/md-bitmap.c
>> +++ b/drivers/md/md-bitmap.c
>> @@ -1221,13 +1221,19 @@ static bitmap_counter_t *md_bitmap_get_counter(struct bitmap_counts *bitmap,
>>   static void mddev_set_timeout(struct mddev *mddev, unsigned long timeout,
>>   			      bool force)
>>   {
>> -	struct md_thread *thread = mddev->thread;
>> +	struct md_thread *thread;
>> +
>> +	rcu_read_lock();
>> +	thread = rcu_dereference(mddev->thread);
>>   
>>   	if (!thread)
>> -		return;
>> +		goto out;
>>   
>>   	if (force || thread->timeout < MAX_SCHEDULE_TIMEOUT)
>>   		thread->timeout = timeout;
>> +
>> +out:
>> +	rcu_read_unlock();
>>   }
>>   
>>   /*
>> diff --git a/drivers/md/md-cluster.c b/drivers/md/md-cluster.c
>> index 10e0c5381d01..672dfa6a40ec 100644
>> --- a/drivers/md/md-cluster.c
>> +++ b/drivers/md/md-cluster.c
>> @@ -362,8 +362,8 @@ static void __recover_slot(struct mddev *mddev, int slot)
>>   
>>   	set_bit(slot, &cinfo->recovery_map);
>>   	if (!cinfo->recovery_thread) {
>> -		cinfo->recovery_thread = md_register_thread(recover_bitmaps,
>> -				mddev, "recover");
>> +		rcu_assign_pointer(cinfo->recovery_thread,
>> +			md_register_thread(recover_bitmaps, mddev, "recover"));
>>   		if (!cinfo->recovery_thread) {
>>   			pr_warn("md-cluster: Could not create recovery thread\n");
>>   			return;
>> @@ -889,7 +889,8 @@ static int join(struct mddev *mddev, int nodes)
>>   	}
>>   	/* Initiate the communication resources */
>>   	ret = -ENOMEM;
>> -	cinfo->recv_thread = md_register_thread(recv_daemon, mddev, "cluster_recv");
>> +	rcu_assign_pointer(cinfo->recv_thread,
>> +			md_register_thread(recv_daemon, mddev, "cluster_recv"));
>>   	if (!cinfo->recv_thread) {
> 
> This looks like it'll hit sparse warnings. without an
> rcu_access_pointer(). Might be nicer to use a temporary variable, check
> that it's not null, then call rcu_assign_pointer().

Sorry this is because I missed following change:

diff --git a/drivers/md/md-cluster.c b/drivers/md/md-cluster.c
index 672dfa6a40ec..2f64673b6ed2 100644
--- a/drivers/md/md-cluster.c
+++ b/drivers/md/md-cluster.c
@@ -75,14 +75,14 @@ struct md_cluster_info {
         sector_t suspend_hi;
         int suspend_from; /* the slot which broadcast suspend_lo/hi */

-       struct md_thread *recovery_thread;
+       struct md_thread __rcu *recovery_thread;
         unsigned long recovery_map;
         /* communication loc resources */
         struct dlm_lock_resource *ack_lockres;
         struct dlm_lock_resource *message_lockres;
         struct dlm_lock_resource *token_lockres;
         struct dlm_lock_resource *no_new_dev_lockres;
-       struct md_thread *recv_thread;
+       struct md_thread __rcu *recv_thread;
         struct completion newdisk_completion;
         wait_queue_head_t wait;
         unsigned long state;

Thanks,
Kuai


  reply	other threads:[~2023-04-14  1:26 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-13 11:32 [PATCH -next v6 0/5] " Yu Kuai
2023-04-13 11:32 ` [PATCH -next v6 1/5] md: factor out a helper to wake up md_thread directly Yu Kuai
2023-04-13 11:32 ` [PATCH -next v6 2/5] dm-raid: remove useless checking in raid_message() Yu Kuai
2023-04-13 11:32 ` [PATCH -next v6 3/5] md/bitmap: always wake up md_thread in timeout_store Yu Kuai
2023-04-13 11:32 ` [PATCH -next v6 4/5] md/bitmap: factor out a helper to set timeout Yu Kuai
2023-04-13 11:32 ` [PATCH -next v6 5/5] md: protect md_thread with rcu Yu Kuai
2023-04-13 15:10   ` Logan Gunthorpe
2023-04-14  1:26     ` Yu Kuai [this message]
2023-04-14  1:32     ` [PATCH -next v7 " Yu Kuai
2023-04-23  2:42       ` Yu Kuai
2023-04-24 22:38         ` Song Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=720261bf-2dfd-1298-4664-853a8e5cf132@huaweicloud.com \
    --to=yukuai1@huaweicloud.com \
    --cc=dm-devel@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-raid@vger.kernel.org \
    --cc=logang@deltatee.com \
    --cc=song@kernel.org \
    --cc=yangerkun@huawei.com \
    --cc=yi.zhang@huawei.com \
    --cc=yukuai3@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®