CVE-2025-40209
Description
In the Linux kernel, the following vulnerability has been resolved:
btrfs: fix memory leak of qgroup_list in btrfs_add_qgroup_relation
When btrfs_add_qgroup_relation() is called with invalid qgroup levels (src >= dst), the function returns -EINVAL directly without freeing the preallocated qgroup_list structure passed by the caller. This causes a memory leak because the caller unconditionally sets the pointer to NULL after the call, preventing any cleanup.
The issue occurs because the level validation check happens before the mutex is acquired and before any error handling path that would free the prealloc pointer. On this early return, the cleanup code at the 'out' label (which includes kfree(prealloc)) is never reached.
In btrfs_ioctl_qgroup_assign(), the code pattern is:
prealloc = kzalloc(sizeof(*prealloc), GFP_KERNEL); ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst, prealloc); prealloc = NULL; // Always set to NULL regardless of return value ... kfree(prealloc); // This becomes kfree(NULL), does nothing
When the level check fails, 'prealloc' is never freed by either the callee or the caller, resulting in a 64-byte memory leak per failed operation. This can be triggered repeatedly by an unprivileged user with access to a writable btrfs mount, potentially exhausting kernel memory.
Fix this by freeing prealloc before the early return, ensuring prealloc is always freed on all error paths.
AI Insight
LLM-synthesized narrative grounded in this CVE's description and references.
Btrfs memory leak in btrfs_add_qgroup_relation when invalid qgroup levels cause early return without freeing preallocated memory.
Vulnerability
In the Linux kernel's btrfs filesystem, the function btrfs_add_qgroup_relation() fails to free a preallocated qgroup_list structure when called with invalid qgroup levels (src >= dst). The function performs a level validation check before acquiring a mutex and before any error handling path that would free the prealloc pointer. On failure, the function returns -EINVAL directly, skipping the cleanup code at the 'out' label which would have called kfree(prealloc).
Exploitation
The caller, btrfs_ioctl_qgroup_assign(), allocates the qgroup_list via kzalloc() and passes it to btrfs_add_qgroup_relation(). After the call, the caller unconditionally sets the pointer to NULL, meaning the allocated memory is never freed. An unprivileged user with write access to a btrfs mount can repeatedly trigger this code path by calling the ioctl with invalid qgroup IDs, causing a 64-byte memory leak per operation. This can be done without any special privileges beyond the ability to interact with a writable btrfs filesystem.
Impact
Repeated exploitation can exhaust kernel memory, leading to denial of service (system instability, out-of-memory kills, or system crash). The vulnerability is a classic memory leak that degrades system availability over time.
Mitigation
The fix involves freeing the prealloc structure before the early return in btrfs_add_qgroup_relation(), ensuring all error paths release the allocated memory. Patches have been committed to the stable kernel tree [1][2][3]. Users should update to a kernel containing the fix. No workaround other than restricting access to btrfs ioctls is available.
AI Insight generated on May 19, 2026. Synthesized from this CVE's description and the cited reference URLs; citations are validated against the source bundle.
Affected products
2Patches
33412d0e973e8a4d9ebe23bcbf260c6aff0b8Vulnerability mechanics
Generated on May 9, 2026. Inputs: CWE entries + fix-commit diffs from this CVE's patches. Citations validated against bundle.
References
3News mentions
0No linked articles in our index yet.