Skip to content

Commit 3bd064c

Browse files
konisgregkh
authored andcommitted
nilfs2: reject invalid block index in GC ioctl
[ Upstream commit a1735ea ] Syzbot reported list corruption caused by a double list_add_tail() call on bh->b_assoc_buffers within nilfs_lookup_dirty_data_buffers(). Analysis revealed that the root cause was the insertion of a page/folio with a page index of ULONG_MAX into the page cache via the GC ioctl. filemap_get_folios_tag(), called by nilfs_lookup_dirty_data_buffers(), repeatedly detects a dirty folio with a page index of ULONG_MAX due to index wrap-around, leading to duplicate processing of dirty buffers. As a preparatory step, the GC ioctl loads the page/folio of the block to be moved during GC and inserts it into the page cache based on information in the nilfs_vdesc structure passed as an argument. Normally, this does not cause issues because the user-space GC library configures the nilfs_vdesc structure properly. However, since there is no range check on the parameters determining the page index, a request with artificially crafted parameters -- such as those generated by Syzbot -- can result in a page/folio being inserted with a page index of ULONG_MAX, triggering the above problem. This resolves the issue by checking the ranges of 'vd_offset' and 'vd_vblocknr' in the nilfs_vdesc structure that determine the page index, thereby preventing the invalid page/folio insertions. Reported-by: syzbot+c37bed40868932d790e9@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=c37bed40868932d790e9 Fixes: 7942b91 ("nilfs2: ioctl operations") Cc: wuyankun <wuyankun@uniontech.com> Cc: stable@vger.kernel.org Signed-off-by: Ryusuke Konishi <konishi.ryusuke@gmail.com> Signed-off-by: Viacheslav Dubeyko <slava@dubeyko.com> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 0f2606a commit 3bd064c

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

fs/nilfs2/ioctl.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ static int nilfs_ioctl_get_bdescs(struct inode *inode, struct file *filp,
513513
* Return: 0 on success, or one of the following negative error codes on
514514
* failure:
515515
* * %-EEXIST - Block conflict detected.
516+
* * %-EINVAL - Invalid virtual block descriptor.
516517
* * %-EIO - I/O error.
517518
* * %-ENOENT - Requested block doesn't exist.
518519
* * %-ENOMEM - Insufficient memory available.
@@ -522,15 +523,30 @@ static int nilfs_ioctl_move_inode_block(struct inode *inode,
522523
struct list_head *buffers)
523524
{
524525
struct buffer_head *bh;
526+
__u64 limit_blkidx = (__u64)inode->i_sb->s_maxbytes >> inode->i_blkbits;
525527
int ret;
526528

527-
if (vdesc->vd_flags == 0)
529+
/*
530+
* vblocknr 0 is reserved as an invalid pointer. Also, limit_blkidx
531+
* ensures that the page index converted from vd_vblocknr never
532+
* overflows the page cache limit and respects the architecture's bmap
533+
* key width.
534+
*/
535+
if (unlikely(vdesc->vd_vblocknr == 0 ||
536+
vdesc->vd_vblocknr >= limit_blkidx))
537+
return -EINVAL;
538+
539+
if (vdesc->vd_flags == 0) {
540+
if (unlikely(vdesc->vd_offset >= limit_blkidx))
541+
return -EINVAL;
542+
528543
ret = nilfs_gccache_submit_read_data(
529544
inode, vdesc->vd_offset, vdesc->vd_blocknr,
530545
vdesc->vd_vblocknr, &bh);
531-
else
546+
} else {
532547
ret = nilfs_gccache_submit_read_node(
533548
inode, vdesc->vd_blocknr, vdesc->vd_vblocknr, &bh);
549+
}
534550

535551
if (unlikely(ret < 0)) {
536552
if (ret == -ENOENT)

0 commit comments

Comments
 (0)