From patchwork Wed Jan 24 11:20:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 191493 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2553:b0:103:945f:af90 with SMTP id p19csp920196dyi; Wed, 24 Jan 2024 03:23:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IHnjpfwX428eZfmZr2x+2CqAOqb4y3NoUPNZ/KDk2SmZIuHPSy4ZirL/1yTi4rxBmZckv2p X-Received: by 2002:a05:6a20:c907:b0:199:e6d0:646f with SMTP id gx7-20020a056a20c90700b00199e6d0646fmr580719pzb.62.1706095409554; Wed, 24 Jan 2024 03:23:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706095409; cv=pass; d=google.com; s=arc-20160816; b=Lb6/O2XlscWMD1+XACXBeKwrr9LqxGTswe5xvQoma8/PhwzWYyRzYk8bRCUlsRS7/S KVGgkLNkdNDFbDpqcK7NTfcax7JKBFCc6A37I5EiIG//JO3BcPlNSUp543WVX2fCWoVE eQzufQGavkZUl+OE+EEMURJHuajKG8E9JWMyhsLA19jHlmRGtD/dTKPzYHe441k/9Ec0 zgTnvHC/+ktFEJsuXBuX+hKuHxCwO3A4u0C+BrByKuX+DnwMx9cS+GEXEN5fvrBscTw4 ytuDMzpOrWv55/3cgknCQyJ9FwtPZCH9etHNVVzi+0dIN4AwCxCw4IjsVECG29xDZJPl LYog== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=yeYmyXZKazzZuN2hgbJgkgGVYq/XVAwngt0nzNHk5M8=; fh=pbMQvrklqG6GdVdPzj2FmnXVkDYvQuGdRzCBEiIMecQ=; b=KVcveDtR1xho4oqINDUs0RqhL879V0zX+bFOCxdLhH07hjTjjVpe2wSoeqEwQCLm5P U9bXshG4aa6zZqMSIiFpGQBldOOQBemxMH1rtU+Lks1Smx+4nk1VUzFfmVWvDwqWOohX w3IhWv/1rNqp7+JEA2/z6kOq0Hxl0utQB3Bgvg9SAEN9gDYtOaTmmh1E6/kkOpH/zzwN cA9WK5oUqwYhwKW7DwxSvRVdnvrYXtq5XlKRg8pAuGrd18B6jgLiwg7fo6FvCzU7hiuS ktUahOy5fxyNAkITmkF9pnwk9+xUNJfySIIZS45/IaSre5AaV492IEMJLbx0jTbXNreS R+0w== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=31S5Peun; arc=pass (i=1 spf=pass spfdomain=flex--aliceryhl.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-36848-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36848-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id kx17-20020a17090b229100b002909c748412si5160464pjb.128.2024.01.24.03.23.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 03:23:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-36848-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=31S5Peun; arc=pass (i=1 spf=pass spfdomain=flex--aliceryhl.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-36848-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36848-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 4F43128A33F for ; Wed, 24 Jan 2024 11:22:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 78F394F613; Wed, 24 Jan 2024 11:21:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="31S5Peun" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 7FA6A2C68F for ; Wed, 24 Jan 2024 11:20:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706095255; cv=none; b=KORzFfFGrh/U4lq18N+ygr+UzfGYKDOxAG16xsFFbnTdyT3MFZwSkWAhL0U49l/3v3/7vzkB3atp8lnzGQMenW9/WsbY3uuzHTwNOomtDaJWH6JiSrbha7ypBz4eH4LwhIOeEFY9KitHfzJ3vfRENYLEU6/plPb6WVRVOrLV3ag= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706095255; c=relaxed/simple; bh=vymGv46wM2XordLIDJAiR3VCTZsi5VESqwv8kajL3uk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=og4hZtizjH9W/X8R1UnVrI2tIAwsE3Hg/9D8KqyWjtTDV1rPdDJfzZIHMN3DZNbnXZCiQFSlZLNcTvuQt/DlMNfXVBLp+O+Z/YuOztxNVGGERr1X7P4FPLP3OCUmbqJKHnL022t2VfmNHo6jLsIUiaQPbVKKU6IBgWa4fnLpqgU= 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=31S5Peun; arc=none smtp.client-ip=209.85.128.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--aliceryhl.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5f53b4554b6so79549037b3.3 for ; Wed, 24 Jan 2024 03:20:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706095251; x=1706700051; 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=yeYmyXZKazzZuN2hgbJgkgGVYq/XVAwngt0nzNHk5M8=; b=31S5PeunZtViVv/ShmkJLqdNi1RkaTIFn8O6t6XUWADrdUHQ1M3u3uAyfrz8KWety6 WmNP/Lm9pQ7M9Ml5Qrs71BrOCqp38bDHvg53TGILL9s/99YmykkaKNAMII9JWehOxuFa m+UhAjXI1CZkGkanJIv0VjuIKKVHBLHLL5h1FgZc0K2oRKk/M8GnCnzDP0NgpZhH8Hsk pxxWP6eAYIbyLykTmR+6uZ/3thIHEstMPqO1H1jhpD1w3AdCK30kHQJJClw9ETrvkAtu 3LHOAs7PUZMN9ENvbmaNT79zcgWRXI2voR+yZ16ur97SwP+31DWtLUyAegv8kQ6n2bhw TXqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706095251; x=1706700051; 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=yeYmyXZKazzZuN2hgbJgkgGVYq/XVAwngt0nzNHk5M8=; b=hBETjGpCwONM6D/Rv/u5RHcQ3PaqXO4qk+o2WHq1heRO7vvlzqYacbJTUqa5FOTcO5 aXUX8uWpy4P/IEG3mGLvULyb/MFyOb5ZhpnD+LombK/7FDZCIY6mUoG+7p9KejMndChV 9TwTDAJ6do1wytIVXKy9lkeNbjAgzPsNclFwhgv/1RA/Xf2FdozlXpkZvAUtzVN/qUTh iR4AhtK+19c71/Fiu1GDy8Y9UZz30hkpAPbznPp0o87rpJaRcN5xCaLu8KzZ26NDP1KB tdh1lq437kOJMZrb4eECaZgb6Ehk4XuKtlUf4bpkV0d5tLosARJX7+NTtOL+TZU9BF+P Yppw== X-Gm-Message-State: AOJu0YyE3EXMIUhwjaoCfjmgHfupub1pQF5CXFo514bbps/vR9NZgC0Y 1F+tGixexkyFh73nehpGalFGIRE9d7k6Z11ek2XwokKyR9BZB+3CVE2+x30PesgjI2c65yCKauU Jk3O9IXwnY6gAjA== X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a81:4ec3:0:b0:5ff:5135:ffcf with SMTP id c186-20020a814ec3000000b005ff5135ffcfmr211243ywb.4.1706095251533; Wed, 24 Jan 2024 03:20:51 -0800 (PST) Date: Wed, 24 Jan 2024 11:20:22 +0000 In-Reply-To: <20240124-alice-mm-v1-0-d1abcec83c44@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240124-alice-mm-v1-0-d1abcec83c44@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=9645; i=aliceryhl@google.com; h=from:subject:message-id; bh=vymGv46wM2XordLIDJAiR3VCTZsi5VESqwv8kajL3uk=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBlsPKJ3p0VhgMk8AKpCRyFubtzOao8ccYGTzkG+ +NZ+y3/eiCJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZbDyiQAKCRAEWL7uWMY5 RokAD/0TXFTgaEHIwdd+VZdCdJ3hpjT/gV/L0H4iqM+pzhwCg2FAbC2zAQUnoY5eSMPfazzRrkR b7I/ZDPT0xZAd2e+TLeiKEJSenYcP0bpVFnQjP1H8QJV1Y53i7lXVV/q8dpAXQ6fbbZvB98Pv06 Bky6MCWbbb8kgmCH14Jv5Vgxa3P83UrVdNlNE7XQu6dgORciRLcuuLi819gqMmRrVAUs1WMceGx t38dHV0F9KRYj2YIEebTN8BNvR+fxrT7RNu3SC6TAjA2p+V0TT4fl7thO2VlttP+UStglznTOL+ jkjEwOGg3VN9XVEPGbnODkUE6L+Pzhgb469AoYErW9epYGP6Com15uAiEsOA9IUmx82YYAopg8N jbVf/P67GLvzss8LprRUNrVQy4veFRwaZhoxrpJ3q7c2QIuDOfVgIXd59tJ+0+pV2KOIyL8F1Is MYjacF8CeI1cKXEUEx0SigY3tSQogrXTPRTb0XFs0yiviDd/mFnWSxCgzobtlYKJNTTcO50saJp 0uWo2Ni9PiyzXnZacb/bkx/GS1bE1V44v3tTlxbDTa2Djy2Cz6/TUEm2ntReBxDgepV4K/XgJnq 2yu18Jm11EpkRMTkio2ISHW42srkLhYsD9yZRMARFPWHqrjuZVamQIWBYq/rhZDz0wL8t5qOVb9 hSf4DHbGeXA8Tfw== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240124-alice-mm-v1-2-d1abcec83c44@google.com> Subject: [PATCH 2/3] rust: add typed accessors for userspace pointers From: Alice Ryhl To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Kees Cook , Al Viro , Andrew Morton Cc: Greg Kroah-Hartman , " =?utf-8?q?Arve_Hj?= =?utf-8?q?=C3=B8nnev=C3=A5g?= " , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan , Arnd Bergmann , linux-mm@kvack.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Christian Brauner X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788970700180232141 X-GMAIL-MSGID: 1788970700180232141 Add safe methods for reading and writing Rust values to and from userspace pointers. The C methods for copying to/from userspace use a function called `check_object_size` to verify that the kernel pointer is not dangling. However, this check is skipped when the length is a compile-time constant, with the assumption that such cases trivially have a correct kernel pointer. In this patch, we apply the same optimization to the typed accessors. For both methods, the size of the operation is known at compile time to be size_of of the type being read or written. Since the C side doesn't provide a variant that skips only this check, we create custom helpers for this purpose. The majority of reads and writes to userspace pointers in the Rust Binder driver uses these accessor methods. Benchmarking has found that skipping the `check_object_size` check makes a big difference for the cases being skipped here. (And that the check doesn't make a difference for the cases that use the raw read/write methods.) This code is based on something that was originally written by Wedson on the old rust branch. It was modified by Alice to skip the `check_object_size` check, and to update various comments, including the notes about kernel pointers in `WritableToBytes`. Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Signed-off-by: Alice Ryhl Signed-off-by: Arnd Bergmann --- rust/helpers.c | 34 +++++++++++++ rust/kernel/user_ptr.rs | 125 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+) diff --git a/rust/helpers.c b/rust/helpers.c index 312b6fcb49d5..187f445fbf19 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -52,6 +52,40 @@ unsigned long rust_helper_copy_to_user(void __user *to, const void *from, } EXPORT_SYMBOL_GPL(rust_helper_copy_to_user); +/* + * These methods skip the `check_object_size` check that `copy_[to|from]_user` + * normally performs. In C, these checks are skipped whenever the length is a + * compile-time constant, since when that is the case, the kernel pointer + * usually points at a local variable that is being initialized and the kernel + * pointer is trivially non-dangling. + * + * These helpers serve the same purpose in Rust. Whenever the length is known at + * compile-time, we call this helper to skip the check. + */ +unsigned long rust_helper_copy_from_user_unsafe_skip_check_object_size(void *to, const void __user *from, unsigned long n) +{ + unsigned long res; + + might_fault(); + instrument_copy_from_user_before(to, from, n); + if (should_fail_usercopy()) + return n; + res = raw_copy_from_user(to, from, n); + instrument_copy_from_user_after(to, from, n, res); + return res; +} +EXPORT_SYMBOL_GPL(rust_helper_copy_from_user_unsafe_skip_check_object_size); + +unsigned long rust_helper_copy_to_user_unsafe_skip_check_object_size(void __user *to, const void *from, unsigned long n) +{ + might_fault(); + if (should_fail_usercopy()) + return n; + instrument_copy_to_user(to, from, n); + return raw_copy_to_user(to, from, n); +} +EXPORT_SYMBOL_GPL(rust_helper_copy_to_user_unsafe_skip_check_object_size); + void rust_helper_mutex_lock(struct mutex *lock) { mutex_lock(lock); diff --git a/rust/kernel/user_ptr.rs b/rust/kernel/user_ptr.rs index 00aa26aa6a83..daa46abe5525 100644 --- a/rust/kernel/user_ptr.rs +++ b/rust/kernel/user_ptr.rs @@ -11,6 +11,7 @@ use crate::{bindings, error::code::*, error::Result}; use alloc::vec::Vec; use core::ffi::{c_ulong, c_void}; +use core::mem::{size_of, MaybeUninit}; /// The maximum length of a operation using `copy_[from|to]_user`. /// @@ -151,6 +152,36 @@ pub unsafe fn read_raw(&mut self, out: *mut u8, len: usize) -> Result { Ok(()) } + /// Reads a value of the specified type. + /// + /// Fails with `EFAULT` if the read encounters a page fault. + pub fn read(&mut self) -> Result { + if size_of::() > self.1 || size_of::() > MAX_USER_OP_LEN { + return Err(EFAULT); + } + let mut out: MaybeUninit = MaybeUninit::uninit(); + // SAFETY: The local variable `out` is valid for writing `size_of::()` bytes. + let res = unsafe { + bindings::copy_from_user_unsafe_skip_check_object_size( + out.as_mut_ptr().cast::(), + self.0, + size_of::() as c_ulong, + ) + }; + if res != 0 { + return Err(EFAULT); + } + // Since this is not a pointer to a valid object in our program, + // we cannot use `add`, which has C-style rules for defined + // behavior. + self.0 = self.0.wrapping_add(size_of::()); + self.1 -= size_of::(); + // SAFETY: The read above has initialized all bytes in `out`, and since + // `T` implements `ReadableFromBytes`, any bit-pattern is a valid value + // for this type. + Ok(unsafe { out.assume_init() }) + } + /// Reads all remaining data in the buffer into a vector. /// /// Fails with `EFAULT` if the read encounters a page fault. @@ -219,4 +250,98 @@ pub fn write_slice(&mut self, data: &[u8]) -> Result { // `len`, so the pointer is valid for reading `len` bytes. unsafe { self.write_raw(ptr, len) } } + + /// Writes the provided Rust value to this userspace pointer. + /// + /// Fails with `EFAULT` if the write encounters a page fault. + pub fn write(&mut self, value: &T) -> Result { + if size_of::() > self.1 || size_of::() > MAX_USER_OP_LEN { + return Err(EFAULT); + } + // SAFETY: The reference points to a value of type `T`, so it is valid + // for reading `size_of::()` bytes. + let res = unsafe { + bindings::copy_to_user_unsafe_skip_check_object_size( + self.0, + (value as *const T).cast::(), + size_of::() as c_ulong, + ) + }; + if res != 0 { + return Err(EFAULT); + } + // Since this is not a pointer to a valid object in our program, + // we cannot use `add`, which has C-style rules for defined + // behavior. + self.0 = self.0.wrapping_add(size_of::()); + self.1 -= size_of::(); + Ok(()) + } } + +/// Specifies that a type is safely readable from bytes. +/// +/// Not all types are valid for all values. For example, a `bool` must be either +/// zero or one, so reading arbitrary bytes into something that contains a +/// `bool` is not okay. +/// +/// It's okay for the type to have padding, as initializing those bytes has no +/// effect. +/// +/// # Safety +/// +/// All bit-patterns must be valid for this type. +pub unsafe trait ReadableFromBytes {} + +// SAFETY: All bit patterns are acceptable values of the types below. +unsafe impl ReadableFromBytes for u8 {} +unsafe impl ReadableFromBytes for u16 {} +unsafe impl ReadableFromBytes for u32 {} +unsafe impl ReadableFromBytes for u64 {} +unsafe impl ReadableFromBytes for usize {} +unsafe impl ReadableFromBytes for i8 {} +unsafe impl ReadableFromBytes for i16 {} +unsafe impl ReadableFromBytes for i32 {} +unsafe impl ReadableFromBytes for i64 {} +unsafe impl ReadableFromBytes for isize {} +// SAFETY: If all bit patterns are acceptable for individual values in an array, +// then all bit patterns are also acceptable for arrays of that type. +unsafe impl ReadableFromBytes for [T] {} +unsafe impl ReadableFromBytes for [T; N] {} + +/// Specifies that a type is safely writable to bytes. +/// +/// If a struct implements this trait, then it is okay to copy it byte-for-byte +/// to userspace. This means that it should not have any padding, as padding +/// bytes are uninitialized. Reading uninitialized memory is not just undefined +/// behavior, it may even lead to leaking sensitive information on the stack to +/// userspace. +/// +/// The struct should also not hold kernel pointers, as kernel pointer addresses +/// are also considered sensitive. However, leaking kernel pointers is not +/// considered undefined behavior by Rust, so this is a correctness requirement, +/// but not a safety requirement. +/// +/// # Safety +/// +/// Values of this type may not contain any uninitialized bytes. +pub unsafe trait WritableToBytes {} + +// SAFETY: Instances of the following types have no uninitialized portions. +unsafe impl WritableToBytes for u8 {} +unsafe impl WritableToBytes for u16 {} +unsafe impl WritableToBytes for u32 {} +unsafe impl WritableToBytes for u64 {} +unsafe impl WritableToBytes for usize {} +unsafe impl WritableToBytes for i8 {} +unsafe impl WritableToBytes for i16 {} +unsafe impl WritableToBytes for i32 {} +unsafe impl WritableToBytes for i64 {} +unsafe impl WritableToBytes for isize {} +unsafe impl WritableToBytes for bool {} +unsafe impl WritableToBytes for char {} +unsafe impl WritableToBytes for str {} +// SAFETY: If individual values in an array have no uninitialized portions, then +// the the array itself does not have any uninitialized portions either. +unsafe impl WritableToBytes for [T] {} +unsafe impl WritableToBytes for [T; N] {}