writeback: Fix use after free in inode_switch_wbs_work_fn()
Subsystems (2)
Jan Kara
reviewerPATCH 1/1 writeback: Fix use after free in inode_switch_wbs_work_fn()
inode_switch_wbs_work_fn() has a loop like:
wb_get(new_wb);
while (1) {
list = llist_del_all(&new_wb->switch_wbs_ctxs);
/* Nothing to do? */
if (!list)
break;
... process the items ...
}Now adding of items to the list looks like:
wb_queue_isw()
if (llist_add(&isw->list, &wb->switch_wbs_ctxs))
queue_work(isw_wq, &wb->switch_work);
Because inode_switch_wbs_work_fn() loops when processing isw items, it can happen that wb->switch_work is pending while wb->switch_wbs_ctxs is empty. This is a problem because in that case wb can get freed (no isw items -> no wb reference) while the work is still pending causing use-after-free issues.
We cannot just fix this by cancelling work when freeing wb because that could still trigger problematic 0 -> 1 transitions on wb refcount due to wb_get() in inode_switch_wbs_work_fn(). It could be all handled with more careful code but that seems unnecessarily complex so let's avoid that until it is proven that the looping actually brings practical benefit. Just remove the loop from inode_switch_wbs_work_fn() instead. That way when wb_queue_isw() queues work, we are guaranteed we have added the first item to wb->switch_wbs_ctxs and nobody is going to remove it (and drop the wb reference it holds) until the queued work runs.
--- fs/fs-writeback.c | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-)
fs/fs-writeback.c +19 -17
@@ -570,28 +570,30 @@ void inode_switch_wbs_work_fn(struct work_struct *work)struct inode_switch_wbs_context *isw, *next_isw;struct llist_node *list; + list =llist_del_all (&new_wb->switch_wbs_ctxs);/* -* Grab out reference to wb so that it cannot get freed under us +* Nothing to do? That would be a problem as references held by isw +* items protect wb from freeing... +*/ +if (WARN_ON_ONCE (!list)) +return ; + +/* +* Grab our reference to wb so that it cannot get freed under us * after we process all the isw items. */ wb_get (new_wb); -while (1 ) { - list =llist_del_all (&new_wb->switch_wbs_ctxs); -/* Nothing to do? */ -if (!list) -break ; -/* -* In addition to synchronizing among switchers, I_WB_SWITCH -* tells the RCU protected stat update paths to grab the i_page -* lock so that stat transfer can synchronize against them. -* Let's continue after I_WB_SWITCH is guaranteed to be -* visible. -*/ -synchronize_rcu (); +/* +* In addition to synchronizing among switchers, I_WB_SWITCH +* tells the RCU protected stat update paths to grab the i_page +* lock so that stat transfer can synchronize against them. +* Let's continue after I_WB_SWITCH is guaranteed to be +* visible. +*/ +synchronize_rcu (); -llist_for_each_entry_safe (isw, next_isw, list, list) -process_inode_switch_wbs (new_wb, isw); - } +llist_for_each_entry_safe (isw, next_isw, list, list) +process_inode_switch_wbs (new_wb, isw);wb_put (new_wb); }
Signature · 1 line
2.51.0
Quoted 34 lines · Jan Kara
inode_switch_wbs_work_fn()has a loop like:wb_get(new_wb); while (1) { list = llist_del_all(&new_wb->switch_wbs_ctxs); /* Nothing to do? */ if (!list) break; ... process the items ... }Now adding of items to the list looks like:
wb_queue_isw()
if (llist_add(&isw->list, &wb->switch_wbs_ctxs))queue_work(isw_wq, &wb->switch_work);Because
inode_switch_wbs_work_fn()loops when processing isw items, it can happen thatwb->switch_workis pending whilewb->switch_wbs_ctxsis empty. This is a problem because in that case wb can get freed (no isw items -> no wb reference) while the work is still pending causing use-after-free issues.We cannot just fix this by cancelling work when freeing wb because that could still trigger problematic 0 -> 1 transitions on wb refcount due to
wb_get()ininode_switch_wbs_work_fn(). It could be all handled with more careful code but that seems unnecessarily complex so let's avoid that until it is proven that the looping actually brings practical benefit. Just remove the loop frominode_switch_wbs_work_fn()instead. That way whenwb_queue_isw()queues work, we are guaranteed we have added the first item towb->switch_wbs_ctxsand nobody is going to remove it (and drop the wb reference it holds) until the queued work runs.
Quoted 3 lines
Fixes e1b849cfa6b6 ("writeback: Avoid contention on wb->list_lock when switching inodes")Signed-off-by Jan Kara <[email protected]>
Oh man, that's too subtle.
Thanks.
Signature · 1 line
tejun
Jan Kara
reviewerQuoted 35 lines · Tejun Heo
Quoted 34 lines · Jan Kara
inode_switch_wbs_work_fn()has a loop like:wb_get(new_wb); while (1) { list = llist_del_all(&new_wb->switch_wbs_ctxs); /* Nothing to do? */ if (!list) break; ... process the items ... }Now adding of items to the list looks like:
wb_queue_isw()
if (llist_add(&isw->list, &wb->switch_wbs_ctxs))queue_work(isw_wq, &wb->switch_work);Because
inode_switch_wbs_work_fn()loops when processing isw items, it can happen thatwb->switch_workis pending whilewb->switch_wbs_ctxsis empty. This is a problem because in that case wb can get freed (no isw items -> no wb reference) while the work is still pending causing use-after-free issues.We cannot just fix this by cancelling work when freeing wb because that could still trigger problematic 0 -> 1 transitions on wb refcount due to
wb_get()ininode_switch_wbs_work_fn(). It could be all handled with more careful code but that seems unnecessarily complex so let's avoid that until it is proven that the looping actually brings practical benefit. Just remove the loop frominode_switch_wbs_work_fn()instead. That way whenwb_queue_isw()queues work, we are guaranteed we have added the first item towb->switch_wbs_ctxsand nobody is going to remove it (and drop the wb reference it holds) until the queued work runs.
Quoted 7 lines
Quoted 3 lines
Fixes e1b849cfa6b6 ("writeback: Avoid contention on wb->list_lock when switching inodes")Signed-off-by Jan Kara <[email protected]>Oh man, that's too subtle.
Acked-by Tejun Heo <[email protected]>
Thanks for review! I forgot to mention in the changelog that these UAF issues were actually reliably hit in practice by one user on a Kubernetes cluster. So it is subtle but actually practical issue...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 11 lines · Jan Kara
inode_switch_wbs_work_fn()has a loop like:wb_get(new_wb); while (1) { list = llist_del_all(&new_wb->switch_wbs_ctxs); /* Nothing to do? */ if (!list) break; ... process the items ... }
Quoted 1 line
[...]
Applied to the vfs.fixes branch of the vfs/vfs.git tree. Patches in the vfs.fixes branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.fixes
[1/1] writeback: Fix use after free in inode_switch_wbs_work_fn()
https://git.kernel.org/vfs/vfs/c/af423cdba49c