Skip to contentSkip to conversation

Prevent out-of-bound reads

PATCH Superseded Lidong Chen 3 participants 13 messages Apr 20, 2023 – Apr 25, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#6 lore raw

PATCH 3/4 fs/hfsplus: Set grub errno to prevent NULL pointer access

In reply to Lidong Chen

When an invalid node size is detected in grub_hfsplus_mount(), data pinter is freed. Thus, file->data is not set. The code should also set the grub error when that happens to indicate an error and to avoid accessing the unintialized file->data in grub_file_close().

---
 grub-core/fs/hfsplus.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
grub-core/fs/hfsplus.c +8 -2
index 9c1f12574..cf13e8a63 100644
@@ -357,7 +357,10 @@ grub_hfsplus_mount (grub_disk_t disk)
 			  (header.key_compare == GRUB_HFSPLUSX_BINARYCOMPARE));
 
   if (data->catalog_tree.nodesize < 2)
-    goto fail;
+    {
+      grub_error (GRUB_ERR_BAD_FS, "invalid catalog node size");
+      goto fail;
+    }
 
   if (grub_hfsplus_read_file (&data->extoverflow_tree.file, 0, 0,
 			      sizeof (struct grub_hfsplus_btnode),
@@ -374,7 +377,10 @@ grub_hfsplus_mount (grub_disk_t disk)
   data->extoverflow_tree.nodesize = grub_be_to_cpu16 (header.nodesize);
 
   if (data->extoverflow_tree.nodesize < 2)
-    goto fail;
+    {
+      grub_error (GRUB_ERR_BAD_FS, "invalid extents overflow node size");
+      goto fail;
+    }
 
   data->extoverflow_tree_ready = 1;
 
Signature · 1 line
2.39.1
#1 lore raw

PATCH 1/4 fs/hfsplus: Validate btree node size

In reply to Lidong Chen

The invalid btree node size can cause crashes when parsing the btree. The fix is to ensure the btree node size is within the valid range defined in the HFS Plus techical note, TN1150.

https://developer.apple.com/library/archive/technotes/tn/tn1150.html

---
 grub-core/fs/hfsplus.c | 7 +++++++
 1 file changed, 7 insertions(+)
grub-core/fs/hfsplus.c +7 -0
index 6337cbfcb..1ffebc8be 100644
@@ -84,6 +84,9 @@ struct grub_hfsplus_catfile
 #define GRUB_HFSPLUS_FILEMODE_DIRECTORY	0040000
 #define GRUB_HFSPLUS_FILEMODE_SYMLINK	0120000
 
+#define HFSPLUS_BTNODE_MINSZ	(1 << 9)
+#define HFSPLUS_BTNODE_MAXSZ	(1 << 15)
+
 /* Some pre-defined file IDs.  */
 enum
   {
@@ -584,6 +587,10 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
       return 0;
     }
 
+  if (btree->nodesize < HFSPLUS_BTNODE_MINSZ ||
+      btree->nodesize > HFSPLUS_BTNODE_MAXSZ)
+    return grub_error (GRUB_ERR_BAD_FS, "invalid HFS+ btree node size");
+
   node = grub_malloc (btree->nodesize);
   if (! node)
     return grub_errno;
Signature · 1 line
2.39.1
#0 lore raw

cover Prevent out-of-bound reads

This set of patches adds checks to ensure the node size is valid before accessing it. In addition, error messages are marked for translation.

Lidong Chen (4):
fs/hfsplus: Validate btree node size
fs/hfsplus: Prevent out of bound access in catalog file fs/hfsplus: Set grub errno to prevent NULL pointer access fs/hfsplus: Mark error strings for translation

 grub-core/fs/hfsplus.c | 49 +++++++++++++++++++++++++++++++-----------
 1 file changed, 36 insertions(+), 13 deletions(-)
Signature · 1 line
2.39.1
#3 lore raw

PATCH 2/4 fs/hfsplus: Prevent out of bound access in catalog file

A corrupted hfsplus can have a catalog key that is out of range. This can lead to out of bound access when advancing the pointer to access catalog file info.

---
 grub-core/fs/hfsplus.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
grub-core/fs/hfsplus.c +10 -0
index 1ffebc8be..9c1f12574 100644
@@ -87,6 +87,9 @@ struct grub_hfsplus_catfile
 #define HFSPLUS_BTNODE_MINSZ	(1 << 9)
 #define HFSPLUS_BTNODE_MAXSZ	(1 << 15)
 
+#define HFSPLUS_CATKEY_MIN_LEN	6
+#define HFSPLUS_CATKEY_MAX_LEN	516
+
 /* Some pre-defined file IDs.  */
 enum
   {
@@ -699,6 +702,13 @@ list_nodes (void *record, void *hook_arg)
 
   catkey = (struct grub_hfsplus_catkey *) record;
 
+  if (grub_be_to_cpu16 (catkey->keylen) < HFSPLUS_CATKEY_MIN_LEN ||
+      grub_be_to_cpu16 (catkey->keylen) > HFSPLUS_CATKEY_MAX_LEN)
+    {
+      grub_error (GRUB_ERR_BAD_FS, "catalog key length is out of range");
+      return 1;
+    }
+
   fileinfo =
     (struct grub_hfsplus_catfile *) ((char *) record
 				     + grub_be_to_cpu16 (catkey->keylen)
Signature · 1 line
2.39.1
#9 lore raw

PATCH 4/4 fs/hfsplus: Mark error strings for translation

In reply to Lidong Chen
---
 grub-core/fs/hfsplus.c | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
grub-core/fs/hfsplus.c +15 -15
index cf13e8a63..f91af1477 100644
@@ -179,7 +179,7 @@ grub_hfsplus_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
       if (node->fileid == GRUB_HFSPLUS_FILEID_OVERFLOW)
 	{
 	  grub_error (GRUB_ERR_READ_ERROR,
-		      "extra extents found in an extend overflow file");
+		      N_("extra extents found in an extend overflow file"));
 	  break;
 	}
 
@@ -190,7 +190,7 @@ grub_hfsplus_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
       if (!node->data->extoverflow_tree_ready)
 	{
 	  grub_error (GRUB_ERR_BAD_FS,
-		      "attempted to read extent overflow tree before loading");
+		      N_("attempted to read extent overflow tree before loading"));
 	  break;
 	}
 
@@ -205,8 +205,8 @@ grub_hfsplus_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
 	  || !nnode)
 	{
 	  grub_error (GRUB_ERR_READ_ERROR,
-		      "no block found for the file id 0x%x and the block"
-		      " offset 0x%" PRIuGRUB_UINT64_T,
+		      N_("no block found for the file id 0x%x and the block"
+		      " offset 0x%" PRIuGRUB_UINT64_T),
 		      node->fileid, fileblock);
 	  break;
 	}
@@ -277,7 +277,7 @@ grub_hfsplus_mount (grub_disk_t disk)
       /* See if there's an embedded HFS+ filesystem.  */
       if (grub_be_to_cpu16 (volheader.hfs.embed_sig) != GRUB_HFSPLUS_MAGIC)
 	{
-	  grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem");
+	  grub_error (GRUB_ERR_BAD_FS, N_("not a HFS+ filesystem"));
 	  goto fail;
 	}
 
@@ -303,7 +303,7 @@ grub_hfsplus_mount (grub_disk_t disk)
       || ((volheader.hfsplus.blksize & (volheader.hfsplus.blksize - 1)) != 0)
       || grub_be_to_cpu32 (volheader.hfsplus.blksize) < GRUB_DISK_SECTOR_SIZE)
     {
-      grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem");
+      grub_error (GRUB_ERR_BAD_FS, N_("not a HFS+ filesystem"));
       goto fail;
     }
 
@@ -358,7 +358,7 @@ grub_hfsplus_mount (grub_disk_t disk)
 
   if (data->catalog_tree.nodesize < 2)
     {
-      grub_error (GRUB_ERR_BAD_FS, "invalid catalog node size");
+      grub_error (GRUB_ERR_BAD_FS, N_("invalid catalog node size"));
       goto fail;
     }
 
@@ -378,7 +378,7 @@ grub_hfsplus_mount (grub_disk_t disk)
 
   if (data->extoverflow_tree.nodesize < 2)
     {
-      grub_error (GRUB_ERR_BAD_FS, "invalid extents overflow node size");
+      grub_error (GRUB_ERR_BAD_FS, N_("invalid extents overflow node size"));
       goto fail;
     }
 
@@ -406,7 +406,7 @@ grub_hfsplus_mount (grub_disk_t disk)
  fail:
 
   if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
-    grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem");
+    grub_error (GRUB_ERR_BAD_FS, N_("not a HFS+ filesystem"));
 
   grub_free (data);
   return 0;
@@ -550,7 +550,7 @@ grub_hfsplus_btree_iterate_node (struct grub_hfsplus_btree *btree,
 
       if (node_count && first_node->next == saved_node)
 	{
-	  grub_error (GRUB_ERR_BAD_FS, "HFS+ btree loop");
+	  grub_error (GRUB_ERR_BAD_FS, N_("HFS+ btree loop"));
 	  return 0;
 	}
       if (!(node_count & (node_count - 1)))
@@ -598,7 +598,7 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
 
   if (btree->nodesize < HFSPLUS_BTNODE_MINSZ ||
       btree->nodesize > HFSPLUS_BTNODE_MAXSZ)
-    return grub_error (GRUB_ERR_BAD_FS, "invalid HFS+ btree node size");
+    return grub_error (GRUB_ERR_BAD_FS, N_("invalid HFS+ btree node size"));
 
   node = grub_malloc (btree->nodesize);
   if (! node)
@@ -613,7 +613,7 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
       if (save_node == currnode)
 	{
 	  grub_free (node);
-	  return grub_error (GRUB_ERR_BAD_FS, "HFS+ btree loop");
+	  return grub_error (GRUB_ERR_BAD_FS, N_("HFS+ btree loop"));
 	}
       if (!(node_count & (node_count - 1)))
 	save_node = currnode;
@@ -626,7 +626,7 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
 				  btree->nodesize, (char *) node) <= 0)
 	{
 	  grub_free (node);
-	  return grub_error (GRUB_ERR_BAD_FS, "couldn't read i-node");
+	  return grub_error (GRUB_ERR_BAD_FS, N_("couldn't read i-node"));
 	}
 
       nodedesc = (struct grub_hfsplus_btnode *) node;
@@ -668,7 +668,7 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
 			 + 2);
 
 	      if ((char *) pointer > node + btree->nodesize - 2)
-		return grub_error (GRUB_ERR_BAD_FS, "HFS+ key beyond end of node");
+		return grub_error (GRUB_ERR_BAD_FS, N_("HFS+ key beyond end of node"));
 
 	      currnode = grub_be_to_cpu32 (grub_get_unaligned32 (pointer));
 	      match = 1;
@@ -711,7 +711,7 @@ list_nodes (void *record, void *hook_arg)
   if (grub_be_to_cpu16 (catkey->keylen) < HFSPLUS_CATKEY_MIN_LEN ||
       grub_be_to_cpu16 (catkey->keylen) > HFSPLUS_CATKEY_MAX_LEN)
     {
-      grub_error (GRUB_ERR_BAD_FS, "catalog key length is out of range");
+      grub_error (GRUB_ERR_BAD_FS, N_("catalog key length is out of range"));
       return 1;
     }
 
Signature · 1 line
2.39.1
#10 lore raw

Re: [PATCH 4/4] fs/hfsplus: Mark error strings for translation

Is there any reason to translate those strings? They refer to deep HFS+ structures and problems and to common people they are meaningless even in their language. And someone in IT is likely to understand those concepts in English. I wouldn't know a word for "extent" in my native language. And those errors are very rare as they indicate a corrupted FS in the files GRUB cares about. OTOH translating them requires a lot of effort on translator's side for little benefit. They were not forgotten. They were not marked for translation on purpose. If we mark all errors for translation we will make our already large list of strings 10x larger and 90% of it are hyper-obscure error conditions. Error conditions are translated only if they are likely to occur and indicate a resolution direction like "linux command needs to be run before initrd command"

Quoted 146 lines · Lidong Chen
---
 grub-core/fs/hfsplus.c | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
grub-core/fs/hfsplus.c +15 -16
index cf13e8a63..f91af1477 100644
@@ -179,7 +179,7 @@ grub_hfsplus_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
       if (node->fileid == GRUB_HFSPLUS_FILEID_OVERFLOW)
        {
          grub_error (GRUB_ERR_READ_ERROR,
-                     "extra extents found in an extend overflow file");
+                     N_("extra extents found in an extend overflow file"));
          break;
        }
 
@@ -190,7 +190,7 @@ grub_hfsplus_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
       if (!node->data->extoverflow_tree_ready)
        {
          grub_error (GRUB_ERR_BAD_FS,
-                     "attempted to read extent overflow tree before loading");
+                     N_("attempted to read extent overflow tree before loading"));
          break;
        }
 
@@ -205,8 +205,8 @@ grub_hfsplus_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
          || !nnode)
        {
          grub_error (GRUB_ERR_READ_ERROR,
-                     "no block found for the file id 0x%x and the block"
-                     " offset 0x%" PRIuGRUB_UINT64_T,
+                     N_("no block found for the file id 0x%x and the block"
+                     " offset 0x%" PRIuGRUB_UINT64_T),
                      node->fileid, fileblock);
          break;
        }
@@ -277,7 +277,7 @@ grub_hfsplus_mount (grub_disk_t disk)
       /* See if there's an embedded HFS+ filesystem.  */
       if (grub_be_to_cpu16 (volheader.hfs.embed_sig) != GRUB_HFSPLUS_MAGIC)
        {
-         grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem");
+         grub_error (GRUB_ERR_BAD_FS, N_("not a HFS+ filesystem"));
          goto fail;
        }
 
@@ -303,7 +303,7 @@ grub_hfsplus_mount (grub_disk_t disk)
       || ((volheader.hfsplus.blksize & (volheader.hfsplus.blksize - 1)) != 0)
       || grub_be_to_cpu32 (volheader.hfsplus.blksize) < GRUB_DISK_SECTOR_SIZE)
     {
-      grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem");
+      grub_error (GRUB_ERR_BAD_FS, N_("not a HFS+ filesystem"));
       goto fail;
     }
 
@@ -358,7 +358,7 @@ grub_hfsplus_mount (grub_disk_t disk)
 
   if (data->catalog_tree.nodesize < 2)
     {
-      grub_error (GRUB_ERR_BAD_FS, "invalid catalog node size");
+      grub_error (GRUB_ERR_BAD_FS, N_("invalid catalog node size"));
       goto fail;
     }
 
@@ -378,7 +378,7 @@ grub_hfsplus_mount (grub_disk_t disk)
 
   if (data->extoverflow_tree.nodesize < 2)
     {
-      grub_error (GRUB_ERR_BAD_FS, "invalid extents overflow node size");
+      grub_error (GRUB_ERR_BAD_FS, N_("invalid extents overflow node size"));
       goto fail;
     }
 
@@ -406,7 +406,7 @@ grub_hfsplus_mount (grub_disk_t disk)
  fail:
 
   if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
-    grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem");
+    grub_error (GRUB_ERR_BAD_FS, N_("not a HFS+ filesystem"));
 
   grub_free (data);
   return 0;
@@ -550,7 +550,7 @@ grub_hfsplus_btree_iterate_node (struct grub_hfsplus_btree *btree,
 
       if (node_count && first_node->next == saved_node)
        {
-         grub_error (GRUB_ERR_BAD_FS, "HFS+ btree loop");
+         grub_error (GRUB_ERR_BAD_FS, N_("HFS+ btree loop"));
          return 0;
        }
       if (!(node_count & (node_count - 1)))
@@ -598,7 +598,7 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
 
   if (btree->nodesize < HFSPLUS_BTNODE_MINSZ ||
       btree->nodesize > HFSPLUS_BTNODE_MAXSZ)
-    return grub_error (GRUB_ERR_BAD_FS, "invalid HFS+ btree node size");
+    return grub_error (GRUB_ERR_BAD_FS, N_("invalid HFS+ btree node size"));
 
   node = grub_malloc (btree->nodesize);
   if (! node)
@@ -613,7 +613,7 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
       if (save_node == currnode)
        {
          grub_free (node);
-         return grub_error (GRUB_ERR_BAD_FS, "HFS+ btree loop");
+         return grub_error (GRUB_ERR_BAD_FS, N_("HFS+ btree loop"));
        }
       if (!(node_count & (node_count - 1)))
        save_node = currnode;
@@ -626,7 +626,7 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
                                  btree->nodesize, (char *) node) <= 0)
        {
          grub_free (node);
-         return grub_error (GRUB_ERR_BAD_FS, "couldn't read i-node");
+         return grub_error (GRUB_ERR_BAD_FS, N_("couldn't read i-node"));
        }
 
       nodedesc = (struct grub_hfsplus_btnode *) node;
@@ -668,7 +668,7 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
                         + 2);
 
              if ((char *) pointer > node + btree->nodesize - 2)
-               return grub_error (GRUB_ERR_BAD_FS, "HFS+ key beyond end of node");
+               return grub_error (GRUB_ERR_BAD_FS, N_("HFS+ key beyond end of node"));
 
              currnode = grub_be_to_cpu32 (grub_get_unaligned32 (pointer));
              match = 1;
@@ -711,7 +711,7 @@ list_nodes (void *record, void *hook_arg)
   if (grub_be_to_cpu16 (catkey->keylen) < HFSPLUS_CATKEY_MIN_LEN ||
       grub_be_to_cpu16 (catkey->keylen) > HFSPLUS_CATKEY_MAX_LEN)
     {
-      grub_error (GRUB_ERR_BAD_FS, "catalog key length is out of range");
+      grub_error (GRUB_ERR_BAD_FS, N_("catalog key length is out of range"));
       return 1;
     }
 
--

2.39.1

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko
#7 lore raw

Re: [PATCH 3/4] fs/hfsplus: Set grub errno to prevent NULL pointer access

In reply to Lidong Chen
Quoted 5 lines · Lidong Chen

When an invalid node size is detected in grub_hfsplus_mount(), data pinter is freed. Thus, file->data is not set. The code should also set the grub error when that happens to indicate an error and to avoid accessing the unintialized file->data in grub_file_close().

file->data is set to NULL in grub_file_open as part of zalloc. What sets it to something else?

Quoted 42 lines
---
 grub-core/fs/hfsplus.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
grub-core/fs/hfsplus.c +8 -3
index 9c1f12574..cf13e8a63 100644
@@ -357,7 +357,10 @@ grub_hfsplus_mount (grub_disk_t disk)
                          (header.key_compare == GRUB_HFSPLUSX_BINARYCOMPARE));
 
   if (data->catalog_tree.nodesize < 2)
-    goto fail;
+    {
+      grub_error (GRUB_ERR_BAD_FS, "invalid catalog node size");
+      goto fail;
+    }
 
   if (grub_hfsplus_read_file (&data->extoverflow_tree.file, 0, 0,
                              sizeof (struct grub_hfsplus_btnode),
@@ -374,7 +377,10 @@ grub_hfsplus_mount (grub_disk_t disk)
   data->extoverflow_tree.nodesize = grub_be_to_cpu16 (header.nodesize);
 
   if (data->extoverflow_tree.nodesize < 2)
-    goto fail;
+    {
+      grub_error (GRUB_ERR_BAD_FS, "invalid extents overflow node size");
+      goto fail;
+    }
 
   data->extoverflow_tree_ready = 1;
 
--

2.39.1

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko
#2 lore raw

Re: [PATCH 1/4] fs/hfsplus: Validate btree node size

In reply to Lidong Chen

LGTM

Quoted 45 lines · Lidong Chen

The invalid btree node size can cause crashes when parsing the btree. The fix is to ensure the btree node size is within the valid range defined in the HFS Plus techical note, TN1150.

https://developer.apple.com/library/archive/technotes/tn/tn1150.html

---
 grub-core/fs/hfsplus.c | 7 +++++++
 1 file changed, 7 insertions(+)
grub-core/fs/hfsplus.c +7 -1
index 6337cbfcb..1ffebc8be 100644
@@ -84,6 +84,9 @@ struct grub_hfsplus_catfile
 #define GRUB_HFSPLUS_FILEMODE_DIRECTORY        0040000
 #define GRUB_HFSPLUS_FILEMODE_SYMLINK  0120000
 
+#define HFSPLUS_BTNODE_MINSZ   (1 << 9)
+#define HFSPLUS_BTNODE_MAXSZ   (1 << 15)
+
 /* Some pre-defined file IDs.  */
 enum
   {
@@ -584,6 +587,10 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
       return 0;
     }
 
+  if (btree->nodesize < HFSPLUS_BTNODE_MINSZ ||
+      btree->nodesize > HFSPLUS_BTNODE_MAXSZ)
+    return grub_error (GRUB_ERR_BAD_FS, "invalid HFS+ btree node size");
+
   node = grub_malloc (btree->nodesize);
   if (! node)
     return grub_errno;
--

2.39.1

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko
#4 lore raw

Re: [PATCH 2/4] fs/hfsplus: Prevent out of bound access in catalog file

In reply to Lidong Chen
Quoted 4 lines · Lidong Chen

A corrupted hfsplus can have a catalog key that is out of range. This can lead to out of bound access when advancing the pointer to access catalog file info.

Can you explain where 6 and 516 come from?

Quoted 42 lines
---
 grub-core/fs/hfsplus.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
grub-core/fs/hfsplus.c +10 -1
index 1ffebc8be..9c1f12574 100644
@@ -87,6 +87,9 @@ struct grub_hfsplus_catfile
 #define HFSPLUS_BTNODE_MINSZ   (1 << 9)
 #define HFSPLUS_BTNODE_MAXSZ   (1 << 15)
 
+#define HFSPLUS_CATKEY_MIN_LEN 6
+#define HFSPLUS_CATKEY_MAX_LEN 516
+
 /* Some pre-defined file IDs.  */
 enum
   {
@@ -699,6 +702,13 @@ list_nodes (void *record, void *hook_arg)
 
   catkey = (struct grub_hfsplus_catkey *) record;
 
+  if (grub_be_to_cpu16 (catkey->keylen) < HFSPLUS_CATKEY_MIN_LEN ||
+      grub_be_to_cpu16 (catkey->keylen) > HFSPLUS_CATKEY_MAX_LEN)
+    {
+      grub_error (GRUB_ERR_BAD_FS, "catalog key length is out of range");
+      return 1;
+    }
+
   fileinfo =
     (struct grub_hfsplus_catfile *) ((char *) record
                                     + grub_be_to_cpu16 (catkey->keylen)
--

2.39.1

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko
#11 lore raw

Re: [PATCH 4/4] fs/hfsplus: Mark error strings for translation

In reply to Vladimir 'phcoder' Serbinenko
Quoted 5 lines · Vladimir 'phcoder' Serbinenko

Is there any reason to translate those strings? They refer to deep HFS+ structures and problems and to common people they are meaningless even in their language. And someone in IT is likely to understand those concepts in English. I wouldn't know a word for "extent" in my native language. And those errors are very rare as they indicate a

Yeah, I know what you mean... :-)

Quoted 8 lines

corrupted FS in the files GRUB cares about. OTOH translating them requires a lot of effort on translator's side for little benefit. They were not forgotten. They were not marked for translation on purpose. If we mark all errors for translation we will make our already large list of strings 10x larger and 90% of it are hyper-obscure error conditions. Error conditions are translated only if they are likely to occur and indicate a resolution direction like "linux command needs to be run before initrd command"

This patch has been suggested by me because I thought the lack of N_() was a mistake. Your comments shed some light to this. Though I still think some messages, e.g. "not a HFS+ filesystem", could be translated.

Daniel

Quoted 141 lines · Lidong Chen
Quoted 140 lines
---
 grub-core/fs/hfsplus.c | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
grub-core/fs/hfsplus.c +15 -16
index cf13e8a63..f91af1477 100644
@@ -179,7 +179,7 @@ grub_hfsplus_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
       if (node->fileid == GRUB_HFSPLUS_FILEID_OVERFLOW)
        {
          grub_error (GRUB_ERR_READ_ERROR,
-                     "extra extents found in an extend overflow file");
+                     N_("extra extents found in an extend overflow file"));
          break;
        }
 
@@ -190,7 +190,7 @@ grub_hfsplus_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
       if (!node->data->extoverflow_tree_ready)
        {
          grub_error (GRUB_ERR_BAD_FS,
-                     "attempted to read extent overflow tree before loading");
+                     N_("attempted to read extent overflow tree before loading"));
          break;
        }
 
@@ -205,8 +205,8 @@ grub_hfsplus_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
          || !nnode)
        {
          grub_error (GRUB_ERR_READ_ERROR,
-                     "no block found for the file id 0x%x and the block"
-                     " offset 0x%" PRIuGRUB_UINT64_T,
+                     N_("no block found for the file id 0x%x and the block"
+                     " offset 0x%" PRIuGRUB_UINT64_T),
                      node->fileid, fileblock);
          break;
        }
@@ -277,7 +277,7 @@ grub_hfsplus_mount (grub_disk_t disk)
       /* See if there's an embedded HFS+ filesystem.  */
       if (grub_be_to_cpu16 (volheader.hfs.embed_sig) != GRUB_HFSPLUS_MAGIC)
        {
-         grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem");
+         grub_error (GRUB_ERR_BAD_FS, N_("not a HFS+ filesystem"));
          goto fail;
        }
 
@@ -303,7 +303,7 @@ grub_hfsplus_mount (grub_disk_t disk)
       || ((volheader.hfsplus.blksize & (volheader.hfsplus.blksize - 1)) != 0)
       || grub_be_to_cpu32 (volheader.hfsplus.blksize) < GRUB_DISK_SECTOR_SIZE)
     {
-      grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem");
+      grub_error (GRUB_ERR_BAD_FS, N_("not a HFS+ filesystem"));
       goto fail;
     }
 
@@ -358,7 +358,7 @@ grub_hfsplus_mount (grub_disk_t disk)
 
   if (data->catalog_tree.nodesize < 2)
     {
-      grub_error (GRUB_ERR_BAD_FS, "invalid catalog node size");
+      grub_error (GRUB_ERR_BAD_FS, N_("invalid catalog node size"));
       goto fail;
     }
 
@@ -378,7 +378,7 @@ grub_hfsplus_mount (grub_disk_t disk)
 
   if (data->extoverflow_tree.nodesize < 2)
     {
-      grub_error (GRUB_ERR_BAD_FS, "invalid extents overflow node size");
+      grub_error (GRUB_ERR_BAD_FS, N_("invalid extents overflow node size"));
       goto fail;
     }
 
@@ -406,7 +406,7 @@ grub_hfsplus_mount (grub_disk_t disk)
  fail:
 
   if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
-    grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem");
+    grub_error (GRUB_ERR_BAD_FS, N_("not a HFS+ filesystem"));
 
   grub_free (data);
   return 0;
@@ -550,7 +550,7 @@ grub_hfsplus_btree_iterate_node (struct grub_hfsplus_btree *btree,
 
       if (node_count && first_node->next == saved_node)
        {
-         grub_error (GRUB_ERR_BAD_FS, "HFS+ btree loop");
+         grub_error (GRUB_ERR_BAD_FS, N_("HFS+ btree loop"));
          return 0;
        }
       if (!(node_count & (node_count - 1)))
@@ -598,7 +598,7 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
 
   if (btree->nodesize < HFSPLUS_BTNODE_MINSZ ||
       btree->nodesize > HFSPLUS_BTNODE_MAXSZ)
-    return grub_error (GRUB_ERR_BAD_FS, "invalid HFS+ btree node size");
+    return grub_error (GRUB_ERR_BAD_FS, N_("invalid HFS+ btree node size"));
 
   node = grub_malloc (btree->nodesize);
   if (! node)
@@ -613,7 +613,7 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
       if (save_node == currnode)
        {
          grub_free (node);
-         return grub_error (GRUB_ERR_BAD_FS, "HFS+ btree loop");
+         return grub_error (GRUB_ERR_BAD_FS, N_("HFS+ btree loop"));
        }
       if (!(node_count & (node_count - 1)))
        save_node = currnode;
@@ -626,7 +626,7 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
                                  btree->nodesize, (char *) node) <= 0)
        {
          grub_free (node);
-         return grub_error (GRUB_ERR_BAD_FS, "couldn't read i-node");
+         return grub_error (GRUB_ERR_BAD_FS, N_("couldn't read i-node"));
        }
 
       nodedesc = (struct grub_hfsplus_btnode *) node;
@@ -668,7 +668,7 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
                         + 2);
 
              if ((char *) pointer > node + btree->nodesize - 2)
-               return grub_error (GRUB_ERR_BAD_FS, "HFS+ key beyond end of node");
+               return grub_error (GRUB_ERR_BAD_FS, N_("HFS+ key beyond end of node"));
 
              currnode = grub_be_to_cpu32 (grub_get_unaligned32 (pointer));
              match = 1;
@@ -711,7 +711,7 @@ list_nodes (void *record, void *hook_arg)
   if (grub_be_to_cpu16 (catkey->keylen) < HFSPLUS_CATKEY_MIN_LEN ||
       grub_be_to_cpu16 (catkey->keylen) > HFSPLUS_CATKEY_MAX_LEN)
     {
-      grub_error (GRUB_ERR_BAD_FS, "catalog key length is out of range");
+      grub_error (GRUB_ERR_BAD_FS, N_("catalog key length is out of range"));
       return 1;
     }
 
--

2.39.1

#12 lore raw

Re: [PATCH 4/4] fs/hfsplus: Mark error strings for translation

Quoted 22 lines · Daniel Kiper
Quoted 5 lines · Vladimir 'phcoder' Serbinenko

Is there any reason to translate those strings? They refer to deep HFS+ structures and problems and to common people they are meaningless even in their language. And someone in IT is likely to understand those concepts in English. I wouldn't know a word for "extent" in my native language. And those errors are very rare as they indicate a

Yeah, I know what you mean... :-)

Quoted 8 lines

corrupted FS in the files GRUB cares about. OTOH translating them requires a lot of effort on translator's side for little benefit. They were not forgotten. They were not marked for translation on purpose. If we mark all errors for translation we will make our already large list of strings 10x larger and 90% of it are hyper-obscure error conditions. Error conditions are translated only if they are likely to occur and indicate a resolution direction like "linux command needs to be run before initrd command"

Quoted 3 lines

This patch has been suggested by me because I thought the lack of N_() was a mistake. Your comments shed some light to this. Though I still think some messages, e.g. "not a HFS+ filesystem", could be translated.

I can revert the changes, except the general messages as mentioned by Daniel.

Thanks,
Lidong

Quoted 144 lines

Daniel

Quoted 141 lines · Lidong Chen
Quoted 140 lines

---
grub-core/fs/hfsplus.c | 30 +++++++++++++++---------------

1 file changed, 15 insertions(+), 15 deletions(-)
grub-core/fs/hfsplus.c +15 -16
index cf13e8a63..f91af1477 100644
@@ -179,7 +179,7 @@ grub_hfsplus_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
      if (node->fileid == GRUB_HFSPLUS_FILEID_OVERFLOW)
       {
         grub_error (GRUB_ERR_READ_ERROR,
-                     "extra extents found in an extend overflow file");
+                     N_("extra extents found in an extend overflow file"));
         break;
       }
 
@@ -190,7 +190,7 @@ grub_hfsplus_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
      if (!node->data->extoverflow_tree_ready)
       {
         grub_error (GRUB_ERR_BAD_FS,
-                     "attempted to read extent overflow tree before loading");
+                     N_("attempted to read extent overflow tree before loading"));
         break;
       }
 
@@ -205,8 +205,8 @@ grub_hfsplus_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
         || !nnode)
       {
         grub_error (GRUB_ERR_READ_ERROR,
-                     "no block found for the file id 0x%x and the block"
-                     " offset 0x%" PRIuGRUB_UINT64_T,
+                     N_("no block found for the file id 0x%x and the block"
+                     " offset 0x%" PRIuGRUB_UINT64_T),
                     node->fileid, fileblock);
         break;
       }
@@ -277,7 +277,7 @@ grub_hfsplus_mount (grub_disk_t disk)
      /* See if there's an embedded HFS+ filesystem.  */
      if (grub_be_to_cpu16 (volheader.hfs.embed_sig) != GRUB_HFSPLUS_MAGIC)
       {
-         grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem");
+         grub_error (GRUB_ERR_BAD_FS, N_("not a HFS+ filesystem"));
         goto fail;
       }
 
@@ -303,7 +303,7 @@ grub_hfsplus_mount (grub_disk_t disk)
      || ((volheader.hfsplus.blksize & (volheader.hfsplus.blksize - 1)) != 0)
      || grub_be_to_cpu32 (volheader.hfsplus.blksize) < GRUB_DISK_SECTOR_SIZE)
    {
-      grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem");
+      grub_error (GRUB_ERR_BAD_FS, N_("not a HFS+ filesystem"));
      goto fail;
    }
 
@@ -358,7 +358,7 @@ grub_hfsplus_mount (grub_disk_t disk)
 
  if (data->catalog_tree.nodesize < 2)
    {
-      grub_error (GRUB_ERR_BAD_FS, "invalid catalog node size");
+      grub_error (GRUB_ERR_BAD_FS, N_("invalid catalog node size"));
      goto fail;
    }
 
@@ -378,7 +378,7 @@ grub_hfsplus_mount (grub_disk_t disk)
 
  if (data->extoverflow_tree.nodesize < 2)
    {
-      grub_error (GRUB_ERR_BAD_FS, "invalid extents overflow node size");
+      grub_error (GRUB_ERR_BAD_FS, N_("invalid extents overflow node size"));
      goto fail;
    }
 
@@ -406,7 +406,7 @@ grub_hfsplus_mount (grub_disk_t disk)
 fail:
 
  if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
-    grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem");
+    grub_error (GRUB_ERR_BAD_FS, N_("not a HFS+ filesystem"));
 
  grub_free (data);
  return 0;
@@ -550,7 +550,7 @@ grub_hfsplus_btree_iterate_node (struct grub_hfsplus_btree *btree,
 
      if (node_count && first_node->next == saved_node)
       {
-         grub_error (GRUB_ERR_BAD_FS, "HFS+ btree loop");
+         grub_error (GRUB_ERR_BAD_FS, N_("HFS+ btree loop"));
         return 0;
       }
      if (!(node_count & (node_count - 1)))
@@ -598,7 +598,7 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
 
  if (btree->nodesize < HFSPLUS_BTNODE_MINSZ ||
      btree->nodesize > HFSPLUS_BTNODE_MAXSZ)
-    return grub_error (GRUB_ERR_BAD_FS, "invalid HFS+ btree node size");
+    return grub_error (GRUB_ERR_BAD_FS, N_("invalid HFS+ btree node size"));
 
  node = grub_malloc (btree->nodesize);
  if (! node)
@@ -613,7 +613,7 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
      if (save_node == currnode)
       {
         grub_free (node);
-         return grub_error (GRUB_ERR_BAD_FS, "HFS+ btree loop");
+         return grub_error (GRUB_ERR_BAD_FS, N_("HFS+ btree loop"));
       }
      if (!(node_count & (node_count - 1)))
       save_node = currnode;
@@ -626,7 +626,7 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
                                 btree->nodesize, (char *) node) <= 0)
       {
         grub_free (node);
-         return grub_error (GRUB_ERR_BAD_FS, "couldn't read i-node");
+         return grub_error (GRUB_ERR_BAD_FS, N_("couldn't read i-node"));
       }
 
      nodedesc = (struct grub_hfsplus_btnode *) node;
@@ -668,7 +668,7 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
                        + 2);
 
             if ((char *) pointer > node + btree->nodesize - 2)
-               return grub_error (GRUB_ERR_BAD_FS, "HFS+ key beyond end of node");
+               return grub_error (GRUB_ERR_BAD_FS, N_("HFS+ key beyond end of node"));
 
             currnode = grub_be_to_cpu32 (grub_get_unaligned32 (pointer));
             match = 1;
@@ -711,7 +711,7 @@ list_nodes (void *record, void *hook_arg)
  if (grub_be_to_cpu16 (catkey->keylen) < HFSPLUS_CATKEY_MIN_LEN ||
      grub_be_to_cpu16 (catkey->keylen) > HFSPLUS_CATKEY_MAX_LEN)
    {
-      grub_error (GRUB_ERR_BAD_FS, "catalog key length is out of range");
+      grub_error (GRUB_ERR_BAD_FS, N_("catalog key length is out of range"));
      return 1;
    }
 
--

2.39.1

#5 lore raw

Re: [PATCH 2/4] fs/hfsplus: Prevent out of bound access in catalog file

In reply to Lidong Chen

BTW,

The HFSPLUS_CATKEY_* macros defined in this patch are based on the Technical Note TN1150: https://developer.apple.com/library/archive/technotes/tn/tn1150.html

"IMPORTANT:

The length of the key varies with the length of the string stored in the nodeName field; it

occupies only the number of bytes required to hold the name. The keyLength field

determines the actual length of the key; it varies between

kHFSPlusCatalogKeyMinimumLength (6) to kHFSPlusCatalogKeyMaximumLength (516).”

Regards,

Lidong

On Apr 20, 2023, at 10:59 AM, Lidong Chen <[email protected]> wrote:

A corrupted hfsplus can have a catalog key that is out of range. This can lead to out of bound access when advancing the pointer to access catalog file info.

---
grub-core/fs/hfsplus.c | 10 ++++++++++

1 file changed, 10 insertions(+)
grub-core/fs/hfsplus.c +0 -0
index 1ffebc8be..9c1f12574 100644
@@ -87,6 +87,9 @@ struct grub_hfsplus_catfile

#define HFSPLUS_BTNODE_MINSZ (1 << 9)
#define HFSPLUS_BTNODE_MAXSZ (1 << 15)

+#define HFSPLUS_CATKEY_MIN_LEN 6
+#define HFSPLUS_CATKEY_MAX_LEN 516
+
/* Some pre-defined file IDs. */
enum
{
@@ -699,6 +702,13 @@ list_nodes (void *record, void *hook_arg)

  catkey = (struct grub_hfsplus_catkey *) record;

+ if (grub_be_to_cpu16 (catkey->keylen) < HFSPLUS_CATKEY_MIN_LEN ||
+ grub_be_to_cpu16 (catkey->keylen) > HFSPLUS_CATKEY_MAX_LEN)
+ {
+ grub_error (GRUB_ERR_BAD_FS, "catalog key length is out of range");
+ return 1;
+ }
+
fileinfo =
(struct grub_hfsplus_catfile *) ((char *) record
+ grub_be_to_cpu16 (catkey->keylen)
--
2.39.1

#8 lore raw

Re: [PATCH 3/4] fs/hfsplus: Set grub errno to prevent NULL pointer access

In reply to Vladimir 'phcoder' Serbinenko

On Apr 20, 2023, at 3:07 PM, Vladimir 'phcoder' Serbinenko <[email protected]> wrote:

On Thu, Apr 20, 2023 at 8:00 PM Lidong Chen <[email protected]<mailto:[email protected]>> wrote:

When an invalid node size is detected in grub_hfsplus_mount(), data pinter is freed. Thus, file->data is not set. The code should also set the grub error when that happens to indicate an error and to avoid accessing the unintialized file->data in grub_file_close().
file->data is set to NULL in grub_file_open as part of zalloc. What sets it to something else?

Thanks for the review. Here is the flow that causes invalid access to file->data in grub_file_close():

static struct grub_hfsplus_data *

grub_hfsplus_mount (grub_disk_t disk)

{

  data->catalog_tree.nodesize = grub_be_to_cpu16 (header.nodesize);

if (data->catalog_tree.nodesize < 2)

goto fail; // grub_errno is not set to indicate the failure here.

fail:

  grub_free (data);

return 0;

}

static grub_err_t

grub_hfsplus_open (struct grub_file *file, const char *name)

{

  data = grub_hfsplus_mount (file->device->disk);

if (!data)

    goto fail;

fail:

   grub_free (data);

return grub_errno; // return GRUB_ERR_NONE

}

grub_file_t

grub_file_open (const char *name, enum grub_file_type type)

{

if ((file->fs->fs_open) (file, file_name) != GRUB_ERR_NONE) // grub_hfsplus_open()

    goto fail;
  file->name = grub_strdup (name);

  grub_errno = GRUB_ERR_NONE;

return file;

}

static grub_err_t

grub_hfsplus_close (grub_file_t file)

{

struct grub_hfsplus_data *data =

    (struct grub_hfsplus_data *) file->data;

grub_free (data->opened_file.cbuf); // SIGSEGV here

return GRUB_ERR_NONE;

}

Regards,

Lidong

---
grub-core/fs/hfsplus.c | 10 ++++++++--

1 file changed, 8 insertions(+), 2 deletions(-)
grub-core/fs/hfsplus.c +8 -2
index 9c1f12574..cf13e8a63 100644
@@ -357,7 +357,10 @@ grub_hfsplus_mount (grub_disk_t disk)
                         (header.key_compare == GRUB_HFSPLUSX_BINARYCOMPARE));
 
  if (data->catalog_tree.nodesize < 2)
-    goto fail;
+    {
+      grub_error (GRUB_ERR_BAD_FS, "invalid catalog node size");
+      goto fail;
+    }
 
  if (grub_hfsplus_read_file (&data->extoverflow_tree.file, 0, 0,
                             sizeof (struct grub_hfsplus_btnode),
@@ -374,7 +377,10 @@ grub_hfsplus_mount (grub_disk_t disk)
  data->extoverflow_tree.nodesize = grub_be_to_cpu16 (header.nodesize);
 
  if (data->extoverflow_tree.nodesize < 2)
-    goto fail;
+    {
+      grub_error (GRUB_ERR_BAD_FS, "invalid extents overflow node size");
+      goto fail;
+    }
 
  data->extoverflow_tree_ready = 1;
 

--
2.39.1

_______________________________________________
Grub-devel mailing list
[email protected]<mailto:[email protected]>
https://lists.gnu.org/mailman/listinfo/grub-devel

--
Regards
Vladimir 'phcoder' Serbinenko