From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dl1-f49.google.com (mail-dl1-f49.google.com [74.125.82.49]) (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 EE5A472618 for ; Sun, 1 Feb 2026 04:33:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769920403; cv=none; b=TMaass7YTLAkALPrPpqQk/4wXKD5WThjWiIWxjOtyywWJpXiP5RSQpmmwLgBH58ptSvxlIqLSp4nMcXpFcjKNUSZkBSch6SUV2pssYaqJoE58xKRthpAnmQ003mHOr4bvvS+eEJ/7gJbiA+sAvVXKhs2a/nXld0MEYLkjTjQI0w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769920403; c=relaxed/simple; bh=pYJ9S4ouEUfVExjHTvplH+HhBz4WHsyWZE2qN94CK/Y=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Xp0JQbcmExHqhikIomqNpxaPF80w4i8ijqU9rhocdecUro4hEKxG/31hK6WxGrYmmMIF41gjOVh/GjSFxB18PWgEf4Aru6bNGyeCzkNlkdbAq5wC5avusL6h16Gw3/mYgdwgeYElio9KlNPmxa5i6ZZkfLyBxop0htVffOkxczM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=kCTVKglk; arc=none smtp.client-ip=74.125.82.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kCTVKglk" Received: by mail-dl1-f49.google.com with SMTP id a92af1059eb24-1248d27f2b9so4503644c88.0 for ; Sat, 31 Jan 2026 20:33:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769920401; x=1770525201; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=4FGWPb9KM6ysaOCeh4W7k0WYOCfJBxvkmeobjGRz7Wk=; b=kCTVKglktvhmza32A1ROVrPPRBDGQrNZsc1QLgtVlM1JTcHe0YVqRCA9DXoRVZItOt KQ8qIBRRLZNUvm/LuzSXLRY/M08SqlWCbdge+EYtCu8sep13Md5AnOeTJ1231gZxik/v Wuv7+dpB4SJVMFkf2TN14Rgd12qO1QOyP3jcR9eycHQyPGOLXrjC5AHK8HIUWfREhYQ7 5lA2MfaFtKp1ruZQX0jkXAno4OAJCUPLGCPKwNdCxFQTQGjqXnm500HRzWtFlbV2MtW6 mYBBKq4aNb6nxdxPd/UKDMxOS2Xe6taK+n/kSz5O47ybd7rqNhy1FJyUWFRp7BOBXdxH uUNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769920401; x=1770525201; h=content-transfer-encoding:in-reply-to: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=4FGWPb9KM6ysaOCeh4W7k0WYOCfJBxvkmeobjGRz7Wk=; b=mFaMthO9MMh6sjf8zYyuui8g3Oi4OvcyuCK+UfsFIjIQmCPuk+qbjh0Mg4+McYwCpE vwifXP0x7twYC+/t1W4Lg5JZyHLxEuZ+RxPPMxtHJGman0hC8fVznUmJi6CdXZxbdo26 eO7UOMBuZyzZeDHgejczboagBf5JAJyY5OUhxnGd8CTGHQnZ3GqEVR8foGu8WIdYz93D AjA47j4/VqW6/ipAj6tUjbkM1c/YwT7ulkCmyIX4JmUBOW5vf8wh+mssaVaCCKe1V+yL EZVZ36uiwxPI34sJoKwGcX+W9qc92OSu5kfFtqnButMa3PjlAZAwjVZ+3LaSVi5GHifp xpUg== X-Forwarded-Encrypted: i=1; AJvYcCXSWfYj6b769APf4rjl+jpopCJQyZPt4lbsmu9oCkFywhEKz+P9x7kbPMN/MKRKTVXskV8vcF+BAMDQ8RE=@vger.kernel.org X-Gm-Message-State: AOJu0YzlVznBx9Nsk1S0YAYaT6MShevg/RSGAl4PPiyopJDfM4Vatts0 mHH26j/tQKIPb7LBBX/sTN93HX0EgjBTWYkDf/cn1X9yQk50nhfGBX6T X-Gm-Gg: AZuq6aJiL7QXhP2SJTgqC1JAXitvlqC7UxBHD1PbqUjHlS1E3jRgI+VTb+uclI3dfP+ GKeq3aUBojdzNsc9wd7/HGqwZxkZ7uAIzyijxWEIVZp7Cyzmn/xhQTOkKd5OgJISyIsQRHZHMxX U+772FxmEACEt+4R30v3AN0Ge+Z65cdFCdD5tF8Czk5GSfS1K1EEC21vVAcZ2gEL9N5oWz8cFhK Gp0SBvrMiz0XfBUb3T5iVXLfjSJ+jsy3rxXfyKalYDPRDacF+PLeBnhc8d8UdnE3pt7LTJVxoju JciQf21lKmtV1ZG7mxX+28uSov9acTHbjbezpMBN1die+n/o4YknG8bnk5Z0Q+NAxOXCTmU38O3 XycMMp8xyHfubzn/eRxmGHnda/WFez/wP/lhszajKG78xNteJh1FPIB6ADUcq/rj8daYlDjwxVn LOprFV46WJJqKlE5pKuYA/yCIocKQ5fh8= X-Received: by 2002:a05:7022:128c:b0:124:65f7:2c2e with SMTP id a92af1059eb24-125c1019b4fmr3272381c88.43.1769920400849; Sat, 31 Jan 2026 20:33:20 -0800 (PST) Received: from [192.168.4.196] ([73.222.117.172]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-124a9d6b663sm15306235c88.1.2026.01.31.20.33.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 31 Jan 2026 20:33:20 -0800 (PST) Message-ID: <61b5f5fd-ed50-40b3-af32-159106f0af7c@gmail.com> Date: Sat, 31 Jan 2026 20:33:18 -0800 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: Qu Wenruo , 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> <0932845c-0ece-4d48-a0d5-4b8de7c301a6@suse.com> Content-Language: en-US From: JP Kobryn In-Reply-To: <0932845c-0ece-4d48-a0d5-4b8de7c301a6@suse.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 1/31/26 3:32 PM, Qu Wenruo wrote: > > > 在 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. > Great. Let me resend this patch and the additional one with the proper stable tags showing the specific kernel versions each apply to.