From nobody Fri Apr 3 06:27:15 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