From patchwork Mon Aug 6 10:12:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anand Jain X-Patchwork-Id: 10556781 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8B19E1390 for ; Mon, 6 Aug 2018 10:09:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 75C6D292B0 for ; Mon, 6 Aug 2018 10:09:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6907929026; Mon, 6 Aug 2018 10:09:12 +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=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI,T_DKIM_INVALID,UNPARSEABLE_RELAY 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 5FD7C29026 for ; Mon, 6 Aug 2018 10:09:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728686AbeHFMR2 (ORCPT ); Mon, 6 Aug 2018 08:17:28 -0400 Received: from userp2120.oracle.com ([156.151.31.85]:55470 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727700AbeHFMR2 (ORCPT ); Mon, 6 Aug 2018 08:17:28 -0400 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w76A8niI024537 for ; Mon, 6 Aug 2018 10:09:06 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : subject : date : message-id : in-reply-to : references; s=corp-2018-07-02; bh=GEOtOHFSI50YEiDuDuHTgvTDCLnhUij4VU3M9sszWvY=; b=JvkvQaYUhHi+wTJguYemO+N5apU2TOSnzMq/W3Eoo6Gv5WF5YTSOLeEWl6AT5/OH2uPP S0WD9EihF7jpQa2/IKAiOYxIu83iikL7I2NuRZC/YBjrrdoJPPZCXdrWfh4oDJRdy2Fv W/pv5b5eUvvsV09xrdjS03+Sog9A8tWcdXEpaUX1Md6AOkZa2O9uPYx+nXPnum3QJsaE KnxJFLSwc27QT75oymegwCFQgpuM5l/bqaNs8AHOe1wt9w3UAhW1v1xeLTj/yht/mECQ DVntXe2ne7UN44nBq3jA60rFIU9qALoas9mQteQtRwiP6d1I33CUjjPN0HXbchTzF3Uh Lg== Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by userp2120.oracle.com with ESMTP id 2kn4spm39s-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Mon, 06 Aug 2018 10:09:06 +0000 Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by aserv0022.oracle.com (8.14.4/8.14.4) with ESMTP id w76A95Ga028746 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Mon, 6 Aug 2018 10:09:05 GMT Received: from abhmp0007.oracle.com (abhmp0007.oracle.com [141.146.116.13]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id w76A94lB006227 for ; Mon, 6 Aug 2018 10:09:05 GMT Received: from localhost.localdomain (/183.90.37.97) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 06 Aug 2018 03:09:04 -0700 From: Anand Jain To: linux-btrfs@vger.kernel.org Subject: [PATCH v2] btrfs: btrfs_shrink_device should call commit transaction Date: Mon, 6 Aug 2018 18:12:37 +0800 Message-Id: <20180806101237.20507-1-anand.jain@oracle.com> X-Mailer: git-send-email 2.15.0 In-Reply-To: <20180725132727.642-1-anand.jain@oracle.com> References: <20180725132727.642-1-anand.jain@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8976 signatures=668707 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=3 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1807170000 definitions=main-1808060110 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP test case btrfs/164 reported UAF.. [ 6712.084324] general protection fault: 0000 [#1] PREEMPT SMP :: [ 6712.195423] btrfs_update_commit_device_size+0x75/0xf0 [btrfs] [ 6712.201424] btrfs_commit_transaction+0x57d/0xa90 [btrfs] [ 6712.206999] btrfs_rm_device+0x627/0x850 [btrfs] [ 6712.211800] btrfs_ioctl+0x2b03/0x3120 [btrfs] :: reason for this is that btrfs_shrink_device() adds the device resized to the fs_devices::resized_devices after it has called the last commit transaction. So the list fs_devices::resized_devices is not empty when btrfs_shrink_device() returns. Now the parent function btrfs_rm_device() calls btrfs_close_bdev(device); call_rcu(&device->rcu, free_device_rcu); and then does the commit transaction. The commit transaction goes through the fs_devices::resized_devices in btrfs_update_commit_device_size() and leads to UAF. Fix this by making sure btrfs_shrink_device() calls the last needed btrfs_commit_transaction() before the return. Reported-by: Lu Fengqi Signed-off-by: Anand Jain Tested-by: Lu Fengqi Reviewed-by: David Sterba --- v1->v2: cleanup the dirty and new meta block group before ending the transaction when btrfs_update_device fails. fs/btrfs/volumes.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index da86706123ff..f4405e430da6 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -4491,7 +4491,12 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) /* Now btrfs_update_device() will change the on-disk size. */ ret = btrfs_update_device(trans, device); - btrfs_end_transaction(trans); + if (ret < 0) { + btrfs_abort_transaction(trans, ret); + btrfs_end_transaction(trans); + } else { + ret = btrfs_commit_transaction(trans); + } done: btrfs_free_path(path); if (ret) {