07cf41c
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
07cf41c
From: Erwan Velu <erwanaliasr1@gmail.com>
07cf41c
Date: Wed, 25 Aug 2021 15:31:52 +0200
07cf41c
Subject: [PATCH] fs/xfs: Fix unreadable filesystem with v4 superblock
07cf41c
07cf41c
The commit 8b1e5d193 (fs/xfs: Add bigtime incompat feature support)
07cf41c
introduced the bigtime support by adding some features in v3 inodes.
07cf41c
This change extended grub_xfs_inode struct by 76 bytes but also changed
07cf41c
the computation of XFS_V2_INODE_SIZE and XFS_V3_INODE_SIZE. Prior this
07cf41c
commit, XFS_V2_INODE_SIZE was 100 bytes. After the commit it's 84 bytes
07cf41c
XFS_V2_INODE_SIZE becomes 16 bytes too small.
07cf41c
07cf41c
As a result, the data structures aren't properly aligned and the GRUB
07cf41c
generates "attempt to read or write outside of partition" errors when
07cf41c
trying to read the XFS filesystem:
07cf41c
07cf41c
                             GNU GRUB  version 2.11
07cf41c
	....
07cf41c
	grub> set debug=efi,gpt,xfs
07cf41c
	grub> insmod part_gpt
07cf41c
	grub> ls (hd0,gpt1)/
07cf41c
	partmap/gpt.c:93: Read a valid GPT header
07cf41c
	partmap/gpt.c:115: GPT entry 0: start=4096, length=1953125
07cf41c
	fs/xfs.c:931: Reading sb
07cf41c
	fs/xfs.c:270: Validating superblock
07cf41c
	fs/xfs.c:295: XFS v4 superblock detected
07cf41c
	fs/xfs.c:962: Reading root ino 128
07cf41c
	fs/xfs.c:515: Reading inode (128) - 64, 0
07cf41c
	fs/xfs.c:515: Reading inode (739521961424144223) - 344365866970255880, 3840
07cf41c
	error: attempt to read or write outside of partition.
07cf41c
07cf41c
This commit change the XFS_V2_INODE_SIZE computation by subtracting 76
07cf41c
bytes instead of 92 bytes from the actual size of grub_xfs_inode struct.
07cf41c
This 76 bytes value comes from added members:
07cf41c
	20 grub_uint8_t   unused5
07cf41c
	 1 grub_uint64_t  flags2
07cf41c
        48 grub_uint8_t   unused6
07cf41c
07cf41c
This patch explicitly splits the v2 and v3 parts of the structure.
07cf41c
The unused4 is still ending of the v2 structures and the v3 starts
07cf41c
at unused5. Thanks to this we will avoid future corruptions of v2
07cf41c
or v3 inodes.
07cf41c
07cf41c
The XFS_V2_INODE_SIZE is returning to its expected size and the
07cf41c
filesystem is back to a readable state:
07cf41c
07cf41c
                      GNU GRUB  version 2.11
07cf41c
	....
07cf41c
	grub> set debug=efi,gpt,xfs
07cf41c
	grub> insmod part_gpt
07cf41c
	grub> ls (hd0,gpt1)/
07cf41c
	partmap/gpt.c:93: Read a valid GPT header
07cf41c
	partmap/gpt.c:115: GPT entry 0: start=4096, length=1953125
07cf41c
	fs/xfs.c:931: Reading sb
07cf41c
	fs/xfs.c:270: Validating superblock
07cf41c
	fs/xfs.c:295: XFS v4 superblock detected
07cf41c
	fs/xfs.c:962: Reading root ino 128
07cf41c
	fs/xfs.c:515: Reading inode (128) - 64, 0
07cf41c
	fs/xfs.c:515: Reading inode (128) - 64, 0
07cf41c
	fs/xfs.c:931: Reading sb
07cf41c
	fs/xfs.c:270: Validating superblock
07cf41c
	fs/xfs.c:295: XFS v4 superblock detected
07cf41c
	fs/xfs.c:962: Reading root ino 128
07cf41c
	fs/xfs.c:515: Reading inode (128) - 64, 0
07cf41c
	fs/xfs.c:515: Reading inode (128) - 64, 0
07cf41c
	fs/xfs.c:515: Reading inode (128) - 64, 0
07cf41c
	fs/xfs.c:515: Reading inode (131) - 64, 768
07cf41c
	efi/ fs/xfs.c:515: Reading inode (3145856) - 1464904, 0
07cf41c
	grub2/ fs/xfs.c:515: Reading inode (132) - 64, 1024
07cf41c
	grub/ fs/xfs.c:515: Reading inode (139) - 64, 2816
07cf41c
	grub>
07cf41c
07cf41c
Fixes: 8b1e5d193 (fs/xfs: Add bigtime incompat feature support)
07cf41c
07cf41c
Signed-off-by: Erwan Velu <e.velu@criteo.com>
07cf41c
Tested-by: Carlos Maiolino <cmaiolino@redhat.com>
07cf41c
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
07cf41c
(cherry picked from commit a4b495520e4dc41a896a8b916a64eda9970c50ea)
07cf41c
---
07cf41c
 grub-core/fs/xfs.c | 14 ++++++++++----
07cf41c
 1 file changed, 10 insertions(+), 4 deletions(-)
07cf41c
07cf41c
diff --git a/grub-core/fs/xfs.c b/grub-core/fs/xfs.c
e622855
index 0f524c3a8a..e3816d1ec4 100644
07cf41c
--- a/grub-core/fs/xfs.c
07cf41c
+++ b/grub-core/fs/xfs.c
07cf41c
@@ -192,6 +192,11 @@ struct grub_xfs_time_legacy
07cf41c
   grub_uint32_t nanosec;
07cf41c
 } GRUB_PACKED;
07cf41c
 
07cf41c
+/*
07cf41c
+ * The struct grub_xfs_inode layout was taken from the
07cf41c
+ * struct xfs_dinode_core which is described here:
07cf41c
+ * https://mirrors.edge.kernel.org/pub/linux/utils/fs/xfs/docs/xfs_filesystem_structure.pdf
07cf41c
+ */
07cf41c
 struct grub_xfs_inode
07cf41c
 {
07cf41c
   grub_uint8_t magic[2];
07cf41c
@@ -208,14 +213,15 @@ struct grub_xfs_inode
07cf41c
   grub_uint32_t nextents;
07cf41c
   grub_uint16_t unused3;
07cf41c
   grub_uint8_t fork_offset;
07cf41c
-  grub_uint8_t unused4[37];
07cf41c
+  grub_uint8_t unused4[17]; /* Last member of inode v2. */
07cf41c
+  grub_uint8_t unused5[20]; /* First member of inode v3. */
07cf41c
   grub_uint64_t flags2;
07cf41c
-  grub_uint8_t unused5[48];
07cf41c
+  grub_uint8_t unused6[48]; /* Last member of inode v3. */
07cf41c
 } GRUB_PACKED;
07cf41c
 
07cf41c
 #define XFS_V3_INODE_SIZE	sizeof(struct grub_xfs_inode)
07cf41c
-/* Size of struct grub_xfs_inode until fork_offset (included). */
07cf41c
-#define XFS_V2_INODE_SIZE	(XFS_V3_INODE_SIZE - 92)
07cf41c
+/* Size of struct grub_xfs_inode v2, up to unused4 member included. */
07cf41c
+#define XFS_V2_INODE_SIZE	(XFS_V3_INODE_SIZE - 76)
07cf41c
 
07cf41c
 struct grub_xfs_dirblock_tail
07cf41c
 {