nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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