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

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