From patchwork Thu Sep 29 12:10:10 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: fangwei X-Patchwork-Id: 9356393 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 046F46077A for ; Thu, 29 Sep 2016 12:08:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E1466299A7 for ; Thu, 29 Sep 2016 12:08:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D5E9B299AB; Thu, 29 Sep 2016 12:08:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B39E2299AA for ; Thu, 29 Sep 2016 12:08:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753332AbcI2MIT (ORCPT ); Thu, 29 Sep 2016 08:08:19 -0400 Received: from szxga01-in.huawei.com ([58.251.152.64]:34841 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752818AbcI2MIT (ORCPT ); Thu, 29 Sep 2016 08:08:19 -0400 Received: from 172.24.1.137 (EHLO SZXEML424-HUB.china.huawei.com) ([172.24.1.137]) by szxrg01-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id DRU33971; Thu, 29 Sep 2016 20:08:10 +0800 (CST) Received: from 138.huawei.com (10.175.124.28) by SZXEML424-HUB.china.huawei.com (10.82.67.153) with Microsoft SMTP Server (TLS) id 14.3.235.1; Thu, 29 Sep 2016 20:08:00 +0800 From: Wei Fang To: , CC: , , Wei Fang , Subject: [RFC][PATCH] vfs, mm: fix a dead loop in truncate_inode_pages_range() Date: Thu, 29 Sep 2016 20:10:10 +0800 Message-ID: <1475151010-40166-1-git-send-email-fangwei1@huawei.com> X-Mailer: git-send-email 1.9.3 MIME-Version: 1.0 X-Originating-IP: [10.175.124.28] X-CFilter-Loop: Reflected Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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. ... [] call_rwsem_down_write_failed+0x17/0x30 [] generic_file_write_iter+0x32/0x1c0 [] ubifs_write_iter+0xcc/0x170 [] __vfs_write+0xc4/0x120 [] vfs_write+0xb2/0x1b0 [] 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 --- 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);