From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B7D4BBA34 for ; Sat, 31 Jan 2026 23:32:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769902334; cv=none; b=atzbG/kEyM3EWHsTnLOdQ2qvEEfkcYNHkwDzPzWPGljhP1GjCtqbpA3fKRLlOg8vHE0p9UbsnJvdLUqk8ahFs4bC+mHUdFQmERcpTKgIhScQ6n0Q9sCJ83wPXLkJv/6ROHwDEqU7gbNnZIkwArN7AppYvbBJHtkBwL3sLaryHxY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769902334; c=relaxed/simple; bh=LSpQdEyj7+wqociODTfTui6CMQaeKW5xXBDeNGSAzqA=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=vEzqbkqzhVSvi0e+KD+bafGRVnpa4JN/o84H70P2JIGSdnpxwIF+2zIX0eKx0K0oouDsQOeB7QYh5rX55LLQ+TVaLzReWl+pTEg2b+IHmZbkQjqhbHRMOaCLhqtYBJ94D0aI9lpdoF3Q3UHzhRaVtm7v9Q3UyR2Yy7v1+gGDJQo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=cxKnKmwc; arc=none smtp.client-ip=209.85.128.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="cxKnKmwc" Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-47ee76e8656so43352285e9.0 for ; Sat, 31 Jan 2026 15:32:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1769902330; x=1770507130; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to; bh=5KOcYgh3TwjZOo3y1JLTR+xDDzOK1+fIpD6nfp3PV7g=; b=cxKnKmwcYU7pNIVPIXQzVlj9LXNEtPuvqPlo4C5a/zhV39aUOvUePHshoB2+u8gUKg +e/2kg29ABnnf7cjuoc3qlmGBhbwr4s/iRz8erEsbeuQRmJpbHKSOXxr/ozN0CF2IRlB mc95CJIcfD2REROp/3c/7y9pGrdnzwkUHOhQ4/HeqJ7f2dajNkoRoBOgh7JtOF8FOHU7 IQNuV8OU4XwzJ8rnwMacRjCgt+ytuyS3wQ/K+04VukNBoLJEduYItLbOajrtBFF/r9XJ D/JLgimiR0T9uUiTrrQpZbNWAsUlWasDjKPKJuVJFiQzyADLLo7YFr5y440nG80d0XNz nMtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769902330; x=1770507130; h=content-transfer-encoding:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=5KOcYgh3TwjZOo3y1JLTR+xDDzOK1+fIpD6nfp3PV7g=; b=UHhDQEGK/3cSsPPlmxw/NspJ374DN0NVDLtmXzfGMxYKOX0K4mKFju+3wrOMXJb6q1 vLQJAxvLrw17iwtopfRAH9htbmze/ktpO5ujm8GgPvOaG0DQMwF7VMSOXZmKMhnAxXE4 9HFlJ17b4T1LxZYbVjKZOZ8dYolPMz4SQF/xruSlmQ6mN7qAYWfeIQ4by5t86Xwy2avY 8w1EApeK90upqNKX6pnpECr5Kh7N/uFQDKrA6xMMFyBT3RF/XTbFqvlrvqrzI3woIVXL itAgdMA3DBeJLDmMzRjtiifUScl/RtuyxcbvqF5mj3QiDMLmQizEIFy/eizg49TB4Bzp gd6A== X-Forwarded-Encrypted: i=1; AJvYcCWq1gRwg7ElJ1CvqrC+mTpQlnOooLqruymrDxxMsAm7MlxnYFEXnd0zhn9Yue9jag8lErjIhuovA/juKak=@vger.kernel.org X-Gm-Message-State: AOJu0YyDjpNQD+IIvUOXzVS686JStnZ9ytlrTZvr3QB/BhMg0w62wGDB boG04I3D6h5nOIfwXRIz8OyTY4pNAn0cpahOShLGQUkfuZZRxiHomGbCqTtUeH9S3Ig= X-Gm-Gg: AZuq6aLmqwvrfSg384ms21deJlwBr/KXnP+sit6PBuPzYUZGvh2G5nltQ1/rjbXCmzr b/ChT00JbmhQpuHeX9smCSJ1Yq2smlp9W5e7IMBQYjT2Ydz7eS7sQ9xPSHSDgHjcGtelWWDI5AV ETSaHeHDZurNntVfX321qSuV/zINK9ccCpVxIV/52ounn+FL9USomPN2IManqsndcU4qtDC9EEs cQ3DzFm88Dyr1KhJgBXqWbzniIGmHogg0wKB2YLy2kueFagGqw+5V7lbWmI1wHpdZGQLTUBpGlD KWprXS4HnHbPelPFQ27EcZ8cZ3k5aVtwqERVoe2B9c7Ww+eZlNWotg43AeZXP+Z8hUccWh5tTHn ZyzXkd80dRcB9hvf9ETO2fhV/Ub5m8FGs69hNHwrEQx/GxxhRnEVIvCu5l4hjpKM1YsqKuc4UE8 Bl0DLvbNpqGKEUGF/Jv3/BsI9KdPHoxJgfc+g+USI= X-Received: by 2002:a05:600c:4e89:b0:477:9fcf:3fe3 with SMTP id 5b1f17b1804b1-482db213a37mr90293935e9.0.1769902330035; Sat, 31 Jan 2026 15:32:10 -0800 (PST) Received: from ?IPV6:2403:580d:fda1::299? (2403-580d-fda1--299.ip6.aussiebb.net. [2403:580d:fda1::299]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82379b1efa6sm11254064b3a.6.2026.01.31.15.32.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 31 Jan 2026 15:32:09 -0800 (PST) Message-ID: <0932845c-0ece-4d48-a0d5-4b8de7c301a6@suse.com> Date: Sun, 1 Feb 2026 10:02:01 +1030 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 6.12] btrfs: prevent use-after-free prealloc_file_extent_cluster() To: JP Kobryn , boris@bur.io, clm@fb.com, dsterba@suse.com Cc: linux-btrfs@vger.kernel.org, stable@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@meta.com References: <20260131185335.72204-1-inwardvessel@gmail.com> <4c37d4e1-e656-48e3-ac80-83c09fe92625@suse.com> Content-Language: en-US From: Qu Wenruo Autocrypt: addr=wqu@suse.com; keydata= xsBNBFnVga8BCACyhFP3ExcTIuB73jDIBA/vSoYcTyysFQzPvez64TUSCv1SgXEByR7fju3o 8RfaWuHCnkkea5luuTZMqfgTXrun2dqNVYDNOV6RIVrc4YuG20yhC1epnV55fJCThqij0MRL 1NxPKXIlEdHvN0Kov3CtWA+R1iNN0RCeVun7rmOrrjBK573aWC5sgP7YsBOLK79H3tmUtz6b 9Imuj0ZyEsa76Xg9PX9Hn2myKj1hfWGS+5og9Va4hrwQC8ipjXik6NKR5GDV+hOZkktU81G5 gkQtGB9jOAYRs86QG/b7PtIlbd3+pppT0gaS+wvwMs8cuNG+Pu6KO1oC4jgdseFLu7NpABEB AAHNGFF1IFdlbnJ1byA8d3F1QHN1c2UuY29tPsLAlAQTAQgAPgIbAwULCQgHAgYVCAkKCwIE FgIDAQIeAQIXgBYhBC3fcuWlpVuonapC4cI9kfOhJf6oBQJnEXVgBQkQ/lqx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PDg9wO JoAXek8MF37R8gpLoGWl41FIUb3hFiu3zhDDvslYM4BmzI18QgQTQnotJH8= In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 在 2026/2/1 09:53, JP Kobryn 写道: > On 1/31/26 1:08 PM, Qu Wenruo wrote: >> >> >> 在 2026/2/1 05:23, JP Kobryn 写道: >>> Users of filemap_lock_folio() need to guard against the situation where >>> release_folio() has been invoked during reclaim but the folio was >>> ultimately not removed from the page cache. This patch covers one >>> location >>> that was overlooked. Affected code has changed as of 6.17, so this >>> patch is >>> only targeting stable trees prior. >>> >>> After acquiring the folio, use set_folio_extent_mapped() to ensure the >>> folio private state is valid. This is especially important in the >>> subpage >>> case, where the private field is an allocated struct containing >>> bitmap and >>> lock data. >>> >>> Without this protection, the race below is possible: >>> >>> [mm] page cache reclaim path        [fs] relocation in subpage mode >>> shrink_folio_list() >>>    folio_trylock() /* lock acquired */ >>>    filemap_release_folio() >>>      mapping->a_ops->release_folio() >>>        btrfs_release_folio() >>>          __btrfs_release_folio() >>>            clear_folio_extent_mapped() >>>              btrfs_detach_folio_state() >>>                bfs = folio_detach_private(folio) >>>                btrfs_free_folio_state(folio) >>>                  kfree(bfs) /* point A */ >>> >>>                                     prealloc_file_extent_cluster() >>>                                       filemap_lock_folio() >>>                                         folio_try_get() /* inc >>> refcount */ >>>                                         folio_lock() /* wait for lock */ >>> >>>    if (...) >>>      ... >>>    else if (!mapping || !__remove_mapping(..)) >>>      /* >>>       * __remove_mapping() returns zero when >>>       * folio_ref_freeze(folio, refcount) fails /* point B */ >>>       */ >>>      goto keep_locked /* folio remains in cache */ >>> >>> keep_locked: >>>    folio_unlock(folio) /* lock released */ >>> >>>                                     /* lock acquired */ >>>                                     btrfs_subpage_clear_updodate() >>>                                       bfs = folio->priv /* use-after- >>> free */ >> >> This patch itself and the root cause look good to me. >> >> Reviewed-by: Qu Wenruo >> > > Much appreciated :) > >>> >>> This patch is intended as a minimal fix for backporting to affected >>> kernels. As of 6.17, a commit [0] replaced the vulnerable >>> filemap_lock_folio() + btrfs_subpage_clear_uptodate() sequence with >>> filemap_invalidate_inode() avoiding the race entirely. That commit >>> was part >>> of a series with a different goal of preparing for large folio >>> support so >>> backporting may not be straight forward. >> >> However I'm not sure if stable tree even accepts non-upstreamed patches. >> >> Thus the stable maintainer may ask you the same question as I did >> before, why not backport the upstream commit 4e346baee95f? > > That commit relies on filemap_invalidate_folio() which was introduced in > 6.10 so it would not apply to earlier stable branches. > > We need to fix as far back as 5.15 so I can send one additional patch to > cover stable trees 5.15 to 6.6. The patch would be almost identical, > with the only change being using the page API instead of the folio API > (set_folio_extent_mapped() -> set_page_extent_mapped()). Let me know if > you're in agreement and I can send the extra patch. If stable chooses to go this path, I'm totally fine with similar backports. Thanks, Qu > >> >> If it's lacking the reason why it's a bug fix, I believe you can >> modify the commit message to include the analyze and the fixes tag. >> >> >> I'm also curious to learn the proper way for such situation. > > It's new to me as well. For reference, there are some commits on the > list that have language like this: "This is a stable-only fix".