nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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