CVE-2025-40209
btrfs: fix memory leak of qgroup_list in btrfs_add_qgroup_relation
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.
INFO
Published Date :
Nov. 21, 2025, 11:15 a.m.
Last Modified :
Oct. 19, 2026, 7:10 p.m.
Remotely Exploit :
No
Source :
416baaa9-dc9f-4396-8d5f-8c081fb06d67
Solution
- Apply the patch to the Linux kernel.
- Ensure memory is freed in error paths.
- Validate qgroup levels before allocation.
- Test for memory leak prevention.
References to Advisories, Solutions, and Tools
Here, you will find a curated list of external links that provide in-depth
information, practical solutions, and valuable tools related to
CVE-2025-40209.
CWE - Common Weakness Enumeration
While CVE identifies
specific instances of vulnerabilities, CWE categorizes the common flaws or
weaknesses that can lead to vulnerabilities. CVE-2025-40209 is
associated with the following CWEs:
Common Attack Pattern Enumeration and Classification (CAPEC)
Common Attack Pattern Enumeration and Classification
(CAPEC)
stores attack patterns, which are descriptions of the common attributes and
approaches employed by adversaries to exploit the CVE-2025-40209
weaknesses.
We scan GitHub repositories to detect new proof-of-concept exploits. Following list is a collection of public exploits and proof-of-concepts, which have been published on GitHub (sorted by the most recently updated).
Results are limited to the first 15 repositories due to potential performance issues.
The following list is the news that have been mention
CVE-2025-40209 vulnerability anywhere in the article.
The following table lists the changes that have been made to the
CVE-2025-40209 vulnerability over time.
Vulnerability history details can be useful for understanding the evolution of a vulnerability, and for identifying the most recent changes that may impact the vulnerability's severity, exploitability, or other characteristics.
-
CVE Translated by [email protected]
Oct. 19, 2026
Action Type Old Value New Value Added Translation Title: Linux, Description: En el kernel de Linux, la siguiente vulnerabilidad ha sido resuelta: btrfs: corrige la fuga de memoria de qgroup_list en btrfs_add_qgroup_relation Cuando se llama a btrfs_add_qgroup_relation() con niveles de qgroup inválidos (src >= dst), la función devuelve -EINVAL directamente sin liberar la estructura qgroup_list preasignada pasada por el llamador. Esto causa una fuga de memoria porque el llamador establece incondicionalmente el puntero en NULL después de la llamada, impidiendo cualquier limpieza. El problema ocurre porque la comprobación de validación de nivel sucede antes de que se adquiera el mutex y antes de cualquier ruta de manejo de errores que liberaría el puntero prealloc. En este retorno temprano, el código de limpieza en la etiqueta 'out' (que incluye kfree(prealloc)) nunca se alcanza. En btrfs_ioctl_qgroup_assign(), el patrón de código es: prealloc = kzalloc(sizeof(prealloc), GFP_KERNEL); ret = btrfs_add_qgroup_relation(trans, sa -> src, sa -> dst, prealloc); prealloc = NULL; // Siempre se establece en NULL independientemente del valor de retorno ... kfree(prealloc); // Esto se convierte en kfree(NULL), no hace nada Cuando la comprobación de nivel falla, 'prealloc' nunca es liberado ni por el llamado ni por el llamador, lo que resulta en una fuga de memoria de 64 bytes por operación fallida. Esto puede ser activado repetidamente por un usuario sin privilegios con acceso a un montaje btrfs escribible, agotando potencialmente la memoria del kernel. Solucione esto liberando prealloc antes del retorno temprano, asegurando que prealloc siempre se libere en todas las rutas de error. -
CVE Modified by 416baaa9-dc9f-4396-8d5f-8c081fb06d67
Jun. 17, 2026
Action Type Old Value New Value Added Affected [{'repo': 'https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git', 'vendor': 'Linux', 'product': 'Linux', 'versions': [{'status': 'affected', 'version': '4addc1ffd67ad34394674dc91379dc04cfdd2537', 'lessThan': '3412d0e973e8f8381747d69033eda809a57a2581', 'versionType': 'git'}, {'status': 'affected', 'version': '4addc1ffd67ad34394674dc91379dc04cfdd2537', 'lessThan': 'a4d9ebe23bcb79d9d057e3c995db73b7b3aae414', 'versionType': 'git'}, {'status': 'affected', 'version': '4addc1ffd67ad34394674dc91379dc04cfdd2537', 'lessThan': 'f260c6aff0b8af236084012d14f9f1bf792ea883', 'versionType': 'git'}], 'programFiles': ['fs/btrfs/qgroup.c'], 'defaultStatus': 'unaffected'}, {'repo': 'https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git', 'vendor': 'Linux', 'product': 'Linux', 'versions': [{'status': 'affected', 'version': '6.11'}, {'status': 'unaffected', 'version': '0', 'lessThan': '6.11', 'versionType': 'semver'}, {'status': 'unaffected', 'version': '6.12.58', 'versionType': 'semver', 'lessThanOrEqual': '6.12.*'}, {'status': 'unaffected', 'version': '6.17.8', 'versionType': 'semver', 'lessThanOrEqual': '6.17.*'}, {'status': 'unaffected', 'version': '6.18', 'versionType': 'original_commit_for_fix', 'lessThanOrEqual': '*'}], 'programFiles': ['fs/btrfs/qgroup.c'], 'defaultStatus': 'affected'}] -
New CVE Received by 416baaa9-dc9f-4396-8d5f-8c081fb06d67
Nov. 21, 2025
Action Type Old Value New Value Added 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. Added Reference https://git.kernel.org/stable/c/3412d0e973e8f8381747d69033eda809a57a2581 Added Reference https://git.kernel.org/stable/c/a4d9ebe23bcb79d9d057e3c995db73b7b3aae414 Added Reference https://git.kernel.org/stable/c/f260c6aff0b8af236084012d14f9f1bf792ea883