From patchwork Tue Feb 20 03:38:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chandan Rajendra X-Patchwork-Id: 10229307 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 8EB816038F for ; Tue, 20 Feb 2018 03:37:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7D80127EED for ; Tue, 20 Feb 2018 03:37:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 72019280CF; Tue, 20 Feb 2018 03:37:49 +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 8082927EED for ; Tue, 20 Feb 2018 03:37:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932408AbeBTDhr (ORCPT ); Mon, 19 Feb 2018 22:37:47 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:46936 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932370AbeBTDhq (ORCPT ); Mon, 19 Feb 2018 22:37:46 -0500 Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w1K3aH0K145739 for ; Mon, 19 Feb 2018 22:37:46 -0500 Received: from e18.ny.us.ibm.com (e18.ny.us.ibm.com [129.33.205.208]) by mx0a-001b2d01.pphosted.com with ESMTP id 2g896tvf3h-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 19 Feb 2018 22:37:45 -0500 Received: from localhost by e18.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 19 Feb 2018 22:37:44 -0500 Received: from b01cxnp22035.gho.pok.ibm.com (9.57.198.25) by e18.ny.us.ibm.com (146.89.104.205) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 19 Feb 2018 22:37:41 -0500 Received: from b01ledav001.gho.pok.ibm.com (b01ledav001.gho.pok.ibm.com [9.57.199.106]) by b01cxnp22035.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w1K3benT45809862; Tue, 20 Feb 2018 03:37:40 GMT Received: from b01ledav001.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id E89FE2803E; Mon, 19 Feb 2018 22:37:03 -0500 (EST) Received: from localhost.localdomain.com (unknown [9.199.38.216]) by b01ledav001.gho.pok.ibm.com (Postfix) with ESMTP id E2D412803D; Mon, 19 Feb 2018 22:37:01 -0500 (EST) From: Chandan Rajendra To: linux-xfs@vger.kernel.org Cc: Chandan Rajendra , hch@infradead.org, darrick.wong@oracle.com, bfoster@redhat.com Subject: [PATCH] xfs: Fix "use after free" of intent items Date: Tue, 20 Feb 2018 09:08:44 +0530 X-Mailer: git-send-email 2.9.5 X-TM-AS-GCONF: 00 x-cbid: 18022003-0044-0000-0000-000003E6D55A X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00008563; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000254; SDB=6.00992289; UDB=6.00504105; IPR=6.00771635; MB=3.00019646; MTD=3.00000008; XFM=3.00000015; UTC=2018-02-20 03:37:43 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18022003-0045-0000-0000-00000816D9AF Message-Id: <20180220033844.13999-1-chandan@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2018-02-20_01:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1011 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1802200045 Sender: linux-xfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP generic/388 can cause the following "use after free" error to occur, ============================================================================= BUG xfs_efi_item (Not tainted): Poison overwritten ----------------------------------------------------------------------------- Disabling lock debugging due to kernel taint INFO: 0x00000000292c4bd4-0x00000000292c4bd4. First byte 0x6a instead of 0x6b INFO: Allocated in .kmem_zone_alloc+0xcc/0x190 age=79 cpu=0 pid=12436 .__slab_alloc+0x54/0x80 .kmem_cache_alloc+0x124/0x350 .kmem_zone_alloc+0xcc/0x190 .xfs_efi_init+0x48/0xf0 .xfs_extent_free_create_intent+0x40/0x130 .xfs_defer_intake_work+0x74/0x1e0 .xfs_defer_finish+0xac/0x5c0 .xfs_itruncate_extents+0x170/0x590 .xfs_inactive_truncate+0xcc/0x170 .xfs_inactive+0x1d8/0x2f0 .xfs_fs_destroy_inode+0xe4/0x3d0 .destroy_inode+0x68/0xb0 .do_unlinkat+0x1e8/0x390 system_call+0x58/0x6c INFO: Freed in .xfs_efi_item_free+0x44/0x80 age=79 cpu=0 pid=12436 .kmem_cache_free+0x120/0x2b0 .xfs_efi_item_free+0x44/0x80 .xfs_trans_free_items+0xd4/0x130 .__xfs_trans_commit+0xd0/0x350 .xfs_trans_roll+0x4c/0x90 .xfs_defer_trans_roll+0xa4/0x2b0 .xfs_defer_finish+0xb8/0x5c0 .xfs_itruncate_extents+0x170/0x590 .xfs_inactive_truncate+0xcc/0x170 .xfs_inactive+0x1d8/0x2f0 .xfs_fs_destroy_inode+0xe4/0x3d0 .destroy_inode+0x68/0xb0 .do_unlinkat+0x1e8/0x390 system_call+0x58/0x6c This happens due to the following interaction, 1. xfs_defer_finish() creates "extent free" intent item and adds it to the per-transction list of log items. 2. xfs_defer_trans_roll() invokes __xfs_trans_commit(). Here, if the XFS_MOUNT_FS_SHUTDOWN flag is set, we invoke io_unlock() operation for each of the log items in the per-transction list. For "extent free" log items xfs_efi_item_unlock() gets invoked which then frees the xfs_efi_log_item. 3. xfs_defer_trans_roll() then invokes xfs_defer_trans_abort(). Since the xfs_defer_pending->dfp_intent is still set to the "extent free" intent item, we invoke xfs_extent_free_abort_intent(). This accesses the previously freed xfs_efi_log_item to decrement the ref count. This commit fixes the bug by invoking xfs_defer_trans_abort() only when the log items in the per-transaction list have been committed to the CIL. The log item "committed" status is being tracked by xfs_defer_ops->dop_committed. This was the behaviour prior to commit 3ab78df2a59a485f479d26852a060acfd8c4ecd7 (xfs: rework xfs_bmap_free callers to use xfs_defer_ops). Reported-by: Christoph Hellwig Signed-off-by: Chandan Rajendra --- fs/xfs/libxfs/xfs_defer.c | 6 +++--- fs/xfs/xfs_bmap_util.c | 2 +- fs/xfs/xfs_inode.c | 2 +- fs/xfs/xfs_trans.c | 8 +++++++- fs/xfs/xfs_trans.h | 2 +- fs/xfs/xfs_trans_inode.c | 2 +- 6 files changed, 14 insertions(+), 8 deletions(-) diff --git a/fs/xfs/libxfs/xfs_defer.c b/fs/xfs/libxfs/xfs_defer.c index 087fea0..eb879a0 100644 --- a/fs/xfs/libxfs/xfs_defer.c +++ b/fs/xfs/libxfs/xfs_defer.c @@ -256,13 +256,13 @@ xfs_defer_trans_roll( trace_xfs_defer_trans_roll((*tp)->t_mountp, dop); /* Roll the transaction. */ - error = xfs_trans_roll(tp); + error = xfs_trans_roll(tp, &dop->dop_committed); if (error) { trace_xfs_defer_trans_roll_error((*tp)->t_mountp, dop, error); - xfs_defer_trans_abort(*tp, dop, error); + if (dop->dop_committed == true) + xfs_defer_trans_abort(*tp, dop, error); return error; } - dop->dop_committed = true; /* Rejoin the joined inodes. */ for (i = 0; i < XFS_DEFER_OPS_NR_INODES && dop->dop_inodes[i]; i++) diff --git a/fs/xfs/xfs_bmap_util.c b/fs/xfs/xfs_bmap_util.c index c83f549..9d84d36 100644 --- a/fs/xfs/xfs_bmap_util.c +++ b/fs/xfs/xfs_bmap_util.c @@ -1830,7 +1830,7 @@ xfs_swap_change_owner( if (error != -EAGAIN) break; - error = xfs_trans_roll(tpp); + error = xfs_trans_roll(tpp, NULL); if (error) break; tp = *tpp; diff --git a/fs/xfs/xfs_inode.c b/fs/xfs/xfs_inode.c index 604ee38..6419549 100644 --- a/fs/xfs/xfs_inode.c +++ b/fs/xfs/xfs_inode.c @@ -1049,7 +1049,7 @@ xfs_dir_ialloc( tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY); } - code = xfs_trans_roll(&tp); + code = xfs_trans_roll(&tp, NULL); if (committed != NULL) *committed = 1; diff --git a/fs/xfs/xfs_trans.c b/fs/xfs/xfs_trans.c index 86f92df..1bf2505 100644 --- a/fs/xfs/xfs_trans.c +++ b/fs/xfs/xfs_trans.c @@ -1057,12 +1057,15 @@ xfs_trans_cancel( */ int xfs_trans_roll( - struct xfs_trans **tpp) + struct xfs_trans **tpp, + bool *committed) { struct xfs_trans *trans = *tpp; struct xfs_trans_res tres; int error; + if (committed) + *committed = false; /* * Copy the critical parameters from one trans to the next. */ @@ -1082,6 +1085,9 @@ xfs_trans_roll( if (error) return error; + if (committed) + *committed = true; + /* * Reserve space in the log for the next transaction. * This also pushes items in the "AIL", the list of logged items, diff --git a/fs/xfs/xfs_trans.h b/fs/xfs/xfs_trans.h index 9d542df..d4deb49 100644 --- a/fs/xfs/xfs_trans.h +++ b/fs/xfs/xfs_trans.h @@ -230,7 +230,7 @@ int xfs_trans_free_extent(struct xfs_trans *, struct xfs_efd_log_item *, xfs_fsblock_t, xfs_extlen_t, struct xfs_owner_info *); int xfs_trans_commit(struct xfs_trans *); -int xfs_trans_roll(struct xfs_trans **); +int xfs_trans_roll(struct xfs_trans **, bool *); int xfs_trans_roll_inode(struct xfs_trans **, struct xfs_inode *); void xfs_trans_cancel(xfs_trans_t *); int xfs_trans_ail_init(struct xfs_mount *); diff --git a/fs/xfs/xfs_trans_inode.c b/fs/xfs/xfs_trans_inode.c index 4a89da4..bedd5fd 100644 --- a/fs/xfs/xfs_trans_inode.c +++ b/fs/xfs/xfs_trans_inode.c @@ -147,7 +147,7 @@ xfs_trans_roll_inode( int error; xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE); - error = xfs_trans_roll(tpp); + error = xfs_trans_roll(tpp, NULL); if (!error) xfs_trans_ijoin(*tpp, ip, 0); return error;