Hello, On Fri, Sep 10, 2021 at 11:46:42AM +0800, Li Jinlin wrote: > Freeing bfqg is protected by queue lock in blkcg_deactivate_policy(), > but getting/using bfqg is protected by blkcg lock in > bfq_io_set_weight_legacy(). If bfq_io_set_weight_legacy() get bfqg > before freeing bfqg and use bfqg in the after, the use-after-free > will occur. > > CPU0 CPU1 > blkcg_deactivate_policy > spin_lock_irq(&q->queue_lock) > bfq_io_set_weight_legacy > spin_lock_irq(&blkcg->lock) > blkg_to_bfqg(blkg) > pd_to_bfqg(blkg->pd[pol->plid]) > ^^^^^^blkg->pd[pol->plid] != NULL > bfqg != NULL > pol->pd_free_fn(blkg->pd[pol->plid]) > pd_to_bfqg(blkg->pd[pol->plid]) > bfqg_put(bfqg) > kfree(bfqg) > blkg->pd[pol->plid] = NULL > spin_unlock_irq(q->queue_lock); > bfq_group_set_weight(bfqg, val, 0) > bfqg->entity.new_weight > ^^^^^^trigger uaf here > spin_unlock_irq(&blkcg->lock); > > To fix this use-after-free, instead of holding blkcg->lock while > walking ->blkg_list and getting/using bfqg, RCU walk ->blkg_list and > hold the blkg's queue lock while getting/using bfqg. I think this is a bug in blkcg_deactivate_policy() than the other way around. blkgs are protected by both q and blkcg locks and holding either should stabilize them. The blkcg lock nests inside q lock, so I think blkcg_deactivate_policy() just needs to grab the matching blkcg lock before trying to destroy blkgs. Thanks. -- tejun