]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
nilfs2: protect access to buffers with no active references
authorRyusuke Konishi <konishi.ryusuke@gmail.com>
Fri, 7 Feb 2025 14:23:49 +0000 (23:23 +0900)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 13 Mar 2025 11:50:45 +0000 (12:50 +0100)
commit 367a9bffabe08c04f6d725032cce3d891b2b9e1a upstream.

nilfs_lookup_dirty_data_buffers(), which iterates through the buffers
attached to dirty data folios/pages, accesses the attached buffers without
locking the folios/pages.

For data cache, nilfs_clear_folio_dirty() may be called asynchronously
when the file system degenerates to read only, so
nilfs_lookup_dirty_data_buffers() still has the potential to cause use
after free issues when buffers lose the protection of their dirty state
midway due to this asynchronous clearing and are unintentionally freed by
try_to_free_buffers().

Eliminate this race issue by adjusting the lock section in this function.

[konishi.ryusuke@gmail.com: adjusted for page/folio conversion]
Link: https://lkml.kernel.org/r/20250107200202.6432-3-konishi.ryusuke@gmail.com
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@gmail.com>
Fixes: 8c26c4e2694a ("nilfs2: fix issue with flush kernel thread after remount in RO mode because of driver's internal error or metadata corruption")
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/nilfs2/segment.c

index 75fd6e86f18a337d0c496f5f8a983b2dab2d238b..2f778234ecf2ce9525b5ad92fd8a02f2a12e8aa8 100644 (file)
@@ -732,7 +732,6 @@ static size_t nilfs_lookup_dirty_data_buffers(struct inode *inode,
                }
                if (!page_has_buffers(page))
                        create_empty_buffers(page, i_blocksize(inode), 0);
-               unlock_page(page);
 
                bh = head = page_buffers(page);
                do {
@@ -742,11 +741,14 @@ static size_t nilfs_lookup_dirty_data_buffers(struct inode *inode,
                        list_add_tail(&bh->b_assoc_buffers, listp);
                        ndirties++;
                        if (unlikely(ndirties >= nlimit)) {
+                               unlock_page(page);
                                pagevec_release(&pvec);
                                cond_resched();
                                return ndirties;
                        }
                } while (bh = bh->b_this_page, bh != head);
+
+               unlock_page(page);
        }
        pagevec_release(&pvec);
        cond_resched();