From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dy1-f202.google.com (mail-dy1-f202.google.com [74.125.82.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6FA6C30B517 for ; Fri, 19 Jun 2026 18:52:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781895171; cv=none; b=cISp1Xtjg9qBwTU6nBu5RW0G29v5AY55TYuovhn2FpZOqCI6WnP3SLN2YQ7EMs6ADEEVpi9Fh+kv1/07TGgWo165i2j9zSuhGs1zdTK+n5qKI4JYCkYfB2/uasWAfbaIuvs29kM3CncbHlHKB8oCAtV9wgMXwKR/qgKyTHu2+fA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781895171; c=relaxed/simple; bh=KmPYFDA00ywcpcV04V/qvoVq6coYe693gi2Digv6cYo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mZ9SxfVd4pLZVH3jBIsaqlPWFRC1CVeDrNtf1FFLNljq750tlAWe76+tKxSWgXLzqLDLFH5Jg5zT8D8KWAHMnOZWFdP8eDbTSyuhP6+IEEBb8J5eejUjmayg+yUFX2ngBNO/zlJG3ArcP8cC00oY1sMOymDbv0+Yl2Gv500GnFc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--cmllamas.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=npnRbttH; arc=none smtp.client-ip=74.125.82.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--cmllamas.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="npnRbttH" Received: by mail-dy1-f202.google.com with SMTP id 5a478bee46e88-30c0f4950ceso836719eec.1 for ; Fri, 19 Jun 2026 11:52:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1781895169; x=1782499969; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=3t8E6qPCGzKBRUKhCyGND86O0xwSlmxNUAVjxI941B0=; b=npnRbttHtak0jWzYKM/9+/8cyOzaRQ3IReyW4/EDXggGAXk2KB2ITL4zMroApdgapF 3r05hPq7Oowrg+MzoY00b5+iR2MvSHPQ4VueGf2bFRy0fXFehe0ShaPsTFkhlSiLafuU /AODfc/yCjC8SMf/NcXVdMgFvwCXIXNEirVj65i3etSy7Eb6gNgS4ZNrkVxdE5Iq2gtC 7itaXG/A3gwP1o9bx/KwnFjrLp2/Yxo2yUx4KqFAawJHGo69QsY57Qa/oXrHX2JaEZQu vxHfbH/C4/92fu2ZdwEZsu3ixVfdQV3pSOyCuPnUC8+nUx+spY5+Q/WMCO5MG9LOtnpp ep3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781895169; x=1782499969; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=3t8E6qPCGzKBRUKhCyGND86O0xwSlmxNUAVjxI941B0=; b=NBQHU3F/v+exfsuuQB/3HJ0NDqHuDAYC1ErnUkh3kk89BnsBKqPUr0FX8PUDPQfrES lvHu5m1iuHh4JEB7WKZLhoWFm9u10rrse42ecNwZN5dvFTkgCAQ80k/SHU5ivc1KH7GC ZU6uUVIxTrPlZIO3xYKHDYEG0DvS5F1FxllFbWzoif3BjOP9HuX10VmNG6jgrevFRaaW jFKaAzAlUXMNixd6sYbMaAMsHNCiuLRdyuhLkY14D3Ys1WJQs1AogQDYsxY4h2ll8aWM 93pPHCK556Zu+eddbxg4ZVoev2SRW76Oohz3UjyIsMjVBtYVy5FWc45b6LdgBUXQ6ikS BBtw== X-Forwarded-Encrypted: i=1; AFNElJ+MiGT2kyELmP1SeyLm+dBmf/VUUTEBt5uwY2Nipt3yDO7wJLMgopStL/rvVmeSyankySnA03J7FqfCwE8=@vger.kernel.org X-Gm-Message-State: AOJu0Ywio5RSt+uSZKWN4uyW1JwTZUhrd08La3zWBh5P96wa+tMJ4ERr MUDH59JP+v+ErUrqrUCkkybUiZFc5C4vIO0IP/ZnP0pHtjxITmoYSBgEHQ/0CayP23yg3jjvUBn OgK/C7eEqr3dr0Q== X-Received: from dycmb20-n1.prod.google.com ([2002:a05:693c:20d4:10b0:30b:e04d:8201]) (user=cmllamas job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:4194:b0:137:9ee5:208d with SMTP id a92af1059eb24-139a368eae1mr1954093c88.33.1781895169173; Fri, 19 Jun 2026 11:52:49 -0700 (PDT) Date: Fri, 19 Jun 2026 18:52:31 +0000 In-Reply-To: <20260619185233.2194678-1-cmllamas@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260619185233.2194678-1-cmllamas@google.com> X-Mailer: git-send-email 2.55.0.rc0.738.g0c8ab3ebcc-goog Message-ID: <20260619185233.2194678-2-cmllamas@google.com> Subject: [PATCH v2 2/2] binder: fix UAF in binder_free_transaction() From: Carlos Llamas To: Greg Kroah-Hartman , "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Christian Brauner , Carlos Llamas , Alice Ryhl Cc: kernel-team@android.com, linux-kernel@vger.kernel.org, stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" In binder_free_transaction(), the t->to_proc is read under the t->lock. However, once the t->lock is dropped, the to_proc can die in parallel. This leads to a use-after-free error when we attempt to acquire its inner lock right afterwards: ================================================================== BUG: KASAN: slab-use-after-free in _raw_spin_lock+0xe4/0x1a0 Write of size 4 at addr ffff00001125da70 by task B/672 CPU: 20 UID: 0 PID: 672 Comm: B Not tainted 7.1.0-rc6-00284-g8e65320d91cd #4 PREEMPT Hardware name: linux,dummy-virt (DT) Call trace: _raw_spin_lock+0xe4/0x1a0 binder_free_transaction+0x8c/0x320 binder_send_failed_reply+0x21c/0x2f8 binder_thread_release+0x488/0x7e0 binder_ioctl+0x12c0/0x29a0 [...] Allocated by task 675: __kmalloc_cache_noprof+0x174/0x444 binder_open+0x118/0xb70 do_dentry_open+0x374/0x1040 vfs_open+0x58/0x3bc [...] Freed by task 212: __kasan_slab_free+0x58/0x80 kfree+0x1a0/0x4a4 binder_proc_dec_tmpref+0x32c/0x5e0 binder_deferred_func+0xc48/0x104c process_one_work+0x53c/0xbc0 [...] ================================================================== To prevent this, pin the target thread (t->to_thread) to guarantee the target process remains alive. Undelivered transactions without a target thread are already safe, as the target process can only be the current context in those paths. Cc: stable@vger.kernel.org Reported-by: Alice Ryhl Closes: https://lore.kernel.org/all/aikJKVuny_eOivwN@google.com/ Fixes: a370003cc301 ("binder: fix possible UAF when freeing buffer") Signed-off-by: Carlos Llamas --- drivers/android/binder.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/drivers/android/binder.c b/drivers/android/binder.c index 09bc052186cf..b85920c39694 100644 --- a/drivers/android/binder.c +++ b/drivers/android/binder.c @@ -1658,10 +1658,19 @@ static void binder_txn_latency_free(struct binder_transaction *t) static void binder_free_transaction(struct binder_transaction *t) { + struct binder_thread *target_thread; struct binder_proc *target_proc; spin_lock(&t->lock); target_proc = t->to_proc; + target_thread = t->to_thread; + /* + * Pin target_thread to keep target_proc alive. Undelivered + * transactions with !target_thread are safe, as target_proc + * can only be the current context there. + */ + if (target_thread) + atomic_inc(&target_thread->tmp_ref); spin_unlock(&t->lock); if (target_proc) { @@ -1676,6 +1685,10 @@ static void binder_free_transaction(struct binder_transaction *t) t->buffer->transaction = NULL; binder_inner_proc_unlock(target_proc); } + + if (target_thread) + binder_thread_dec_tmpref(target_thread); + if (trace_binder_txn_latency_free_enabled()) binder_txn_latency_free(t); /* -- 2.55.0.rc0.738.g0c8ab3ebcc-goog