{"id":"CVE-2025-40303","aliases":["CVE-2025-40303"],"related":[],"kind":"vulnerability","state":"published","title":"btrfs: ensure no dirty metadata is written back for an fs with errors","summary":"In the Linux kernel, the following vulnerability has been resolved:\n\nbtrfs: ensure no dirty metadata is written back for an fs with errors\n\n[BUG]\nDuring development of a minor feature (make sure all btrfs_bio::end_io()\nis called in task context), I noticed a crash in generic/388, where\nmetadata writes triggered new works after btrfs_stop_all_workers().\n\nIt turns out that it can even happen without any code modification, just\nusing RAID5 for metadata and the same workload from generic/388 is going\nto trigger the use-after-free.\n\n[CAUSE]\nIf btrfs hits an error, the fs is marked as error, no new\ntransaction is allowed thus metadata is in a frozen state.\n\nBut there are some metadata modifications before that error, and they are\nstill in the btree inode page cache.\n\nSince there will be no real transaction commit, all those dirty folios\nare just kept as is in the page cache, and they can not be invalidated\nby invalidate_inode_pages2() call inside close_ctree(), because they are\ndirty.\n\nAnd finally after btrfs_stop_all_workers(), we call iput() on btree\ninode, which triggers writeback of those dirty metadata.\n\nAnd if the fs is using RAID56 metadata, this will trigger RMW and queue\nnew works into rmw_workers, which is already stopped, causing warning\nfrom queue_work() and use-after-free.\n\n[FIX]\nAdd a special handling for write_one_eb(), that if the fs is already in\nan error state, immediately mark the bbio as failure, instead of really\nsubmitting them.\n\nThen during close_ctree(), iput() will just discard all those dirty\ntree blocks without really writing them back, thus no more new jobs for\nalready stopped-and-freed workqueues.\n\nThe extra discard in write_one_eb() also acts as an extra safenet.\nE.g. the transaction abort is triggered by some extent/free space\ntree corruptions, and since extent/free space tree is already corrupted\nsome tree blocks may be allocated where they shouldn't be (overwriting\nexisting tree blocks). In that case writing them back will further\ncorrupting the fs.","summarySource":"cvelist","descriptions":[{"lang":"en","text":"In the Linux kernel, the following vulnerability has been resolved:\n\nbtrfs: ensure no dirty metadata is written back for an fs with errors\n\n[BUG]\nDuring development of a minor feature (make sure all btrfs_bio::end_io()\nis called in task context), I noticed a crash in generic/388, where\nmetadata writes triggered new works after btrfs_stop_all_workers().\n\nIt turns out that it can even happen without any code modification, just\nusing RAID5 for metadata and the same workload from generic/388 is going\nto trigger the use-after-free.\n\n[CAUSE]\nIf btrfs hits an error, the fs is marked as error, no new\ntransaction is allowed thus metadata is in a frozen state.\n\nBut there are some metadata modifications before that error, and they are\nstill in the btree inode page cache.\n\nSince there will be no real transaction commit, all those dirty folios\nare just kept as is in the page cache, and they can not be invalidated\nby invalidate_inode_pages2() call inside close_ctree(), because they are\ndirty.\n\nAnd finally after btrfs_stop_all_workers(), we call iput() on btree\ninode, which triggers writeback of those dirty metadata.\n\nAnd if the fs is using RAID56 metadata, this will trigger RMW and queue\nnew works into rmw_workers, which is already stopped, causing warning\nfrom queue_work() and use-after-free.\n\n[FIX]\nAdd a special handling for write_one_eb(), that if the fs is already in\nan error state, immediately mark the bbio as failure, instead of really\nsubmitting them.\n\nThen during close_ctree(), iput() will just discard all those dirty\ntree blocks without really writing them back, thus no more new jobs for\nalready stopped-and-freed workqueues.\n\nThe extra discard in write_one_eb() also acts as an extra safenet.\nE.g. the transaction abort is triggered by some extent/free space\ntree corruptions, and since extent/free space tree is already corrupted\nsome tree blocks may be allocated where they shouldn't be (overwriting\nexisting tree blocks). In that case writing them back will further\ncorrupting the fs.","source":"cvelist"},{"lang":"es","text":"En el kernel de Linux, la siguiente vulnerabilidad ha sido resuelta:\n\nbtrfs: asegurar que no se escriban metadatos sucios para un sistema de archivos con errores\n\n[ERROR]\nDurante 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().\n\nResulta 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.\n\n[CAUSA]\nSi 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.\n\nPero hay algunas modificaciones de metadatos antes de ese error, y todavía están en la caché de páginas de inodos del btree.\n\nDado 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.\n\nY 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.\n\nY 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.\n\n[SOLUCIÓN]\nAñ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.\n\nLuego, 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.\n\nEl descarte adicional en write_one_eb() también actúa como una red de seguridad adicional.\nPor 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.","source":"nvd"}],"published":"2025-12-08T00:46:27.820Z","modified":"2026-10-14T22:10:00.210Z","severity":{"scorer":"Linux","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H","score":7.8,"rating":"HIGH"},"severities":[{"scorer":"Linux","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H","score":7.8,"rating":"HIGH"}],"cwes":[],"affected":[{"ranges":[{"type":"git","introduced":"13e6c37b989859e70b0d73d3f2cb0aa022159b17","fixed":"066ee13f05fbd82ada01883e51f0695172f98dff","status":"affected"},{"type":"git","introduced":"13e6c37b989859e70b0d73d3f2cb0aa022159b17","fixed":"e2b3859067bf012d53c49b3f885fef40624a2c83","status":"affected"},{"type":"git","introduced":"13e6c37b989859e70b0d73d3f2cb0aa022159b17","fixed":"54a5b5a15588e3b0b294df31474d08a2678d4291","status":"affected"},{"type":"git","introduced":"13e6c37b989859e70b0d73d3f2cb0aa022159b17","fixed":"2618849f31e7cf51fadd4a5242458501a6d5b315","status":"affected"}],"vendor":"Linux","product":"Linux","repo":"https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git","source":"cvelist"},{"ranges":[{"version":"3.10","status":"affected"},{"type":"semver","introduced":"0","fixed":"3.10","status":"unaffected"},{"type":"semver","introduced":"6.6.117","lastAffected":"6.6.*","status":"unaffected"},{"type":"semver","introduced":"6.12.58","lastAffected":"6.12.*","status":"unaffected"},{"type":"semver","introduced":"6.17.8","lastAffected":"6.17.*","status":"unaffected"},{"type":"original_commit_for_fix","introduced":"6.18","lastAffected":"*","status":"unaffected"}],"vendor":"Linux","product":"Linux","repo":"https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git","source":"cvelist"}],"references":[{"url":"https://git.kernel.org/stable/c/066ee13f05fbd82ada01883e51f0695172f98dff","tags":[],"sources":["cvelist","nvd"]},{"url":"https://git.kernel.org/stable/c/e2b3859067bf012d53c49b3f885fef40624a2c83","tags":[],"sources":["cvelist","nvd"]},{"url":"https://git.kernel.org/stable/c/54a5b5a15588e3b0b294df31474d08a2678d4291","tags":[],"sources":["cvelist","nvd"]},{"url":"https://git.kernel.org/stable/c/2618849f31e7cf51fadd4a5242458501a6d5b315","tags":[],"sources":["cvelist","nvd"]}],"credits":[],"assigner":"Linux","sources":[{"source":"cvelist","name":"CVE List","id":"CVE-2025-40303","url":"https://www.cve.org/CVERecord?id=CVE-2025-40303","licence":"CVE Terms of Use","modified":"2026-08-05T12:09:22.649Z"},{"source":"nvd","name":"NVD","id":"CVE-2025-40303","url":"https://nvd.nist.gov/vuln/detail/CVE-2025-40303","licence":"Public domain","modified":"2026-10-14T22:10:00.210Z"}],"updatedAt":"2026-11-25T22:59:08.395Z","epss":{"score":0.0015,"percentile":0.04358,"date":"2026-12-01T12:00:22.000Z"}}