nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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