From nobody Sat Feb 7 17:04:38 2026 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (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 A01FD27146A for ; Mon, 29 Dec 2025 15:38:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767022705; cv=none; b=RnCG3uHXxKFV3yKmKv05hMxWbCrf8QfPzLKvZ09nZZpFHVgPzZ9SHUxQaXPo+8B9VmJTd5Sc8nXEYZUtKkucvLhLaX0cbIAMXDqobvZUjMMjtPhoHebZk25qtjXn3QbfrkvaortliyBGPn271ImqQaBWA6ep5TCQezqCIb4qAXA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767022705; c=relaxed/simple; bh=Y6Ea4Z7dcYryMC4FzmOI5WW/TWDy28tX9/x+j04ymkw=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=gBMbCaITBDsqCmeZIA+Tsj/nQGu43RQL5o3bwQ857AaO2+n8IqRoOqWihy0P6RkiMuKyDivsdGsM639iF+Twgo/9CCizC0H0WI+L4Gwrr10AukWApr8hSvYczsmMTM9c/q5wrmEqSTnSUrowQjWrsSW7oB83A+pupp12doG7+0g= 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=YXz7+seg; arc=none smtp.client-ip=209.85.221.74 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="YXz7+seg" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-430f57cd2caso8896472f8f.0 for ; Mon, 29 Dec 2025 07:38:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1767022702; x=1767627502; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=jv1bfL8UVpkwL6HAaa3ZJfw2xxgzOOcpqTF8GDMtxH0=; b=YXz7+seguY+npplDN+AUcnxIYNPmkTmwJwfqrR/OR7hhSKEM0hjz0ZOPOZz1ZN84vf md9a5GnZFU5lx06YHhHPoei5t5UXzRskDCaP/T4hTc1Y6fB1FizvKoB8rfSRf2NIL5/y axwBcRr8fOVlJg3dwD7YcK8lEZlh8l9+OpBO2T4IO86kyOL90VpPax/m0HU0E0lQozFc 0e5B2oIXBHINd1i7f+s2YC9uzRyt1aKuUnBgGnfJrS+I0SPcRBeCDYXdSnjOGWvVF6Zu umORnBjEMP/TCyP+LZADwKM7gdmkLmWHXLCHbBGI/ggrI77l6c3PQty3GeITjZ0AllUp ZX0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767022702; x=1767627502; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=jv1bfL8UVpkwL6HAaa3ZJfw2xxgzOOcpqTF8GDMtxH0=; b=h8gW6chIltJGB2HTOxxlXCkJQBWnJ/fG5Ugb9R18csUhCfhLby+5uiLkGEGmtR46lo Td60Gp9fN8AiO30ghHFLT/B6B5Mr/tQjoISfSCLPKnWG7Mrbet5+BCk/03Ox5PP0sh+U GVjxkfP2A7GohxyFBthLVijE1puuhS5TUKg28WsYTOsKjIzqFLEeTVPIm8I0tVpwEPeH xpTXL7dlhNi4pmASC3cWi+XEyqWCSOKbd1nD/mQwBZOzhUtOz16DV4/Wdv5OFUL+IK5v YQn+6Eo846287yKvaxlMtbZzSK8osIH3AzPtsVFQrFzyqENPP5pmhhXifbySgFJFjqZE JHeg== X-Forwarded-Encrypted: i=1; AJvYcCVTib5cpTY66lJAEzN9ki/vRWCSZFNz4wKUPT8ergxtB1SwdrTAEr1s5Sy1rqLRVOX0MatX2+UbbASC6R0=@vger.kernel.org X-Gm-Message-State: AOJu0Yx+ISkq53qgMT9CWF4xhzAXJ/V8YVc6GXaKWCUYswmPcNeKSYJg paqC6NQd9ZQaP0Pab3QKxOXduuSP0ziprmwSxY9MBvgJJ7reEsYnC8mOhLNo538IjpukajWAm5I g+V4bOgZgfl331K8e7w== X-Google-Smtp-Source: AGHT+IFxtD0GdZpJz0pNJUbAiSxNd2h4uts5rUN32W6Te6/i3nu/CNsM+WlL7lc3O57NG4BfPLHyiF6OP7LgfAM= X-Received: from wrbgv10.prod.google.com ([2002:a05:6000:460a:b0:42f:8d68:be9c]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a5d:64e3:0:b0:430:fced:90a with SMTP id ffacd0b85a97d-4324e4cd1c4mr33849938f8f.16.1767022702052; Mon, 29 Dec 2025 07:38:22 -0800 (PST) Date: Mon, 29 Dec 2025 15:38:14 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAGWgUmkC/6tWKk4tykwtVrJSqFYqSi3LLM7MzwNyDHUUlJIzE vPSU3UzU4B8JSMDI1NDIyNL3bSURN2q1KJ8XZNUE7NUU7OkVFOLFCWg8oKi1LTMCrBR0bG1tQC wRqEWWgAAAA== X-Change-Id: 20251229-fda-zero-4e46e56be58d X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=7110; i=aliceryhl@google.com; h=from:subject:message-id; bh=Y6Ea4Z7dcYryMC4FzmOI5WW/TWDy28tX9/x+j04ymkw=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBpUqBnxjnuT+VFuVlxhfLtXrABPQCbIpTVcAIOz T98k2Gz2AKJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaVKgZwAKCRAEWL7uWMY5 Rm9VD/472WCNE+7sP7xX7AqIPS43qVHqs8yZO4o/RQ4X4sVivANeKmbMnQusZNpimGpX6KT+J1l 0cLDR7EssaNrFgKPFmcBtrICXQxYi8kiiN8uNQ9DiaUWqm2SBpc5SsNjmR00MDTb8wCvaU27rQO ZHkmScZcuj6YftGRUeTERy0gZo8dhATpqkOTqk/yc2GpiacrHJZbXp5ChbG3OEkjk6zWFB8/11l P/Y1BtRJ9WayGHg/GYSo6nKhtf4e5i+0sTpQk9GOP4iWdRC0imlusmuuzIDJbek03vT8x6fuCte DBSioeqWLnx8jqeo29FGCyOTyrVNaNOCamdh4Z9er0UeJmiJQDjqlvwmMnZSv8579vADL+M13Hk GT8PIMByR9aJ30zgS2hXAmxdXxPZjLhjLD5ULEbojG34GmhG04C8HDktMSO8JCdHQGO2j8wnR0f lvVlFURNnwkS1SX67aPWojpgiDHap39MSE4fb8uvf29tXdXz4jlsqa2u76Aemm0XX1ruF1llnRE wahTtg8WXQgc+dv84F5rs7lN04qW0Vgxb1CDy+QAitxQ5NjCWoelJeVvz6ApQNC8tR3tifqOrfW NME7Xatx2FCmaxeJMkExtckyOP1LXlkEQSPAj0S+aqyoxKPlANkF8i9+JZkpWGWwconcoUvxJOu f1WmjQCkTk5u2Lw== X-Mailer: b4 0.14.2 Message-ID: <20251229-fda-zero-v1-1-58a41cb0e7ec@google.com> Subject: [PATCH] rust_binder: correctly handle FDA objects of length zero From: Alice Ryhl To: Greg Kroah-Hartman , Carlos Llamas Cc: Miguel Ojeda , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, stable@vger.kernel.org, DeepChirp , Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Fix a bug where an empty FDA (fd array) object with 0 fds would cause an out-of-bounds error. The previous implementation used `skip =3D=3D 0` to mean "this is a pointer fixup", but 0 is also the correct skip length for an empty FDA. If the FDA is at the end of the buffer, then this results in an attempt to write 8-bytes out of bounds. This is caught and results in an EINVAL error being returned to userspace. The pattern of using `skip =3D=3D 0` as a special value originates from the C-implementation of Binder. As part of fixing this bug, this pattern is replaced with a Rust enum. I considered the alternate option of not pushing a fixup when the length is zero, but I think it's cleaner to just get rid of the zero-is-special stuff. The root cause of this bug was diagnosed by Gemini CLI on first try. I used the following prompt: > There appears to be a bug in @drivers/android/binder/thread.rs where > the Fixups oob bug is triggered with 316 304 316 324. This implies > that we somehow ended up with a fixup where buffer A has a pointer to > buffer B, but the pointer is located at an index in buffer A that is > out of bounds. Please investigate the code to find the bug. You may > compare with @drivers/android/binder.c that implements this correctly. Cc: stable@vger.kernel.org Reported-by: DeepChirp Closes: https://github.com/waydroid/waydroid/issues/2157 Fixes: eafedbc7c050 ("rust_binder: add Rust Binder driver") Tested-by: DeepChirp Signed-off-by: Alice Ryhl Acked-by: Carlos Llamas --- drivers/android/binder/thread.rs | 59 +++++++++++++++++++++++-------------= ---- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/drivers/android/binder/thread.rs b/drivers/android/binder/thre= ad.rs index 1a8e6fdc0dc42369ee078e720aa02b2554fb7332..dcd47e10aeb8c748d04320fbbe1= 5ad35201684b9 100644 --- a/drivers/android/binder/thread.rs +++ b/drivers/android/binder/thread.rs @@ -69,17 +69,24 @@ struct ScatterGatherEntry { } =20 /// This entry specifies that a fixup should happen at `target_offset` of = the -/// buffer. If `skip` is nonzero, then the fixup is a `binder_fd_array_obj= ect` -/// and is applied later. Otherwise if `skip` is zero, then the size of the -/// fixup is `sizeof::()` and `pointer_value` is written to the buffe= r. -struct PointerFixupEntry { - /// The number of bytes to skip, or zero for a `binder_buffer_object` = fixup. - skip: usize, - /// The translated pointer to write when `skip` is zero. - pointer_value: u64, - /// The offset at which the value should be written. The offset is rel= ative - /// to the original buffer. - target_offset: usize, +/// buffer. +enum PointerFixupEntry { + /// A fixup for a `binder_buffer_object`. + Fixup { + /// The translated pointer to write. + pointer_value: u64, + /// The offset at which the value should be written. The offset is= relative + /// to the original buffer. + target_offset: usize, + }, + /// A skip for a `binder_fd_array_object`. + Skip { + /// The number of bytes to skip. + skip: usize, + /// The offset at which the skip should happen. The offset is rela= tive + /// to the original buffer. + target_offset: usize, + }, } =20 /// Return type of `apply_and_validate_fixup_in_parent`. @@ -762,8 +769,7 @@ fn translate_object( =20 parent_entry.fixup_min_offset =3D info.new_min_offset; parent_entry.pointer_fixups.push( - PointerFixupEntry { - skip: 0, + PointerFixupEntry::Fixup { pointer_value: buffer_ptr_in_user_space, target_offset: info.target_offset, }, @@ -807,9 +813,8 @@ fn translate_object( parent_entry .pointer_fixups .push( - PointerFixupEntry { + PointerFixupEntry::Skip { skip: fds_len, - pointer_value: 0, target_offset: info.target_offset, }, GFP_KERNEL, @@ -871,17 +876,21 @@ fn apply_sg(&self, alloc: &mut Allocation, sg_state: = &mut ScatterGatherState) -> let mut reader =3D UserSlice::new(UserPtr::from_addr(sg_entry.sender_uaddr), = sg_entry.length).reader(); for fixup in &mut sg_entry.pointer_fixups { - let fixup_len =3D if fixup.skip =3D=3D 0 { - size_of::() - } else { - fixup.skip + let (fixup_len, fixup_offset) =3D match fixup { + PointerFixupEntry::Fixup { target_offset, .. } =3D> { + (size_of::(), *target_offset) + } + PointerFixupEntry::Skip { + skip, + target_offset, + } =3D> (*skip, *target_offset), }; =20 - let target_offset_end =3D fixup.target_offset.checked_add(= fixup_len).ok_or(EINVAL)?; - if fixup.target_offset < end_of_previous_fixup || offset_e= nd < target_offset_end { + let target_offset_end =3D fixup_offset.checked_add(fixup_l= en).ok_or(EINVAL)?; + if fixup_offset < end_of_previous_fixup || offset_end < ta= rget_offset_end { pr_warn!( "Fixups oob {} {} {} {}", - fixup.target_offset, + fixup_offset, end_of_previous_fixup, offset_end, target_offset_end @@ -890,13 +899,13 @@ fn apply_sg(&self, alloc: &mut Allocation, sg_state: = &mut ScatterGatherState) -> } =20 let copy_off =3D end_of_previous_fixup; - let copy_len =3D fixup.target_offset - end_of_previous_fix= up; + let copy_len =3D fixup_offset - end_of_previous_fixup; if let Err(err) =3D alloc.copy_into(&mut reader, copy_off,= copy_len) { pr_warn!("Failed copying into alloc: {:?}", err); return Err(err.into()); } - if fixup.skip =3D=3D 0 { - let res =3D alloc.write::(fixup.target_offset, &f= ixup.pointer_value); + if let PointerFixupEntry::Fixup { pointer_value, .. } =3D = fixup { + let res =3D alloc.write::(fixup_offset, pointer_v= alue); if let Err(err) =3D res { pr_warn!("Failed copying ptr into alloc: {:?}", er= r); return Err(err.into()); --- base-commit: 8f0b4cce4481fb22653697cced8d0d04027cb1e8 change-id: 20251229-fda-zero-4e46e56be58d Best regards, --=20 Alice Ryhl