From nobody Sun Apr 5 13:06:01 2026 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 3B27A36C0A5 for ; Tue, 17 Feb 2026 14:22:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771338175; cv=none; b=KQvTGVmUW7rylT4rbXa4REYyw71YMZhdRrtmaZKBvnRTcv6ZThRF82WIVNpJ+6nhlYBSMo9QwMFUU09/m+jD0V89kdHvOXAc+tNU0sKcsLi1/Wi6IOkQVYqU1ZrkMKR/UV0wEtepHnFDlW6e5sW5XPJ1GuPJMVKPN6Hnp8J2ffQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771338175; c=relaxed/simple; bh=nQm0AngjwMT2UjJFuUZvKHEzTseqZByPMN8Hgm6xrz0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bWAZFa9XLF2V3xi7aNdpqyuGNGbeUDlukoL90Pw7abvE6HwfjQjZZ/JIHA4dNtJl8uonudd4QTerIUTGaIKCyiXZpd6FgVsJ5oea0brQsScxqnLkgEje5Q33wTkhhbW39D6/vmG2dbGeHjIf6sffHArKJ9rsAMe+GNYcngsXfRY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=uBDOIn76; arc=none smtp.client-ip=209.85.128.73 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--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="uBDOIn76" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4837b9913c9so24240955e9.0 for ; Tue, 17 Feb 2026 06:22:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771338173; x=1771942973; 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=82Js1rcnoAKTgbxsBEWU8ya5WrRt0JL+YHVGM9jGo+A=; b=uBDOIn76Lyf0G3yEJiZ0b4XjGmDMsmSt5t1tVwzGE5NZfRpMs24Bo3XIpHKmzQLUC9 +JaaS/mFsBuUjd4C+9ZaDeJ4/zLvJeqT/1Gwz4KIia0xYwdliOHmGaW22onOMRtx/TNg AbKB48rqQf9LDlSwoMSssOKsyzMZVXHKcXV5UnYY1SujCKD5WLN1c5Wmc3vIy/SravIi cTBivMqxt7qH8EFTBP98maw3RIFfr4Q3mJzVXt4gi+/c/g9uh/ftvfL59tNhdRmRl0j9 NPoOAZIl67b3RGDj5njkNaUUlcQnvzk3BnMwkcE/7CPCkf3i7Bbn48UrGFwFX2jtm1IG iD5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771338173; x=1771942973; 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=82Js1rcnoAKTgbxsBEWU8ya5WrRt0JL+YHVGM9jGo+A=; b=G5S1XRRi76vsUs84P7EDdxxGS+CrCkmDUaqV8h2YqnYZFT6pFBDt20ekioqA4UUQ4O ixqW3NPQ11+CDoeaQJfGb+vRiFdeIhqoJq1v6u3Ou0wfjsAbaOUdR6WrdO1XCtTZozTU qTTVPoMxo8sB7Qu/PI8FdnKp+rs2g21IlLJ5MrXYUEbcHohios2Fp1hylSb36ir113JS CVwvxRe7p5K0P1NifFo80L/r8f3bOkK48RMA+tRAU9QW9LNTOqEdoOJQ8YGUC7BbT/Ph y0qw/zR1YYNgwLRD2aKM9VD8DoIzTk1esFVfW9/IWI+fxIg1kD4eR5B0ZmncjUDtIVeb Nqlg== X-Forwarded-Encrypted: i=1; AJvYcCW7GLtX8qH0Td3LaJn8OmkkCbilthnQf/VMKsSeSEbrOZSTb+wDJ2v1CYqq0O0p0VKuNB/HLr2IxLR43r4=@vger.kernel.org X-Gm-Message-State: AOJu0YxZvqwpqRnLseKQPaqUqPrEGQdRhWX0WMGx9obSdGSDNdAA1LUT OkxuS6o45H1GcguhbR/4f5dRFqUxkRWvlfwUdgq4sTZy2imt9IERgZyGjOp0+punkVBPavVoBIq R88LmSxI2+87OIrlaiw== X-Received: from wmpz8.prod.google.com ([2002:a05:600c:a08:b0:483:248b:3e95]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:6096:b0:483:612d:7a9a with SMTP id 5b1f17b1804b1-483737b89a4mr196316545e9.0.1771338172603; Tue, 17 Feb 2026 06:22:52 -0800 (PST) Date: Tue, 17 Feb 2026 14:22:38 +0000 In-Reply-To: <20260217-binder-vma-check-v1-0-1a2b37f7b762@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260217-binder-vma-check-v1-0-1a2b37f7b762@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=6670; i=aliceryhl@google.com; h=from:subject:message-id; bh=nQm0AngjwMT2UjJFuUZvKHEzTseqZByPMN8Hgm6xrz0=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBplHm5X0xpCMTJgViI69+ZHnDddFhIjmNIbX3+H 22nzhl4nK+JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaZR5uQAKCRAEWL7uWMY5 RvfVEACevIXSL/nDzA5yoF/95U0BuALOFRvt2mjZyyKCqmRHKG9aBPN8m+1nL/RiNvc5mBC/Zwk Zey/Sl5m6/sD4ByVyYcIK3WFhrQ4qJ56vGXB6kTaEDjvJcrC97c2IQz2CaQoFXaafhth3ZPUhWC kRbmr1DB/5f/owITX8pEibPhT9k0seZ5ehsQOhleqwoX2d49V8q5LisgL6pzxJVAgItj53wcUJa jEk8+qm9TXYV7lHzkrpUE1PfjjCfTatfBHtupQMxsje6oxHcwnxWG7M4C3YbMAQC28s+j5nepA8 HIkErf5ecF2gZ2W20F0um9mOe4jlhoOrWn2EmvoV9SfR0xU+7wdq9D61ZpaZN5lPtkaSZXmc0rQ ifxWJLTPlfL9eETm8SnFGqZf5OAJN+OX/KXsvbOIImLEJk6PlUwgu5p6RoN9LtfPnMZFwKw77Y/ ZacwJnRlncRWqto1hLWU+D92I/Klj1QdyQfRI6YPAlnF02qMf/oSIFMA4kJSvuQJvYGpKoRDgIx oUFK2uhuuCY5lMhrOK0EuQrHjtSOKOcrS2swG1FsaWVPRzaGEMhn1OdU2+D/jJEBqo0JDW324tb 1ypvZ/ou6BQrVGMUsgzjHiPjbe65SCrn9sn74AUW458o3QesKzrTDY7ftjtdZdnx5OW+b0Cfx3e R1fE2AJ/jGgNmcw== X-Mailer: b4 0.14.2 Message-ID: <20260217-binder-vma-check-v1-1-1a2b37f7b762@google.com> Subject: [PATCH 1/2] rust_binder: check ownership before using vma From: Alice Ryhl To: Greg Kroah-Hartman , Carlos Llamas , Jann Horn Cc: Miguel Ojeda , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , Lorenzo Stoakes , "Liam R. Howlett" , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-mm@kvack.org, Alice Ryhl , stable@vger.kernel.org Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable When installing missing pages (or zapping them), Rust Binder will look up the vma in the mm by address, and then call vm_insert_page (or zap_page_range_single). However, if the vma is closed and replaced with a different vma at the same address, this can lead to Rust Binder installing pages into the wrong vma. By installing the page into a writable vma, it becomes possible to write to your own binder pages, which are normally read-only. Although you're not supposed to be able to write to those pages, the intent behind the design of Rust Binder is that even if you get that ability, it should not lead to anything bad. Unfortunately, due to another bug, that is not the case. To fix this, I will store a pointer in vm_private_data and check that the vma returned by vma_lookup() has the right vm_ops and vm_private_data before trying to use the vma. This should ensure that Rust Binder will refuse to interact with any other VMA. I will follow up this patch with more vma abstractions to avoid this unsafe access to vm_ops and vm_private_data, but for now I'd like to start with the simplest possible fix. C Binder performs the same check in a slightly different way: it provides a vm_ops->close that sets a boolean to true, then checks that boolean after calling vma_lookup(), but I think this is more fragile than the solution in this patch. (We probably still want to do both, but I'll add the vm_ops->close callback with the follow-up vma API changes.) Cc: stable@vger.kernel.org Fixes: eafedbc7c050 ("rust_binder: add Rust Binder driver") Reported-by: Jann Horn Signed-off-by: Alice Ryhl Reviewed-by: Jann Horn --- drivers/android/binder/page_range.rs | 78 +++++++++++++++++++++++++++-----= ---- 1 file changed, 58 insertions(+), 20 deletions(-) diff --git a/drivers/android/binder/page_range.rs b/drivers/android/binder/= page_range.rs index fdd97112ef5c8b2341e498dc3567b659f05e3fd7..90bab18961443c6e59699cb7345= e41e0db80f0dd 100644 --- a/drivers/android/binder/page_range.rs +++ b/drivers/android/binder/page_range.rs @@ -142,6 +142,27 @@ pub(crate) struct ShrinkablePageRange { _pin: PhantomPinned, } =20 +// We do not define any ops. For now, used only to check identity of vmas. +static BINDER_VM_OPS: bindings::vm_operations_struct =3D pin_init::zeroed(= ); + +// To ensure that we do not accidentally install pages into or zap pages f= rom the wrong vma, we +// check its vm_ops and private data before using it. +fn check_vma(vma: &virt::VmaRef, owner: *const ShrinkablePageRange) -> Opt= ion<&virt::VmaMixedMap> { + // SAFETY: Just reading the vm_ops pointer of any active vma is safe. + let vm_ops =3D unsafe { (*vma.as_ptr()).vm_ops }; + if !ptr::eq(vm_ops, &BINDER_VM_OPS) { + return None; + } + + // SAFETY: Reading the vm_private_data pointer of a binder-owned vma i= s safe. + let vm_private_data =3D unsafe { (*vma.as_ptr()).vm_private_data }; + if !ptr::eq(vm_private_data, owner.cast()) { + return None; + } + + vma.as_mixedmap_vma() +} + struct Inner { /// Array of pages. /// @@ -308,6 +329,16 @@ pub(crate) fn register_with_vma(&self, vma: &virt::Vma= New) -> Result { inner.size =3D num_pages; inner.vma_addr =3D vma.start(); =20 + // This pointer is only used for comparison - it's not dereference= d. + // + // SAFETY: We own the vma, and we don't use any methods on VmaNew = that rely on + // `vm_private_data`. + unsafe { (*vma.as_ptr()).vm_private_data =3D self as *const Self a= s *mut c_void }; + + // SAFETY: We own the vma, and we don't use any methods on VmaNew = that rely on + // `vm_ops`. + unsafe { (*vma.as_ptr()).vm_ops =3D &BINDER_VM_OPS }; + Ok(num_pages) } =20 @@ -399,22 +430,24 @@ unsafe fn use_page_slow(&self, i: usize) -> Result<()= > { // // Using `mmput_async` avoids this, because then the `mm` cleanup = is instead queued to a // workqueue. - MmWithUser::into_mmput_async(self.mm.mmget_not_zero().ok_or(ESRCH)= ?) - .mmap_read_lock() - .vma_lookup(vma_addr) - .ok_or(ESRCH)? - .as_mixedmap_vma() - .ok_or(ESRCH)? - .vm_insert_page(user_page_addr, &new_page) - .inspect_err(|err| { - pr_warn!( - "Failed to vm_insert_page({}): vma_addr:{} i:{} err:{:= ?}", - user_page_addr, - vma_addr, - i, - err - ) - })?; + check_vma( + MmWithUser::into_mmput_async(self.mm.mmget_not_zero().ok_or(ES= RCH)?) + .mmap_read_lock() + .vma_lookup(vma_addr) + .ok_or(ESRCH)?, + self, + ) + .ok_or(ESRCH)? + .vm_insert_page(user_page_addr, &new_page) + .inspect_err(|err| { + pr_warn!( + "Failed to vm_insert_page({}): vma_addr:{} i:{} err:{:?}", + user_page_addr, + vma_addr, + i, + err + ) + })?; =20 let inner =3D self.lock.lock(); =20 @@ -667,12 +700,15 @@ fn drop(self: Pin<&mut Self>) { let mmap_read; let mm_mutex; let vma_addr; + let range_ptr; =20 { // CAST: The `list_head` field is first in `PageInfo`. let info =3D item as *mut PageInfo; // SAFETY: The `range` field of `PageInfo` is immutable. - let range =3D unsafe { &*((*info).range) }; + range_ptr =3D unsafe { (*info).range }; + // SAFETY: The `range` outlives its `PageInfo` values. + let range =3D unsafe { &*range_ptr }; =20 mm =3D match range.mm.mmget_not_zero() { Some(mm) =3D> MmWithUser::into_mmput_async(mm), @@ -717,9 +753,11 @@ fn drop(self: Pin<&mut Self>) { // SAFETY: The lru lock is locked when this method is called. unsafe { bindings::spin_unlock(&raw mut (*lru).lock) }; =20 - if let Some(vma) =3D mmap_read.vma_lookup(vma_addr) { - let user_page_addr =3D vma_addr + (page_index << PAGE_SHIFT); - vma.zap_page_range_single(user_page_addr, PAGE_SIZE); + if let Some(unchecked_vma) =3D mmap_read.vma_lookup(vma_addr) { + if let Some(vma) =3D check_vma(unchecked_vma, range_ptr) { + let user_page_addr =3D vma_addr + (page_index << PAGE_SHIFT); + vma.zap_page_range_single(user_page_addr, PAGE_SIZE); + } } =20 drop(mmap_read); --=20 2.53.0.273.g2a3d683680-goog From nobody Sun Apr 5 13:06:01 2026 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (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 228DC36C0CF for ; Tue, 17 Feb 2026 14:22:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771338179; cv=none; b=Hwnypcrb9lWFzYX5+BvkuCXkWtG7XAMSLmbd7nN6M9lte9tEGag3PSnVukH2ZZxs9C7H4D60tJva0xEPBxNgIi5qB/hHnHEDg0KfVmloD5tOPaxFneKhI0ywUcX62r+gnJEb3aAVyOlkAVWlmP+je4CabxYEnTyRTc+ARqdP5kU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771338179; c=relaxed/simple; bh=Efrfc+ezEwByI/gNWdWXysiNKRrRQRbM6CMnAM/EeeM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YjDPZ/36x/wfrcCRDjO3oUHm2EHh7e4NiLUDHBKvQ2H+gvH9cqvCj2NaT3/rbJ+Xrm8Q9cayFZSC5GR3PbYfHhTqm8rPU9VNxh/fvbimtNzwIjjeArygaPHTgg74afHoQckBCe+JGcUonOleeCB86estGvSzJCs4xHUyLQDvrAY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=nT1YAq7n; arc=none smtp.client-ip=209.85.221.73 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--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="nT1YAq7n" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-43637c70876so3280536f8f.2 for ; Tue, 17 Feb 2026 06:22:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771338176; x=1771942976; 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=6LeKap7jZK//r6ZxbwvZGHpBK/lawg1BApnHVA3ELvI=; b=nT1YAq7nGZb1vHuJs3IBdSVY8q3dpaImDMox2CMHkufbX3H+dJt8LjRJfzyU1B0vg/ lmzjPNcZCNrudJew2DnjDFHjD9T9eE2Ndy48OyF4f6pfmkOCQuIVyHwk5VxVHZ4UuTWM oHD/+txFg74uuUaGfxhe7+6vNy5hvmC+bULTbawxlx4jmEes6TMI5iLoiBBUIWsL4p4b 3OB0vgboR3CL2zd5I3DEIhQVz4n7FwiZer9AYVIsoACf8vUD4Mteq7dprE03smq2mYfO HW3W53Y+mByMSBlG0GmT8WrUJ/s5oYVQVYyUEH6zWxRPG4wUVQRVvxGJqNa1gZ0ZwoUR gSpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771338176; x=1771942976; 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=6LeKap7jZK//r6ZxbwvZGHpBK/lawg1BApnHVA3ELvI=; b=hREBsCQVjkzc+wcujNeiXRmvpbEaV/VYkPlMyNjca0IcRsCALG8D+YzTHKlx/2JRWj sZqqxjbS1k7V8ltR7eWquvlAE980M3tM542oTwSywdCCHLLZH32pSnkScwrl7SRipQpC VEnxT70Tew47MJvvKONSpuUMW+SPGuXx0Z8DtVzdKU8BF1PbNZ4/3wMd6GudmtQ1na3d o0s78ymAJSsoD8fbKB9m1/5T1up3fBo4LAlIZl2YEcBo4iJpdcV2WcjSWUS/EgZyxO1N /zE+a5CVj0eJwYXjysMQZ2+AZwAi9HdCvaAyXZZM0xatopMidMkEOExOhciHqARgnal3 UZQQ== X-Forwarded-Encrypted: i=1; AJvYcCXzfwZRc+Vg47OwQVlGHO6Q1qTuxaPJW8vuD9swpLbQWBu+6cT7R8VehwtcoyOtMyy1BgeXu2eEguxZuFc=@vger.kernel.org X-Gm-Message-State: AOJu0YwBF4GPKA9iwlKtW/6cb7hfCwhbyIJcuuOM0wwaTPAHbNr3kyzx 3dWYrY26LQTZFn78z1JNWaQKKa6I7qmTCfREE5zOyQU/LSO8ocHyAmLiHI98cizV+MtldEk2EzU T5AdgB/V4qHpF+wyI5A== X-Received: from wrnc10.prod.google.com ([2002:adf:e74a:0:b0:437:6b00:8cbc]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:240e:b0:431:808:2d50 with SMTP id ffacd0b85a97d-4379db24fb0mr20850357f8f.13.1771338176111; Tue, 17 Feb 2026 06:22:56 -0800 (PST) Date: Tue, 17 Feb 2026 14:22:39 +0000 In-Reply-To: <20260217-binder-vma-check-v1-0-1a2b37f7b762@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260217-binder-vma-check-v1-0-1a2b37f7b762@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2851; i=aliceryhl@google.com; h=from:subject:message-id; bh=Efrfc+ezEwByI/gNWdWXysiNKRrRQRbM6CMnAM/EeeM=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBplHm5prYIkklTAyDnHpvdtT4ZnH/PWKGUlo9A7 e0m3cQkth6JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaZR5uQAKCRAEWL7uWMY5 RjRCD/9Hw/hm2Xm3vaKkplhmlEb0PY2QkvIEUCwKHyT/1GELqXg87R/lLUOS5SfbeX6FB3MKzyZ qMQrby2qSbni8YHedTtzjFZZNS5kKbyOIXtkyiK0htiI4E+oV637qnqGzSqId3M9Sc1FbgYhZmD M5ot3f02kV1P9LLybjMEAXBxuEgYKjHv/Cdc1tan0KELqEmRyXF7wneln2hM9GGoXaXeUrk6/EW aDQo1JuVycWlZgzXY1bfMUCYbABcHl1xVcVt6+HAfCHpTPleAIvy1xFDoNtMUB//NwUT3ne7AjJ XodQxTZxTZZqsD2QSBzk+b6i8yJRFv9EVxBEFHn5N/F5PsPv2/HYU1eXWe6X7oh6UBFkeOHE/g9 wQ2Xt4wa7HWgQwUYEofVqhJwcadiMbw9NAuB5ghDBrEZoprpr5Gg6zyYq6FbuJU9l5vWTKgarOb /5rddyZlLCgqDppocuz5wInoSxvJkpVwSwk10MjRSIyyURCpZtFQ2m2HDTqhYP5pgc8UQ1Ke5Nh 8XFyDEqHwCRFMaxZ8ZaGWp4OFfAPKVJXkBPqToS90XrexsvF6L9m7Nhq5qSIejoh0JLBvUzILyQ XxBk0KpMUnllvaBB7ZXdoilUxvFY1GS5qS29U3uVRQcjiFtOdMyTifisrClGZ3F/2bpSewTI7f3 mliJbE3uEwn0XCg== X-Mailer: b4 0.14.2 Message-ID: <20260217-binder-vma-check-v1-2-1a2b37f7b762@google.com> Subject: [PATCH 2/2] rust_binder: avoid reading the written value in offsets array From: Alice Ryhl To: Greg Kroah-Hartman , Carlos Llamas , Jann Horn Cc: Miguel Ojeda , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , Lorenzo Stoakes , "Liam R. Howlett" , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-mm@kvack.org, Alice Ryhl , stable@vger.kernel.org Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable When sending a transaction, its offsets array is first copied into the target proc's vma, and then the values are read back from there. This is normally fine because the vma is a read-only mapping, so the target process cannot change the value under us. However, if the target process somehow gains the ability to write to its own vma, it could change the offset before it's read back, causing the kernel to misinterpret what the sender meant. If the sender happens to send a payload with a specific shape, this could in the worst case lead to the receiver being able to privilege escalate into the sender. The intent is that gaining the ability to change the read-only vma of your own process should not be exploitable, so remove this TOCTOU read even though it's unexploitable without another Binder bug. Cc: stable@vger.kernel.org Fixes: eafedbc7c050 ("rust_binder: add Rust Binder driver") Reported-by: Jann Horn Signed-off-by: Alice Ryhl --- drivers/android/binder/thread.rs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/drivers/android/binder/thread.rs b/drivers/android/binder/thre= ad.rs index 1f1709a6a77abc1c865cc9387e7ba7493448c71d..f58ecccf5bb10a4b916d14a38db= b3bdfdda24ff8 100644 --- a/drivers/android/binder/thread.rs +++ b/drivers/android/binder/thread.rs @@ -1016,12 +1016,9 @@ pub(crate) fn copy_transaction_data( =20 // Copy offsets if there are any. if offsets_size > 0 { - { - let mut reader =3D - UserSlice::new(UserPtr::from_addr(trd_data_ptr.offsets= as _), offsets_size) - .reader(); - alloc.copy_into(&mut reader, aligned_data_size, offsets_si= ze)?; - } + let mut offsets_reader =3D + UserSlice::new(UserPtr::from_addr(trd_data_ptr.offsets as = _), offsets_size) + .reader(); =20 let offsets_start =3D aligned_data_size; let offsets_end =3D aligned_data_size + offsets_size; @@ -1042,11 +1039,9 @@ pub(crate) fn copy_transaction_data( .step_by(size_of::()) .enumerate() { - let offset: usize =3D view - .alloc - .read::(index_offset)? - .try_into() - .map_err(|_| EINVAL)?; + let offset =3D offsets_reader.read::()?; + view.alloc.write(index_offset, &offset)?; + let offset: usize =3D offset.try_into().map_err(|_| EINVAL= )?; =20 if offset < end_of_previous_object || !is_aligned(offset, = size_of::()) { pr_warn!("Got transaction with invalid offset."); --=20 2.53.0.273.g2a3d683680-goog