nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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