Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp353721rdb; Thu, 8 Feb 2024 07:49:16 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUMQ2adr7s8pRK7P+QKHj05x2+3UbC5C+0dUdcGM/5h/6BHtVPACkiglIDPup/TK1LLH65etxHry7CDa0+KS3K0gZsXHM+2KQCLOUYPeQ== X-Google-Smtp-Source: AGHT+IFapidc6cWclhP17XO9Flb/jIixxcKOEzOkyNIs7BvlmQ38ZRBy0cZkkokq6iz/ZW2sKIOo X-Received: by 2002:a05:622a:551:b0:42c:69a:b8d3 with SMTP id m17-20020a05622a055100b0042c069ab8d3mr4354605qtx.27.1707407355904; Thu, 08 Feb 2024 07:49:15 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707407355; cv=pass; d=google.com; s=arc-20160816; b=eolW7ByPj3tGyjKVjC9Z6Wtq6OKulvGK3nEL1J8QmUwQN8HSQP67+DRsOP/fdmQHyj WajJDokse4Ajv4w3d0BlgnrgAI8pc53wLKK0xbtX7/FJWMwD3pjK3Bp2sQnIzjKpUcb1 qpfy2qEg1UNPtW/OiZzQzeH0g9IGKJYlmMAfvNnKdNqv+6X/aZOy+nyDGs/RpeNpGgtu K1e9CI3scrH9PCKAR9diDEmNSlWnvmPsn17dZJDXP/UIHZ8RkRPgCSkI1vAi6YTTgxQ3 /0gx+HgA9Qh8MN4i0CczWmptvZI2l51xUTNX9DAm4VBXAVTkWRrWK2VOfApR8WuTPtKo 2hWw== 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=CwTlsoVPkNqfFulipAlKYLhGzJVf8adFPt8507NifN8=; b=tTGDJbB9EJPiVo6KKNSlABXx/7YYevcdWt4KsjV4q/2ZwbwFyfZZyQT6hcp85MrGm+ B5NrPt+Ak3aS74BzZX4Zpr2DmA18u7OXth3h1a/hrIklX6Hu9x9v4AIOhKsCSuYE0TN+ wmBWdnyZkp5Mx6qrTDMNj40tRi0NcDAwTUcsZWU7OOe6/OseqK8gRXzB8krzV2Mp3YWh uGimPskvMAyFItytpgQ+9+QdKBY6Y5OtlOSHBqSJLh/9Y0Pnj1QOOQZQfxRmP31vhZch NxrxwQFDBGU/o/85nE9tXC87Obuw+pYn2v+OAwEnrYhulY0hDV3LyPBU7VXPlvq2Ztpn +cVg==; 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-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58302-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCW0VZEfJXllvL+r/dhSO5R1Oe7SX+uY4sYpIb8yVlNgthkBILxQOqMeg/FF21RO1aWWClKz6plji9Xz2pze34QtFi0y2qqxbKtnaKMvUQ== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id g5-20020ac87f45000000b0042c4e226901si246773qtk.594.2024.02.08.07.49.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 07:49:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-58302-linux.lists.archive=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-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58302-linux.lists.archive=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 9A0351C226FB for ; Thu, 8 Feb 2024 15:49:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EAEC17E56E; Thu, 8 Feb 2024 15:48:50 +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=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 Content-Type: text/plain; charset="utf-8" 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 -- 2.43.0.594.gd9cf4e227d-goog