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

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