• Tejun Heo's avatar
    block: fix double-free in the failure path of cgwb_bdi_init() · dea972f3
    Tejun Heo authored
    commit 5f478e4e 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: default avatarTejun Heo <tj@kernel.org>
    Reported-by: default avatarDmitry Vyukov <dvyukov@google.com>
    Fixes: a13f35e8 ("writeback: don't embed root bdi_writeback_congested in bdi_writeback")
    Signed-off-by: default avatarJens Axboe <axboe@fb.com>
    Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
    dea972f3
backing-dev.c 26.4 KB