linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yufen Yu <yuyufen@huawei.com>
To: <axboe@kernel.dk>, <linux-block@vger.kernel.org>
Cc: <jack@suse.cz>, <bvanassche@acm.org>, <tj@kernel.org>
Subject: Re: [PATCH] bdi: fix use-after-free for bdi device
Date: Tue, 11 Feb 2020 21:57:44 +0800	[thread overview]
Message-ID: <b7cd6193-586b-f4e0-9a5d-cc961eafaf81@huawei.com> (raw)
In-Reply-To: <20200211140038.146629-1-yuyufen@huawei.com>


> __blkg_prfill_rwstat() tries to get the device name by
> 'bdi->dev', while the 'dev' has been freed by bdi_unregister().
> Then, blkg_dev_name() will return an invalid name pointer,
> resulting in crash on string(). The race as following:
> 
> CPU1                          CPU2
> blkg_print_stat_bytes
>                                __scsi_remove_device
>                                del_gendisk
>                                  bdi_unregister
> 
>                                  put_device(bdi->dev)
>                                    kfree(bdi->dev)
> __blkg_prfill_rwstat
>    blkg_dev_name
>      //use the freed bdi->dev
>      dev_name(blkg->q->backing_dev_info->dev)
>                                  bdi->dev = NULL
> 

There is another simple way to fix the problem.

Since blkg_dev_name() have been coverd by rcu_read_lock/unlock(),
we can wait all rcu reader to finish before free 'bdi->dev' to avoid use-after-free.

But I am not sure if this solution will introduce new problems.


diff --git a/mm/backing-dev.c b/mm/backing-dev.c
index 62f05f605fb5..6f322473ca4d 100644
--- a/mm/backing-dev.c
+++ b/mm/backing-dev.c
@@ -1007,15 +1007,19 @@ static void bdi_remove_from_list(struct backing_dev_info *bdi)

  void bdi_unregister(struct backing_dev_info *bdi)
  {
+       struct device *dev = bdi->dev;
         /* make sure nobody finds us on the bdi_list anymore */
         bdi_remove_from_list(bdi);
         wb_shutdown(&bdi->wb);
         cgwb_bdi_unregister(bdi);

-       if (bdi->dev) {
+       if (dev) {
                 bdi_debug_unregister(bdi);
-               device_unregister(bdi->dev);
                 bdi->dev = NULL;
+               device_del(dev);
+               /* wait all rcu reader of bdi->dev before free dev */
+               synchronize_rcu();
+               put_device(dev);
         }

         if (bdi->owner) {

  reply	other threads:[~2020-02-11 13:57 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-11 14:00 [PATCH] bdi: fix use-after-free for bdi device Yufen Yu
2020-02-11 13:57 ` Yufen Yu [this message]
2020-02-12 21:33   ` Tejun Heo
2020-02-13  2:46     ` Yufen Yu
2020-02-13  3:48       ` Tejun Heo
2020-02-13  7:51         ` Yufen Yu
2020-02-13 13:58           ` Tejun Heo
2020-02-14  2:50             ` Yufen Yu
2020-02-14 14:05               ` Tejun Heo
2020-02-15 13:54                 ` Yufen Yu
2020-02-19 12:55                   ` Jan Kara
2020-02-19 15:12                     ` Tejun Heo
2020-02-20 11:07                     ` Yufen Yu
2020-02-20 12:07                       ` Jan Kara

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=b7cd6193-586b-f4e0-9a5d-cc961eafaf81@huawei.com \
    --to=yuyufen@huawei.com \
    --cc=axboe@kernel.dk \
    --cc=bvanassche@acm.org \
    --cc=jack@suse.cz \
    --cc=linux-block@vger.kernel.org \
    --cc=tj@kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).