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

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