Re: [PATCH v3 1/2] binder: Refactor binder_node print synchronization

From: Alice Ryhl
Date: Fri May 09 2025 - 04:58:00 EST


On Thu, May 08, 2025 at 07:01:38PM +0000, Tiffany Yang wrote:
> Alice Ryhl <aliceryhl@xxxxxxxxxx> writes:
> > I don't buy this logic. Imagine the following scenario:
> >
> > 1. print_binder_proc is called, and we loop over proc->nodes.
> > 2. We call binder_inner_proc_unlock(node->proc).
> > 3. On another thread, binder_deferred_release() is called.
> > 4. The node is removed from proc->nodes and node->proc is set to NULL.
> > 5. Back in print_next_binder_node_ilocked(), we now call
> > spin_lock(&binder_dead_nodes_lock) and return.
> > 6. In print_binder_proc(), we think that we hold the proc lock, but
> > actually we hold the dead nodes lock instead. BOOM.
> >
> > What happens with the current code is that print_binder_proc() takes the
> > proc lock again after the node was removed from proc->nodes, and then it
> > exits the loop because rb_next(n) returns NULL when called on a node not
> > in any rb-tree.
> >
> > Alice
>
>
> Thanks for catching this!! I think this race could be solved by passing
> "proc" in as a parameter (NULL if iterating over the dead_nodes_list),
> and locking/unlocking based on that instead of node->proc. WDYT?

I believe that would work.

Alice