Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp3920466imm; Mon, 8 Oct 2018 11:42:29 -0700 (PDT) X-Google-Smtp-Source: ACcGV62jpRYwUIjUnJ17KjMTeDssbW0p074XY/pw07vPfuR5NLTVX9vnvbvi+gd8I2S3Ob4caRmB X-Received: by 2002:a63:7f0e:: with SMTP id a14-v6mr22330592pgd.296.1539024149526; Mon, 08 Oct 2018 11:42:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539024149; cv=none; d=google.com; s=arc-20160816; b=dt+p1SY/m7dYOGiKsLhVt1ERkxMJoFd2LLEqC4zuxsYxcJ/zHp0AKK6DxfoNm9vUrA PInhr6eb9HGDmCZJ+Q/FPVsAH9cBbUEss18fEqyi4gE/+Out1UloCLrhHyvmxaR3Nync 9SDKC4taApBjUCnniCReRjbqZmqBLHrjGT/Ws6Olju+gqEPbmleZpVrv7585tWd0q1DB plLHBhN1iON2Hp6oWoulLxbtuFiyK/3IHWrSIR4V478bU0VNUdNzQ7YIis7qHQH389hb tw/PfWNPCXNI1W+osqwKdwIdGZiLr7DQ6RjjSMauNlWTZs34NxR3S56jHXmuMQniho39 qdGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ro9H3EhNMqrz4z62jNKoFXQSZ3O6yajQVtmgzLCGnIE=; b=y/iTFjnCK2mc8W2augNaGgApbn7NCqL5XlQs5QGaZcETJA1ws6IAFrHznlzDre/BRx w2F6CA96i0swnk6htkrV0JO9Uk59EpvvTwrGDTYmFShA9vtPrr+kswroHH4qv/B6JZMO R6kk8tGxhIkLhre1EfXXAv8KZq3atLleC9zADz9hDm+7EzxAERXrf1Jtgl6Vm2Wx7bKc ui7cycX+BLWH/juBwz2pe3nml+t00OvXd+BBgGHNMm/Gb09i7Tou9+0wiFsN6B/T+8du Skd6C3e64aKPkyQtaNxYwoNZCZagOqIHGsp1v5nZB/psq4e3kLyyMeOROjyRUf277m13 kQqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=aR0A5RE4; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w6-v6si18150591pgb.180.2018.10.08.11.42.14; Mon, 08 Oct 2018 11:42:29 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=aR0A5RE4; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730179AbeJIBzO (ORCPT + 99 others); Mon, 8 Oct 2018 21:55:14 -0400 Received: from mail.kernel.org ([198.145.29.99]:42794 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730155AbeJIBzO (ORCPT ); Mon, 8 Oct 2018 21:55:14 -0400 Received: from localhost (ip-213-127-77-176.ip.prioritytelecom.net [213.127.77.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E1DBD21479; Mon, 8 Oct 2018 18:42:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1539024129; bh=tQyXYkY79xFbx/iCTjMmmSWNdubQY0llE5xEAZ/MCpc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aR0A5RE47DFBWOBk2YERyfZJPb+TIw7nNirvOlOaBo0YeU5wZhEmQk41S6eVnebeY NwDEqUMGdc534OKZIkzZa9Cj6peT0Am3LpdttOjZ4PQ/pZmH9DillnJVy+yU+dbibl K7bB2JE8gywgtSYGq+TV0bNQy95/OmigkxWWRjlA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lu Fengqi , Anand Jain , David Sterba , Sasha Levin Subject: [PATCH 4.14 08/94] btrfs: btrfs_shrink_device should call commit transaction at the end Date: Mon, 8 Oct 2018 20:30:49 +0200 Message-Id: <20181008175605.456547473@linuxfoundation.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20181008175605.067676667@linuxfoundation.org> References: <20181008175605.067676667@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Anand Jain [ Upstream commit 801660b040d132f67fac6a95910ad307c5929b49 ] Test case btrfs/164 reports use-after-free: [ 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 resized device 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 transactio ncommit. It goes through the fs_devices::resized_devices in btrfs_update_commit_device_size and leads to use-after-free. Fix this by making sure btrfs_shrink_device calls the last needed btrfs_commit_transaction before the return. This is consistent with what the grow counterpart does and this makes sure the on-disk state is persistent when the function returns. Reported-by: Lu Fengqi Tested-by: Lu Fengqi Signed-off-by: Anand Jain Reviewed-by: David Sterba [ update changelog ] Signed-off-by: David Sterba Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/volumes.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -4517,7 +4517,12 @@ again: /* 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) {