nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0534-fs-f2fs-Do-not-read-past-the-end-of-nat-journal-entr.patch

0ccc47
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
0ccc47
From: Sudhakar Kuppusamy <sudhakar@linux.ibm.com>
0ccc47
Date: Wed, 6 Apr 2022 18:03:37 +0530
0ccc47
Subject: [PATCH] fs/f2fs: Do not read past the end of nat journal entries
0ccc47
0ccc47
A corrupt f2fs file system could specify a nat journal entry count
0ccc47
that is beyond the maximum NAT_JOURNAL_ENTRIES.
0ccc47
0ccc47
Check if the specified nat journal entry count before accessing the
0ccc47
array, and throw an error if it is too large.
0ccc47
0ccc47
Signed-off-by: Sudhakar Kuppusamy <sudhakar@linux.ibm.com>
0ccc47
Signed-off-by: Daniel Axtens <dja@axtens.net>
0ccc47
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
0ccc47
(cherry picked from commit a3988cb3f0a108dd67ac127a79a4c8479d23334e)
0ccc47
(cherry picked from commit 7125978aa7d6068812ef6da0ab38ce521ae7eba1)
0ccc47
(cherry picked from commit e488538cbf9fc63796c7047550b0598e1ef95c03)
0ccc47
---
0ccc47
 grub-core/fs/f2fs.c | 21 ++++++++++++++-------
0ccc47
 1 file changed, 14 insertions(+), 7 deletions(-)
0ccc47
0ccc47
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
0ccc47
index 1cad2615f3..09dc932420 100644
0ccc47
--- a/grub-core/fs/f2fs.c
0ccc47
+++ b/grub-core/fs/f2fs.c
0ccc47
@@ -632,23 +632,27 @@ get_nat_journal (struct grub_f2fs_data *data)
0ccc47
   return err;
0ccc47
 }
0ccc47
 
0ccc47
-static grub_uint32_t
0ccc47
-get_blkaddr_from_nat_journal (struct grub_f2fs_data *data, grub_uint32_t nid)
0ccc47
+static grub_err_t
0ccc47
+get_blkaddr_from_nat_journal (struct grub_f2fs_data *data, grub_uint32_t nid,
0ccc47
+                              grub_uint32_t *blkaddr)
0ccc47
 {
0ccc47
   grub_uint16_t n = grub_le_to_cpu16 (data->nat_j.n_nats);
0ccc47
-  grub_uint32_t blkaddr = 0;
0ccc47
   grub_uint16_t i;
0ccc47
 
0ccc47
+  if (n >= NAT_JOURNAL_ENTRIES)
0ccc47
+    return grub_error (GRUB_ERR_BAD_FS,
0ccc47
+                       "invalid number of nat journal entries");
0ccc47
+
0ccc47
   for (i = 0; i < n; i++)
0ccc47
     {
0ccc47
       if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
0ccc47
         {
0ccc47
-          blkaddr = grub_le_to_cpu32 (data->nat_j.entries[i].ne.block_addr);
0ccc47
+          *blkaddr = grub_le_to_cpu32 (data->nat_j.entries[i].ne.block_addr);
0ccc47
           break;
0ccc47
         }
0ccc47
     }
0ccc47
 
0ccc47
-  return blkaddr;
0ccc47
+  return GRUB_ERR_NONE;
0ccc47
 }
0ccc47
 
0ccc47
 static grub_uint32_t
0ccc47
@@ -656,10 +660,13 @@ get_node_blkaddr (struct grub_f2fs_data *data, grub_uint32_t nid)
0ccc47
 {
0ccc47
   struct grub_f2fs_nat_block *nat_block;
0ccc47
   grub_uint32_t seg_off, block_off, entry_off, block_addr;
0ccc47
-  grub_uint32_t blkaddr;
0ccc47
+  grub_uint32_t blkaddr = 0;
0ccc47
   grub_err_t err;
0ccc47
 
0ccc47
-  blkaddr = get_blkaddr_from_nat_journal (data, nid);
0ccc47
+  err = get_blkaddr_from_nat_journal (data, nid, &blkaddr);
0ccc47
+  if (err != GRUB_ERR_NONE)
0ccc47
+    return 0;
0ccc47
+
0ccc47
   if (blkaddr)
0ccc47
     return blkaddr;
0ccc47