7.8high
btrfs: ensure no dirty metadata is written back for an fs with errors
- Severity
- 7.8high CVSS:3.1 by Linux
- Exploit likelihood
- 0.15% (higher than 4% of CVEs; EPSS, by FIRST)
- Published
- Last changed
- Assigned by
- Linux
Description
In the Linux kernel, the following vulnerability has been resolved: btrfs: ensure no dirty metadata is written back for an fs with errors [BUG] During development of a minor feature (make sure all btrfs_bio::end_io() is called in task context), I noticed a crash in generic/388, where metadata writes triggered new works after btrfs_stop_all_workers(). It turns out that it can even happen without any code modification, just using RAID5 for metadata and the same workload from generic/388 is going to trigger the use-after-free. [CAUSE] If btrfs hits an error, the fs is marked as error, no new transaction is allowed thus metadata is in a frozen state. But there are some metadata modifications before that error, and they are still in the btree inode page cache. Since there will be no real transaction commit, all those dirty folios are just kept as is in the page cache, and they can not be invalidated by invalidate_inode_pages2() call inside close_ctree(), because they are dirty. And finally after btrfs_stop_all_workers(), we call iput() on btree inode, which triggers writeback of those dirty metadata. And if the fs is using RAID56 metadata, this will trigger RMW and queue new works into rmw_workers, which is already stopped, causing warning from queue_work() and use-after-free. [FIX] Add a special handling for write_one_eb(), that if the fs is already in an error state, immediately mark the bbio as failure, instead of really submitting them. Then during close_ctree(), iput() will just discard all those dirty tree blocks without really writing them back, thus no more new jobs for already stopped-and-freed workqueues. The extra discard in write_one_eb() also acts as an extra safenet. E.g. the transaction abort is triggered by some extent/free space tree corruptions, and since extent/free space tree is already corrupted some tree blocks may be allocated where they shouldn't be (overwriting existing tree blocks). In that case writing them back will further corrupting the fs.
Affected
| Product or package | Versions | Reported by |
|---|---|---|
| Linux Linux | from 13e6c37b989859e70b0d73d3f2cb0aa022159b17 before 066ee13f05fbd82ada01883e51f0695172f98dff, from 13e6c37b989859e70b0d73d3f2cb0aa022159b17 before e2b3859067bf012d53c49b3f885fef40624a2c83, from 13e6c37b989859e70b0d73d3f2cb0aa022159b17 before 54a5b5a15588e3b0b294df31474d08a2678d4291, from 13e6c37b989859e70b0d73d3f2cb0aa022159b17 before 2618849f31e7cf51fadd4a5242458501a6d5b315, 3.10 Not affected: before 3.10, from 6.6.117 up to 6.6.*, from 6.12.58 up to 6.12.*, from 6.17.8 up to 6.17.*, from 6.18 up to * | CVE List |
What each source says
CVE List
In the Linux kernel, the following vulnerability has been resolved: btrfs: ensure no dirty metadata is written back for an fs with errors [BUG] During development of a minor feature (make sure all btrfs_bio::end_io() is called in task context), I noticed a crash in generic/388, where metadata writes triggered new works after btrfs_stop_all_workers(). It turns out that it can even happen without any code modification, just using RAID5 for metadata and the same workload from generic/388 is going to trigger the use-after-free. [CAUSE] If btrfs hits an error, the fs is marked as error, no new transaction is allowed thus metadata is in a frozen state. But there are some metadata modifications before that error, and they are still in the btree inode page cache. Since there will be no real transaction commit, all those dirty folios are just kept as is in the page cache, and they can not be invalidated by invalidate_inode_pages2() call inside close_ctree(), because they are dirty. And finally after btrfs_stop_all_workers(), we call iput() on btree inode, which triggers writeback of those dirty metadata. And if the fs is using RAID56 metadata, this will trigger RMW and queue new works into rmw_workers, which is already stopped, causing warning from queue_work() and use-after-free. [FIX] Add a special handling for write_one_eb(), that if the fs is already in an error state, immediately mark the bbio as failure, instead of really submitting them. Then during close_ctree(), iput() will just discard all those dirty tree blocks without really writing them back, thus no more new jobs for already stopped-and-freed workqueues. The extra discard in write_one_eb() also acts as an extra safenet. E.g. the transaction abort is triggered by some extent/free space tree corruptions, and since extent/free space tree is already corrupted some tree blocks may be allocated where they shouldn't be (overwriting existing tree blocks). In that case writing them back will further corrupting the fs.
NVD (es)
En el kernel de Linux, la siguiente vulnerabilidad ha sido resuelta: btrfs: asegurar que no se escriban metadatos sucios para un sistema de archivos con errores [ERROR] Durante el desarrollo de una característica menor (asegurar que todas las llamadas a btrfs_bio::end_io() se realicen en contexto de tarea), noté un fallo en generic/388, donde las escrituras de metadatos activaron nuevos trabajos después de btrfs_stop_all_workers(). Resulta que puede ocurrir incluso sin ninguna modificación de código, solo usando RAID5 para metadatos y la misma carga de trabajo de generic/388 va a activar el uso después de liberación. [CAUSA] Si btrfs encuentra un error, el sistema de archivos se marca como error, no se permite ninguna nueva transacción, por lo tanto, los metadatos están en un estado congelado. Pero hay algunas modificaciones de metadatos antes de ese error, y todavía están en la caché de páginas de inodos del btree. Dado que no habrá una confirmación de transacción real, todos esos folios sucios se mantienen tal cual en la caché de páginas, y no pueden ser invalidados por la llamada a invalidate_inode_pages2() dentro de close_ctree(), porque están sucios. Y finalmente después de btrfs_stop_all_workers(), llamamos a iput() en el inodo del btree, lo que activa la escritura de esos metadatos sucios. Y si el sistema de archivos está usando metadatos RAID56, esto activará RMW y encolará nuevos trabajos en rmw_workers, que ya está detenido, causando una advertencia de queue_work() y uso después de liberación. [SOLUCIÓN] Añadir un manejo especial para write_one_eb(), que si el sistema de archivos ya está en un estado de error, marque inmediatamente el bbio como fallo, en lugar de realmente enviarlos. Luego, durante close_ctree(), iput() simplemente descartará todos esos bloques de árbol sucios sin realmente escribirlos de vuelta, por lo tanto, no más trabajos nuevos para las colas de trabajo ya detenidas y liberadas. El descarte adicional en write_one_eb() también actúa como una red de seguridad adicional. Por ejemplo, la anulación de la transacción es activada por algunas corrupciones del árbol de extensiones/espacio libre, y dado que el árbol de extensiones/espacio libre ya está corrupto, algunos bloques de árbol pueden ser asignados donde no deberían (sobrescribiendo bloques de árbol existentes). En ese caso, escribirlos de vuelta corromperá aún más el sistema de archivos.
References
Sources
| Source | Record | Licence |
|---|---|---|
| CVE List | CVE-2025-40303 changed 2026-08-05 | CVE Terms of Use |
| NVD | CVE-2025-40303 changed 2026-10-14 | Public domain |