From nobody Fri Nov 29 06:52:12 2024 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (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 2406E1AC8AC for ; Tue, 24 Sep 2024 18:44:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727203448; cv=none; b=obEKX0XW0IG0iBEkSw+CRcZ+w9E9dBYZpwDtM2+8quLLupM3hiYwTu156HFQw61XVOHbjsGZjasepzHKj//YyubIlMQfkRdSGI22x83l+n83xTSCtlDgeT68E2EGGsdIpS1ZA6tAb/m+u+4V5nicuz6Q6fCF7yZ/z5O0cm3Z3oI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727203448; c=relaxed/simple; bh=6IcAOQbnIDXKr2A0ZKbc2TQ+WiL9rcFfhDZ+QZYUbO4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=LZ6+rTPiKVK9LShdevR+K/QSxbpKuNkdPU2h3LZesIpAsNjO34oTcr4CIp1rMgwhGX0MsQ5PimHsQzxY8FZqEx0ztXtijwMX5fNmbRg9AP7HdBxloHKWDU6dCilAyQc1q76QH3iwAm7J20q8ykFikXXeJJXOD6Eao2+gwpMeu1M= 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=LkrjiQcY; arc=none smtp.client-ip=209.85.215.201 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="LkrjiQcY" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-72c1d0fafb3so4694567a12.2 for ; Tue, 24 Sep 2024 11:44:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727203446; x=1727808246; 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=p9nV8NJnDAA8uw9/rFdmJcfCelAbKvt9jSwnFiN7UHQ=; b=LkrjiQcYGjpRI/XXk2P4QgHq+7JazinMGwPTznY1t+cCkmee1jS3pplxHKnslKihOD vH3nodAigK4fce37x0hEmUwaTwBUI9F8mE5kYtsGJ0eXJ2xX67CoeobvtCCpL+Xi2WVE mns0q6oJZWuXUSoTOJNBQ/QgoXVDVBkV1xYBgQie2Zvo1E/Hhjq1kzrWm8NX8X5wK34W q97OthS78ZaKGlYR86rl+DZm5J/UeE6wKaqJIl/MyM7hmeWUSNXUtNvu90iMuioKQHsP xuWPKSlyOuzyY7O65Omh/+dh/LhTn805jk12HWRg39i1ZjRvvTbss7pMooRfOPnUmD/5 NnDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727203446; x=1727808246; 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=p9nV8NJnDAA8uw9/rFdmJcfCelAbKvt9jSwnFiN7UHQ=; b=gCBmdW+979hZa8eSJGRGjpVe2D/+H70IqjUP+m1R7HzwmiWs8vvkr68P5prL3EKW8U YDr4nbdVsJHv4jmKlWOJRNtnFjZhi0lhkvqVsCUlKIZKA738R/8sPxRgA0D7kWWvIpe5 gHJcK9RtJyYdjAqygK6Xl6X1mXrjpXNX26du5LfNC+KmURescScS+bs0HFkbQOqnbdCs jvTZkx/jLOFJUa3IzhLDmWcHdP9+h/ikXuoGR6fTWmbJU01e0JHIeOWGUvm1mMUIsZGG 7KkausiWms86krbSqpwfoamUsm77w4KnIo3HB55JMU2ZWiQ+3ZC0dPHbCocxEiTCTbYB bBdg== X-Gm-Message-State: AOJu0YxON40j6GiDZHDP5/QXtiIg4h/cNY+Av5wVaJZQxs1Fbotatxqb H5XGa7ef7gzWxD0ysjDJSvwOn6GhINXrFsq/lkPuo2dbfcCdex0+yuQvyz2eLK+Ri7/9+Q/jP7R YOHmfRmHUqQ== X-Google-Smtp-Source: AGHT+IED5bbPPsmWzf1m5V5YEsxBnJHtsfLGQbUbAh1wa2CPeDYb/L6NPSsFpciX/Ya40Q/IMW/yE35PTwxa8g== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a17:90b:81:b0:2dd:92e5:701a with SMTP id 98e67ed59e1d1-2e06affde31mr56a91.8.1727203446127; Tue, 24 Sep 2024 11:44:06 -0700 (PDT) Date: Tue, 24 Sep 2024 18:43:53 +0000 In-Reply-To: <20240924184401.76043-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: <20240924184401.76043-1-cmllamas@google.com> X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240924184401.76043-2-cmllamas@google.com> Subject: [PATCH 1/4] binder: fix node UAF in binder_add_freeze_work() From: Carlos Llamas To: Greg Kroah-Hartman , "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Carlos Llamas , Suren Baghdasaryan , Yu-Ting Tseng Cc: linux-kernel@vger.kernel.org, kernel-team@android.com, Alice Ryhl , stable@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In binder_add_freeze_work() we iterate over the proc->nodes with the proc->inner_lock held. However, this lock is temporarily dropped in order to acquire the node->lock first (lock nesting order). This can race with binder_node_release() and trigger a use-after-free: =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D BUG: KASAN: slab-use-after-free in _raw_spin_lock+0xe4/0x19c Write of size 4 at addr ffff53c04c29dd04 by task freeze/640 CPU: 5 UID: 0 PID: 640 Comm: freeze Not tainted 6.11.0-07343-ga727812a8d4= 5 #17 Hardware name: linux,dummy-virt (DT) Call trace: _raw_spin_lock+0xe4/0x19c binder_add_freeze_work+0x148/0x478 binder_ioctl+0x1e70/0x25ac __arm64_sys_ioctl+0x124/0x190 Allocated by task 637: __kmalloc_cache_noprof+0x12c/0x27c binder_new_node+0x50/0x700 binder_transaction+0x35ac/0x6f74 binder_thread_write+0xfb8/0x42a0 binder_ioctl+0x18f0/0x25ac __arm64_sys_ioctl+0x124/0x190 Freed by task 637: kfree+0xf0/0x330 binder_thread_read+0x1e88/0x3a68 binder_ioctl+0x16d8/0x25ac __arm64_sys_ioctl+0x124/0x190 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Fix the race by taking a temporary reference on the node before releasing the proc->inner lock. This ensures the node remains alive while in use. Fixes: d579b04a52a1 ("binder: frozen notification") Cc: stable@vger.kernel.org Signed-off-by: Carlos Llamas Acked-by: Todd Kjos Reviewed-by: Alice Ryhl --- drivers/android/binder.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/android/binder.c b/drivers/android/binder.c index 978740537a1a..4d90203ea048 100644 --- a/drivers/android/binder.c +++ b/drivers/android/binder.c @@ -5552,6 +5552,7 @@ static bool binder_txns_pending_ilocked(struct binder= _proc *proc) =20 static void binder_add_freeze_work(struct binder_proc *proc, bool is_froze= n) { + struct binder_node *prev =3D NULL; struct rb_node *n; struct binder_ref *ref; =20 @@ -5560,7 +5561,10 @@ static void binder_add_freeze_work(struct binder_pro= c *proc, bool is_frozen) struct binder_node *node; =20 node =3D rb_entry(n, struct binder_node, rb_node); + binder_inc_node_tmpref_ilocked(node); binder_inner_proc_unlock(proc); + if (prev) + binder_put_node(prev); binder_node_lock(node); hlist_for_each_entry(ref, &node->refs, node_entry) { /* @@ -5586,10 +5590,13 @@ static void binder_add_freeze_work(struct binder_pr= oc *proc, bool is_frozen) } binder_inner_proc_unlock(ref->proc); } + prev =3D node; binder_node_unlock(node); binder_inner_proc_lock(proc); } binder_inner_proc_unlock(proc); + if (prev) + binder_put_node(prev); } =20 static int binder_ioctl_freeze(struct binder_freeze_info *info, --=20 2.46.0.792.g87dc391469-goog