Skip to content

Commit de56348

Browse files
htejungregkh
authored andcommitted
block: fix double-free in the failure path of cgwb_bdi_init()
commit 5f478e4ea5c5560b4e40eb136991a09f9389f331 upstream. When !CONFIG_CGROUP_WRITEBACK, bdi has single bdi_writeback_congested at bdi->wb_congested. cgwb_bdi_init() allocates it with kzalloc() and doesn't do further initialization. This usually works fine as the reference count gets bumped to 1 by wb_init() and the put from wb_exit() releases it. However, when wb_init() fails, it puts the wb base ref automatically freeing the wb and the explicit kfree() in cgwb_bdi_init() error path ends up trying to free the same pointer the second time causing a double-free. Fix it by explicitly initilizing the refcnt to 1 and putting the base ref from cgwb_bdi_destroy(). Signed-off-by: Tejun Heo <tj@kernel.org> Reported-by: Dmitry Vyukov <dvyukov@google.com> Fixes: a13f35e ("writeback: don't embed root bdi_writeback_congested in bdi_writeback") Signed-off-by: Jens Axboe <axboe@fb.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 1569697 commit de56348

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

mm/backing-dev.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,15 +757,20 @@ static int cgwb_bdi_init(struct backing_dev_info *bdi)
757757
if (!bdi->wb_congested)
758758
return -ENOMEM;
759759

760+
atomic_set(&bdi->wb_congested->refcnt, 1);
761+
760762
err = wb_init(&bdi->wb, bdi, 1, GFP_KERNEL);
761763
if (err) {
762-
kfree(bdi->wb_congested);
764+
wb_congested_put(bdi->wb_congested);
763765
return err;
764766
}
765767
return 0;
766768
}
767769

768-
static void cgwb_bdi_destroy(struct backing_dev_info *bdi) { }
770+
static void cgwb_bdi_destroy(struct backing_dev_info *bdi)
771+
{
772+
wb_congested_put(bdi->wb_congested);
773+
}
769774

770775
#endif /* CONFIG_CGROUP_WRITEBACK */
771776

0 commit comments

Comments
 (0)