From patchwork Mon Oct 24 11:27:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 9653 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp534305wru; Mon, 24 Oct 2022 09:10:56 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4Lwg/8GnP1skMcXMfeUJAZBNq7FpNJiBBsYAKISYuCtavwxBqAI/WrAeY8W8k3HkS3Mt+a X-Received: by 2002:a17:906:6a04:b0:78d:ee09:d956 with SMTP id qw4-20020a1709066a0400b0078dee09d956mr27473233ejc.698.1666627855963; Mon, 24 Oct 2022 09:10:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666627855; cv=none; d=google.com; s=arc-20160816; b=jZYCQJMUdH8I3ldiuKDUVV0xw5gMnjpCnKEKkeq1+aes33YCc/vUPg9/P+oWMo9n1i fQBjofH5ptw2JmGbAIsJjqkvwWQtJVIPwaTh4YyM3QbGrgRYPQhDHT5NtEWRGDEU/orz P9earm8iUfeIbQcH+ajAKerG8mI9kxEZ7ISo+zfZWJxlE7BZ9Z4frQ+rIoP7JlqdhPfx rAj6eRtu7LaVbtiOM/WAMZ3CcDYj/5UNOMGMq6QSckv7YkBaFzwcuNdKkbCwDW744NKi s/+fBt/mDd1vIG4171V1pl+VGFfTgfUdZdJydn4RZuiJBTfqK3zXzOvCsETq8AZ3hYez AVdA== 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=OeL3m5AC9YJS5UGyJofO6hamGoivCXP5cRNAJYp/YwU=; b=iAUKoVzEiElPyfqg7cM59aBeub939ywJxJxvgJHtWj/gBEwg9A5atGcSwaX4Pj1wDe MX0kcMDVxuk9KNuwEX8jO9sm6TpDblP6Og7ndsUdyn8v6klfzlDwKH8V2lsWfqq/J6rf 5VZ689Z6DrqcU41zbBWHp629vq1J9g6Z9MX4QJS4sgnftcBY63MtGRBqikiHbmyMfjNZ 6l2yx2Jy/AjdPdRMDh9ke5193gWjmBnu6yKcyU1/UxxsDzSKXfOAAMuQPYJvYrNb5KW/ EQjxLPuf9ua3Fsqd5oSB19iER8C/+Lj6FDTN/yE//mapFaY3q9yxu97/61MBXz857dR+ aqvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=TMLYSoU5; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l7-20020a056402254700b004612915d1a0si153818edb.507.2022.10.24.09.10.32; Mon, 24 Oct 2022 09:10:55 -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=TMLYSoU5; 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 S233492AbiJXQIm (ORCPT + 99 others); Mon, 24 Oct 2022 12:08:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233505AbiJXQEp (ORCPT ); Mon, 24 Oct 2022 12:04:45 -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 F262013F6C; Mon, 24 Oct 2022 07:57:09 -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 D99FBB81630; Mon, 24 Oct 2022 12:18:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3D139C433D6; Mon, 24 Oct 2022 12:18:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666613926; bh=Q39EAbnq4NOHgHPvPqteBhV/UhoTJX8hhLH9VJiuayQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TMLYSoU5j5rc/9ZJmCPKR53VLoi9qWCHlamM24FEXi3QR45Vgk1ARKuvye2OxxUUB Ppt6MEa7zTwijqxfUE8p5zd7TML8xJ5YTnyAeFM56FFOXxwlJ4svJhVsaGCJp65Pjr WyObJzo0z7y8+y8Hu88VWXDw7Gj3kt1dtqYzawRw= 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.10 049/390] btrfs: fix race between quota enable and quota rescan ioctl Date: Mon, 24 Oct 2022 13:27:26 +0200 Message-Id: <20221024113024.720487503@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113022.510008560@linuxfoundation.org> References: <20221024113022.510008560@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.6 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747585970600616006?= X-GMAIL-MSGID: =?utf-8?q?1747585970600616006?= 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 @@ -1158,6 +1158,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: