CVE-2024-35784

Source
https://cve.org/CVERecord?id=CVE-2024-35784
Import Source
https://storage.googleapis.com/osv-test-cve-osv-conversion/osv-output/CVE-2024-35784.json
JSON Data
https://api.test.osv.dev/v1/vulns/CVE-2024-35784
Downstream
Related
Published
2024-05-17T12:24:24.421Z
Modified
2026-03-13T07:55:20.797595Z
Summary
btrfs: fix deadlock with fiemap and extent locking
Details

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

btrfs: fix deadlock with fiemap and extent locking

While working on the patchset to remove extent locking I got a lockdep splat with fiemap and pagefaulting with my new extent lock replacement lock.

This deadlock exists with our normal code, we just don't have lockdep annotations with the extent locking so we've never noticed it.

Since we're copying the fiemap extent to user space on every iteration we have the chance of pagefaulting. Because we hold the extent lock for the entire range we could mkwrite into a range in the file that we have mmap'ed. This would deadlock with the following stack trace

[<0>] lockextent+0x28d/0x2f0 [<0>] btrfspagemkwrite+0x273/0x8a0 [<0>] dopagemkwrite+0x50/0xb0 [<0>] dofault+0xc1/0x7b0 [<0>] __handlemmfault+0x2fa/0x460 [<0>] handlemmfault+0xa4/0x330 [<0>] douseraddrfault+0x1f4/0x800 [<0>] excpagefault+0x7c/0x1e0 [<0>] asmexcpagefault+0x26/0x30 [<0>] repmovsalternative+0x33/0x70 [<0>] copytouser+0x49/0x70 [<0>] fiemapfillnextextent+0xc8/0x120 [<0>] emitfiemapextent+0x4d/0xa0 [<0>] extentfiemap+0x7f8/0xad0 [<0>] btrfsfiemap+0x49/0x80 [<0>] __x64sysioctl+0x3e1/0xb50 [<0>] dosyscall64+0x94/0x1a0 [<0>] entrySYSCALL64afterhwframe+0x6e/0x76

I wrote an fstest to reproduce this deadlock without my replacement lock and verified that the deadlock exists with our existing locking.

To fix this simply don't take the extent lock for the entire duration of the fiemap. This is safe in general because we keep track of where we are when we're searching the tree, so if an ordered extent updates in the middle of our fiemap call we'll still emit the correct extents because we know what offset we were on before.

The only place we maintain the lock is searching delalloc. Since the delalloc stuff can change during writeback we want to lock the extent range so we have a consistent view of delalloc at the time we're checking to see if we need to set the delalloc flag.

With this patch applied we no longer deadlock with my testcase.

Database specific
{
    "cna_assigner": "Linux",
    "osv_generated_from": "https://github.com/CVEProject/cvelistV5/tree/main/cves/2024/35xxx/CVE-2024-35784.json"
}
References

Affected packages

Git / git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git

Affected ranges

Type
GIT
Repo
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
Events
Introduced
1506fcc8189cdd4b95e06df7845a09f18b4526a6
Fixed
ded566b4637f1b6b4c9ba74e7d0b8493e93f19cf
Fixed
89bca7fe6382d61e88c67a0b0e7bce315986fb8b
Fixed
b0ad381fa7690244802aed119b478b4bdafc31dd

Database specific

source
"https://storage.googleapis.com/osv-test-cve-osv-conversion/osv-output/CVE-2024-35784.json"