All of lore.kernel.org
 help / color / mirror / Atom feed
From: Christoph Hellwig <hch@lst.de>
To: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
Cc: axboe@kernel.dk, Christoph Hellwig <hch@lst.de>,
	linux-block@vger.kernel.org
Subject: Re: [PATCH] block: genhd: fix double kfree() in __alloc_disk_node()
Date: Mon, 20 Sep 2021 08:40:28 +0200	[thread overview]
Message-ID: <20210920064028.GB26999@lst.de> (raw)
In-Reply-To: <3999c511-cd27-1554-d3a6-4288c3eca298@i-love.sakura.ne.jp>

On Sun, Sep 19, 2021 at 11:44:29PM +0900, Tetsuo Handa wrote:
> syzbot is reporting use-after-free read at bdev_free_inode() [1], for
> kfree() from __alloc_disk_node() is called before bdev_free_inode()
> (which is called after RCU grace period) reads bdev->bd_disk and calls
> kfree(bdev->bd_disk).
> 
> Fix use-after-free read followed by double kfree() problem
> by explicitly resetting bdev->bd_disk to NULL before calling iput().
> 
> Link: https://syzkaller.appspot.com/bug?extid=8281086e8a6fbfbd952a [1]
> Reported-by: syzbot <syzbot+8281086e8a6fbfbd952a@syzkaller.appspotmail.com>
> Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
> ---
> This patch is not tested due to lack of reproducer. Is this fix correct?
> 
>  block/bdev.c  | 1 +
>  block/genhd.c | 1 +
>  2 files changed, 2 insertions(+)
> 
> diff --git a/block/bdev.c b/block/bdev.c
> index cf2780cb44a7..f6b8bac83bd8 100644
> --- a/block/bdev.c
> +++ b/block/bdev.c
> @@ -495,6 +495,7 @@ struct block_device *bdev_alloc(struct gendisk *disk, u8 partno)
>  	bdev->bd_inode = inode;
>  	bdev->bd_stats = alloc_percpu(struct disk_stats);
>  	if (!bdev->bd_stats) {
> +		bdev->bd_disk = NULL;
>  		iput(inode);
>  		return NULL;
>  	}

I was going to suggest to just move the bd_disk initialization after
the bd_stats allocations,  but iseems like we currently don't even
the zero the bdev on allocation.  So I suspect we should do that first
to avoid nasty surprises.

  reply	other threads:[~2021-09-20  6:40 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-19 13:35 [syzbot] KASAN: use-after-free Read in bdev_free_inode syzbot
2021-09-19 13:37 ` Tetsuo Handa
2021-09-19 14:44   ` [PATCH] block: genhd: fix double kfree() in __alloc_disk_node() Tetsuo Handa
2021-09-20  6:40     ` Christoph Hellwig [this message]
2021-09-20  8:02       ` Tetsuo Handa
2021-09-20  8:05         ` Christoph Hellwig
2021-09-20  8:31           ` [PATCH v2] " Tetsuo Handa
2021-09-20  8:40             ` Christoph Hellwig
2021-10-09  4:53 ` [syzbot] KASAN: use-after-free Read in bdev_free_inode syzbot

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=20210920064028.GB26999@lst.de \
    --to=hch@lst.de \
    --cc=axboe@kernel.dk \
    --cc=linux-block@vger.kernel.org \
    --cc=penguin-kernel@i-love.sakura.ne.jp \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.