In btree_gc_rewrite_node(), btree_node_alloc_replacement() is assigned to n and return error code or NULL on failure. n is passed to bch_btree_node_write_sync() and there is a dereference of it in bch_btree_node_write_sync() without checks, which may lead to wild pointer dereference or NULL pointer dereference depending on n. Fix this bug by adding IS_ERR_OR_NULL check of n. This bug was found by a static analyzer. Builds with 'make allyesconfig' show no new warnings, and our static analyzer no longer warns about this code. Fixes: ("bcache: Rework btree cache reserve handling") Signed-off-by: Zhou Qingyang <zhou1615@xxxxxxx> --- The analysis employs differential checking to identify inconsistent security operations (e.g., checks or kfrees) between two code paths and confirms that the inconsistent operations are not recovered in the current function or the callers, so they constitute bugs. Note that, as a bug found by static analysis, it can be a false positive or hard to trigger. Multiple researchers have cross-reviewed the bug. drivers/md/bcache/btree.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c index 88c573eeb598..06d42292e86c 100644 --- a/drivers/md/bcache/btree.c +++ b/drivers/md/bcache/btree.c @@ -1504,6 +1504,8 @@ static int btree_gc_rewrite_node(struct btree *b, struct btree_op *op, return 0; n = btree_node_alloc_replacement(replace, NULL); + if (IS_ERR_OR_NULL(n)) + return 0; /* recheck reserve after allocating replacement node */ if (btree_check_reserve(b, NULL)) { -- 2.25.1