From patchwork Thu Feb 8 15:47:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 198450 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp259982dyd; Thu, 8 Feb 2024 07:49:33 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXjFdB0fcftDeyLZH4dI8JL6H72sbgMVPn1+QfHl0RU4FMOFGAhSVOpc6lXljny/YFAoJtyZBsA4MTTFhmNiaVuHz8N6Q== X-Google-Smtp-Source: AGHT+IG4i8WnzCB+28ElmM97vbSgiwZS7s81uOSl21jSH/lNj1HZrXpM037Lb8KMWqLl2UhJRAUO X-Received: by 2002:a05:620a:15a9:b0:785:5b0b:ef75 with SMTP id f9-20020a05620a15a900b007855b0bef75mr3936657qkk.29.1707407373543; Thu, 08 Feb 2024 07:49:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707407373; cv=pass; d=google.com; s=arc-20160816; b=Fyu89UQcfiiVjgWYIH9ph9gfXh020wtpBsidXP2bdqT0bFm+TY2BZaaIS5Ez5u9JXi Yq43SSCIaNMPmq3iMAzfb/JBNT/MmcaweWgEvDxtFj0m/JRM3iizwrCRLypIgxyeb+vY L7h0jpPlruRLg5wS+XHlt2DTDJdmJxuppnr0CgzuPz+RE5UWiBXYSnOOBzMCGqqgKyR/ 32wN2CE94ZI+6tF7LNrLvv/ESivvH0kS3iPI2rsIk39eyCO/ne+a3khshKAdTyeeGskk XyUsvHsY7WbriAz+xg+vYwffeTcr+7Cm4PUK4la2ieQV3HTDUbXMQctdkLUcecxoG421 SOJw== 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=1MPoST0g8AYkDBhG2VsbV1sslw+0urv81QcaeUn+juw=; fh=lA9vuezNrZG/fL3+HquQxI8ddN/czGsBwgSlTzg9tRc=; b=0BYx090Uk6ER3+ozSwCCWtAeRsa4kiTYQQsMfJf6UM7AI1/D2rXxs/yP1XOVRUm4Pc vaDicwmQa2mZNDQyeySPUEOh32xIBmdVMBu+x6JTGTAXoZO9eQHmwdAC3jUWebD0x6WL CWH8iDv/NMU5YRNKGfJ4RxyYs7FybejP7tsO2fFlf/azTmAu/JOBCgAt2+YayQkZihsg xCOfXFHpkWYyI/cG9sZ53quEVNAlgYxLju9scarmNJFZOU1/wC6rQS30fIyKyt+tnFbw VvkJYI+0ZMnPt/gOZ4bfV/UL3G82+CyjbxPhS62XuS2fZLSXy/KA34cA0pAPh3RoREXy 6SiQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mhcle2eZ; 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-58302-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58302-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCVQtb/N6lQb+k2qrt+EFWCeO1ImYkuWsMWG8LTWG2wOuTPC1WYbfg+i5uB6p9zYU8TFK2u5eryCco6BgLKbsXOn99cITw== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id e6-20020a05620a208600b007858e9c0716si221374qka.488.2024.02.08.07.49.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 07:49:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-58302-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mhcle2eZ; 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-58302-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58302-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 388281C22848 for ; Thu, 8 Feb 2024 15:49:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 93BEC7EEF5; Thu, 8 Feb 2024 15:48:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="mhcle2eZ" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.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 BC4F17D3F6 for ; Thu, 8 Feb 2024 15:48:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707407329; cv=none; b=IKXT+sMeVdMeoGX7hLgpPT4sRXWnItzkFwEN9VfHzxrSpxgL8sRO9+SCg5ch+Q2dtnTSX/DU8yYhrK6CgOp76heSV05XDYGKoGtY+AVaTio1xtmzFNG9u+NMTnYJblLqwdMCKyqtS88igzI+mxeXDqnvWUrWtNeoTpzQzwPIe0w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707407329; c=relaxed/simple; bh=JiNY7UD5yChmAMASqdU3YDYjcAqL6joPoUB8j2lzZeY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NtaWVYqmfCS+cKFoahN8/MINzKLOtIGkMRxWV8Jvej8kizhSFA9s53k7We/gxZjrElJWwoztTiTvL2p00dq2GtbHvbx6xvcmNbjNcIP6ZC7dMy/a4fJX/VOQjdTjGkuseVAPXpemsG4ItdeEbxUAstAbHfaJKAnWsBv+DYDUphw= 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=mhcle2eZ; arc=none smtp.client-ip=209.85.219.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-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc74ac7d015so302251276.0 for ; Thu, 08 Feb 2024 07:48:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707407326; x=1708012126; 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=1MPoST0g8AYkDBhG2VsbV1sslw+0urv81QcaeUn+juw=; b=mhcle2eZCdtlsc9l4HKuZLLrbhdElCfqlSSGlxuTHyUP2BtmZ7p5AU0/XAD3sUDP5p u9bTEkLBQoOOPkSRAdoTWHRxwZpsK+7guY5NPpB3rMJSh3fSnPVAr2hT7Ddrz56yZk4G DNi+4+GaQuUZv8oul/z5l12kkLF7XXIphLgBIGVE4TafxE4izKiHQOcD0A6PDWSKKaj+ jrgpOBsFxB5rg0mSuS9nHwcpARZTnqmBB0QrchKn1U6UpW8tyYvTA9USN7ejt4Ww4PiI l2JdWLDXvXhkVWx+jF1sFcDNNpTrQTrB6SVJCgbAVyepNaLwr6F0+BMx3ukzQugPyx1U elJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707407326; x=1708012126; 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=1MPoST0g8AYkDBhG2VsbV1sslw+0urv81QcaeUn+juw=; b=vnEdLScBDriw7gvyxiIj8e6BjiMyKP0sHt3VJ96zPbRor71SZCSQ3S+bCRJ2YeQ3BG aYmynycneEw5oFQwJxeCGvS+eBQJY50PrBfJqbhWRpTMGvfZaR9LYBQ49swozdsUVg4P Qh0vQw+mKT0EeJR0aVocnmBNA5LMqyTPpH76813uWZZIILyzMOWMoGYMA88u+etfcLMf dsPDmip8NpDxCOOPoH89U2eTidxpTcHPgKEIIvzGlxIYH02Wl3SKlTpiYA2AG0uV1V8t nQ1OO8Gt7VKKyCZ94kOLfFmZ9t6rkOXOMbEl4EPxgrX7e6oiZNASAFxfcuyiy5L+diSw UMVw== X-Forwarded-Encrypted: i=1; AJvYcCXGv9O/C6J3mkE+DMBi9GnfD1MiecFkbglClZpOq/CszXgTdPD+8l7OE6DLXvc91ccIj7JKBOkZ5diL1t3rZy9XNgiLrOEI0L4q7FbE X-Gm-Message-State: AOJu0YzU4IiJofZhPTPml3hrV2FUOlmGWGWbTJ8eM1U5mb1W8px1xWZy PrNIe2XsEM1MkEo0y1hCtppuuVsAFKLzguDJ4yt5pwNJSl9vVyJhs/fwqAAFhmA1GqjF1VflF5d xT43zYmgj8oA/Zg== X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:6902:1b88:b0:dc6:ec15:5f6f with SMTP id ei8-20020a0569021b8800b00dc6ec155f6fmr340204ybb.6.1707407325732; Thu, 08 Feb 2024 07:48:45 -0800 (PST) Date: Thu, 08 Feb 2024 15:47:52 +0000 In-Reply-To: <20240208-alice-mm-v2-0-d821250204a6@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240208-alice-mm-v2-0-d821250204a6@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4643; i=aliceryhl@google.com; h=from:subject:message-id; bh=5Gt2wgkxBTJYGkfZMpeUeH1SE3k+76BKAUtR156EeQ0=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBlxPfTuQDsKBYaMhTNxSmNY7PLIkKVkVnXBgh2h FRUyBw0ZDOJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZcT30wAKCRAEWL7uWMY5 RrCTEAC0DaTqeHguZBQPjq0PPyT3rF4dgf8yp7QxrvoiztTXntK43kWYjtU6x4CoxRG+Lfp6NCx ahAklS4wQAQLPnwFB7n84XdxDQiFmRKJNiDdfrZtYj9+37q3KZnmt4B0N9TU2/tv2596QUrr7g5 8FHFh2cgpuJV3O8Gb9U2movzF0Y0EBrHeg7p8AhkHNs9taPgH7S885BL8gs2rLzgkPYKf1sNY/C 5hA4aJH/a9yzOjOWaRmFlkB1kZRm4KiftHLzWE3u3ZMHhfwXiLR9fMIvCjoKUcGfYAYHunM3gkc guKnoQhyh9Jo/b2AwXr5KVNQFbWpKIpRAFkieNgtgtxoFyYag+hQzN+8zEukWaMv/Tcw5nn2l7G fTgEJjZLgfS18WZg8tIiwBGCpb7CGApvnSE6czD3O116L7MaFZgsyQ18xEcrnwnisEh8jGkEpRS mcAYoaxEnBgCGDCF7wRrOWQGk60Sl2TD38A8wJYuhuz/XZ5kzXa0TAu7H4id2ZqjQ2Vt4E+07s6 ObN4KfrT1dPCAm9M80Bn1u4Emonb0v4S5nykU0Awlen1fu+wttxiLdNlG+zwcaHELAt39pm2Spf PfDE4plBBAgcYG75I1TyaKWvyIb0JcNlLzTdXEskioQXW+n0ByNOrNohTMoZoXiY2vxVWX0YcK/ CE7RtNd2PYweYRQ== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240208-alice-mm-v2-2-d821250204a6@google.com> Subject: [PATCH v2 2/4] uaccess: always export _copy_[from|to]_user with CONFIG_RUST 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: 1790346394212986781 X-GMAIL-MSGID: 1790346394212986781 From: Arnd Bergmann Rust code needs to be able to access _copy_from_user and _copy_to_user so that it can skip the check_copy_size check in cases where the length is known at compile-time, mirroring the logic for when C code will skip check_copy_size. To do this, we ensure that exported versions of these methods are available when CONFIG_RUST is enabled. Signed-off-by: Arnd Bergmann Signed-off-by: Alice Ryhl --- include/linux/uaccess.h | 37 +++++++++++++++++++++++-------------- lib/usercopy.c | 30 ++++-------------------------- 2 files changed, 27 insertions(+), 40 deletions(-) diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h index 3064314f4832..835aa175d0ee 100644 --- a/include/linux/uaccess.h +++ b/include/linux/uaccess.h @@ -138,13 +138,18 @@ __copy_to_user(void __user *to, const void *from, unsigned long n) return raw_copy_to_user(to, from, n); } -#ifdef INLINE_COPY_FROM_USER static inline __must_check unsigned long -_copy_from_user(void *to, const void __user *from, unsigned long n) +_inline_copy_from_user(void *to, const void __user *from, unsigned long n) { unsigned long res = n; might_fault(); if (!should_fail_usercopy() && likely(access_ok(from, n))) { + /* + * Ensure that bad access_ok() speculation will not + * lead to nasty side effects *after* the copy is + * finished: + */ + barrier_nospec(); instrument_copy_from_user_before(to, from, n); res = raw_copy_from_user(to, from, n); instrument_copy_from_user_after(to, from, n, res); @@ -153,14 +158,11 @@ _copy_from_user(void *to, const void __user *from, unsigned long n) memset(to + (n - res), 0, res); return res; } -#else extern __must_check unsigned long _copy_from_user(void *, const void __user *, unsigned long); -#endif -#ifdef INLINE_COPY_TO_USER static inline __must_check unsigned long -_copy_to_user(void __user *to, const void *from, unsigned long n) +_inline_copy_to_user(void __user *to, const void *from, unsigned long n) { might_fault(); if (should_fail_usercopy()) @@ -171,25 +173,32 @@ _copy_to_user(void __user *to, const void *from, unsigned long n) } return n; } -#else extern __must_check unsigned long _copy_to_user(void __user *, const void *, unsigned long); -#endif static __always_inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n) { - if (check_copy_size(to, n, false)) - n = _copy_from_user(to, from, n); - return n; + if (!check_copy_size(to, n, false)) + return n; +#ifdef INLINE_COPY_FROM_USER + return _inline_copy_from_user(to, from, n); +#else + return _copy_from_user(to, from, n); +#endif } static __always_inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n) { - if (check_copy_size(from, n, true)) - n = _copy_to_user(to, from, n); - return n; + if (!check_copy_size(from, n, true)) + return n; + +#ifdef INLINE_COPY_TO_USER + return _inline_copy_to_user(to, from, n); +#else + return _copy_to_user(to, from, n); +#endif } #ifndef copy_mc_to_kernel diff --git a/lib/usercopy.c b/lib/usercopy.c index d29fe29c6849..de7f30618293 100644 --- a/lib/usercopy.c +++ b/lib/usercopy.c @@ -7,40 +7,18 @@ /* out-of-line parts */ -#ifndef INLINE_COPY_FROM_USER +#if !defined(INLINE_COPY_FROM_USER) || defined(CONFIG_RUST) unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n) { - unsigned long res = n; - might_fault(); - if (!should_fail_usercopy() && likely(access_ok(from, n))) { - /* - * Ensure that bad access_ok() speculation will not - * lead to nasty side effects *after* the copy is - * finished: - */ - barrier_nospec(); - instrument_copy_from_user_before(to, from, n); - res = raw_copy_from_user(to, from, n); - instrument_copy_from_user_after(to, from, n, res); - } - if (unlikely(res)) - memset(to + (n - res), 0, res); - return res; + return _inline_copy_from_user(to, from, n); } EXPORT_SYMBOL(_copy_from_user); #endif -#ifndef INLINE_COPY_TO_USER +#if !defined(INLINE_COPY_TO_USER) || defined(CONFIG_RUST) unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n) { - might_fault(); - if (should_fail_usercopy()) - return n; - if (likely(access_ok(to, n))) { - instrument_copy_to_user(to, from, n); - n = raw_copy_to_user(to, from, n); - } - return n; + return _inline_copy_to_user(to, from, n); } EXPORT_SYMBOL(_copy_to_user); #endif