From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from sender6-op-o11.zoho.com (sender6-op-o11.zoho.com [165.173.180.11]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CEE2A2F8EB0; Thu, 15 Oct 2026 01:03:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=165.173.180.11 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1792026184; cv=pass; b=BU6ygVIE/C4UQAWhIzBVoZA8U2uNAyt4HYaOtu5R6whx8zI9q6xdX0UV69rU8xbZ/DGjkPvAvrtqGbIj9Dmvy1PPBT5+KDAxlPrl/3uoxCvrB2fItA382UvqShmi6tfZG3gPstsoyH+tSfYnLiV0jue3QfIqFLck9A1UTgrSHms= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1792026184; c=relaxed/simple; bh=lcbg5kQkDw781rY68G2Ek+MWgzTSurA5kI/dRhDzgI8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=q8toWl+/EJGL0zDSnPZMqF+E3GlSUuEljvVjlnPk4Atnv+LTPbcjAfA8MgLvftg7cM43wKYi6VjRm8gYqrNCzgovLmfxi9aXuvJB+O7jHADBm6bvFMg4S+IDScpkXptVOid9lD9vPWw09EDjBrhfquSkclnx+p+hCZcRpiiBJqs= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (1024-bit key) header.d=collabora.com header.i=deborah.brouwer@collabora.com header.b=X4oIpwxu; arc=pass smtp.client-ip=165.173.180.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=collabora.com header.i=deborah.brouwer@collabora.com header.b="X4oIpwxu" ARC-Seal: i=1; a=rsa-sha256; t=1792026145; cv=none; d=zohomail.com; s=zohoarc; b=N3aYmouYibetF8oGV/HXf9nEKTyxQy0s1HSMLugz18pmMk4LnZ76hv3Y2VKMi+kg94RCYEscR5p7bnKy+TKBgpMHm73UibzzW59/KwzWEOF0UYLtSldH+AC0/JahYSyUgGeqdtCcvj7aolCMKaO/oFi5G6gvgHiU1ASUyHBAw8Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792026145; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=TZj4JAKEk9rxGsNQ8glMZXj+CrnFlCGfm141H5rrCCg=; b=YyanWU6360+pNsJ4he3P6L8EAUmh7KIHUvp5tp583ftJEoi9QQySWT6mtrNcAbLcfj8gzJpjF2IJK4qEkyShHL0zBvULY5Fg4VXk+wfuADmnvVxBilNP2NxcY8xawVkANBws9lYQ6TXUK5YHH9L+jmU0LjIEXAmeQMW+GMJf6yY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=deborah.brouwer@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1792026145; s=zohomail; d=collabora.com; i=deborah.brouwer@collabora.com; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=TZj4JAKEk9rxGsNQ8glMZXj+CrnFlCGfm141H5rrCCg=; b=X4oIpwxubvNg8B9/AiZeh64qbySPwlJjt6Hr6nlX1vrFCUWC13kvR3fv1tuMsJwo WyQR7oKwnyRrAwm1bY+DQBG39KdDbTzC7KdSt7/ENHKv6Z8OxhecrwrkWXtdwHso/TG VofVTAdTfZ+o0Gpy4No7nuLUEQDyCeuL+EdbD1G4= Received: by smtp.zohomail.com with SMTPS id 1792026143456594.7050805266969; Wed, 14 Oct 2026 18:02:23 -0700 (PDT) From: Deborah Brouwer Date: Wed, 14 Oct 2026 18:02:03 -0700 Subject: [PATCH 1/2] rust: uaccess: add UserSliceWriter::clear() Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261014-b4-dev_query_fixes-v1-1-92043100b472@collabora.com> References: <20261014-b4-dev_query_fixes-v1-0-92043100b472@collabora.com> In-Reply-To: <20261014-b4-dev_query_fixes-v1-0-92043100b472@collabora.com> To: Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , David Airlie , Simona Vetter Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, Deborah Brouwer X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=2303; i=deborah.brouwer@collabora.com; h=from:subject:message-id; bh=lcbg5kQkDw781rY68G2Ek+MWgzTSurA5kI/dRhDzgI8=; b=owGbwMvMwCVWuULzOU9c7WvG02pJDFkX1GQyqndUHmNYUJrhlOmc/5ZrzfeomzFdvw9NTgpWT csSq5LtKGVhEONikBVTZDlrb9QjXvXeSHf+/2aYOaxMIEMYuDgFYCKbzBj+CoVYmzlpZ3y9sae6 h42zadMSY+1zSXPbtI/pMF7xznvylJHhGcufg9etCwxy7178ayGr+lf3Yxub+r21L2XerXpw+Fs sGwA= X-Developer-Key: i=deborah.brouwer@collabora.com; a=openpgp; fpr=CD3F328C177AEF322D9FFF8379A829E70C5E7DEB Add a method to zero the remaining bytes in a UserSliceWriter. If userspace provides a buffer that is larger than necessary, provide a method to zero out the remaining UserSliceWriter buffer. This avoids userspace consuming potentially uninitialized memory and/or mistaking it for data intentionally provided by the kernel. Since clear_user() is architecturally defined and may be a macro or inline function, add a rust helper to make clear_user() callable from rust. Signed-off-by: Deborah Brouwer --- rust/helpers/uaccess.c | 6 ++++++ rust/kernel/uaccess.rs | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/rust/helpers/uaccess.c b/rust/helpers/uaccess.c index 6e59cc9c665c..e799d9ac9804 100644 --- a/rust/helpers/uaccess.c +++ b/rust/helpers/uaccess.c @@ -27,3 +27,9 @@ unsigned long rust_helper__copy_to_user(void __user *to, const void *from, unsig return _inline_copy_to_user(to, from, n); } #endif + +__rust_helper unsigned long +rust_helper_clear_user(void __user *to, unsigned long n) +{ + return clear_user(to, n); +} diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs index f1aecda7a90c..6838e7f90abb 100644 --- a/rust/kernel/uaccess.rs +++ b/rust/kernel/uaccess.rs @@ -638,6 +638,29 @@ pub fn write(&mut self, value: &T) -> Result { pub fn write_truncated(&mut self, value: &T) -> Result { self.write_slice_partial(value.as_bytes(), 0) } + + /// Zeros remaining bytes in the userspace buffer. + /// + /// Fails with [`EFAULT`] if the userspace memory cannot be cleared. + pub fn clear(&mut self) -> Result { + let len = self.length; + + if len == 0 { + return Ok(()); + } + // SAFETY: `clear_user` checks the userspace address and range and handles + // faults when accessing userspace memory + let res = unsafe { bindings::clear_user(self.ptr.as_mut_ptr(), len) }; + + if res != 0 { + return Err(EFAULT); + } + + self.ptr = self.ptr.wrapping_byte_add(len); + self.length = 0; + + Ok(()) + } } /// Reads a nul-terminated string into `dst` and returns the length. -- 2.56.0