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

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