fs/buffer: add alert in try_to_free_buffers() for folios without buffers
PATCH 1/1 fs/buffer: add alert in try_to_free_buffers() for folios without buffers
try_to_free_buffers() can be called on folios with no buffers attached when filemap_release_folio() is invoked on a folio belonging to a mapping with AS_RELEASE_ALWAYS set but no release_folio operation defined.
In such cases, folio_needs_release() returns true because of the AS_RELEASE_ALWAYS flag, but the folio has no private buffer data. This causes try_to_free_buffers() to call drop_buffers() on a folio with no buffers, leading to a null pointer dereference.
Adding a check in try_to_free_buffers() to return early if the folio has no buffers attached, with WARN_ON_ONCE() to alert about the misconfiguration. This provides defensive hardening.
--- fs/buffer.c | 4 ++++ 1 file changed, 4 insertions(+)
fs/buffer.c +4 -0
@@ -2948,6 +2948,10 @@ bool try_to_free_buffers(struct folio *folio)if (folio_test_writeback (folio))return false; +/* Misconfigured folio check */ +if (WARN_ON_ONCE (!folio_buffers (folio))) +return false; +if (mapping ==NULL ) {/* can this still happen? */ ret =drop_buffers (folio, &buffers_to_free); goto out;
Signature · 1 line
2.52.0
Jan Kara
reviewerQuoted 23 lines · Deepakkumar Karn
try_to_free_buffers()can be called on folios with no buffers attached whenfilemap_release_folio()is invoked on a folio belonging to a mapping withAS_RELEASE_ALWAYSset but norelease_foliooperation defined.In such cases,
folio_needs_release()returns true because of theAS_RELEASE_ALWAYSflag, but the folio has no private buffer data. This causestry_to_free_buffers()to calldrop_buffers()on a folio with no buffers, leading to a null pointer dereference.Adding a check in
try_to_free_buffers()to return early if the folio has no buffers attached, withWARN_ON_ONCE()to alert about the misconfiguration. This provides defensive hardening.Signed-off-by Deepakkumar Karn <[email protected]>--- fs/buffer.c | 4 ++++ 1 file changed, 4 insertions(+)fs/buffer.c +0
-0
fs/buffer.c bool try_to_free_buffers(struct folio *folio) · line 2953
Quoted 6 lines
fs/buffer.c +3
-0@@ -2948,6 +2948,10 @@ bool try_to_free_buffers(struct folio *folio)if (folio_test_writeback (folio))return false; +/* Misconfigured folio check */ +if (WARN_ON_ONCE (!folio_buffers (folio))) +return false;
This should really be returning true. Otherwise the folio will never get released.
Honza
Quoted 7 lines
fs/buffer.c +1
-0+if (mapping ==NULL ) {/* can this still happen? */ ret =drop_buffers (folio, &buffers_to_free); goto out;Signature · 1 line
2.52.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
PATCH v2 1/1 fs/buffer: add alert in try_to_free_buffers() for folios without buffers
try_to_free_buffers() can be called on folios with no buffers attached when filemap_release_folio() is invoked on a folio belonging to a mapping with AS_RELEASE_ALWAYS set but no release_folio operation defined.
In such cases, folio_needs_release() returns true because of the AS_RELEASE_ALWAYS flag, but the folio has no private buffer data. This causes try_to_free_buffers() to call drop_buffers() on a folio with no buffers, leading to a null pointer dereference.
Adding a check in try_to_free_buffers() to return early if the folio has no buffers attached, with WARN_ON_ONCE() to alert about the misconfiguration. This provides defensive hardening.
--- fs/buffer.c | 4 ++++ 1 file changed, 4 insertions(+)
fs/buffer.c +4 -0
@@ -2948,6 +2948,10 @@ bool try_to_free_buffers(struct folio *folio)if (folio_test_writeback (folio))return false; +/* Misconfigured folio check */ +if (WARN_ON_ONCE (!folio_buffers (folio))) +return true; +if (mapping ==NULL ) {/* can this still happen? */ ret =drop_buffers (folio, &buffers_to_free); goto out;
Signature · 1 line
2.52.0
Quoted 9 lines · Deepakkumar Karn
try_to_free_buffers()can be called on folios with no buffers attached whenfilemap_release_folio()is invoked on a folio belonging to a mapping withAS_RELEASE_ALWAYSset but norelease_foliooperation defined.In such cases,
folio_needs_release()returns true because of theAS_RELEASE_ALWAYSflag, but the folio has no private buffer data. This causestry_to_free_buffers()to calldrop_buffers()on a folio with no buffers, leading to a null pointer dereference.
Quoted 5 lines
Adding a check in
try_to_free_buffers()to return early if the folio has no buffers attached, withWARN_ON_ONCE()to alert about the misconfiguration. This provides defensive hardening.Signed-off-by Deepakkumar Karn <[email protected]>
Looks good to me now. Feel free to add:
Honza
Quoted 22 lines
--- fs/buffer.c | 4 ++++ 1 file changed, 4 insertions(+)fs/buffer.c +4
-0@@ -2948,6 +2948,10 @@ bool try_to_free_buffers(struct folio *folio)if (folio_test_writeback (folio))return false; +/* Misconfigured folio check */ +if (WARN_ON_ONCE (!folio_buffers (folio))) +return true; +if (mapping ==NULL ) {/* can this still happen? */ ret =drop_buffers (folio, &buffers_to_free); goto out;Signature · 1 line
2.52.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 10 lines · Deepakkumar Karn
try_to_free_buffers()can be called on folios with no buffers attached whenfilemap_release_folio()is invoked on a folio belonging to a mapping withAS_RELEASE_ALWAYSset but norelease_foliooperation defined.In such cases,
folio_needs_release()returns true because of theAS_RELEASE_ALWAYSflag, but the folio has no private buffer data. This causestry_to_free_buffers()to calldrop_buffers()on a folio with no buffers, leading to a null pointer dereference.[...]
Applied to the vfs-7.0.misc branch of the vfs/vfs.git tree. Patches in the vfs-7.0.misc branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs-7.0.misc
[1/1] fs/buffer: add alert in try_to_free_buffers() for folios without buffers
https://git.kernel.org/vfs/vfs/c/b68f91ef3b3f
Quoted 10 lines · Christian Brauner
Quoted 9 lines · Deepakkumar Karn
try_to_free_buffers()can be called on folios with no buffers attached whenfilemap_release_folio()is invoked on a folio belonging to a mapping withAS_RELEASE_ALWAYSset but norelease_foliooperation defined.In such cases,
folio_needs_release()returns true because of theAS_RELEASE_ALWAYSflag, but the folio has no private buffer data. This causestry_to_free_buffers()to calldrop_buffers()on a folio with no buffers, leading to a null pointer dereference.
Quoted 4 lines
Quoted 1 line
[...]
Applied to the vfs-7.0.misc branch of the vfs/vfs.git tree. Patches in the vfs-7.0.misc branch should appear in linux-next soon.
No, this is the wrong fix. Please drop.
Jan Kara
reviewerQuoted 11 lines · Matthew Wilcox
Quoted 10 lines · Christian Brauner
Quoted 9 lines · Deepakkumar Karn
try_to_free_buffers()can be called on folios with no buffers attached whenfilemap_release_folio()is invoked on a folio belonging to a mapping withAS_RELEASE_ALWAYSset but norelease_foliooperation defined.In such cases,
folio_needs_release()returns true because of theAS_RELEASE_ALWAYSflag, but the folio has no private buffer data. This causestry_to_free_buffers()to calldrop_buffers()on a folio with no buffers, leading to a null pointer dereference.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
[...]
Applied to the vfs-7.0.misc branch of the vfs/vfs.git tree. Patches in the vfs-7.0.misc branch should appear in linux-next soon.
No, this is the wrong fix. Please drop.
Nobody says this is a fix (it has WARN_ON() in it after all). But it's a sensible hardening regardless of other changes we do in this area. After our discussion I agree that the change to mapping_set_release_always() is not needed if we instead rework how .release_folio (and try_to_free_buffers()) is called but this is a separate change...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR