Skip to contentSkip to conversation

fs: xfs: Fix possible null-pointer dereferences in xchk_da_btree_block_check_sibling()

PATCH Superseded Jia-Ju Bai 2 participants 4 messages Jul 29, 2019 XFS filesystem

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jia-Ju Bai

author
#0 lore raw

PATCH 1/1 fs: xfs: Fix possible null-pointer dereferences in xchk_da_btree_block_check_sibling()

In xchk_da_btree_block_check_sibling(), there is an if statement on line 274 to check whether ds->state->altpath.blk[level].bp is NULL:

    if (ds->state->altpath.blk[level].bp)

When ds->state->altpath.blk[level].bp is NULL, it is used on line 281:

    xfs_trans_brelse(..., ds->state->altpath.blk[level].bp);
        struct xfs_buf_log_item	*bip = bp->b_log_item;
        ASSERT(bp->b_transp == tp);

Thus, possible null-pointer dereferences may occur.

To fix these bugs, ds->state->altpath.blk[level].bp is checked before being used.

These bugs are found by a static analysis tool STCheck written by us.

---
 fs/xfs/scrub/dabtree.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/xfs/scrub/dabtree.c +3 -1
index 94c4f1de1922..33ff90c0dd70 100644
@@ -278,7 +278,9 @@ xchk_da_btree_block_check_sibling(
 	/* Compare upper level pointer to sibling pointer. */
 	if (ds->state->altpath.blk[level].blkno != sibling)
 		xchk_da_set_corrupt(ds, level);
-	xfs_trans_brelse(ds->dargs.trans, ds->state->altpath.blk[level].bp);
+	if (ds->state->altpath.blk[level].bp)
+		xfs_trans_brelse(ds->dargs.trans, 
+						ds->state->altpath.blk[level].bp);
 out:
 	return error;
 }
Signature · 1 line
2.17.0
#1 lore raw
Quoted 26 lines · Jia-Ju Bai

In xchk_da_btree_block_check_sibling(), there is an if statement on line 274 to check whether ds->state->altpath.blk[level].bp is NULL:

    if (ds->state->altpath.blk[level].bp)

When ds->state->altpath.blk[level].bp is NULL, it is used on line 281:

    xfs_trans_brelse(..., ds->state->altpath.blk[level].bp);
        struct xfs_buf_log_item	*bip = bp->b_log_item;
        ASSERT(bp->b_transp == tp);

Thus, possible null-pointer dereferences may occur.

To fix these bugs, ds->state->altpath.blk[level].bp is checked before being used.

These bugs are found by a static analysis tool STCheck written by us.

---
 fs/xfs/scrub/dabtree.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/xfs/scrub/dabtree.c +0 -0
index 94c4f1de1922..33ff90c0dd70 100644

fs/xfs/scrub/dabtree.c xchk_da_btree_block_check_sibling( · line 283

Quoted 7 lines
fs/xfs/scrub/dabtree.c +3 -1
@@ -278,7 +278,9 @@ xchk_da_btree_block_check_sibling(
 	/* Compare upper level pointer to sibling pointer. */
 	if (ds->state->altpath.blk[level].blkno != sibling)
 		xchk_da_set_corrupt(ds, level);
-	xfs_trans_brelse(ds->dargs.trans, ds->state->altpath.blk[level].bp);
+	if (ds->state->altpath.blk[level].bp)
+		xfs_trans_brelse(ds->dargs.trans, 
+						ds->state->altpath.blk[level].bp);

Indentation here (in xfs we use two spaces)

Also, uh, shouldn't we set ds->state->altpath.blk[level].bp to NULL since we've released the buffer?

--D

Quoted 6 lines
fs/xfs/scrub/dabtree.c +0 -0
 out:
 	return error;
 }
Signature · 1 line
2.17.0
author
#2 lore raw
Quoted 22 lines · Darrick J. Wong
Quoted 21 lines · Jia-Ju Bai

In xchk_da_btree_block_check_sibling(), there is an if statement on line 274 to check whether ds->state->altpath.blk[level].bp is NULL:

     if (ds->state->altpath.blk[level].bp)

When ds->state->altpath.blk[level].bp is NULL, it is used on line 281:

     xfs_trans_brelse(..., ds->state->altpath.blk[level].bp);
         struct xfs_buf_log_item	*bip = bp->b_log_item;
         ASSERT(bp->b_transp == tp);

Thus, possible null-pointer dereferences may occur.

To fix these bugs, ds->state->altpath.blk[level].bp is checked before being used.

These bugs are found by a static analysis tool STCheck written by us.

---
  fs/xfs/scrub/dabtree.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 13 lines

fs/xfs/scrub/dabtree.c xchk_da_btree_block_check_sibling( · line 283

Quoted 12 lines
fs/xfs/scrub/dabtree.c +3 -1
index 94c4f1de1922..33ff90c0dd70 100644
@@ -278,7 +278,9 @@ xchk_da_btree_block_check_sibling(
  	/* Compare upper level pointer to sibling pointer. */
  	if (ds->state->altpath.blk[level].blkno != sibling)
  		xchk_da_set_corrupt(ds, level);
-	xfs_trans_brelse(ds->dargs.trans, ds->state->altpath.blk[level].bp);
+	if (ds->state->altpath.blk[level].bp)
+		xfs_trans_brelse(ds->dargs.trans,
+						ds->state->altpath.blk[level].bp);

Indentation here (in xfs we use two spaces)

Okay, I will fix this.

Quoted 3 lines

Also, uh, shouldn't we set ds->state->altpath.blk[level].bp to NULL since we've released the buffer?

So I should set ds->state->altpath.blk[level].bp to NULL at the end of the function xchk_da_btree_block_check_sibling()? Like:
    if (ds->state->altpath.blk[level].bp)
        xfs_trans_brelse(ds->dargs.trans,
                ds->state->altpath.blk[level].bp);
    ds->state->altpath.blk[level].bp = NULL;

Best wishes,
Jia-Ju Bai

#3 lore raw
Quoted 45 lines · Jia-Ju Bai
Quoted 22 lines · Darrick J. Wong
Quoted 21 lines · Jia-Ju Bai

In xchk_da_btree_block_check_sibling(), there is an if statement on line 274 to check whether ds->state->altpath.blk[level].bp is NULL:

     if (ds->state->altpath.blk[level].bp)

When ds->state->altpath.blk[level].bp is NULL, it is used on line 281:

     xfs_trans_brelse(..., ds->state->altpath.blk[level].bp);
         struct xfs_buf_log_item	*bip = bp->b_log_item;
         ASSERT(bp->b_transp == tp);

Thus, possible null-pointer dereferences may occur.

To fix these bugs, ds->state->altpath.blk[level].bp is checked before being used.

These bugs are found by a static analysis tool STCheck written by us.

---
  fs/xfs/scrub/dabtree.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 13 lines

fs/xfs/scrub/dabtree.c xchk_da_btree_block_check_sibling( · line 283

Quoted 12 lines
fs/xfs/scrub/dabtree.c +3 -1
index 94c4f1de1922..33ff90c0dd70 100644
@@ -278,7 +278,9 @@ xchk_da_btree_block_check_sibling(
  	/* Compare upper level pointer to sibling pointer. */
  	if (ds->state->altpath.blk[level].blkno != sibling)
  		xchk_da_set_corrupt(ds, level);
-	xfs_trans_brelse(ds->dargs.trans, ds->state->altpath.blk[level].bp);
+	if (ds->state->altpath.blk[level].bp)
+		xfs_trans_brelse(ds->dargs.trans,
+						ds->state->altpath.blk[level].bp);

Indentation here (in xfs we use two spaces)

Okay, I will fix this.

Quoted 3 lines

Also, uh, shouldn't we set ds->state->altpath.blk[level].bp to NULL since we've released the buffer?

Quoted 7 lines

So I should set ds->state->altpath.blk[level].bp to NULL at the end of the function xchk_da_btree_block_check_sibling()?
Like:
    if (ds->state->altpath.blk[level].bp) xfs_trans_brelse(ds->dargs.trans, ds->state->altpath.blk[level].bp); ds->state->altpath.blk[level].bp = NULL;

You could put the whole thing in a single if clause, e.g.

	if (ds->state->altpath.blk[level].bp) {
		xfs_trans_brelse(ds->dargs.trans,
				 ds->state->altpath.blk[level].bp);
		ds->state->altpath.blk[level].bp = NULL;
	}

--D

Quoted 4 lines

Best wishes,
Jia-Ju Bai