Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp8127206imu; Tue, 4 Dec 2018 03:28:45 -0800 (PST) X-Google-Smtp-Source: AFSGD/U7lv7q6vCM3DjMAnQ2AlrLT5z5S8IW9FNLpkPpSp0Qw/80M658gFNosC5+KkMIIL+QozOk X-Received: by 2002:a62:f247:: with SMTP id y7mr19731313pfl.25.1543922925033; Tue, 04 Dec 2018 03:28:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543922925; cv=none; d=google.com; s=arc-20160816; b=oOfOBQieq/2bqo4mIvA+84gmT3rRFe5JVySP/YL2uTvAdGiQc4rX2zhcctXnmUYxLv WdULJYVzvhiztxI5Mfy1sQwMv9poOJZmMFKxyoKpAktxEaV+5hUKLhFKYp4lkd995m55 EILKI6+STJU8Va/WVRcwwA2sW6Ttnhtcp2Ii/NmCkUIEKOh9Lww8afJHz3XV3LfAHLHP RrBxBOux97F+R/0UYFFMgOn3bYl/CjMIS3Y8LYfH/qH1hfohrKG/lrTBGt5FKo8GLx8I y80yxl/V1Xc7V/mwkPqsd5j3BVWeq6qpXJDDTg7jZV11m00FxbMxCmcYuNF8fbVv8x3j Og2g== 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=rngHqVXuDtjbboWmJto9ZqJurfdIFroNinWSHfFyesc=; b=IfFxWpR8P+EH7Pdh+gibPxXlZuPfHoVtjdqM59SgNn/wi+4opqMJE+NaEdbyuiTsHO aOtv5EtmdrR52DcUtcrPa/GMQjU3i8mQhUGwer26o5Q8DFvL/Xcar+dZN70iw/4hWyqG WxntVeP0ZLYlxgtZXe3CAdp+TFgPruOZqnrfJc/PXElo4bYEFiXF/W5/Y2v4B2ZmbY8N tOSNGHz1kUzbatJUxzj5JBwBPWhj0s7Ov9qS4z232VGcNPwa49NMJbMi71KlWdSKax6B ID8IIcJrLvO2BQiPX4q3ovOVfK8bYHynzPAL1oh6NXNCZ9Jb13YR/yWqbL5ZG3kOScev Sp1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mLohTg0O; 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 b15si17463421plm.431.2018.12.04.03.28.30; Tue, 04 Dec 2018 03:28:45 -0800 (PST) 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=mLohTg0O; 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 S1727074AbeLDL0r (ORCPT + 99 others); Tue, 4 Dec 2018 06:26:47 -0500 Received: from mail.kernel.org ([198.145.29.99]:44688 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727166AbeLDK7n (ORCPT ); Tue, 4 Dec 2018 05:59:43 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (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 691012087F; Tue, 4 Dec 2018 10:59:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1543921181; bh=seDYJtr5bJpUEzg7YnGW6ML+9BhaqsNVTJJDjvRTs/4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mLohTg0OqFUiAlxg7bpB5lNO4d+7v53Pu/SAoBI2s/S374Ir0szbu7jZFO/e8Alff VnPGDwV5LNZW7LiZZHUPOPSOuAStCTAvBuCf/E/4fAxJJj20b+4/RZNWjSTiSYsvCC 96riwnDKXGbCIW8jR3lKM+d7xrv7naeRFSTao81g= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Qu Wenruo , Filipe Manana , David Sterba Subject: [PATCH 4.19 077/139] Btrfs: fix race between enabling quotas and subvolume creation Date: Tue, 4 Dec 2018 11:49:18 +0100 Message-Id: <20181204103653.312216806@linuxfoundation.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181204103649.950154335@linuxfoundation.org> References: <20181204103649.950154335@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Filipe Manana commit 552f0329c75b3e1d7f9bb8c9e421d37403f192cd upstream. We have a race between enabling quotas end subvolume creation that cause subvolume creation to fail with -EINVAL, and the following diagram shows how it happens: CPU 0 CPU 1 btrfs_ioctl() btrfs_ioctl_quota_ctl() btrfs_quota_enable() mutex_lock(fs_info->qgroup_ioctl_lock) btrfs_ioctl() create_subvol() btrfs_qgroup_inherit() -> save fs_info->quota_root into quota_root -> stores a NULL value -> tries to lock the mutex qgroup_ioctl_lock -> blocks waiting for the task at CPU0 -> sets BTRFS_FS_QUOTA_ENABLED in fs_info -> sets quota_root in fs_info->quota_root (non-NULL value) mutex_unlock(fs_info->qgroup_ioctl_lock) -> checks quota enabled flag is set -> returns -EINVAL because fs_info->quota_root was NULL before it acquired the mutex qgroup_ioctl_lock -> ioctl returns -EINVAL Returning -EINVAL to user space will be confusing if all the arguments passed to the subvolume creation ioctl were valid. Fix it by grabbing the value from fs_info->quota_root after acquiring the mutex. CC: stable@vger.kernel.org # 4.4+ Reviewed-by: Qu Wenruo Signed-off-by: Filipe Manana Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/qgroup.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) --- a/fs/btrfs/qgroup.c +++ b/fs/btrfs/qgroup.c @@ -2244,7 +2244,7 @@ int btrfs_qgroup_inherit(struct btrfs_tr int i; u64 *i_qgroups; struct btrfs_fs_info *fs_info = trans->fs_info; - struct btrfs_root *quota_root = fs_info->quota_root; + struct btrfs_root *quota_root; struct btrfs_qgroup *srcgroup; struct btrfs_qgroup *dstgroup; u32 level_size = 0; @@ -2254,6 +2254,7 @@ int btrfs_qgroup_inherit(struct btrfs_tr if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) goto out; + quota_root = fs_info->quota_root; if (!quota_root) { ret = -EINVAL; goto out;