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

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