Unverified Commit 6689f01d authored by Jan Kara's avatar Jan Kara Committed by Christian Brauner
Browse files

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.

Fixes: e1b849cf ("writeback: Avoid contention on wb->list_lock when switching inodes")
CC: stable@vger.kernel.org
Signed-off-by: default avatarJan Kara <jack@suse.cz>
Link: https://patch.msgid.link/20260413093618.17244-2-jack@suse.cz


Acked-by: default avatarTejun Heo <tj@kernel.org>
Signed-off-by: default avatarChristian Brauner <brauner@kernel.org>
parent c03ce417
Loading
Loading
Loading
Loading
+19 −17
Original line number Diff line number Diff line
@@ -570,16 +570,19 @@ 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);
	/*
	 * 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 out reference to wb so that it cannot get freed under us
	 * 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
@@ -591,7 +594,6 @@ void inode_switch_wbs_work_fn(struct work_struct *work)

	llist_for_each_entry_safe(isw, next_isw, list, list)
		process_inode_switch_wbs(new_wb, isw);
	}
	wb_put(new_wb);
}