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

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