Blame SOURCES/0245-fs-btrfs-Fix-more-fuzz-issues-related-to-chunks.patch

fd0330
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
fd0330
From: Darren Kenny <darren.kenny@oracle.com>
fd0330
Date: Thu, 7 Apr 2022 15:18:12 +0000
fd0330
Subject: [PATCH] fs/btrfs: Fix more fuzz issues related to chunks
fd0330
fd0330
The corpus we generating issues in grub_btrfs_read_logical() when
fd0330
attempting to iterate over nstripes entries in the boot mapping.
fd0330
fd0330
In most cases the reason for the failure was that the number of strips
fd0330
exceeded the possible space statically allocated in superblock bootmapping
fd0330
space. Each stripe entry in the bootmapping block consists of
fd0330
a grub_btrfs_key followed by a grub_btrfs_chunk_stripe.
fd0330
fd0330
Another issue that came up was that while calculating the chunk size,
fd0330
in an earlier piece of code in that function, depending on the data
fd0330
provided in the btrfs file system, it would end up calculating a size
fd0330
that was too small to contain even 1 grub_btrfs_chunk_item, which is
fd0330
obviously invalid too.
fd0330
fd0330
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
fd0330
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
fd0330
(cherry picked from commit e00cd76cbadcc897a9cc4087cb2fcb5dbe15e596)
fd0330
---
fd0330
 grub-core/fs/btrfs.c | 24 ++++++++++++++++++++++++
fd0330
 1 file changed, 24 insertions(+)
fd0330
fd0330
diff --git a/grub-core/fs/btrfs.c b/grub-core/fs/btrfs.c
fd0330
index 0e9b450413..47325f6ad7 100644
fd0330
--- a/grub-core/fs/btrfs.c
fd0330
+++ b/grub-core/fs/btrfs.c
fd0330
@@ -947,6 +947,17 @@ grub_btrfs_read_logical (struct grub_btrfs_data *data, grub_disk_addr_t addr,
fd0330
 	  return grub_error (GRUB_ERR_BAD_FS,
fd0330
 			     "got an invalid zero-size chunk");
fd0330
 	}
fd0330
+
fd0330
+      /*
fd0330
+       * The space being allocated for a chunk should at least be able to
fd0330
+       * contain one chunk item.
fd0330
+       */
fd0330
+      if (chsize < sizeof (struct grub_btrfs_chunk_item))
fd0330
+       {
fd0330
+         grub_dprintf ("btrfs", "chunk-size too small\n");
fd0330
+         return grub_error (GRUB_ERR_BAD_FS,
fd0330
+                            "got an invalid chunk size");
fd0330
+       }
fd0330
       chunk = grub_malloc (chsize);
fd0330
       if (!chunk)
fd0330
 	return grub_errno;
fd0330
@@ -1194,6 +1205,13 @@ grub_btrfs_read_logical (struct grub_btrfs_data *data, grub_disk_addr_t addr,
fd0330
 	if (csize > (grub_uint64_t) size)
fd0330
 	  csize = size;
fd0330
 
fd0330
+	/*
fd0330
+	 * The space for a chunk stripe is limited to the space provide in the super-block's
fd0330
+	 * bootstrap mapping with an initial btrfs key at the start of each chunk.
fd0330
+	 */
fd0330
+	grub_size_t avail_stripes = sizeof (data->sblock.bootstrap_mapping) /
fd0330
+	  (sizeof (struct grub_btrfs_key) + sizeof (struct grub_btrfs_chunk_stripe));
fd0330
+
fd0330
 	for (j = 0; j < 2; j++)
fd0330
 	  {
fd0330
 	    grub_size_t est_chunk_alloc = 0;
fd0330
@@ -1220,6 +1238,12 @@ grub_btrfs_read_logical (struct grub_btrfs_data *data, grub_disk_addr_t addr,
fd0330
 		break;
fd0330
 	      }
fd0330
 
fd0330
+	   if (grub_le_to_cpu16 (chunk->nstripes) > avail_stripes)
fd0330
+             {
fd0330
+               err = GRUB_ERR_BAD_FS;
fd0330
+               break;
fd0330
+             }
fd0330
+
fd0330
 	    if (is_raid56)
fd0330
 	      {
fd0330
 		err = btrfs_read_from_chunk (data, chunk, stripen,