nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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