CVE-2024-38306: btrfs: protect folio::private when attaching extent buffer folios

Published Jun 25, 2024
·
Updated

In the Linux kernel, the following vulnerability has been resolved:

btrfs: protect folio::private when attaching extent buffer folios

[BUG] Since v6.8 there are rare kernel crashes reported by various people, the common factor is bad page status error messages like this:

BUG: Bad page state in process kswapd0 pfn:d6e840 page: refcount:0 mapcount:0 mapping:000000007512f4f2 index:0x2796c2c7c pfn:0xd6e840 aops:btreeaops ino:1 flags: 0x17ffffe0000008(uptodate|node=0|zone=2|lastcpupid=0x3fffff) pagetype: 0xffffffff() raw: 0017ffffe0000008 dead000000000100 dead000000000122 ffff88826d0be4c0 raw: 00000002796c2c7c 0000000000000000 00000000ffffffff 0000000000000000 page dumped because: non-NULL mapping

[CAUSE] Commit 09e6cef19c9f ("btrfs: refactor allocextentbuffer() to allocate-then-attach method") changes the sequence when allocating a new extent buffer.

Previously we always called grabextentbuffer() under mapping->iprivatelock, to ensure the safety on modification on folio::private (which is a pointer to extent buffer for regular sectorsize).

This can lead to the following race:

Thread A is trying to allocate an extent buffer at bytenr X, with 4 4K pages, meanwhile thread B is trying to release the page at X + 4K (the second page of the extent buffer at X).

Thread A | Thread B -----------------------------------+------------------------------------- | btreereleasefolio() | | This is for the page at X + 4K, | | Not page X. | | allocextentbuffer() | |- releaseextentbuffer() |- filemapaddfolio() for the | | |- atomicdecandtest(eb->refs) | page at bytenr X (the first | | | | page). | | | | Which returned -EEXIST. | | | | | | | |- filemaplockfolio() | | | | Returned the first page locked. | | | | | | | |- grabextentbuffer() | | | | |- atomicincnotzero() | | | | | Returned false | | | | |- foliodetachprivate() | | |- foliodetachprivate() for X | |- foliotestprivate() | | |- foliotestprivate() | Returned true | | | Returned true |- folioput() | |- folioput()

Now there are two puts on the same folio at folio X, leading to refcount underflow of the folio X, and eventually causing the BUGON() on the page->mapping.

The condition is not that easy to hit:

- The release must be triggered for the middle page of an eb If the release is on the same first page of an eb, page lock would kick in and prevent the race.

- foliodetachprivate() has a very small race window It's only between foliotestprivate() and folioclearprivate().

That's exactly when mapping->iprivatelock is used to prevent such race, and commit 09e6cef19c9f ("btrfs: refactor allocextentbuffer() to allocate-then-attach method") screwed that up.

At that time, I thought the page lock would kick in as filemapreleasefolio() also requires the page to be locked, but forgot the filemapreleasefolio() only locks one page, not all pages of an extent buffer.

[FIX] Move all the code requiring iprivatelock into attachebfoliotofilemap(), so that everything is done with proper lock protection.

Furthermore to prevent future problems, add an extra lockdepassertlocked() to ensure we're holding the proper lock.

To reproducer that is able to hit the race (takes a few minutes with instrumented code inserting delays to allocextentbuffer()):

#!/bin/sh dropcaches () { while(true); do echo 3 > /proc/sys/vm/dropcaches echo 1 > /proc/sys/vm/compactmemory done }

runtar () { while(true); do for x in seq 1 80 ; do tar cf /dev/zero /mnt > /dev/null & done wait done }

mkfs.btrfs -f -d single -m single ---truncated---

Affected Software

4 affected componentsFixes available
debian/linux
5.10.223-15.10.234-16.1.129-16.1.135-16.12.25-16.12.27-1
Linux Linux kernel>=6.8<6.9.5
Linux Linux kernel=6.10-rc1
Linux Linux kernel=6.10-rc2

Remediation

Recommended actions to resolve this vulnerability, in priority order.

  1. Upgrade

    Upgrade debian/linux to a version that resolves this vulnerability.

    Fixed in 5.10.223-1Fixed in 5.10.234-1Fixed in 6.1.129-1Fixed in 6.1.135-1Fixed in 6.12.25-1Fixed in 6.12.27-1
  2. Upgrade

    Upgrade to a fixed release to a version that resolves this vulnerability.

    Patch 09e6cef19c9f
  3. Upgrade

    Upgrade Linux kernel - btrfs: protect folio::private when attaching extent buffer folios to a version that resolves this vulnerability.

    Fixed in v6.8
  4. Configuration

    Update the btrfs extent-buffer attachment logic so all code requiring mapping->i_private_lock is moved into attach_eb_folio_to_filemap(), with proper lock protection to prevent the folio::private race window during alloc_extent_buffer() / folio_detach_private() interleavings.

    btrfs folio::private attachment locking (i_private_lock usage) = Move all the code requiring i_private_lock into attach_eb_folio_to_filemap()

Event History

Jun 25, 2024
CVE Published
via MITRE·02:22 PM
Data Sourced
via MITRE·02:22 PM
DescriptionSeverity
Data Sourced
via NVD·03:15 PM
Description
Data Sourced
via NVD·03:15 PM
RemedySeverityWeaknessAffected Software
May 1, 2025
Data Sourced
via Ubuntu·05:13 PM
RemedyDescriptionSeverityAffected Software

Frequently Asked Questions

1

What is the severity of CVE-2024-38306?

CVE-2024-38306 has a high severity associated with potential kernel crashes.

2

How do I fix CVE-2024-38306?

To mitigate CVE-2024-38306, update your Linux kernel to versions 5.10.223-1, 5.10.226-1, 6.1.119-1, 6.1.123-1, 6.12.11-1, or 6.12.12-1.

3

What software is affected by CVE-2024-38306?

CVE-2024-38306 affects specific versions of the Linux kernel, particularly from Debian distributions.

4

What are the symptoms of CVE-2024-38306?

The primary symptom of CVE-2024-38306 is rare kernel crashes accompanied by bad page status error messages.

5

When was CVE-2024-38306 reported?

CVE-2024-38306 was reported after the introduction of affected changes in kernel version 6.8.

Contact

SecAlerts Pty Ltd.
132 Wickham Terrace
Fortitude Valley,
QLD 4006, Australia
info@secalerts.co
By using SecAlerts services, you agree to our services end-user license agreement. This website is safeguarded by reCAPTCHA and governed by the Google Privacy Policy and Terms of Service. All names, logos, and brands of products are owned by their respective owners, and any usage of these names, logos, and brands for identification purposes only does not imply endorsement. If you possess any content that requires removal, please get in touch with us.
© 2026 SecAlerts Pty Ltd.
ABN: 70 645 966 203, ACN: 645 966 203