Skip to contentSkip to conversation

fs/xfs: Handle non-continuous data blocks in directory extents

PATCH In review Jon DeVree 5 participants 9 messages Feb 11, 2024 – Mar 18, 2024

lore
Share & tools
lore

Jon DeVree

author
#0 lore raw

PATCH 1/1 fs/xfs: Handle non-continuous data blocks in directory extents

The directory extent list does not have to be a continuous list of data blocks. When GRUB tries to read a non-existant member of the list, grub_xfs_read_file() will return a block of zero'ed memory. Checking for a zero'ed magic number is sufficient to skip this non-existant data block.

Prior to commit 07318ee7e this was handled as a subtle side effect of reading the (non-existant) tail data structure. Since the block was zero'ed the computation of the number of directory entries in the block would return 0 as well.

Fixes 07318ee7e (fs/xfs: Fix XFS directory extent parsing)
---
 grub-core/fs/xfs.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
grub-core/fs/xfs.c +10 -0
index bc2224dbb..a6cce9cfe 100644
@@ -902,6 +902,7 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir,
 					grub_xfs_first_de(dir->data, dirblock);
 	    int entries = -1;
 	    char *end = dirblock + dirblk_size;
+	    grub_uint32_t magic;
 
 	    numread = grub_xfs_read_file (dir, 0, 0,
 					  blk << dirblk_log2,
@@ -912,6 +913,15 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir,
 	        return 0;
 	      }
 
+	    /*
+	     * If this data block isn't actually part of the extent list then
+	     * grub_xfs_read_file() returns a block of zeros. So if the magic number
+	     * field is all zeros then this block should be skipped.
+	     */
+	    magic = *(grub_uint32_t *)dirblock;
+	    if (!magic)
+	      continue;
+
 	    /*
 	     * Leaf and tail information are only in the data block if the number
 	     * of extents is 1.
Signature · 7 lines
2.43.0


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

Adding Marta, Nicolas, Sebastian and Vladimir...

Jon, thank you for the patch!

Marta, Nicolas and Sebastian, may I ask you to run tests with this fix?

Daniel

Quoted 47 lines · Jon DeVree

The directory extent list does not have to be a continuous list of data blocks. When GRUB tries to read a non-existant member of the list, grub_xfs_read_file() will return a block of zero'ed memory. Checking for a zero'ed magic number is sufficient to skip this non-existant data block.

Prior to commit 07318ee7e this was handled as a subtle side effect of reading the (non-existant) tail data structure. Since the block was zero'ed the computation of the number of directory entries in the block would return 0 as well.

Fixes 07318ee7e (fs/xfs: Fix XFS directory extent parsing)
---
 grub-core/fs/xfs.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
grub-core/fs/xfs.c +10 -1
index bc2224dbb..a6cce9cfe 100644
@@ -902,6 +902,7 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir,
 					grub_xfs_first_de(dir->data, dirblock);
 	    int entries = -1;
 	    char *end = dirblock + dirblk_size;
+	    grub_uint32_t magic;
 
 	    numread = grub_xfs_read_file (dir, 0, 0,
 					  blk << dirblk_log2,
@@ -912,6 +913,15 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir,
 	        return 0;
 	      }
 
+	    /*
+	     * If this data block isn't actually part of the extent list then
+	     * grub_xfs_read_file() returns a block of zeros. So if the magic number
+	     * field is all zeros then this block should be skipped.
+	     */
+	    magic = *(grub_uint32_t *)dirblock;
+	    if (!magic)
+	      continue;
+
 	    /*
 	     * Leaf and tail information are only in the data block if the number
 	     * of extents is 1.
--
Quoted 1 line

2.43.0

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

#2 lore raw
Quoted 6 lines · Daniel Kiper

Adding Marta, Nicolas, Sebastian and Vladimir...

Jon, thank you for the patch!

Marta, Nicolas and Sebastian, may I ask you to run tests with this fix?

Thanks Jon, Daniel.
Sure, I will give it a try.
I was actually about to send my own version for review.

Quoted 38 lines

Daniel

Quoted 34 lines · Jon DeVree

The directory extent list does not have to be a continuous list of data blocks. When GRUB tries to read a non-existant member of the list, grub_xfs_read_file() will return a block of zero'ed memory. Checking for a zero'ed magic number is sufficient to skip this non-existant data block.

Prior to commit 07318ee7e this was handled as a subtle side effect of reading the (non-existant) tail data structure. Since the block was zero'ed the computation of the number of directory entries in the block would return 0 as well.

Fixes 07318ee7e (fs/xfs: Fix XFS directory extent parsing)
---
 grub-core/fs/xfs.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
grub-core/fs/xfs.c +1 -0
index bc2224dbb..a6cce9cfe 100644
@@ -902,6 +902,7 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir,
                                      grub_xfs_first_de(dir->data, dirblock);
          int entries = -1;
          char *end = dirblock + dirblk_size;
+         grub_uint32_t magic;
 
          numread = grub_xfs_read_file (dir, 0, 0,
                                        blk << dirblk_log2,
@@ -912,6 +913,15 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir,
              return 0;
            }
 
Quoted 15 lines
Quoted 13 lines
grub-core/fs/xfs.c +9 -1
+         /*
+          * If this data block isn't actually part of the extent list then
+          * grub_xfs_read_file() returns a block of zeros. So if the magic number
+          * field is all zeros then this block should be skipped.
+          */
+         magic = *(grub_uint32_t *)dirblock;
+         if (!magic)
+           continue;
+
          /*
           * Leaf and tail information are only in the data block if the number
           * of extents is 1.
--
Quoted 1 line

2.43.0

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

#3 lore raw

Tested with a VM disk that has unmapped blocks and that fixes it.

Quoted 5 lines · Nicolas Frayer
Quoted 3 lines · Daniel Kiper

Adding Marta, Nicolas, Sebastian and Vladimir...

Quoted 61 lines
Quoted 3 lines

Jon, thank you for the patch!

Marta, Nicolas and Sebastian, may I ask you to run tests with this fix?

Thanks Jon, Daniel.
Sure, I will give it a try.
I was actually about to send my own version for review.

Quoted 53 lines

Daniel

Quoted 48 lines · Jon DeVree

The directory extent list does not have to be a continuous list of data blocks. When GRUB tries to read a non-existant member of the list, grub_xfs_read_file() will return a block of zero'ed memory. Checking for a zero'ed magic number is sufficient to skip this non-existant data block.

Prior to commit 07318ee7e this was handled as a subtle side effect of reading the (non-existant) tail data structure. Since the block was zero'ed the computation of the number of directory entries in the block would return 0 as well.

Fixes 07318ee7e (fs/xfs: Fix XFS directory extent parsing)
---
 grub-core/fs/xfs.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
grub-core/fs/xfs.c +10 -1
index bc2224dbb..a6cce9cfe 100644
@@ -902,6 +902,7 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir,
                                      grub_xfs_first_de(dir->data, dirblock);
          int entries = -1;
          char *end = dirblock + dirblk_size;
+         grub_uint32_t magic;
 
          numread = grub_xfs_read_file (dir, 0, 0,
                                        blk << dirblk_log2,
@@ -912,6 +913,15 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir,
              return 0;
            }
 
+         /*
+          * If this data block isn't actually part of the extent list then
+          * grub_xfs_read_file() returns a block of zeros. So if the magic number
+          * field is all zeros then this block should be skipped.
+          */
+         magic = *(grub_uint32_t *)dirblock;
+         if (!magic)
+           continue;
+
          /*
           * Leaf and tail information are only in the data block if the number
           * of extents is 1.
--

2.43.0

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

#4 lore raw
In reply to Daniel Kiper

Thank you, Jon. I am also happy to test. Nicolas and I are all too familiar with this now!

-marta

Quoted 43 lines · Daniel Kiper

Adding Marta, Nicolas, Sebastian and Vladimir...

Jon, thank you for the patch!

Marta, Nicolas and Sebastian, may I ask you to run tests with this fix?

Daniel

Quoted 34 lines · Jon DeVree

The directory extent list does not have to be a continuous list of data blocks. When GRUB tries to read a non-existant member of the list, grub_xfs_read_file() will return a block of zero'ed memory. Checking for a zero'ed magic number is sufficient to skip this non-existant data block.

Prior to commit 07318ee7e this was handled as a subtle side effect of reading the (non-existant) tail data structure. Since the block was zero'ed the computation of the number of directory entries in the block would return 0 as well.

Fixes 07318ee7e (fs/xfs: Fix XFS directory extent parsing)
---
  grub-core/fs/xfs.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
grub-core/fs/xfs.c +1 -0
index bc2224dbb..a6cce9cfe 100644
@@ -902,6 +902,7 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir,
  					grub_xfs_first_de(dir->data, dirblock);
  	    int entries = -1;
  	    char *end = dirblock + dirblk_size;
+	    grub_uint32_t magic;
 
  	    numread = grub_xfs_read_file (dir, 0, 0,
  					  blk << dirblk_log2,
@@ -912,6 +913,15 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir,
  	        return 0;
  	      }
 
Quoted 14 lines
Quoted 13 lines
grub-core/fs/xfs.c +9 -1
+	    /*
+	     * If this data block isn't actually part of the extent list then
+	     * grub_xfs_read_file() returns a block of zeros. So if the magic number
+	     * field is all zeros then this block should be skipped.
+	     */
+	    magic = *(grub_uint32_t *)dirblock;
+	    if (!magic)
+	      continue;
+
  	    /*
  	     * Leaf and tail information are only in the data block if the number
  	     * of extents is 1.
--
Quoted 1 line

2.43.0

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

#5 lore raw
In reply to Jon DeVree

Is the pointer guaranteed to be aligned? If not you have to use unaligned access. Any reason not to check it against correct magic and not just zero-out?

Quoted 54 lines · Jon DeVree

The directory extent list does not have to be a continuous list of data blocks. When GRUB tries to read a non-existant member of the list, grub_xfs_read_file() will return a block of zero'ed memory. Checking for a zero'ed magic number is sufficient to skip this non-existant data block.

Prior to commit 07318ee7e this was handled as a subtle side effect of reading the (non-existant) tail data structure. Since the block was zero'ed the computation of the number of directory entries in the block would return 0 as well.

Fixes 07318ee7e (fs/xfs: Fix XFS directory extent parsing)
---
 grub-core/fs/xfs.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
grub-core/fs/xfs.c +1 -0
index bc2224dbb..a6cce9cfe 100644
@@ -902,6 +902,7 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir,
                                        grub_xfs_first_de(dir->data,
 dirblock);
            int entries = -1;
            char *end = dirblock + dirblk_size;
+           grub_uint32_t magic;
 
            numread = grub_xfs_read_file (dir, 0, 0,
                                          blk << dirblk_log2,
grub-core/fs/xfs.c +2 -0
@@ -912,6 +913,15 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir,
                return 0;
              }
 
+           /*
+            * If this data block isn't actually part of the extent list

then
+ * grub_xfs_read_file() returns a block of zeros. So if the
magic number

grub-core/fs/xfs.c +6 -0
+            * field is all zeros then this block should be skipped.
+            */
+           magic = *(grub_uint32_t *)dirblock;
+           if (!magic)
+             continue;
+
            /*
             * Leaf and tail information are only in the data block if the

number
* of extents is 1.
--
2.43.0

Quoted 5 lines

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

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

author
#6 lore raw
Quoted 2 lines · Vladimir 'phcoder' Serbinenko

Is the pointer guaranteed to be aligned? If not you have to use unaligned access.

I don't know what alignment guarantees grub_malloc() makes. As long as they are close to what the regular C malloc() guarantees then it should be fine.

Quoted 2 lines

Any reason not to check it against correct magic and not just zero-out?

I found at least 4 different magic numbers in the XFS documentation and I wasn't sure if there were any more.

Signature · 8 lines
Jon
Doge Wrangler
X(7): A program for managing terminal windows. See also screen(1) and tmux(1).

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#7 lore raw
Quoted 8 lines · Jon DeVree
Quoted 2 lines · Vladimir 'phcoder' Serbinenko

Is the pointer guaranteed to be aligned? If not you have to use unaligned access.

I don't know what alignment guarantees grub_malloc() makes. As long as they are close to what the regular C malloc() guarantees then it should be fine.

grub_malloc design guarantees the largest natural alignment of all types. grub_malloc implementation guarantees alignment of 4 * sizeof(void*).

My concern was that the pointer is moved after alloc but it turns own it was not the case.
So there are no unaligned access concerns

The code still violates cast-align and strict aliasing invariants but so is the rest of XFS code.
So I'm happy to merge this patch

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

Reviewed-by
#8 lore raw
Quoted 17 lines · Vladimir 'phcoder' Serbinenko
Quoted 8 lines · Jon DeVree
Quoted 2 lines · Vladimir 'phcoder' Serbinenko

Is the pointer guaranteed to be aligned? If not you have to use unaligned access.

I don't know what alignment guarantees grub_malloc() makes. As long as they are close to what the regular C malloc() guarantees then it should be fine.

grub_malloc design guarantees the largest natural alignment of all types. grub_malloc implementation guarantees alignment of 4 * sizeof(void*).

My concern was that the pointer is moved after alloc but it turns own it was not the case.
So there are no unaligned access concerns

Quoted 5 lines

The code still violates cast-align and strict aliasing invariants but so is the rest of XFS code.
So I'm happy to merge this patch

I will push this patch together with other patches this week. Sorry for delay...

Daniel

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