hfsplus: Prevent overflows in comparisons
PATCH 1/1 hfsplus: Prevent overflows in comparisons
When very high Catalog Node IDs are in use, comparing CNIDs via subtraction may overflow. This causes files and folders to appear to be missing in the btree. It's safer to just use comparison operations rather than subtraction.
Also fixes the parent field in grub_hfsplus_catkey_internal, which is currently using an unqualified int to hold an unsigned value.
=== modified file 'grub-core/fs/hfsplus.c'
fs/hfsplus.c +22 -13
@@ -178,7 +178,7 @@/* Internal representation of a catalog key. */ struct grub_hfsplus_catkey_internal { -int parent; +grub_uint32_t parent;char *name; };
@@ -520,9 +520,12 @@int i;int diff; - diff =grub_be_to_cpu32 (catkey_a->parent) - catkey_b->parent; -if (diff) -return diff; +/* Safe unsigned comparison */ +grub_uint32_t aparent =grub_be_to_cpu32 (catkey_a->parent); +if (aparent > catkey_b->parent) +return 1 ; +if (aparent < catkey_b->parent) +return -1 ;/* Change the filename in keya so the endianness is correct. */ for (i =0 ; i <grub_be_to_cpu16 (catkey_a->namelen); i++)
@@ -555,15 +558,21 @@ {struct grub_hfsplus_extkey *extkey_a = &keya->extkey;struct grub_hfsplus_extkey_internal *extkey_b = &keyb->extkey; -int diff; - - diff =grub_be_to_cpu32 (extkey_a->fileid) - extkey_b->fileid; - -if (diff) -return diff; - - diff =grub_be_to_cpu32 (extkey_a->start) - extkey_b->start; -return diff; +grub_uint32_t akey; + +/* Safe unsigned comparison */ + akey =grub_be_to_cpu32 (extkey_a->fileid); +if (akey > extkey_b->fileid) +return 1 ; +if (akey < extkey_b->fileid) +return -1 ; + + akey =grub_be_to_cpu32 (extkey_a->start); +if (akey > extkey_b->start) +return 1 ; +if (akey < extkey_b->start) +return -1 ; +return 0 ; } static char *
Now with a ChangeLog entry. -V
=== modified file 'ChangeLog'
ChangeLog +8 -0
@@ -1,3 +1,11 @@ +2010-12-30 Dave Vasilevsky <[email protected]> + + * grub-core/fs/hfsplus.c: Parent field of + grub_hfsplus_catkey_internal should be unsigned. + (grub_hfsplus_cmp_catkey): Don't compare using subtraction, it + overflows. + (grub_hfsplus_cmp_extkey): Likewise + 2010-12-27 Vladimir Serbinenko <[email protected]> * grub-core/loader/xnu.c (grub_cmd_xnu_kernel) [! GRUB_MACHINE_EFI]:
=== modified file 'grub-core/fs/hfsplus.c'
fs/hfsplus.c +22 -13
@@ -178,7 +178,7 @@/* Internal representation of a catalog key. */ struct grub_hfsplus_catkey_internal { -int parent; +grub_uint32_t parent;char *name; };
@@ -520,9 +520,12 @@int i;int diff; - diff =grub_be_to_cpu32 (catkey_a->parent) - catkey_b->parent; -if (diff) -return diff; +/* Safe unsigned comparison */ +grub_uint32_t aparent =grub_be_to_cpu32 (catkey_a->parent); +if (aparent > catkey_b->parent) +return 1 ; +if (aparent < catkey_b->parent) +return -1 ;/* Change the filename in keya so the endianness is correct. */ for (i =0 ; i <grub_be_to_cpu16 (catkey_a->namelen); i++)
@@ -555,15 +558,21 @@ {struct grub_hfsplus_extkey *extkey_a = &keya->extkey;struct grub_hfsplus_extkey_internal *extkey_b = &keyb->extkey; -int diff; - - diff =grub_be_to_cpu32 (extkey_a->fileid) - extkey_b->fileid; - -if (diff) -return diff; - - diff =grub_be_to_cpu32 (extkey_a->start) - extkey_b->start; -return diff; +grub_uint32_t akey; + +/* Safe unsigned comparison */ + akey =grub_be_to_cpu32 (extkey_a->fileid); +if (akey > extkey_b->fileid) +return 1 ; +if (akey < extkey_b->fileid) +return -1 ; + + akey =grub_be_to_cpu32 (extkey_a->start); +if (akey > extkey_b->start) +return 1 ; +if (akey < extkey_b->start) +return -1 ; +return 0 ; } static char *
Applied, thanks.
Quoted 79 lines · Dave Vasilevsky
=== modified file 'ChangeLog'
ChangeLog +8
-0@@ -1,3 +1,11 @@ +2010-12-30 Dave Vasilevsky <[email protected]> + + * grub-core/fs/hfsplus.c: Parent field of + grub_hfsplus_catkey_internal should be unsigned. + (grub_hfsplus_cmp_catkey): Don't compare using subtraction, it + overflows. + (grub_hfsplus_cmp_extkey): Likewise + 2010-12-27 Vladimir Serbinenko <[email protected]> * grub-core/loader/xnu.c (grub_cmd_xnu_kernel) [! GRUB_MACHINE_EFI]:=== modified file '
grub-core/fs/hfsplus.c'fs/hfsplus.c +22
-13@@ -178,7 +178,7 @@/* Internal representation of a catalog key. */ struct grub_hfsplus_catkey_internal { -int parent; +grub_uint32_t parent;char *name; };@@ -520,9 +520,12 @@int i;int diff; - diff =grub_be_to_cpu32 (catkey_a->parent) - catkey_b->parent; -if (diff) -return diff; +/* Safe unsigned comparison */ +grub_uint32_t aparent =grub_be_to_cpu32 (catkey_a->parent); +if (aparent > catkey_b->parent) +return 1 ; +if (aparent < catkey_b->parent) +return -1 ;/* Change the filename in keya so the endianness is correct. */ for (i =0 ; i <grub_be_to_cpu16 (catkey_a->namelen); i++)@@ -555,15 +558,21 @@ {struct grub_hfsplus_extkey *extkey_a = &keya->extkey;struct grub_hfsplus_extkey_internal *extkey_b = &keyb->extkey; -int diff; - - diff =grub_be_to_cpu32 (extkey_a->fileid) - extkey_b->fileid; - -if (diff) -return diff; - - diff =grub_be_to_cpu32 (extkey_a->start) - extkey_b->start; -return diff; +grub_uint32_t akey; + +/* Safe unsigned comparison */ + akey =grub_be_to_cpu32 (extkey_a->fileid); +if (akey > extkey_b->fileid) +return 1 ; +if (akey < extkey_b->fileid) +return -1 ; + + akey =grub_be_to_cpu32 (extkey_a->start); +if (akey > extkey_b->start) +return 1 ; +if (akey < extkey_b->start) +return -1 ; +return 0 ; } static char *
Signature · 2 lines
Regards Vladimir 'φ-coder/phcoder' Serbinenko