Skip to content

Commit 68aa9ab

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 44b379a commit 68aa9ab

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
@@ -527,6 +527,7 @@ static int nilfs_ioctl_get_bdescs(struct inode *inode, struct file *filp,
527527
* Return: 0 on success, or one of the following negative error codes on
528528
* failure:
529529
* * %-EEXIST - Block conflict detected.
530+
* * %-EINVAL - Invalid virtual block descriptor.
530531
* * %-EIO - I/O error.
531532
* * %-ENOENT - Requested block doesn't exist.
532533
* * %-ENOMEM - Insufficient memory available.
@@ -536,15 +537,30 @@ static int nilfs_ioctl_move_inode_block(struct inode *inode,
536537
struct list_head *buffers)
537538
{
538539
struct buffer_head *bh;
540+
__u64 limit_blkidx = (__u64)inode->i_sb->s_maxbytes >> inode->i_blkbits;
539541
int ret;
540542

541-
if (vdesc->vd_flags == 0)
543+
/*
544+
* vblocknr 0 is reserved as an invalid pointer. Also, limit_blkidx
545+
* ensures that the page index converted from vd_vblocknr never
546+
* overflows the page cache limit and respects the architecture's bmap
547+
* key width.
548+
*/
549+
if (unlikely(vdesc->vd_vblocknr == 0 ||
550+
vdesc->vd_vblocknr >= limit_blkidx))
551+
return -EINVAL;
552+
553+
if (vdesc->vd_flags == 0) {
554+
if (unlikely(vdesc->vd_offset >= limit_blkidx))
555+
return -EINVAL;
556+
542557
ret = nilfs_gccache_submit_read_data(
543558
inode, vdesc->vd_offset, vdesc->vd_blocknr,
544559
vdesc->vd_vblocknr, &bh);
545-
else
560+
} else {
546561
ret = nilfs_gccache_submit_read_node(
547562
inode, vdesc->vd_blocknr, vdesc->vd_vblocknr, &bh);
563+
}
548564

549565
if (unlikely(ret < 0)) {
550566
if (ret == -ENOENT)

0 commit comments

Comments
 (0)