jfs: jfs_dmap: Validate db_l2nbperpage while mounting

Message ID 20230605140151.635604-1-code@siddh.me
State New
Headers
Series jfs: jfs_dmap: Validate db_l2nbperpage while mounting |

Commit Message

Siddh Raman Pant June 5, 2023, 2:01 p.m. UTC
  In jfs_dmap.c at line 381, BLKTODMAP is used to get a logical block
number inside dbFree(). db_l2nbperpage, which is the log2 number of
blocks per page, is passed as an argument to BLKTODMAP which uses it
for shifting.

Syzbot reported a shift out-of-bounds crash because db_l2nbperpage is
too big. This happens because the large value is set without any
validation in dbMount() at line 181.

Thus, make sure that db_l2nbperpage is correct while mounting.

Reported-and-tested-by: syzbot+d2cd27dcf8e04b232eb2@syzkaller.appspotmail.com
Link: https://syzkaller.appspot.com/bug?id=2a70a453331db32ed491f5cbb07e81bf2d225715
Cc: stable@vger.kernel.org
Signed-off-by: Siddh Raman Pant <code@siddh.me>
---
 fs/jfs/jfs_dmap.c | 6 ++++++
 1 file changed, 6 insertions(+)
  

Comments

Dave Kleikamp June 16, 2023, 10:16 p.m. UTC | #1
On 6/5/23 9:01AM, Siddh Raman Pant wrote:
> In jfs_dmap.c at line 381, BLKTODMAP is used to get a logical block
> number inside dbFree(). db_l2nbperpage, which is the log2 number of
> blocks per page, is passed as an argument to BLKTODMAP which uses it
> for shifting.
> 
> Syzbot reported a shift out-of-bounds crash because db_l2nbperpage is
> too big. This happens because the large value is set without any
> validation in dbMount() at line 181.
> 
> Thus, make sure that db_l2nbperpage is correct while mounting.
> 
> Reported-and-tested-by: syzbot+d2cd27dcf8e04b232eb2@syzkaller.appspotmail.com
> Link: https://syzkaller.appspot.com/bug?id=2a70a453331db32ed491f5cbb07e81bf2d225715
> Cc: stable@vger.kernel.org
> Signed-off-by: Siddh Raman Pant <code@siddh.me>
> ---
>   fs/jfs/jfs_dmap.c | 6 ++++++
>   1 file changed, 6 insertions(+)
> 
> diff --git a/fs/jfs/jfs_dmap.c b/fs/jfs/jfs_dmap.c
> index a3eb1e826947..62f058822a3a 100644
> --- a/fs/jfs/jfs_dmap.c
> +++ b/fs/jfs/jfs_dmap.c
> @@ -178,7 +178,13 @@ int dbMount(struct inode *ipbmap)
>   	dbmp_le = (struct dbmap_disk *) mp->data;
>   	bmp->db_mapsize = le64_to_cpu(dbmp_le->dn_mapsize);
>   	bmp->db_nfree = le64_to_cpu(dbmp_le->dn_nfree);
> +
>   	bmp->db_l2nbperpage = le32_to_cpu(dbmp_le->dn_l2nbperpage);
> +	if (bmp->db_l2nbperpage > L2MAXL0SIZE) {

Actually the sanity check should be much smaller. The maximum value 
should be L2PSIZE - log2(MINBLOCKSIZE). In reality, I think it's always 
zero.

Shaggy

> +		err = -EINVAL;
> +		goto err_release_metapage;
> +	}
> +
>   	bmp->db_numag = le32_to_cpu(dbmp_le->dn_numag);
>   	if (!bmp->db_numag) {
>   		err = -EINVAL;
  
Siddh Raman Pant June 19, 2023, 12:02 p.m. UTC | #2
On Sat, 17 Jun 2023 03:46:09 +0530, Dave Kleikamp wrote:
> Actually the sanity check should be much smaller. The maximum value
> should be L2PSIZE - log2(MINBLOCKSIZE). In reality, I think it's always
> zero.

Thanks for the review, will amend the patch and send.

Thanks,
Siddh
  

Patch

diff --git a/fs/jfs/jfs_dmap.c b/fs/jfs/jfs_dmap.c
index a3eb1e826947..62f058822a3a 100644
--- a/fs/jfs/jfs_dmap.c
+++ b/fs/jfs/jfs_dmap.c
@@ -178,7 +178,13 @@  int dbMount(struct inode *ipbmap)
 	dbmp_le = (struct dbmap_disk *) mp->data;
 	bmp->db_mapsize = le64_to_cpu(dbmp_le->dn_mapsize);
 	bmp->db_nfree = le64_to_cpu(dbmp_le->dn_nfree);
+
 	bmp->db_l2nbperpage = le32_to_cpu(dbmp_le->dn_l2nbperpage);
+	if (bmp->db_l2nbperpage > L2MAXL0SIZE) {
+		err = -EINVAL;
+		goto err_release_metapage;
+	}
+
 	bmp->db_numag = le32_to_cpu(dbmp_le->dn_numag);
 	if (!bmp->db_numag) {
 		err = -EINVAL;