Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp3924344rwi; Sat, 22 Oct 2022 02:18:20 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7o47FqQKIJtsFdFpabZ+hS9M/YDrTZvaA+KqF+DSba/IZKFV/ROguoTeF9AsiE6yul7G4Y X-Received: by 2002:aa7:87d0:0:b0:563:2c7e:752 with SMTP id i16-20020aa787d0000000b005632c7e0752mr23285530pfo.71.1666430299834; Sat, 22 Oct 2022 02:18:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666430299; cv=none; d=google.com; s=arc-20160816; b=A1EgRrpO6PVBVjlal+voCaTvQEtfriGtGNpUNEDAaRlKXVTt+QwlvW/Udr/oNlGdUY gm4dcTw9LdYHH+zrb+0QM7LLuHDNit7S0kQDREarCFkxNCeJcevi//GPw33HB0k+zolf CTn6CIJQLJrWJzURAHr1wQQl3rHbvifkKwsDHsEd2aTjAKBc20fOLU1PDf7ophj7ysb0 i07Z9hybV6BPAeSgIK//agpYDAvo4MseJu0GCA8yh+1j4ZZzmznRoOGVocVQcOHQkPWb tFU2o4pGZ1dhqYTrxct5LkJGpFcVSUx9nEaN34IJpnFaajO36aemjFBx3x0jgFoojpEp W+VA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=bo/eZhqN7ddbxQAWAgRCEo+12zdQ1aGVpJ+QWM5IQu0=; b=DTijTO8T8XhCZX1hCeCiCo4fPbDB4yjRYZsjZTe7wq6tmLyB/39VT3F9bY++NEYcgK FML50BnXfDO1JU+dU3XOT7sSn8FsmrWOpw0/mRHt8MfGnVtGDET+FxyszHIJbZLGw1PK ANwFXfFk8rD2OrUGFxAORiIGKGTKK+Zk1qN/9GzxdYNNluZO8w5WiQ+4eEu4C2hBscQ3 n5O9hhRdZ0HnIuetd+ZRkF82OjtdWZmboDyCoTpAlZ3JfAoeUNREZ/7KR6xDcNUR/DgN Ei9r1A9JiHQeFYoMvp49eratyv4Cd6430kGAOPr3Pv02ammZGPeH3IrXpsiJzvPez3hS PEBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uh1K5F2A; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l17-20020a656811000000b0045a2d89c9cfsi28301602pgt.348.2022.10.22.02.18.07; Sat, 22 Oct 2022 02:18:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uh1K5F2A; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232374AbiJVH6b (ORCPT + 99 others); Sat, 22 Oct 2022 03:58:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55592 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232087AbiJVHyA (ORCPT ); Sat, 22 Oct 2022 03:54:00 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C947B2CA7E2; Sat, 22 Oct 2022 00:46:57 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id B9614B82DFE; Sat, 22 Oct 2022 07:38:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 155CAC433D6; Sat, 22 Oct 2022 07:38:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666424337; bh=J3hY5Ke9dVOuH+qLSGit5n24qZH/9BcKbPIcxeJC+gA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uh1K5F2AcHc6tGdq4BwVzNoEGLan4/HDbGIO7A6hjqPGDoUuJOAgbTrPht+uaUNxK fEG8rj1j1se0B5Bt3J8Cpc9xMbTyc2dWJVrmq4XWc4dGzuSXXA3j8pRH0N5Rbqo8OR dPjsEPzA60nBsWIdlxdU0ZRX8BnqJuH/9plNEQQw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ye Bin , Qu Wenruo , Filipe Manana , David Sterba Subject: [PATCH 5.19 109/717] btrfs: fix race between quota enable and quota rescan ioctl Date: Sat, 22 Oct 2022 09:19:48 +0200 Message-Id: <20221022072434.704677757@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221022072415.034382448@linuxfoundation.org> References: <20221022072415.034382448@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Filipe Manana commit 331cd9461412e103d07595a10289de90004ac890 upstream. When enabling quotas, at btrfs_quota_enable(), after committing the transaction, we change fs_info->quota_root to point to the quota root we created and set BTRFS_FS_QUOTA_ENABLED at fs_info->flags. Then we try to start the qgroup rescan worker, first by initializing it with a call to qgroup_rescan_init() - however if that fails we end up freeing the quota root but we leave fs_info->quota_root still pointing to it, this can later result in a use-after-free somewhere else. We have previously set the flags BTRFS_FS_QUOTA_ENABLED and BTRFS_QGROUP_STATUS_FLAG_ON, so we can only fail with -EINPROGRESS at btrfs_quota_enable(), which is possible if someone already called the quota rescan ioctl, and therefore started the rescan worker. So fix this by ignoring an -EINPROGRESS and asserting we can't get any other error. Reported-by: Ye Bin Link: https://lore.kernel.org/linux-btrfs/20220823015931.421355-1-yebin10@huawei.com/ CC: stable@vger.kernel.org # 4.19+ Reviewed-by: Qu Wenruo Signed-off-by: Filipe Manana Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/qgroup.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) --- a/fs/btrfs/qgroup.c +++ b/fs/btrfs/qgroup.c @@ -1174,6 +1174,21 @@ out_add_root: fs_info->qgroup_rescan_running = true; btrfs_queue_work(fs_info->qgroup_rescan_workers, &fs_info->qgroup_rescan_work); + } else { + /* + * We have set both BTRFS_FS_QUOTA_ENABLED and + * BTRFS_QGROUP_STATUS_FLAG_ON, so we can only fail with + * -EINPROGRESS. That can happen because someone started the + * rescan worker by calling quota rescan ioctl before we + * attempted to initialize the rescan worker. Failure due to + * quotas disabled in the meanwhile is not possible, because + * we are holding a write lock on fs_info->subvol_sem, which + * is also acquired when disabling quotas. + * Ignore such error, and any other error would need to undo + * everything we did in the transaction we just committed. + */ + ASSERT(ret == -EINPROGRESS); + ret = 0; } out_free_path: