linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Wei Fang <fangwei1@huawei.com>
To: viro@ZenIV.linux.org.uk, akpm@linux-foundation.org
Cc: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org,
	Wei Fang <fangwei1@huawei.com>,
	stable@vger.kernel.org
Subject: [RFC][PATCH] vfs,mm: fix a dead loop in truncate_inode_pages_range()
Date: Thu, 29 Sep 2016 20:10:10 +0800	[thread overview]
Message-ID: <1475151010-40166-1-git-send-email-fangwei1@huawei.com> (raw)

We triggered a deadloop in truncate_inode_pages_range() on 32 bits
architecture with the test case bellow:
	...
	fd = open();
	write(fd, buf, 4096);
	preadv64(fd, &iovec, 1, 0xffffffff000);
	ftruncate(fd, 0);
	...
Then ftruncate() will not return forever.

The filesystem used in this case is ubifs, but it can be triggered
on many other filesystems.

When preadv64() is called with offset=0xffffffff000, a page with
index=0xffffffff will be added to the radix tree of ->mapping.
Then this page can be found in ->mapping with pagevec_lookup().
After that, truncate_inode_pages_range(), which is called in
ftruncate(), will fall into an infinite loop:
* find a page with index=0xffffffff, since index>=end, this page
  won't be truncated
* index++, and index become 0
* the page with index=0xffffffff will be found again

The data type of index is unsigned long, so index won't overflow to
0 on 64 bits architecture in this case, and the dead loop won't
happen.

Since truncate_inode_pages_range() is executed with holding lock
of inode->i_rwsem, any operation related with this lock will be
blocked, and a hung task will happen, e.g.:

INFO: task truncate_test:3364 blocked for more than 120 seconds.
...
[<c03c2c44>] call_rwsem_down_write_failed+0x17/0x30
[<c00b93bc>] generic_file_write_iter+0x32/0x1c0
[<c01b7078>] ubifs_write_iter+0xcc/0x170
[<c00fae48>] __vfs_write+0xc4/0x120
[<c00fb784>] vfs_write+0xb2/0x1b0
[<c00fbbe4>] SyS_write+0x46/0xa0

The page with index=0xffffffff added to ->mapping is useless.
Fix this by checking the read position before allocating pages.

Cc: stable@vger.kernel.org
Signed-off-by: Wei Fang <fangwei1@huawei.com>
---
 mm/filemap.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/mm/filemap.c b/mm/filemap.c
index 1345f09..6946346 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -1674,6 +1674,10 @@ static ssize_t do_generic_file_read(struct file *filp, loff_t *ppos,
 	unsigned int prev_offset;
 	int error = 0;
 
+	if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
+		return -EINVAL;
+	iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
+
 	index = *ppos >> PAGE_SHIFT;
 	prev_index = ra->prev_pos >> PAGE_SHIFT;
 	prev_offset = ra->prev_pos & (PAGE_SIZE-1);
-- 
1.9.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

             reply	other threads:[~2016-09-29 12:08 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-29 12:10 Wei Fang [this message]
2016-09-29 13:43 ` [RFC][PATCH] vfs,mm: fix a dead loop in truncate_inode_pages_range() Christoph Hellwig
2016-09-30  0:54   ` Wei Fang
2016-09-30  1:02   ` Dave Chinner

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=1475151010-40166-1-git-send-email-fangwei1@huawei.com \
    --to=fangwei1@huawei.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=stable@vger.kernel.org \
    --cc=viro@ZenIV.linux.org.uk \
    /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).