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

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