Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

nilfs2: fix null-ptr-deref in block_dirty_buffer tracepoint

When using the "block:block_dirty_buffer" tracepoint, mark_buffer_dirty()
may cause a NULL pointer dereference, or a general protection fault when
KASAN is enabled.

This happens because, since the tracepoint was added in
mark_buffer_dirty(), it references the dev_t member bh->b_bdev->bd_dev
regardless of whether the buffer head has a pointer to a block_device
structure.

In the current implementation, nilfs_grab_buffer(), which grabs a buffer
to read (or create) a block of metadata, including b-tree node blocks,
does not set the block device, but instead does so only if the buffer is
not in the "uptodate" state for each of its caller block reading
functions. However, if the uptodate flag is set on a folio/page, and the
buffer heads are detached from it by try_to_free_buffers(), and new buffer
heads are then attached by create_empty_buffers(), the uptodate flag may
be restored to each buffer without the block device being set to
bh->b_bdev, and mark_buffer_dirty() may be called later in that state,
resulting in the bug mentioned above.

Fix this issue by making nilfs_grab_buffer() always set the block device
of the super block structure to the buffer head, regardless of the state
of the buffer's uptodate flag.

Link: https://lkml.kernel.org/r/20241106160811.3316-3-konishi.ryusuke@gmail.com
Fixes: 5305cb830834 ("block: add block_{touch|dirty}_buffer tracepoint")
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@gmail.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: Ubisectech Sirius <bugreport@valiantsec.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Ryusuke Konishi and committed by
Andrew Morton
2026559a cd45e963

+2 -6
-2
fs/nilfs2/btnode.c
··· 68 68 goto failed; 69 69 } 70 70 memset(bh->b_data, 0, i_blocksize(inode)); 71 - bh->b_bdev = inode->i_sb->s_bdev; 72 71 bh->b_blocknr = blocknr; 73 72 set_buffer_mapped(bh); 74 73 set_buffer_uptodate(bh); ··· 132 133 goto found; 133 134 } 134 135 set_buffer_mapped(bh); 135 - bh->b_bdev = inode->i_sb->s_bdev; 136 136 bh->b_blocknr = pblocknr; /* set block address for read */ 137 137 bh->b_end_io = end_buffer_read_sync; 138 138 get_bh(bh);
+1 -3
fs/nilfs2/gcinode.c
··· 83 83 goto out; 84 84 } 85 85 86 - if (!buffer_mapped(bh)) { 87 - bh->b_bdev = inode->i_sb->s_bdev; 86 + if (!buffer_mapped(bh)) 88 87 set_buffer_mapped(bh); 89 - } 90 88 bh->b_blocknr = pbn; 91 89 bh->b_end_io = end_buffer_read_sync; 92 90 get_bh(bh);
-1
fs/nilfs2/mdt.c
··· 89 89 if (buffer_uptodate(bh)) 90 90 goto failed_bh; 91 91 92 - bh->b_bdev = sb->s_bdev; 93 92 err = nilfs_mdt_insert_new_block(inode, block, bh, init_block); 94 93 if (likely(!err)) { 95 94 get_bh(bh);
+1
fs/nilfs2/page.c
··· 63 63 folio_put(folio); 64 64 return NULL; 65 65 } 66 + bh->b_bdev = inode->i_sb->s_bdev; 66 67 return bh; 67 68 } 68 69