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

> Archived entry. Full-text search covers entries since 2026-11-10; exact IDs work for all years.

- **Severity:** HIGH 7.8
- **CVSS:** 7.8 (v3.1, CNA) `CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H`
- **CVSS metrics:** Attack vector: Local; Attack complexity: Low; Privileges required: Low; User interaction: None; Scope: Unchanged; Confidentiality impact: High; Integrity impact: High; Availability impact: High
- **Exploited in the wild:** Not in CISA KEV
- **Public exploit / PoC:** None known
- **Remote exploitation:** Not remotely exploitable (local or adjacent access needed)
- **EPSS:** 0.21% (percentile 7, 2026-11-26)
- **CWE:** CWE-362, CWE-617
- **Published:** 2024-06-25T14:22:36.903Z
- **Modified:** 2026-08-05T11:32:32.056Z

## Who should check

- Anyone running Linux \>= 09e6cef19c9fc0e10547135476865b5272aa0406, \< 952f048eb901881a7cc6f7c1368b53cd386ead7b; \>= 09e6cef19c9fc0e10547135476865b5272aa0406, \< f3a5367c679d31473d3fbb391675055b4792c309
- Anyone running Linux 6.8

## Description

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:btree\_aops ino:1
  flags: 0x17ffffe0000008(uptodate\|node=0\|zone=2\|lastcpupid=0x3fffff)
  page\_type: 0xffffffff()
  raw: 0017ffffe0000008 dead000000000100 dead000000000122 ffff88826d0be4c0
  raw: 00000002796c2c7c 0000000000000000 00000000ffffffff 0000000000000000
  page dumped because: non-NULL mapping

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

Previously we always called grab\_extent\_buffer() under
mapping-\>i\_private\_lock, 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
-----------------------------------+-------------------------------------
                                   \| btree\_release\_folio()
				   \| \| This is for the page at X + 4K,
				   \| \| Not page X.
				   \| \|
alloc\_extent\_buffer()              \| \|- release\_extent\_buffer()
\|- filemap\_add\_folio() for the     \| \|  \|- atomic\_dec\_and\_test(eb-\>refs)
\|  page at bytenr X (the first     \| \|  \|
\|  page).                          \| \|  \|
\|  Which returned -EEXIST.         \| \|  \|
\|                                  \| \|  \|
\|- filemap\_lock\_folio()            \| \|  \|
\|  Returned the first page locked. \| \|  \|
\|                                  \| \|  \|
\|- grab\_extent\_buffer()            \| \|  \|
\|  \|- atomic\_inc\_not\_zero()        \| \|  \|
\|  \|  Returned false               \| \|  \|
\|  \|- folio\_detach\_private()       \| \|  \|- folio\_detach\_private() for X
\|     \|- folio\_test\_private()      \| \|     \|- folio\_test\_private()
      \|  Returned true             \| \|     \|  Returned true
      \|- folio\_put()               \|       \|- folio\_put()

Now there are two puts on the same folio at folio X, leading to refcount
underflow of the folio X, and eventually causing the BUG\_ON() 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.

- folio\_detach\_private() has a very small race window
  It's only between folio\_test\_private() and folio\_clear\_private().

That's exactly when mapping-\>i\_private\_lock is used to prevent such race,
and commit 09e6cef19c9f ("btrfs: refactor alloc\_extent\_buffer() to
allocate-then-attach method") screwed that up.

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

\[FIX\]
Move all the code requiring i\_private\_lock into
attach\_eb\_folio\_to\_filemap(), so that everything is done with proper
lock protection.

Furthermore to prevent future problems, add an extra
lockdep\_assert\_locked() 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 alloc\_extent\_buffer()):

  \#!/bin/sh
  drop\_caches () {
	  while(true); do
		  echo 3 \> /proc/sys/vm/drop\_caches
		  echo 1 \> /proc/sys/vm/compact\_memory
	  done
  }

  run\_tar () {
	  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 and fixed versions

- Linux: \>= 09e6cef19c9fc0e10547135476865b5272aa0406, \< 952f048eb901881a7cc6f7c1368b53cd386ead7b; \>= 09e6cef19c9fc0e10547135476865b5272aa0406, \< f3a5367c679d31473d3fbb391675055b4792c309 (cvelist)
- Linux: 6.8 (cvelist)

## Mitigation

- No fix listed yet: follow the vendor advisory.

## Check your systems with an agent

Paste this into a coding agent:

```text
Check whether anything I run is affected by CVE-2024-38306. Work read-only: do not install, upgrade, delete or change anything.

Details: https://flawdb.com/CVE-2024-38306.md
Affected: Linux >= 09e6cef19c9fc0e10547135476865b5272aa0406, < 952f048eb901881a7cc6f7c1368b53cd386ead7b; >= 09e6cef19c9fc0e10547135476865b5272aa0406, < f3a5367c679d31473d3fbb391675055b4792c309; Linux 6.8
Fixed in: Linux 952f048eb901881a7cc6f7c1368b53cd386ead7b

Steps:
1. Search this machine and project for Linux: dependency manifests and lockfiles, installed packages, container images and running services.
2. For each match, note the installed version and whether it is inside the affected range.
3. Report a table: location, installed version, affected (yes / no / unsure), evidence. If affected, say which fixed version or mitigation from the details link applies, but don't apply it.
```

## FAQ

### Is CVE-2024-38306 being exploited?

There is no confirmed exploitation: CVE-2024-38306 is not in the CISA Known Exploited Vulnerabilities catalog. No public exploit code is known. EPSS estimates a 0.21% chance of exploitation in the next 30 days (as of 2026-11-26).

### How serious is CVE-2024-38306?

High: CVSS 7.8 (v3.1, scored by CNA). Not remotely exploitable (local or adjacent access needed).

### Which versions are affected by CVE-2024-38306?

Linux \>= 09e6cef19c9fc0e10547135476865b5272aa0406, \< 952f048eb901881a7cc6f7c1368b53cd386ead7b; \>= 09e6cef19c9fc0e10547135476865b5272aa0406, \< f3a5367c679d31473d3fbb391675055b4792c309; Linux 6.8.

### Is there a fix for CVE-2024-38306?

Yes. Fixed in Linux 952f048eb901881a7cc6f7c1368b53cd386ead7b. No fix listed yet: follow the vendor advisory.

### How do I check if I'm affected by CVE-2024-38306?

Find where you use the affected software (dependency lockfiles, installed packages, container images, servers) and compare the installed version with the affected range above. The agent prompt on this page does this read-only.

## References

- patch: <https://git.kernel.org/stable/c/952f048eb901881a7cc6f7c1368b53cd386ead7b>
- patch: <https://git.kernel.org/stable/c/f3a5367c679d31473d3fbb391675055b4792c309>

## Attribution

- CVE Program: CVE® is a registered trademark of The MITRE Corporation. CVE records © 1999–2026 The MITRE Corporation. <https://www.cve.org/Legal/TermsOfUse>
- NVD: This product uses data from the NVD API but is not endorsed or certified by the NVD. <https://nvd.nist.gov/vuln/detail/CVE-2024-38306>

HTML: <https://flawdb.com/CVE-2024-38306> · JSON: <https://flawdb.com/api/v1/entries/CVE-2024-38306>
