Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp46309pxb; Wed, 14 Apr 2021 09:06:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy2rUs8RsHAl+l1JMvthq0csfgHv4qmPyjWESkgmoWYpC/66u4CoX0KmsXqmZUvVDS8ZUQ0 X-Received: by 2002:a17:907:94cc:: with SMTP id dn12mr38376527ejc.177.1618416360470; Wed, 14 Apr 2021 09:06:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618416360; cv=none; d=google.com; s=arc-20160816; b=hC2sFfKD/UnyylI7QnIvECcOJUN9sGFUa19gNbW+v6VrgwOIubwg8YqcJm8h7mY1Zm NlQlXgeA31xHZ/J7iSAmhl3HpuC03kIKFXk627ol5JLNu7/BqnARj3ZmhcgO4mqZ0Kc6 1fcKozcUgBqRyjJg/2M2hhmVcdfiiZ7LE5hhOZI1pchTyXHTsF1XslLmDpr1MiM4lnNZ 2eDA8fadF4SQr8KsHzdnd4fn+iQimt74sI5r51ams8Zp1kOOnl6OAYJQT+G0UBJIe1WO dtBuiKg5QRoHLefBYywNMue/p2xAl2lAf5HI6EHgyxhrAwPyrNMYXBfVwnlBX3UOu6oF zWWw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=smtpk3XDtx68Bq1fxY2YqVZmgh/ksKPGEvKuGgciUz0=; b=BaEdrexfdNm5ObPSWuexH0BeZWCFJnCQG90oZyuxN2Q2N5F3w0EIgVd1pjB2z5guqF RGclifFetbQWgntOFjwWCQlMAmSguqUGbHaCjcUO18hUHuuq4GOJyvNQpGu7YyDWAjjR g//os0gfN5K+V8prtKDXYNrrWm2HVdvEWZZ418uHMn3wgviKBuPRD5G7mYaX2pD8xPLi r8ZUn0rgCFKrluemyBwjS+/JtOx77opkojb0Bf4rq8aRuBp34118SKmLW46d5vLsSaNr z3bCloVWDW2pan2voac6EGLX+24kbb36hO3AWXvvP2CDLQaxMM0fTFHp+J03zn9cSkyO 7LpA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q26si12629086ejt.650.2021.04.14.09.05.36; Wed, 14 Apr 2021 09:06:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351440AbhDNNkG (ORCPT + 99 others); Wed, 14 Apr 2021 09:40:06 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:16919 "EHLO szxga06-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351422AbhDNNkD (ORCPT ); Wed, 14 Apr 2021 09:40:03 -0400 Received: from DGGEMS411-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga06-in.huawei.com (SkyGuard) with ESMTP id 4FL3SV0hfJzjZSG; Wed, 14 Apr 2021 21:37:46 +0800 (CST) Received: from huawei.com (10.175.127.227) by DGGEMS411-HUB.china.huawei.com (10.3.19.211) with Microsoft SMTP Server id 14.3.498.0; Wed, 14 Apr 2021 21:39:29 +0800 From: Zhang Yi To: CC: , , , , , Subject: [RFC PATCH v2 5/7] ext4: use RCU to protect accessing superblock in blkdev_releasepage() Date: Wed, 14 Apr 2021 21:47:35 +0800 Message-ID: <20210414134737.2366971-6-yi.zhang@huawei.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20210414134737.2366971-1-yi.zhang@huawei.com> References: <20210414134737.2366971-1-yi.zhang@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.127.227] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org In blkdev_releasepage() we access the superblock structure directly, it could be raced by umount filesystem on destroy superblock in put_super(), and end up triggering a use after free issue. drop cache umount filesystem bdev_try_to_free_page() get superblock deactivate_locked_super() ... put_super() and free sb by destroy_work access superblock <-- trigger use after free This issue doesn't trigger easily in general because we get page locked when invoking bdev_try_to_free_page(), and when umount filesystem the kill_block_super()->..->kill_bdev()->truncate_inode_pages_range() procedure wait on page unlock, but it's not a guarantee. Fix this race by use RCU to protect superblock in blkdev_releasepage(). Fixes: 87d8fe1ee6b8 ("add releasepage hooks to block devices which can be used by file systems") Reported-by: Jan Kara Signed-off-by: Zhang Yi --- fs/block_dev.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/fs/block_dev.c b/fs/block_dev.c index 5ed79a9063f6..cb84f347fb04 100644 --- a/fs/block_dev.c +++ b/fs/block_dev.c @@ -1734,11 +1734,14 @@ EXPORT_SYMBOL_GPL(blkdev_read_iter); */ static int blkdev_releasepage(struct page *page, gfp_t wait) { - struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super; + struct super_block *super; int ret = 0; + rcu_read_lock(); + super = READ_ONCE(BDEV_I(page->mapping->host)->bdev.bd_super); if (super && super->s_op->bdev_try_to_free_page) ret = super->s_op->bdev_try_to_free_page(super, page, wait); + rcu_read_unlock(); if (!ret) return try_to_free_buffers(page); return 0; -- 2.25.4