Skip to contentSkip to conversation

xfs_repair: fix off by one error when rebuilding high keys

PATCH In review Darrick J. Wong 3 participants 3 messages Jan 3, 2019 – Jan 4, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs_repair: fix off by one error when rebuilding high keys

From: Darrick J. Wong <[email protected]>

Fix an off-by-one error when scanning a rmap btree block for high keys as part of rebuilding rmap btrees during phase 5. This causes xfs_repair to emit a corrupt filesystem, which is bad.

This can be reproduced pretty easily by exporting TEST_XFS_REPAIR_REBUILD=1 and running generic/051 with a 1k block size.

---
 repair/phase5.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
repair/phase5.c +1 -1
index 85d1f4fb..1bacfc7f 100644
@@ -1500,7 +1500,7 @@ prop_rmap_highkey(
 		bt_key->rm_offset = cpu_to_be64(
 				libxfs_rmap_irec_offset_pack(&high_key));
 
-		for (i = 1; i < numrecs - 1; i++) {
+		for (i = 1; i <= numrecs; i++) {
 			bt_key = XFS_RMAP_HIGH_KEY_ADDR(bt_hdr, i);
 			key.rm_startblock = be32_to_cpu(bt_key->rm_startblock);
 			key.rm_owner = be64_to_cpu(bt_key->rm_owner);
#1 lore raw
Quoted 19 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Fix an off-by-one error when scanning a rmap btree block for high keys as part of rebuilding rmap btrees during phase 5. This causes xfs_repair to emit a corrupt filesystem, which is bad.

This can be reproduced pretty easily by exporting TEST_XFS_REPAIR_REBUILD=1 and running generic/051 with a 1k block size.

---
 repair/phase5.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
repair/phase5.c +0 -0
index 85d1f4fb..1bacfc7f 100644

repair/phase5.c prop_rmap_highkey( · line 1503

Quoted 5 lines
repair/phase5.c +1 -1
@@ -1500,7 +1500,7 @@ prop_rmap_highkey(
 		bt_key->rm_offset = cpu_to_be64(
 				libxfs_rmap_irec_offset_pack(&high_key));
 
-		for (i = 1; i < numrecs - 1; i++) {
+		for (i = 1; i <= numrecs; i++) {

isn't that actually off by 2?

Why do all the XFS_RMAP_*() macros take a 1-based index and then subtract 1 to get back to 0-based? Seems like that's the kind of oddity that leads to bugs like this, no? Maybe I'm not seeing the big picture.

-Eric

Quoted 4 lines
repair/phase5.c +0 -0
 			bt_key = XFS_RMAP_HIGH_KEY_ADDR(bt_hdr, i);
 			key.rm_startblock = be32_to_cpu(bt_key->rm_startblock);
 			key.rm_owner = be64_to_cpu(bt_key->rm_owner);
Reviewed-by on v1 1/1
#2 lore raw
In reply to Darrick J. Wong
Quoted 10 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Fix an off-by-one error when scanning a rmap btree block for high keys as part of rebuilding rmap btrees during phase 5. This causes xfs_repair to emit a corrupt filesystem, which is bad.

This can be reproduced pretty easily by exporting TEST_XFS_REPAIR_REBUILD=1 and running generic/051 with a 1k block size.

changing the weird 1-indexing does sound like a good idea tho.

Quoted 18 lines
---
 repair/phase5.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
repair/phase5.c +1 -1
index 85d1f4fb..1bacfc7f 100644
@@ -1500,7 +1500,7 @@ prop_rmap_highkey(
 		bt_key->rm_offset = cpu_to_be64(
 				libxfs_rmap_irec_offset_pack(&high_key));
 
-		for (i = 1; i < numrecs - 1; i++) {
+		for (i = 1; i <= numrecs; i++) {
 			bt_key = XFS_RMAP_HIGH_KEY_ADDR(bt_hdr, i);
 			key.rm_startblock = be32_to_cpu(bt_key->rm_startblock);
 			key.rm_owner = be64_to_cpu(bt_key->rm_owner);