Skip to contentSkip to conversation

xfs_repair: skip freelist scan of dodgy agf in no-modify mode

PATCH Superseded Eric Sandeen 3 participants 10 messages Mar 1, 2013 – Mar 11, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs_repair: skip freelist scan of dodgy agf in no-modify mode

In no-modify mode (-n), verify_set_agf doesn't fix up bad freelist blocks that it finds. When we get to scan_freelist, this can wreak havoc if, for example, first > last and the loop never exits; we index agfl->agfl_bno[i] off into the weeds.

To fix this, re-check the values in no-modify mode, and if they're off, warn about it and skip the scan.

In addition, add a check to verify_set_agf() to ensure that first <= last.

---

repair/agheader.c +8 -0
index 769022d..68789fe 100644
@@ -86,6 +86,14 @@ verify_set_agf(xfs_mount_t *mp, xfs_agf_t *agf, xfs_agnumber_t i)
 	 * check first/last AGF fields.  if need be, lose the free
 	 * space in the AGFL, we'll reclaim it later.
 	 */
+	if (be32_to_cpu(agf->agf_flfirst) > be32_to_cpu(agf->agf_fllast)) {
+		do_warn(_("flfirst %d in agf %d > fllast %d\n"),
+			be32_to_cpu(agf->agf_flfirst),
+			i, be32_to_cpu(agf->agf_fllast));
+		if (!no_modify)
+			agf->agf_fllast = agf->agf_flfirst = cpu_to_be32(0);
+	}
+
 	if (be32_to_cpu(agf->agf_flfirst) >= XFS_AGFL_SIZE(mp))  {
 		do_warn(_("flfirst %d in agf %d too large (max = %zu)\n"),
 			be32_to_cpu(agf->agf_flfirst),
repair/scan.c +11 -0
index 5345094..0f83fb4 100644
@@ -1067,6 +1067,17 @@ scan_freelist(
 	}
 	agfl = XFS_BUF_TO_AGFL(agflbuf);
 	i = be32_to_cpu(agf->agf_flfirst);
+	if (no_modify) {
+		/* agf values not sanitized, so double check */
+		if (i >= XFS_AGFL_SIZE(mp) ||
+		    be32_to_cpu(agf->agf_fllast) >= XFS_AGFL_SIZE(mp) ||
+		    i > be32_to_cpu(agf->agf_fllast))
+			do_warn(_("agf %d freelist blocks bad, skipping scan\n"),
+				i);
+			return;
+	} else /* should have been fixed in verify_set_agf() */
+		ASSERT(0);
+
 	count = 0;
 	for (;;) {
 		bno = be32_to_cpu(agfl->agfl_bno[i]);

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#1 lore raw
Quoted 20 lines · Eric Sandeen

In no-modify mode (-n), verify_set_agf doesn't fix up bad freelist blocks that it finds. When we get to scan_freelist, this can wreak havoc if, for example, first > last and the loop never exits; we index agfl->agfl_bno[i] off into the weeds.

To fix this, re-check the values in no-modify mode, and if they're off, warn about it and skip the scan.

In addition, add a check to verify_set_agf() to ensure that first <= last.

---

repair/agheader.c +0 -0
index 769022d..68789fe 100644

repair/agheader.c verify_set_agf(xfs_mount_t *mp, xfs_agf_t *agf, xfs_agnumber_t i) · line 95

Quoted 10 lines
repair/agheader.c +7 -0
@@ -86,6 +86,14 @@ verify_set_agf(xfs_mount_t *mp, xfs_agf_t *agf, xfs_agnumber_t i)
 	 * check first/last AGF fields.  if need be, lose the free
 	 * space in the AGFL, we'll reclaim it later.
 	 */
+	if (be32_to_cpu(agf->agf_flfirst) > be32_to_cpu(agf->agf_fllast)) {
+		do_warn(_("flfirst %d in agf %d > fllast %d\n"),
+			be32_to_cpu(agf->agf_flfirst),
+			i, be32_to_cpu(agf->agf_fllast));
+		if (!no_modify)
+			agf->agf_fllast = agf->agf_flfirst = cpu_to_be32(0);
+	}

I don't think that test is correct. The free list is circular and is indexed as a pair of head/tail pointers. Hence flfirst > fllast can be actually valid. e.g. flcount = 4, flfirst=126, fllast = 2 is a valid free list.

Quoted 9 lines
repair/agheader.c +1 -0
+
 	if (be32_to_cpu(agf->agf_flfirst) >= XFS_AGFL_SIZE(mp))  {
 		do_warn(_("flfirst %d in agf %d too large (max = %zu)\n"),
 			be32_to_cpu(agf->agf_flfirst),
repair/scan.c +0 -0
index 5345094..0f83fb4 100644

repair/scan.c scan_freelist( · line 1075

Quoted 9 lines
repair/scan.c +6 -0
@@ -1067,6 +1067,17 @@ scan_freelist(
 	}
 	agfl = XFS_BUF_TO_AGFL(agflbuf);
 	i = be32_to_cpu(agf->agf_flfirst);
+	if (no_modify) {
+		/* agf values not sanitized, so double check */
+		if (i >= XFS_AGFL_SIZE(mp) ||
+		    be32_to_cpu(agf->agf_fllast) >= XFS_AGFL_SIZE(mp) ||
+		    i > be32_to_cpu(agf->agf_fllast))
+			do_warn(_("agf %d freelist blocks bad, skipping scan\n"),

"skipping freelist scan"

repair/scan.c scan_freelist( · line 1077

Quoted 2 lines
repair/scan.c +2 -0
+				i);
+			return;

Also, you might be missing a set of {} there - that will always return immediately if no_modify is set....

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#2 lore raw
Quoted 28 lines · Dave Chinner
Quoted 27 lines · Eric Sandeen

In no-modify mode (-n), verify_set_agf doesn't fix up bad freelist blocks that it finds. When we get to scan_freelist, this can wreak havoc if, for example, first > last and the loop never exits; we index agfl->agfl_bno[i] off into the weeds.

To fix this, re-check the values in no-modify mode, and if they're off, warn about it and skip the scan.

In addition, add a check to verify_set_agf() to ensure that first <= last.

---

repair/agheader.c +4 -0
index 769022d..68789fe 100644
@@ -86,6 +86,14 @@ verify_set_agf(xfs_mount_t *mp, xfs_agf_t *agf, xfs_agnumber_t i)
    * check first/last AGF fields.  if need be, lose the free
    * space in the AGFL, we'll reclaim it later.
    */
+    if (be32_to_cpu(agf->agf_flfirst) > be32_to_cpu(agf->agf_fllast)) {
+        do_warn(_("flfirst %d in agf %d > fllast %d\n"),
+            be32_to_cpu(agf->agf_flfirst),
+            i, be32_to_cpu(agf->agf_fllast));
Quoted 9 lines

repair/agheader.c verify_set_agf(xfs_mount_t *mp, xfs_agf_t *agf, xfs_agnumber_t i) · line 95

Quoted 3 lines
repair/agheader.c +3 -0
+        if (!no_modify)
+            agf->agf_fllast = agf->agf_flfirst = cpu_to_be32(0);
+    }

I don't think that test is correct. The free list is circular and is indexed as a pair of head/tail pointers. Hence flfirst > fllast can be actually valid. e.g. flcount = 4, flfirst=126, fllast = 2 is a valid free list.

Doh, ok. Will fix.

Quoted 21 lines
Quoted 9 lines
repair/agheader.c +1 -0
+
   if (be32_to_cpu(agf->agf_flfirst) >= XFS_AGFL_SIZE(mp))  {
       do_warn(_("flfirst %d in agf %d too large (max = %zu)\n"),
           be32_to_cpu(agf->agf_flfirst),
repair/scan.c +0 -0
index 5345094..0f83fb4 100644

repair/scan.c scan_freelist( · line 1075

Quoted 9 lines
repair/scan.c +6 -0
@@ -1067,6 +1067,17 @@ scan_freelist(
   }
   agfl = XFS_BUF_TO_AGFL(agflbuf);
   i = be32_to_cpu(agf->agf_flfirst);
+    if (no_modify) {
+        /* agf values not sanitized, so double check */
+        if (i >= XFS_AGFL_SIZE(mp) ||
+            be32_to_cpu(agf->agf_fllast) >= XFS_AGFL_SIZE(mp) ||
+            i > be32_to_cpu(agf->agf_fllast))
+            do_warn(_("agf %d freelist blocks bad, skipping scan\n"),

"skipping freelist scan"

Quoted 6 lines

repair/scan.c scan_freelist( · line 1077

Quoted 2 lines
repair/scan.c +2 -0
+                i);
+            return;

Also, you might be missing a set of {} there - that will always return immediately if no_modify is set....

Cripes. 1 demerit for me, sorry. Will fix and resend.

Thanks for the embarrassing review. :)

-Eric

Quoted 7 lines

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#3 lore raw

PATCH v2 1/1 xfs_repair: skip freelist scan of corrupt agf in no-modify mode

In reply to Eric Sandeen

In xfs_repair's no-modify mode (-n), verify_set_agf doesn't fix up bad freelist blocks that it finds. When we get to scan_freelist, this can wreak havoc if, for example, first > last and the loop never exits; we index agfl->agfl_bno[i] off into the weeds.

To fix this, re-check the values in no-modify mode, and if they're off, warn about it and skip the scan.

---

V2: Remove dumb mistakes :/

repair/scan.c +12 -0
index 5345094..1d39bdc 100644
@@ -1066,6 +1066,18 @@ scan_freelist(
 		return;
 	}
 	agfl = XFS_BUF_TO_AGFL(agflbuf);
+
+	if (no_modify) {
+		/* agf values not fixed in verify_set_agf, so recheck */
+		if (be32_to_cpu(agf->agf_flfirst) >= XFS_AGFL_SIZE(mp) ||
+		    be32_to_cpu(agf->agf_fllast) >= XFS_AGFL_SIZE(mp)) {
+			do_warn(_("agf %d freelist blocks bad, skipping "
+				  "freelist scan\n"), i);
+			return;
+		}
+	} else /* should have been fixed in verify_set_agf() */
+		ASSERT(0);
+
 	i = be32_to_cpu(agf->agf_flfirst);
 	count = 0;
 	for (;;) {

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#4 lore raw

Re: [PATCH V2] xfs_repair: skip freelist scan of corrupt agf in no-modify mode

Quoted 27 lines · Eric Sandeen

In xfs_repair's no-modify mode (-n), verify_set_agf doesn't fix up bad freelist blocks that it finds. When we get to scan_freelist, this can wreak havoc if, for example, first > last and the loop never exits; we index agfl->agfl_bno[i] off into the weeds.

To fix this, re-check the values in no-modify mode, and if they're off, warn about it and skip the scan.

---

V2: Remove dumb mistakes :/

repair/scan.c +5 -0
index 5345094..1d39bdc 100644
@@ -1066,6 +1066,18 @@ scan_freelist(
 		return;
 	}
 	agfl = XFS_BUF_TO_AGFL(agflbuf);
+
+	if (no_modify) {
+		/* agf values not fixed in verify_set_agf, so recheck */
+		if (be32_to_cpu(agf->agf_flfirst) >= XFS_AGFL_SIZE(mp) ||
+		    be32_to_cpu(agf->agf_fllast) >= XFS_AGFL_SIZE(mp)) {

repair/scan.c scan_freelist( · line 1083

Quoted 10 lines
repair/scan.c +7 -0
+			do_warn(_("agf %d freelist blocks bad, skipping "
+				  "freelist scan\n"), i);
+			return;
+		}
+	} else /* should have been fixed in verify_set_agf() */
+		ASSERT(0);
+
 	i = be32_to_cpu(agf->agf_flfirst);
 	count = 0;
 	for (;;) {

Looks ok, but IIRC there are overruns in these functions for the same reason (i.e. unchecked use of agf->agf_flfirst as an array index)

db/check.c::scan_freelist()
db/freesp.c::scan_freelist()

I found lots of almost-but-not-quite-exact code duplication like this recenty when doing CRC updates to the userspace code....

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Reviewed-by on v2 1/1
#5 lore raw

Re: [PATCH V2] xfs_repair: skip freelist scan of corrupt agf in no-modify mode

In reply to Eric Sandeen

This version looks good. ;)

This has been committed.

commit 7e8e3cce00f38ee1533df0e7bda6bcb584b03e96

Author: Eric Sandeen <[email protected]>
Date:   Sat Mar 2 21:23:12 2013 +0000

xfsprogs: xfs_repair skip freelist scan of corrupt agf in no-modify
mode

In xfs_repair's no-modify mode (-n), verify_set_agf doesn't fix up bad freelist blocks that it finds. When we get to scan_freelist, this can wreak havoc if, for example, first > last and the loop never exits; we index agfl->agfl_bno[i] off into the weeds.

To fix this, re-check the values in no-modify mode, and if they're off, warn about it and skip the scan.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#6 lore raw

Re: [PATCH V2] xfs_repair: skip freelist scan of corrupt agf in no-modify mode

Quoted 5 lines · Rich Johnston

This version looks good. ;)

This has been committed.

Thanks; from what Dave said there are probably other spots that need love as well.

-Eric

Quoted 6 lines

commit 7e8e3cce00f38ee1533df0e7bda6bcb584b03e96

Author: Eric Sandeen <[email protected]>
Date:   Sat Mar 2 21:23:12 2013 +0000

xfsprogs: xfs_repair skip freelist scan of corrupt agf in no-modify mode

Quoted 10 lines

In xfs_repair's no-modify mode (-n), verify_set_agf doesn't fix up bad freelist blocks that it finds. When we get to scan_freelist, this can wreak havoc if, for example, first > last and the loop never exits; we index agfl->agfl_bno[i] off into the weeds.

To fix this, re-check the values in no-modify mode, and if they're off, warn about it and skip the scan.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#7 lore raw

Re: [PATCH V2] xfs_repair: skip freelist scan of corrupt agf in no-modify mode

In reply to Rich Johnston
Quoted 5 lines · Rich Johnston

This version looks good. ;)

This has been committed.

Except that all review comments have not been addressed. i.e there are two places that have the same bug and they haven't been fixed.

It's great that you want to commit quickly, but commits should not happen while there are unaddressed issues still outstanding....

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#8 lore raw

Re: [PATCH V2] xfs_repair: skip freelist scan of corrupt agf in no-modify mode

Quoted 5 lines · Dave Chinner
Quoted 4 lines · Rich Johnston

This version looks good. ;)

Quoted 7 lines
Quoted 1 line

This has been committed.

Except that all review comments have not been addressed. i.e there are two places that have the same bug and they haven't been fixed.

It's great that you want to commit quickly, but commits should not happen while there are unaddressed issues still outstanding....

Sorry Dave I misunderstood your comments. I took them to mean you were going to make those changes with your CRC patch. I will make sure to clarify before committing.

Quoted 5 lines

Cheers,

Dave.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#9 lore raw

Re: [PATCH V2] xfs_repair: skip freelist scan of corrupt agf in no-modify mode

In reply to Rich Johnston
Quoted 1 line · Rich Johnston

This version looks good. ;)

Actually it's not quite:

scan.c: In function ‘scan_freelist’:
scan.c:1074: warning: ‘i’ may be used uninitialized in this function

I don't know what's wrong with my brain these days. :/

I will send a patch to fix that along w/ the other scan_freelists shortly.

Sorry :/

-Eric

Quoted 20 lines

This has been committed.

commit 7e8e3cce00f38ee1533df0e7bda6bcb584b03e96

Author: Eric Sandeen <[email protected]>
Date:   Sat Mar 2 21:23:12 2013 +0000

xfsprogs: xfs_repair skip freelist scan of corrupt agf in no-modify mode

In xfs_repair's no-modify mode (-n), verify_set_agf doesn't fix up bad freelist blocks that it finds. When we get to scan_freelist, this can wreak havoc if, for example, first > last and the loop never exits; we index agfl->agfl_bno[i] off into the weeds.

To fix this, re-check the values in no-modify mode, and if they're off, warn about it and skip the scan.

Quoted 5 lines

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs