jfs: handle NULL folio->mapping in metapage_write_one() and release_metapage()
Subsystems (2)
PATCH 1/1 jfs: handle NULL folio->mapping in metapage_write_one() and release_metapage()
syzbot reported a NULL pointer dereference in metapage_write_folio() called from metapage_write_one() during lazy commit teardown:
KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] RIP: 0010:metapage_write_folio+0xa7/0xcf0 fs/jfs/jfs_metapage.c:428 Call Trace: metapage_write_one+0x2c8/0x520 fs/jfs/jfs_metapage.c:818 release_metapage+0x5b9/0xab0 fs/jfs/jfs_metapage.c:884 txUnlock+0x509/0xdc0 fs/jfs/jfs_txnmgr.c:948 jfs_lazycommit+0x52c/0xa10 fs/jfs/jfs_txnmgr.c:2734
During unmount or inode eviction, truncate_inode_pages_final() can remove a folio from its address_space (setting folio->mapping to NULL) while jfsCommit still holds a reference to a dirty metapage on that folio. When jfsCommit subsequently runs txUnlock() -> release_metapage():
1. If META_sync is set, metapage_write_one() proceeds with a NULL mapping and calls metapage_write_folio(), which dereferences folio->mapping->host (and filemap_check_errors(mapping) would also dereference NULL).
2. If META_dirty is set without META_sync, folio_mark_dirty() is a no-op on an unmapped folio, leaving META_dirty set and the metapage stranded on log->synclist; drop_metapage() then skips freeing it and jfs_flush_journal() stalls waiting for log->synclist to empty.
Fix both paths under the folio lock:
- In metapage_write_one(), if !mapping, clear META_dirty and call remove_from_logsync() on any attached metapages, clear the folio dirty state, unlock the folio, and return 0.
- In release_metapage(), if META_dirty is set on an unmapped folio, clear META_dirty and META_sync and remove the metapage from logsync so drop_metapage() can release it cleanly.
Tested locally in QEMU (x86_64, 4 CPUs, KASAN+UBSAN) using syzbot's C reproducer with per-iteration mount namespaces: crashed in <1s before the patch, and completed 56 mount/remount/unmount iterations in 15s with zero hangs, KASAN, Oops, or synclist warnings after the patch.
--- fs/jfs/jfs_metapage.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-)
fs/jfs/jfs_metapage.c +29 -5
@@ -812,6 +812,23 @@ static int metapage_write_one(struct folio *folio)BUG_ON (!folio_test_locked (folio)); +if (unlikely (!mapping)) { +struct metapage *mp; +unsigned int offset; + +for (offset =0 ; offset <PAGE_SIZE ; offset +=PSIZE ) { + mp =folio_to_mp (folio, offset); +if (!mp) +continue ; +clear_bit (META_dirty, &mp->flag); +if (mp->lsn) +remove_from_logsync (mp); + } +folio_clear_dirty_for_io (folio); +folio_unlock (folio); +return 0 ; + } +folio_wait_writeback (folio);if (folio_clear_dirty_for_io (folio)) {
@@ -879,12 +896,19 @@ void release_metapage(struct metapage * mp) }if (test_bit (META_dirty, &mp->flag)) { -folio_mark_dirty (folio); -if (test_bit (META_sync, &mp->flag)) { +if (unlikely (!folio->mapping)) { +clear_bit (META_dirty, &mp->flag);clear_bit (META_sync, &mp->flag); -if (metapage_write_one (folio)) -jfs_error (mp->sb,"metapage_write_one() failed\n" ); -folio_lock (folio); +if (mp->lsn) +remove_from_logsync (mp); + }else { +folio_mark_dirty (folio); +if (test_bit (META_sync, &mp->flag)) { +clear_bit (META_sync, &mp->flag); +if (metapage_write_one (folio)) +jfs_error (mp->sb,"metapage_write_one() failed\n" ); +folio_lock (folio); + } } }else if (mp->lsn)/* discard_metapage doesn't remove it */ remove_from_logsync (mp);
Signature · 1 line
2.53.0