|
|
0ccc47 |
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
0ccc47 |
From: Sudhakar Kuppusamy <sudhakar@linux.ibm.com>
|
|
|
0ccc47 |
Date: Wed, 6 Apr 2022 18:49:09 +0530
|
|
|
0ccc47 |
Subject: [PATCH] fs/f2fs: Do not read past the end of nat bitmap
|
|
|
0ccc47 |
|
|
|
0ccc47 |
A corrupt f2fs filesystem could have a block offset or a bitmap
|
|
|
0ccc47 |
offset that would cause us to read beyond the bounds of the nat
|
|
|
0ccc47 |
bitmap.
|
|
|
0ccc47 |
|
|
|
0ccc47 |
Introduce the nat_bitmap_size member in grub_f2fs_data which holds
|
|
|
0ccc47 |
the size of nat bitmap.
|
|
|
0ccc47 |
|
|
|
0ccc47 |
Set the size when loading the nat bitmap in nat_bitmap_ptr(), and
|
|
|
0ccc47 |
catch when an invalid offset would create a pointer past the end of
|
|
|
0ccc47 |
the allocated space.
|
|
|
0ccc47 |
|
|
|
0ccc47 |
Check against the bitmap size in grub_f2fs_test_bit() test bit to avoid
|
|
|
0ccc47 |
reading past the end of the nat bitmap.
|
|
|
0ccc47 |
|
|
|
0ccc47 |
Signed-off-by: Sudhakar Kuppusamy <sudhakar@linux.ibm.com>
|
|
|
0ccc47 |
Signed-off-by: Daniel Axtens <dja@axtens.net>
|
|
|
0ccc47 |
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
|
|
|
0ccc47 |
(cherry picked from commit 62d63d5e38c67a6e349148bf7cb87c560e935a7e)
|
|
|
0ccc47 |
(cherry picked from commit 92219e6d379b5b4d30b05361830b72ab1d95d281)
|
|
|
0ccc47 |
(cherry picked from commit c23d97e3b56594bf0f802d94062e14b221143115)
|
|
|
0ccc47 |
---
|
|
|
0ccc47 |
grub-core/fs/f2fs.c | 33 +++++++++++++++++++++++++++------
|
|
|
0ccc47 |
1 file changed, 27 insertions(+), 6 deletions(-)
|
|
|
0ccc47 |
|
|
|
0ccc47 |
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
|
|
|
0ccc47 |
index 09dc932420..33e565b180 100644
|
|
|
0ccc47 |
--- a/grub-core/fs/f2fs.c
|
|
|
0ccc47 |
+++ b/grub-core/fs/f2fs.c
|
|
|
0ccc47 |
@@ -122,6 +122,7 @@ GRUB_MOD_LICENSE ("GPLv3+");
|
|
|
0ccc47 |
#define F2FS_INLINE_DOTS 0x10 /* File having implicit dot dentries. */
|
|
|
0ccc47 |
|
|
|
0ccc47 |
#define MAX_VOLUME_NAME 512
|
|
|
0ccc47 |
+#define MAX_NAT_BITMAP_SIZE 3900
|
|
|
0ccc47 |
|
|
|
0ccc47 |
enum FILE_TYPE
|
|
|
0ccc47 |
{
|
|
|
0ccc47 |
@@ -183,7 +184,7 @@ struct grub_f2fs_checkpoint
|
|
|
0ccc47 |
grub_uint32_t checksum_offset;
|
|
|
0ccc47 |
grub_uint64_t elapsed_time;
|
|
|
0ccc47 |
grub_uint8_t alloc_type[MAX_ACTIVE_LOGS];
|
|
|
0ccc47 |
- grub_uint8_t sit_nat_version_bitmap[3900];
|
|
|
0ccc47 |
+ grub_uint8_t sit_nat_version_bitmap[MAX_NAT_BITMAP_SIZE];
|
|
|
0ccc47 |
grub_uint32_t checksum;
|
|
|
0ccc47 |
} GRUB_PACKED;
|
|
|
0ccc47 |
|
|
|
0ccc47 |
@@ -302,6 +303,7 @@ struct grub_f2fs_data
|
|
|
0ccc47 |
|
|
|
0ccc47 |
struct grub_f2fs_nat_journal nat_j;
|
|
|
0ccc47 |
char *nat_bitmap;
|
|
|
0ccc47 |
+ grub_uint32_t nat_bitmap_size;
|
|
|
0ccc47 |
|
|
|
0ccc47 |
grub_disk_t disk;
|
|
|
0ccc47 |
struct grub_f2fs_node *inode;
|
|
|
0ccc47 |
@@ -377,15 +379,20 @@ sum_blk_addr (struct grub_f2fs_data *data, int base, int type)
|
|
|
0ccc47 |
}
|
|
|
0ccc47 |
|
|
|
0ccc47 |
static void *
|
|
|
0ccc47 |
-nat_bitmap_ptr (struct grub_f2fs_data *data)
|
|
|
0ccc47 |
+nat_bitmap_ptr (struct grub_f2fs_data *data, grub_uint32_t *nat_bitmap_size)
|
|
|
0ccc47 |
{
|
|
|
0ccc47 |
struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
|
|
|
0ccc47 |
grub_uint32_t offset;
|
|
|
0ccc47 |
+ *nat_bitmap_size = MAX_NAT_BITMAP_SIZE;
|
|
|
0ccc47 |
|
|
|
0ccc47 |
if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
|
|
|
0ccc47 |
return ckpt->sit_nat_version_bitmap;
|
|
|
0ccc47 |
|
|
|
0ccc47 |
offset = grub_le_to_cpu32 (ckpt->sit_ver_bitmap_bytesize);
|
|
|
0ccc47 |
+ if (offset >= MAX_NAT_BITMAP_SIZE)
|
|
|
0ccc47 |
+ return NULL;
|
|
|
0ccc47 |
+
|
|
|
0ccc47 |
+ *nat_bitmap_size = *nat_bitmap_size - offset;
|
|
|
0ccc47 |
|
|
|
0ccc47 |
return ckpt->sit_nat_version_bitmap + offset;
|
|
|
0ccc47 |
}
|
|
|
0ccc47 |
@@ -438,11 +445,15 @@ grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
|
|
|
0ccc47 |
}
|
|
|
0ccc47 |
|
|
|
0ccc47 |
static int
|
|
|
0ccc47 |
-grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
|
|
|
0ccc47 |
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p, grub_uint32_t len)
|
|
|
0ccc47 |
{
|
|
|
0ccc47 |
int mask;
|
|
|
0ccc47 |
+ grub_uint32_t shifted_nr = (nr >> 3);
|
|
|
0ccc47 |
|
|
|
0ccc47 |
- p += (nr >> 3);
|
|
|
0ccc47 |
+ if (shifted_nr >= len)
|
|
|
0ccc47 |
+ return -1;
|
|
|
0ccc47 |
+
|
|
|
0ccc47 |
+ p += shifted_nr;
|
|
|
0ccc47 |
mask = 1 << (7 - (nr & 0x07));
|
|
|
0ccc47 |
|
|
|
0ccc47 |
return mask & *p;
|
|
|
0ccc47 |
@@ -662,6 +673,7 @@ get_node_blkaddr (struct grub_f2fs_data *data, grub_uint32_t nid)
|
|
|
0ccc47 |
grub_uint32_t seg_off, block_off, entry_off, block_addr;
|
|
|
0ccc47 |
grub_uint32_t blkaddr = 0;
|
|
|
0ccc47 |
grub_err_t err;
|
|
|
0ccc47 |
+ int result_bit;
|
|
|
0ccc47 |
|
|
|
0ccc47 |
err = get_blkaddr_from_nat_journal (data, nid, &blkaddr);
|
|
|
0ccc47 |
if (err != GRUB_ERR_NONE)
|
|
|
0ccc47 |
@@ -682,8 +694,15 @@ get_node_blkaddr (struct grub_f2fs_data *data, grub_uint32_t nid)
|
|
|
0ccc47 |
((seg_off * data->blocks_per_seg) << 1) +
|
|
|
0ccc47 |
(block_off & (data->blocks_per_seg - 1));
|
|
|
0ccc47 |
|
|
|
0ccc47 |
- if (grub_f2fs_test_bit (block_off, data->nat_bitmap))
|
|
|
0ccc47 |
+ result_bit = grub_f2fs_test_bit (block_off, data->nat_bitmap,
|
|
|
0ccc47 |
+ data->nat_bitmap_size);
|
|
|
0ccc47 |
+ if (result_bit > 0)
|
|
|
0ccc47 |
block_addr += data->blocks_per_seg;
|
|
|
0ccc47 |
+ else if (result_bit == -1)
|
|
|
0ccc47 |
+ {
|
|
|
0ccc47 |
+ grub_free (nat_block);
|
|
|
0ccc47 |
+ return 0;
|
|
|
0ccc47 |
+ }
|
|
|
0ccc47 |
|
|
|
0ccc47 |
err = grub_f2fs_block_read (data, block_addr, nat_block);
|
|
|
0ccc47 |
if (err)
|
|
|
0ccc47 |
@@ -832,7 +851,9 @@ grub_f2fs_mount (grub_disk_t disk)
|
|
|
0ccc47 |
if (err)
|
|
|
0ccc47 |
goto fail;
|
|
|
0ccc47 |
|
|
|
0ccc47 |
- data->nat_bitmap = nat_bitmap_ptr (data);
|
|
|
0ccc47 |
+ data->nat_bitmap = nat_bitmap_ptr (data, &data->nat_bitmap_size);
|
|
|
0ccc47 |
+ if (data->nat_bitmap == NULL)
|
|
|
0ccc47 |
+ goto fail;
|
|
|
0ccc47 |
|
|
|
0ccc47 |
err = get_nat_journal (data);
|
|
|
0ccc47 |
if (err)
|