Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp3925985imm; Mon, 8 Oct 2018 11:48:06 -0700 (PDT) X-Google-Smtp-Source: ACcGV61Y/pF0hitm+7QsLBxbbC+9Ult3kJ+CWpVIjoGp4OqjltvGENuuYJeANQ75iLQZEtt1ghnv X-Received: by 2002:a17:902:a5ca:: with SMTP id t10-v6mr24775119plq.113.1539024486498; Mon, 08 Oct 2018 11:48:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539024486; cv=none; d=google.com; s=arc-20160816; b=sN6pzt5QegmjCV26X/9AismvC452ShZjipoJrMYXi3AupxKFyCkQutq2BVGSE3Qw8j Snysnd1tJLQOu/Yvz7HtYsInXo/dyiq0wbG0p/rcoGWgR0wqsF/L8yA2tRUaik1GVjyk 69DDrbK6FJWZkXdoONI6j4M2oc2Y0lfqTK/VqeGeYuVecNvndok3FeEHOA+4r+Efxhvb wCImbXOkVAvBXvHDFHOL4U5szp02zNFlcaWSbcAGJs2JLiG1B5dVuhJBNQEK8OVMNEFj Vq0OYFuqAPbvuN9wn+u8lo6GlBnfsEbHLZb37EUfd06engktwxOLOQreMFneid2t3sdn pFtw== 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=aYklpNGovMLi43pusQNkOoKCTk5vltcdZYtwNX+MMtU=; b=sBhFG9EfL3SCdkr0FCs3xFA56PzxYYK3COvb3VPJVqLR+An6fRSFLDF9DY+KogWt/1 T6gTgm30gfPRojZTsjJlFi7PSZDItEZ6OAMW/A6+q63cAh6XHYFQksccjf/XpDrkK4T2 btDwIuoSAqoeJvhipNPp0Uax4tXndQaThknu7JmNMnPjdI3JPWy9r3yr55HJSnJ3gyRJ IER+MuTtAg/y4zyun6q2q1Uuc+KM1U9PT7o6UzNhZRlJoy2jDPry0G3ScdMwzugrn3Jm llXoPplC2RyzkiHJoM7PgBY1psK2xbN28svpmYMp2Zs3+OaezKW0AcejE1Z8QJG5h5Nk qsFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=FlGf2mDk; 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 g24-v6si5079637pgh.188.2018.10.08.11.47.51; Mon, 08 Oct 2018 11:48:06 -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=FlGf2mDk; 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 S1731428AbeJICAb (ORCPT + 99 others); Mon, 8 Oct 2018 22:00:31 -0400 Received: from mail.kernel.org ([198.145.29.99]:49246 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730798AbeJICAb (ORCPT ); Mon, 8 Oct 2018 22:00:31 -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 86D1221479; Mon, 8 Oct 2018 18:47:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1539024445; bh=zIPigG2jmZhiZ714rjhrue2IRWSCPsJsKbdjB+XO9Zs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FlGf2mDknV9LgUmwOD84/F37RW6pUk99qskHCC9THSplBQsD8+iI4NgvUpN1jtThi aDaKh8346aJvxpRSHVF6kqNyHXvlF8N3wJETiyqgzP5XO4P42KFiSWNBhzsGlap6TU yG3MrBGDZ7ZrIafK38+f4BgzEyHrDIIzZL3fONqY= 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.18 007/168] btrfs: btrfs_shrink_device should call commit transaction at the end Date: Mon, 8 Oct 2018 20:29:47 +0200 Message-Id: <20181008175620.320937883@linuxfoundation.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20181008175620.043587728@linuxfoundation.org> References: <20181008175620.043587728@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.18-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 @@ -4584,7 +4584,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) {