Skip to contentSkip to conversation

xfsprogs/db: fix a segfault in the print command

PATCH In review Shan Hai 3 participants 5 messages Apr 7, 2017

lore
Share & tools
lore

Shan Hai

author
#0 lore raw

cover xfsprogs/db: fix a segfault in the print command

There is a segfault in the xfs_db as below:

mkfs.xfs -f /dev/sda1
xfs_db -r -c "agf 0" -c "addr rmaproot" -c "p" /dev/sda1 Metadata CRC error detected at xfs_rmapbt block 0x0/0x1000 Segmentation fault (core dumped)

---
 db/print.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Thanks
Shan Hai

author
#1 lore raw

PATCH 1/1 xfsprogs/db: fix a segfault by bailing out the print on a corrupted block

The print command of the xfs_db would cause segfault on a corrupted or CRC error block, fix it by bailing out the print when encountered a corrupted or CRC error block, below is an example to reproduce the segfault:

mkfs.xfs -f /dev/sda1
xfs_db -r -c "agf 0" -c "addr rmaproot" -c "p" /dev/sda1 Metadata CRC error detected at xfs_rmapbt block 0x0/0x1000 Segmentation fault (core dumped)

---
 db/print.c | 8 ++++++++
 1 file changed, 8 insertions(+)
db/print.c +8 -0
index e31372f..08bb39a 100644
@@ -69,11 +69,19 @@ print_f(
 	char	**argv)
 {
 	pfunc_t	pf;
+	int err;
 
 	if (cur_typ == NULL) {
 		dbprintf(_("no current type\n"));
 		return 0;
 	}
+
+	err = iocur_top->bp->b_error;
+	if ((err == -EFSCORRUPTED) || (err == -EFSBADCRC)) {
+		dbprintf(_("data corrupted or CRC error\n"));
+		return 0;
+	}
+
 	pf = cur_typ->pfunc;
 	if (pf == NULL) {
 		dbprintf(_("no print function for type %s\n"), cur_typ->name);
Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH 1/1] xfsprogs/db: fix a segfault by bailing out the print on a corrupted block

Quoted 22 lines · Shan Hai

The print command of the xfs_db would cause segfault on a corrupted or CRC error block, fix it by bailing out the print when encountered a corrupted or CRC error block, below is an example to reproduce the segfault:

mkfs.xfs -f /dev/sda1
xfs_db -r -c "agf 0" -c "addr rmaproot" -c "p" /dev/sda1 Metadata CRC error detected at xfs_rmapbt block 0x0/0x1000 Segmentation fault (core dumped)

---
 db/print.c | 8 ++++++++
 1 file changed, 8 insertions(+)
db/print.c +0 -0
index e31372f..08bb39a 100644
@@ -69,11 +69,19 @@ print_f(
 	char	**argv)
 {

db/print.c print_f( · line 80

Quoted 10 lines
db/print.c +4 -0
 	pfunc_t	pf;
+	int err;
 
 	if (cur_typ == NULL) {
 		dbprintf(_("no current type\n"));
 		return 0;
 	}
+
+	err = iocur_top->bp->b_error;
+	if ((err == -EFSCORRUPTED) || (err == -EFSBADCRC)) {

The double parentheses aren't needed for integer comparison.

Also, no need to send a cover letter if you're only sending one patch.

--D

Quoted 14 lines
db/print.c +4 -0
+		dbprintf(_("data corrupted or CRC error\n"));
+		return 0;
+	}
+
 	pf = cur_typ->pfunc;
 	if (pf == NULL) {
 		dbprintf(_("no print function for type %s\n"), cur_typ->name);
Signature · 6 lines
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw

Re: [PATCH 1/1] xfsprogs/db: fix a segfault by bailing out the print on a corrupted block

In reply to Shan Hai
Quoted 9 lines · Shan Hai

The print command of the xfs_db would cause segfault on a corrupted or CRC error block, fix it by bailing out the print when encountered a corrupted or CRC error block, below is an example to reproduce the segfault:

mkfs.xfs -f /dev/sda1
xfs_db -r -c "agf 0" -c "addr rmaproot" -c "p" /dev/sda1 Metadata CRC error detected at xfs_rmapbt block 0x0/0x1000 Segmentation fault (core dumped)

Simply refusing to print any structure with corruption would defeat the purpose of xfs_db, I think - it is difficult to analyze a corrupted structure if you cannot examine it.

Today:

# xfs_db -x fsfile2
xfs_db> inode 128
xfs_db> write -c core.magic 0x4949
Allowing write of corrupted data and bad CRC
core.magic = 0x4949
xfs_db> quit
# xfs_db -x fsfile2
xfs_db> inode 128
Metadata corruption detected at xfs_inode block 0x80/0x2000
Metadata CRC error detected for ino 128
xfs_db> p core.magic
core.magic = 0x4949
xfs_db> 

i.e. we can read the structure and print the values even if it is corrupted.

but with your change on the same damaged inode:

xfs_db> p core.magic
data corrupted or CRC error
xfs_db>

That's not useful.

I think you need to find the root cause of the segfault, and handle it if possible, instead of refusing to print any corrupted structures.

-Eric

Quoted 30 lines
---
 db/print.c | 8 ++++++++
 1 file changed, 8 insertions(+)
db/print.c +8 -0
index e31372f..08bb39a 100644
@@ -69,11 +69,19 @@ print_f(
 	char	**argv)
 {
 	pfunc_t	pf;
+	int err;
 
 	if (cur_typ == NULL) {
 		dbprintf(_("no current type\n"));
 		return 0;
 	}
+
+	err = iocur_top->bp->b_error;
+	if ((err == -EFSCORRUPTED) || (err == -EFSBADCRC)) {
+		dbprintf(_("data corrupted or CRC error\n"));
+		return 0;
+	}
+
 	pf = cur_typ->pfunc;
 	if (pf == NULL) {
 		dbprintf(_("no print function for type %s\n"), cur_typ->name);
#4 lore raw

Re: [PATCH 1/1] xfsprogs/db: fix a segfault by bailing out the print on a corrupted block

Quoted 37 lines · Eric Sandeen
Quoted 9 lines · Shan Hai

The print command of the xfs_db would cause segfault on a corrupted or CRC error block, fix it by bailing out the print when encountered a corrupted or CRC error block, below is an example to reproduce the segfault:

mkfs.xfs -f /dev/sda1
xfs_db -r -c "agf 0" -c "addr rmaproot" -c "p" /dev/sda1 Metadata CRC error detected at xfs_rmapbt block 0x0/0x1000 Segmentation fault (core dumped)

Simply refusing to print any structure with corruption would defeat the purpose of xfs_db, I think - it is difficult to analyze a corrupted structure if you cannot examine it.

Today:

# xfs_db -x fsfile2
xfs_db> inode 128
xfs_db> write -c core.magic 0x4949
Allowing write of corrupted data and bad CRC
core.magic = 0x4949
xfs_db> quit
# xfs_db -x fsfile2
xfs_db> inode 128
Metadata corruption detected at xfs_inode block 0x80/0x2000
Metadata CRC error detected for ino 128
xfs_db> p core.magic
core.magic = 0x4949
xfs_db> 

i.e. we can read the structure and print the values even if it is corrupted.

but with your change on the same damaged inode:

Quoted 8 lines

xfs_db> p core.magic
data corrupted or CRC error
xfs_db>

That's not useful.

I think you need to find the root cause of the segfault, and handle it if possible, instead of refusing to print any corrupted structures.

For corrupted btree blocks, the reason for the segfault is that we call block_to_bt to find the btree geometry based on the magic number. If the block's magic number doesn't match any of the known btree magics, it returns NULL and btblock_*_offset blow up on the NULL pointer.

The iocursor already knows the supposed type of the thing we're looking at, so if we don't get a match on the magic number we could try using the iocursor type to look up the supposed btree geometry, and use that to print the btblock fields.

Of course once we do that we run into the next problem, which is that when numrecs is too large, we happily print arrays right off the end of the buffer and crash.

Eh, having worked all that out I might as well pretty the patches and send them.

--D

Quoted 37 lines

-Eric

Quoted 20 lines
---
 db/print.c | 8 ++++++++
 1 file changed, 8 insertions(+)
db/print.c +1 -0
index e31372f..08bb39a 100644
@@ -69,11 +69,19 @@ print_f(
 	char	**argv)
 {
 	pfunc_t	pf;
+	int err;
 
 	if (cur_typ == NULL) {
 		dbprintf(_("no current type\n"));
 		return 0;
 	}

db/print.c print_f( · line 87

Quoted 10 lines
db/print.c +7 -0
+
+	err = iocur_top->bp->b_error;
+	if ((err == -EFSCORRUPTED) || (err == -EFSBADCRC)) {
+		dbprintf(_("data corrupted or CRC error\n"));
+		return 0;
+	}
+
 	pf = cur_typ->pfunc;
 	if (pf == NULL) {
 		dbprintf(_("no print function for type %s\n"), cur_typ->name);

--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html