Skip to contentSkip to conversation

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

PATCH In review Jia-Ju Bai 2 participants 2 messages Jul 30, 2019 XFS filesystem

lore
Share & tools
lore

Jia-Ju Bai

author
#0 lore raw

PATCH v2 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.

---
v2:
* Adjust the code and add an assignment.
Thank Darrick J. Wong for helpful advice.

---
 fs/xfs/scrub/dabtree.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/xfs/scrub/dabtree.c +5 -1
index 94c4f1de1922..77ff9f97bcda 100644
@@ -278,7 +278,11 @@ 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);
+		ds->state->altpath.blk[level].bp = NULL;
+	}
 out:
 	return error;
 }
Signature · 1 line
2.17.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 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);
Quoted 8 lines

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.

Looks ok,

--D

Quoted 29 lines

---
v2:
* Adjust the code and add an assignment.
Thank Darrick J. Wong for helpful advice.

---
 fs/xfs/scrub/dabtree.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/xfs/scrub/dabtree.c +5 -1
index 94c4f1de1922..77ff9f97bcda 100644
@@ -278,7 +278,11 @@ 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);
+		ds->state->altpath.blk[level].bp = NULL;
+	}
 out:
 	return error;
 }
Signature · 1 line
2.17.0