public inbox for linux-ext4@vger.kernel.org
 help / color / mirror / Atom feed
From: Xiaoguang Wang <xiaoguang.wang@linux.alibaba.com>
To: Jan Kara <jack@suse.cz>, Ted Tso <tytso@mit.edu>
Cc: linux-ext4@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	Eric Sandeen <sandeen@sandeen.net>
Subject: Re: [PATCH 1/3] fs: Avoid leaving freed inode on dirty list
Date: Sat, 25 Apr 2020 14:42:03 +0800	[thread overview]
Message-ID: <7ffc64ad-4741-27ca-ba9d-3d23af0a9216@linux.alibaba.com> (raw)
In-Reply-To: <20200421085445.5731-2-jack@suse.cz>

hi,

> evict() can race with writeback_sb_inodes() and so
> list_empty(&inode->i_io_list) check can race with list_move() from
> redirty_tail() possibly resulting in list_empty() returning false and
                                                     ^^^^^^^^^^^^^^^
                                                     returning true?
if (!list_empty(&inode->i_io_list))
     inode_io_list_del(inode);
so "!list_empty(&inode->i_io_list)" returns false, and will not remove
inode for wb->b_dirty list.
> thus we end up leaving freed inode in wb->b_dirty list leading to
> use-after-free issues.
> 
> Fix the problem by using list_empty_careful() check and add assert that
> inode's i_io_list is empty in clear_inode() to catch the problem earlier
> in the future.
 From list_empty_careful()'s comments, using list_empty_careful() without
synchronization can only be safe if the only activity that can happen to the
list entry is list_del_init(), but list_move() does not use list_del_init().

static inline void list_move(struct list_head *list, struct list_head *head)
{
	__list_del_entry(list);
	list_add(list, head);
}

So I wonder whether list_empty(&inode->i_io_list) check in evict() can race with
list_move() from redirty_tail()?

Regards,
Xiaoguang Wang
> 
> Signed-off-by: Jan Kara <jack@suse.cz>
> ---
>   fs/inode.c | 9 ++++++++-
>   1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/inode.c b/fs/inode.c
> index 93d9252a00ab..a73c8a7aa71a 100644
> --- a/fs/inode.c
> +++ b/fs/inode.c
> @@ -534,6 +534,7 @@ void clear_inode(struct inode *inode)
>   	BUG_ON(!(inode->i_state & I_FREEING));
>   	BUG_ON(inode->i_state & I_CLEAR);
>   	BUG_ON(!list_empty(&inode->i_wb_list));
> +	BUG_ON(!list_empty(&inode->i_io_list));
>   	/* don't need i_lock here, no concurrent mods to i_state */
>   	inode->i_state = I_FREEING | I_CLEAR;
>   }
> @@ -559,7 +560,13 @@ static void evict(struct inode *inode)
>   	BUG_ON(!(inode->i_state & I_FREEING));
>   	BUG_ON(!list_empty(&inode->i_lru));
>   
> -	if (!list_empty(&inode->i_io_list))
> +	/*
> +	 * We are the only holder of the inode so it cannot be marked dirty.
> +	 * Flusher thread won't start new writeback but there can be still e.g.
> +	 * redirty_tail() running from writeback_sb_inodes(). So we have to be
> +	 * careful to remove inode from dirty/io list in all the cases.
> +	 */
> +	if (!list_empty_careful(&inode->i_io_list))
>   		inode_io_list_del(inode);
>   
>   	inode_sb_list_del(inode);
> 

  reply	other threads:[~2020-04-25  6:42 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-21  8:54 ext4: Fix use after free issues with journalled data Jan Kara
2020-04-21  8:54 ` [PATCH 1/3] fs: Avoid leaving freed inode on dirty list Jan Kara
2020-04-25  6:42   ` Xiaoguang Wang [this message]
2020-04-27 10:05     ` Jan Kara
2020-04-21  8:54 ` [PATCH 2/3] writeback: Export inode_io_list_del() Jan Kara
2020-05-14 14:57   ` Theodore Y. Ts'o
2020-04-21  8:54 ` [PATCH 3/3] ext4: Avoid freeing inodes on dirty list Jan Kara
2020-05-14 14:57   ` Theodore Y. Ts'o

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=7ffc64ad-4741-27ca-ba9d-3d23af0a9216@linux.alibaba.com \
    --to=xiaoguang.wang@linux.alibaba.com \
    --cc=jack@suse.cz \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=sandeen@sandeen.net \
    --cc=tytso@mit.edu \
    /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