Blame SOURCES/0243-fs-btrfs-Fix-several-fuzz-issues-with-invalid-dir-it.patch

fd0330
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
fd0330
From: Darren Kenny <darren.kenny@oracle.com>
fd0330
Date: Tue, 29 Mar 2022 10:49:56 +0000
fd0330
Subject: [PATCH] fs/btrfs: Fix several fuzz issues with invalid dir item
fd0330
 sizing
fd0330
fd0330
According to the btrfs code in Linux, the structure of a directory item
fd0330
leaf should be of the form:
fd0330
fd0330
  |struct btrfs_dir_item|name|data|
fd0330
fd0330
in GRUB the name len and data len are in the grub_btrfs_dir_item
fd0330
structure's n and m fields respectively.
fd0330
fd0330
The combined size of the structure, name and data should be less than
fd0330
the allocated memory, a difference to the Linux kernel's struct
fd0330
btrfs_dir_item is that the grub_btrfs_dir_item has an extra field for
fd0330
where the name is stored, so we adjust for that 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 6d3f06c0b6a8992b9b1bb0e62af93ac5ff2781f0)
fd0330
[rharwood: we've an extra variable here]
fd0330
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
fd0330
---
fd0330
 grub-core/fs/btrfs.c | 26 ++++++++++++++++++++++++++
fd0330
 1 file changed, 26 insertions(+)
fd0330
fd0330
diff --git a/grub-core/fs/btrfs.c b/grub-core/fs/btrfs.c
fd0330
index 07c0ff874b..2fcfb738fe 100644
fd0330
--- a/grub-core/fs/btrfs.c
fd0330
+++ b/grub-core/fs/btrfs.c
fd0330
@@ -2254,6 +2254,7 @@ grub_btrfs_dir (grub_device_t device, const char *path,
fd0330
   grub_uint64_t tree;
fd0330
   grub_uint8_t type;
fd0330
   char *new_path = NULL;
fd0330
+  grub_size_t est_size = 0;
fd0330
 
fd0330
   if (!data)
fd0330
     return grub_errno;
fd0330
@@ -2320,6 +2321,18 @@ grub_btrfs_dir (grub_device_t device, const char *path,
fd0330
 	  break;
fd0330
 	}
fd0330
 
fd0330
+      if (direl == NULL ||
fd0330
+	  grub_add (grub_le_to_cpu16 (direl->n),
fd0330
+		    grub_le_to_cpu16 (direl->m), &est_size) ||
fd0330
+	  grub_add (est_size, sizeof (*direl), &est_size) ||
fd0330
+	  grub_sub (est_size, sizeof (direl->name), &est_size) ||
fd0330
+	  est_size > allocated)
fd0330
+       {
fd0330
+         grub_errno = GRUB_ERR_OUT_OF_RANGE;
fd0330
+         r = -grub_errno;
fd0330
+         goto out;
fd0330
+       }
fd0330
+
fd0330
       for (cdirel = direl;
fd0330
 	   (grub_uint8_t *) cdirel - (grub_uint8_t *) direl
fd0330
 	   < (grub_ssize_t) elemsize;
fd0330
@@ -2330,6 +2343,19 @@ grub_btrfs_dir (grub_device_t device, const char *path,
fd0330
 	  char c;
fd0330
 	  struct grub_btrfs_inode inode;
fd0330
 	  struct grub_dirhook_info info;
fd0330
+
fd0330
+	  if (cdirel == NULL ||
fd0330
+	      grub_add (grub_le_to_cpu16 (cdirel->n),
fd0330
+			grub_le_to_cpu16 (cdirel->m), &est_size) ||
fd0330
+	      grub_add (est_size, sizeof (*cdirel), &est_size) ||
fd0330
+	      grub_sub (est_size, sizeof (cdirel->name), &est_size) ||
fd0330
+	      est_size > allocated)
fd0330
+	   {
fd0330
+	     grub_errno = GRUB_ERR_OUT_OF_RANGE;
fd0330
+	     r = -grub_errno;
fd0330
+	     goto out;
fd0330
+	   }
fd0330
+
fd0330
 	  err = grub_btrfs_read_inode (data, &inode, cdirel->key.object_id,
fd0330
 				       tree);
fd0330
 	  grub_memset (&info, 0, sizeof (info));