Skip to content

Commit 40d4418

Browse files
toddkjosgregkh
authored andcommitted
binder: fix UAF when releasing todo list
commit f3277cb upstream. When releasing a thread todo list when tearing down a binder_proc, the following race was possible which could result in a use-after-free: 1. Thread 1: enter binder_release_work from binder_thread_release 2. Thread 2: binder_update_ref_for_handle() -> binder_dec_node_ilocked() 3. Thread 2: dec nodeA --> 0 (will free node) 4. Thread 1: ACQ inner_proc_lock 5. Thread 2: block on inner_proc_lock 6. Thread 1: dequeue work (BINDER_WORK_NODE, part of nodeA) 7. Thread 1: REL inner_proc_lock 8. Thread 2: ACQ inner_proc_lock 9. Thread 2: todo list cleanup, but work was already dequeued 10. Thread 2: free node 11. Thread 2: REL inner_proc_lock 12. Thread 1: deref w->type (UAF) The problem was that for a BINDER_WORK_NODE, the binder_work element must not be accessed after releasing the inner_proc_lock while processing the todo list elements since another thread might be handling a deref on the node containing the binder_work element leading to the node being freed. Signed-off-by: Todd Kjos <tkjos@google.com> Link: https://lore.kernel.org/r/20201009232455.4054810-1-tkjos@google.com Cc: <stable@vger.kernel.org> # 4.14, 4.19, 5.4, 5.8 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent dd57433 commit 40d4418

1 file changed

Lines changed: 10 additions & 25 deletions

File tree

‎drivers/android/binder.c‎

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ static struct binder_transaction_log_entry *binder_transaction_log_add(
223223
struct binder_work {
224224
struct list_head entry;
225225

226-
enum {
226+
enum binder_work_type {
227227
BINDER_WORK_TRANSACTION = 1,
228228
BINDER_WORK_TRANSACTION_COMPLETE,
229229
BINDER_WORK_RETURN_ERROR,
@@ -885,27 +885,6 @@ static struct binder_work *binder_dequeue_work_head_ilocked(
885885
return w;
886886
}
887887

888-
/**
889-
* binder_dequeue_work_head() - Dequeues the item at head of list
890-
* @proc: binder_proc associated with list
891-
* @list: list to dequeue head
892-
*
893-
* Removes the head of the list if there are items on the list
894-
*
895-
* Return: pointer dequeued binder_work, NULL if list was empty
896-
*/
897-
static struct binder_work *binder_dequeue_work_head(
898-
struct binder_proc *proc,
899-
struct list_head *list)
900-
{
901-
struct binder_work *w;
902-
903-
binder_inner_proc_lock(proc);
904-
w = binder_dequeue_work_head_ilocked(list);
905-
binder_inner_proc_unlock(proc);
906-
return w;
907-
}
908-
909888
static void
910889
binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
911890
static void binder_free_thread(struct binder_thread *thread);
@@ -4589,13 +4568,17 @@ static void binder_release_work(struct binder_proc *proc,
45894568
struct list_head *list)
45904569
{
45914570
struct binder_work *w;
4571+
enum binder_work_type wtype;
45924572

45934573
while (1) {
4594-
w = binder_dequeue_work_head(proc, list);
4574+
binder_inner_proc_lock(proc);
4575+
w = binder_dequeue_work_head_ilocked(list);
4576+
wtype = w ? w->type : 0;
4577+
binder_inner_proc_unlock(proc);
45954578
if (!w)
45964579
return;
45974580

4598-
switch (w->type) {
4581+
switch (wtype) {
45994582
case BINDER_WORK_TRANSACTION: {
46004583
struct binder_transaction *t;
46014584

@@ -4629,9 +4612,11 @@ static void binder_release_work(struct binder_proc *proc,
46294612
kfree(death);
46304613
binder_stats_deleted(BINDER_STAT_DEATH);
46314614
} break;
4615+
case BINDER_WORK_NODE:
4616+
break;
46324617
default:
46334618
pr_err("unexpected work type, %d, not freed\n",
4634-
w->type);
4619+
wtype);
46354620
break;
46364621
}
46374622
}

0 commit comments

Comments
 (0)