Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2510572pxj; Mon, 10 May 2021 04:52:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwvhsfupeMFosKVFUTaVK/uyzRf5f2UqM5q1pGnTjtwj2rmv/uD+vdH7zt//M7LyH/XmIsL X-Received: by 2002:a17:906:f119:: with SMTP id gv25mr26033300ejb.87.1620647463153; Mon, 10 May 2021 04:51:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620647463; cv=none; d=google.com; s=arc-20160816; b=SKxlfCRxwdIQmiJwHCeIs1YYhnmX4G6aJB16/Bg6gWWBadwtuRdyBs0JmgNIAdTZXg BKIQHWqcfkKuRgzAjuFpT2Jfvsl1aSYQgm74DYpVWsOSqbAXxqAAfa62tEQ/4N9HMA3Z 53/cxHFs9zsAgjsjknpSs33xCVyTXsLHlblIo01Q4jiPDlfgMXsTP6ENsBcpDB45gw6e bTK52v2vjk+JwZVn79/GXg6U8uxjh+TmQEEMa58Empc34Yce9RzWka11vYVH5CheAjui ZWUsWfrIHcAggMw32JQbtZjZINsOmWJ4IM77evzm0e9jMD+hGzCpOX1zgMybjqj2Y1Wy XqJg== 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=QsqL45fABpbf2bodv5vCMV3mdaTOzfLhWsbPK7bCgBo=; b=NzS30BhZ+LWkPb46LFTpXETjc+Sd0UzszOeedOO4VVt4h5UHylscmaJSM0kBimkJVH 1j1uCpveRFBqzVdp9tVzV9PjDQ2KL1mGo16mvXIi5MZ9psn/ehtqCy9NpudEZ4Midrgj o+5bPhX3uCmKWimLvy2XnChG3lfNKd9IHY7o85ZDC5O1XdarUqdgT8BHOLuuAl8G3xB4 Ei34my1myIWv/0ZBxyO2b9tOS5ehsIdeEh7T7pF4u5ptBksVxzajsIz34adfFQCZNyjk qQmfSeFaAMPIx6nzR8bH3VsTN+uWFwLPks6hvKTHvFxQcR9Hjix3CvZmkimqvjhw0/U2 EuIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="v/Ga/xVm"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ce10si1985169edb.39.2021.05.10.04.50.37; Mon, 10 May 2021 04:51:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="v/Ga/xVm"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S233235AbhEJLjI (ORCPT + 99 others); Mon, 10 May 2021 07:39:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:46508 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234603AbhEJK4l (ORCPT ); Mon, 10 May 2021 06:56:41 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id B117E61458; Mon, 10 May 2021 10:47:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620643650; bh=HjluC2N3I32cUFD4y6+oqHofg8Wl0e8qOQOIPhatPTo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=v/Ga/xVmhZZkKuR5ZvLVcVPaRTFOEr0Rx/dWD/BXbZzG3/gXAsAD7KoMVLLovQw2B 0Y/xtSpzsha/qGgH174sCETmGcz7Mct0fSlaLTyyqjRZ07MXcUF4by6gZAiRVXXxuD /vckZo2wRAkhQVRMpPzSen3jThlahubDMHhmp990= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Josef Bacik , Qu Wenruo , David Sterba Subject: [PATCH 5.11 055/342] btrfs: handle remount to no compress during compression Date: Mon, 10 May 2021 12:17:25 +0200 Message-Id: <20210510102011.941048658@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210510102010.096403571@linuxfoundation.org> References: <20210510102010.096403571@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Qu Wenruo commit 1d8ba9e7e785b6625f4d8e978e8a284b144a7077 upstream. [BUG] When running btrfs/071 with inode_need_compress() removed from compress_file_range(), we got the following crash: BUG: kernel NULL pointer dereference, address: 0000000000000018 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page Workqueue: btrfs-delalloc btrfs_work_helper [btrfs] RIP: 0010:compress_file_range+0x476/0x7b0 [btrfs] Call Trace: ? submit_compressed_extents+0x450/0x450 [btrfs] async_cow_start+0x16/0x40 [btrfs] btrfs_work_helper+0xf2/0x3e0 [btrfs] process_one_work+0x278/0x5e0 worker_thread+0x55/0x400 ? process_one_work+0x5e0/0x5e0 kthread+0x168/0x190 ? kthread_create_worker_on_cpu+0x70/0x70 ret_from_fork+0x22/0x30 ---[ end trace 65faf4eae941fa7d ]--- This is already after the patch "btrfs: inode: fix NULL pointer dereference if inode doesn't need compression." [CAUSE] @pages is firstly created by kcalloc() in compress_file_extent(): pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); Then passed to btrfs_compress_pages() to be utilized there: ret = btrfs_compress_pages(... pages, &nr_pages, ...); btrfs_compress_pages() will initialize each page as output, in zlib_compress_pages() we have: pages[nr_pages] = out_page; nr_pages++; Normally this is completely fine, but there is a special case which is in btrfs_compress_pages() itself: switch (type) { default: return -E2BIG; } In this case, we didn't modify @pages nor @out_pages, leaving them untouched, then when we cleanup pages, the we can hit NULL pointer dereference again: if (pages) { for (i = 0; i < nr_pages; i++) { WARN_ON(pages[i]->mapping); put_page(pages[i]); } ... } Since pages[i] are all initialized to zero, and btrfs_compress_pages() doesn't change them at all, accessing pages[i]->mapping would lead to NULL pointer dereference. This is not possible for current kernel, as we check inode_need_compress() before doing pages allocation. But if we're going to remove that inode_need_compress() in compress_file_extent(), then it's going to be a problem. [FIX] When btrfs_compress_pages() hits its default case, modify @out_pages to 0 to prevent such problem from happening. Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=212331 CC: stable@vger.kernel.org # 5.10+ Reviewed-by: Josef Bacik Signed-off-by: Qu Wenruo Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/compression.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) --- a/fs/btrfs/compression.c +++ b/fs/btrfs/compression.c @@ -80,10 +80,15 @@ static int compression_compress_pages(in case BTRFS_COMPRESS_NONE: default: /* - * This can't happen, the type is validated several times - * before we get here. As a sane fallback, return what the - * callers will understand as 'no compression happened'. + * This can happen when compression races with remount setting + * it to 'no compress', while caller doesn't call + * inode_need_compress() to check if we really need to + * compress. + * + * Not a big deal, just need to inform caller that we + * haven't allocated any pages yet. */ + *out_pages = 0; return -E2BIG; } }