Re: [PATCH v3] fs/sysv: Null check to prevent null-ptr-deref bug

From: Christian Brauner
Date: Tue May 30 2023 - 04:26:13 EST


On Sun, May 28, 2023 at 11:44:22AM -0700, Prince Kumar Maurya wrote:
> sb_getblk(inode->i_sb, parent) return a null ptr and taking lock on
> that leads to the null-ptr-deref bug.
>
> Reported-by: syzbot+aad58150cbc64ba41bdc@xxxxxxxxxxxxxxxxxxxxxxxxx
> Closes: https://syzkaller.appspot.com/bug?extid=aad58150cbc64ba41bdc
> Signed-off-by: Prince Kumar Maurya <princekumarmaurya06@xxxxxxxxx>
> ---
> Change since v2: Updated subject and added Reported-by and closes tags.
>
> fs/sysv/itree.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/fs/sysv/itree.c b/fs/sysv/itree.c
> index b22764fe669c..3a6b66e719fd 100644
> --- a/fs/sysv/itree.c
> +++ b/fs/sysv/itree.c
> @@ -145,6 +145,8 @@ static int alloc_branch(struct inode *inode,
> */
> parent = block_to_cpu(SYSV_SB(inode->i_sb), branch[n-1].key);
> bh = sb_getblk(inode->i_sb, parent);
> + if (!bh)
> + break;

When you break here you'll hit:

/* Allocation failed, free what we already allocated */
for (i = 1; i < n; i++)
bforget(branch[i].bh);
for (i = 0; i < n; i++)
sysv_free_block(inode->i_sb, branch[i].key);

below. The cleanup paths were coded in the assumption that sb_getblk()
can't fail. So bforget() can assume that branch[i].bh has been allocated
and set up. So that bforget(branch[i].bh) is your next pending NULL
deref afaict.