Whamcloud - gitweb
libext2fs: Add some fail-safe checks to the 32-bit bitmap code
authorTheodore Ts'o <tytso@mit.edu>
Sun, 31 Jan 2010 01:08:11 +0000 (20:08 -0500)
committerTheodore Ts'o <tytso@mit.edu>
Sun, 31 Jan 2010 01:20:21 +0000 (20:20 -0500)
If a 64-bit bitmap is passed to a 32-bit bitmap function, add some
checks to make sure that we print a useful error message so we can
better catch potential problems.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
lib/ext2fs/ext2fsP.h
lib/ext2fs/gen_bitmap.c
lib/ext2fs/gen_bitmap64.c

index 300e442..8b1e54e 100644 (file)
@@ -138,4 +138,6 @@ extern errcode_t ext2fs_set_generic_bmap_range(ext2fs_generic_bitmap bitmap,
 extern errcode_t ext2fs_get_generic_bmap_range(ext2fs_generic_bitmap bitmap,
                                               __u64 start, unsigned int num,
                                               void *out);
+extern int ext2fs_warn_bitmap32(ext2fs_generic_bitmap bitmap, const char *func);
+
 extern int ext2fs_mem_is_zero(const char *mem, size_t len);
index d6deafb..532537a 100644 (file)
@@ -38,6 +38,16 @@ struct ext2fs_struct_generic_bitmap {
        __u32           reserved[7];
 };
 
+#define EXT2FS_IS_32_BITMAP(bmap) \
+       (((bmap)->magic == EXT2_ET_MAGIC_GENERIC_BITMAP) || \
+        ((bmap)->magic == EXT2_ET_MAGIC_BLOCK_BITMAP) || \
+        ((bmap)->magic == EXT2_ET_MAGIC_INODE_BITMAP))
+
+#define EXT2FS_IS_64_BITMAP(bmap) \
+       (((bmap)->magic == EXT2_ET_MAGIC_GENERIC_BITMAP64) || \
+        ((bmap)->magic == EXT2_ET_MAGIC_BLOCK_BITMAP64) || \
+        ((bmap)->magic == EXT2_ET_MAGIC_INODE_BITMAP64))
+
 /*
  * Used by previously inlined function, so we have to export this and
  * not change the function signature
@@ -160,6 +170,18 @@ void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap)
 int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
                                        blk_t bitno)
 {
+       if (!EXT2FS_IS_32_BITMAP(bitmap)) {
+               if (EXT2FS_IS_64_BITMAP(bitmap)) {
+                       ext2fs_warn_bitmap32(bitmap, __func__);
+                       return ext2fs_test_generic_bmap(bitmap, bitno);
+               }
+#ifndef OMIT_COM_ERR
+               com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
+                       "test_bitmap(%lu)", (unsigned long) bitno);
+               return 0;
+       }
+#endif
+
        if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
                ext2fs_warn_bitmap2(bitmap, EXT2FS_TEST_ERROR, bitno);
                return 0;
@@ -170,6 +192,18 @@ int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
 int ext2fs_mark_generic_bitmap(ext2fs_generic_bitmap bitmap,
                                         __u32 bitno)
 {
+       if (!EXT2FS_IS_32_BITMAP(bitmap)) {
+               if (EXT2FS_IS_64_BITMAP(bitmap)) {
+                       ext2fs_warn_bitmap32(bitmap, __func__);
+                       return ext2fs_mark_generic_bmap(bitmap, bitno);
+               }
+#ifndef OMIT_COM_ERR
+               com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
+                       "mark_bitmap(%lu)", (unsigned long) bitno);
+               return 0;
+       }
+#endif
+
        if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
                ext2fs_warn_bitmap2(bitmap, EXT2FS_MARK_ERROR, bitno);
                return 0;
@@ -180,6 +214,18 @@ int ext2fs_mark_generic_bitmap(ext2fs_generic_bitmap bitmap,
 int ext2fs_unmark_generic_bitmap(ext2fs_generic_bitmap bitmap,
                                           blk_t bitno)
 {
+       if (!EXT2FS_IS_32_BITMAP(bitmap)) {
+               if (EXT2FS_IS_64_BITMAP(bitmap)) {
+                       ext2fs_warn_bitmap32(bitmap, __func__);
+                       return ext2fs_mark_generic_bmap(bitmap, bitno);
+               }
+#ifndef OMIT_COM_ERR
+               com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
+                       "mark_bitmap(%lu)", (unsigned long) bitno);
+               return 0;
+       }
+#endif
+
        if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
                ext2fs_warn_bitmap2(bitmap, EXT2FS_UNMARK_ERROR, bitno);
                return 0;
@@ -189,18 +235,51 @@ int ext2fs_unmark_generic_bitmap(ext2fs_generic_bitmap bitmap,
 
 __u32 ext2fs_get_generic_bitmap_start(ext2fs_generic_bitmap bitmap)
 {
+       if (!EXT2FS_IS_32_BITMAP(bitmap)) {
+               if (EXT2FS_IS_64_BITMAP(bitmap)) {
+                       ext2fs_warn_bitmap32(bitmap, __func__);
+                       return ext2fs_get_generic_bmap_start(bitmap);
+               }
+#ifndef OMIT_COM_ERR
+               com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
+                       "get_bitmap_start");
+               return 0;
+       }
+#endif
+
        return bitmap->start;
 }
 
 __u32 ext2fs_get_generic_bitmap_end(ext2fs_generic_bitmap bitmap)
 {
+       if (!EXT2FS_IS_32_BITMAP(bitmap)) {
+               if (EXT2FS_IS_64_BITMAP(bitmap)) {
+                       ext2fs_warn_bitmap32(bitmap, __func__);
+                       return ext2fs_get_generic_bmap_end(bitmap);
+               }
+#ifndef OMIT_COM_ERR
+               com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
+                       "get_bitmap_end");
+               return 0;
+       }
+#endif
        return bitmap->end;
 }
 
 void ext2fs_clear_generic_bitmap(ext2fs_generic_bitmap bitmap)
 {
-       if (check_magic(bitmap))
+       if (!EXT2FS_IS_32_BITMAP(bitmap)) {
+               if (EXT2FS_IS_64_BITMAP(bitmap)) {
+                       ext2fs_warn_bitmap32(bitmap, __func__);
+                       ext2fs_clear_generic_bmap(bitmap);
+                       return;
+               }
+#ifndef OMIT_COM_ERR
+               com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
+                       "clear_generic_bitmap");
                return;
+       }
+#endif
 
        memset(bitmap->bitmap, 0,
               (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
index fb806a2..9f23b92 100644 (file)
@@ -565,3 +565,16 @@ void ext2fs_unmark_block_bitmap_range2(ext2fs_block_bitmap bmap,
 
        bmap->bitmap_ops->unmark_bmap_extent(bmap, block, num);
 }
+
+int ext2fs_warn_bitmap32(ext2fs_generic_bitmap bitmap, const char *func)
+{
+#ifndef OMIT_COM_ERR
+       if (bitmap && bitmap->description)
+               com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
+                       "called %s with 64-bit bitmap for %s", func,
+                       bitmap->description);
+       else
+               com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
+                       "called %s with 64-bit bitmap", func);
+#endif
+}