nilfs2: prevent general protection fault in nilfs_clear_dirty_page()
authorRyusuke Konishi <konishi.ryusuke@gmail.com>
Mon, 12 Jun 2023 02:14:56 +0000 (11:14 +0900)
committerAndrew Morton <akpm@linux-foundation.org>
Mon, 19 Jun 2023 20:19:35 +0000 (13:19 -0700)
In a syzbot stress test that deliberately causes file system errors on
nilfs2 with a corrupted disk image, it has been reported that
nilfs_clear_dirty_page() called from nilfs_clear_dirty_pages() can cause a
general protection fault.

In nilfs_clear_dirty_pages(), when looking up dirty pages from the page
cache and calling nilfs_clear_dirty_page() for each dirty page/folio
retrieved, the back reference from the argument page to "mapping" may have
been changed to NULL (and possibly others).  It is necessary to check this
after locking the page/folio.

So, fix this issue by not calling nilfs_clear_dirty_page() on a page/folio
after locking it in nilfs_clear_dirty_pages() if the back reference
"mapping" from the page/folio is different from the "mapping" that held
the page/folio just before.

Link: https://lkml.kernel.org/r/20230612021456.3682-1-konishi.ryusuke@gmail.com
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@gmail.com>
Reported-by: syzbot+53369d11851d8f26735c@syzkaller.appspotmail.com
Closes: https://lkml.kernel.org/r/000000000000da4f6b05eb9bf593@google.com
Tested-by: Ryusuke Konishi <konishi.ryusuke@gmail.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
fs/nilfs2/page.c

index 5cf30827f244c42d2a201cc2d85c815655cbbed1..b4e54d079b7d030aeb0a46af9a5b18ae4bc7b4ba 100644 (file)
@@ -370,7 +370,15 @@ void nilfs_clear_dirty_pages(struct address_space *mapping, bool silent)
                        struct folio *folio = fbatch.folios[i];
 
                        folio_lock(folio);
-                       nilfs_clear_dirty_page(&folio->page, silent);
+
+                       /*
+                        * This folio may have been removed from the address
+                        * space by truncation or invalidation when the lock
+                        * was acquired.  Skip processing in that case.
+                        */
+                       if (likely(folio->mapping == mapping))
+                               nilfs_clear_dirty_page(&folio->page, silent);
+
                        folio_unlock(folio);
                }
                folio_batch_release(&fbatch);