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: 13549982 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8CAF6C48260 for ; Thu, 8 Feb 2024 15:48:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 066A86B0080; Thu, 8 Feb 2024 10:48:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 015176B0081; Thu, 8 Feb 2024 10:48:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DD4776B0083; Thu, 8 Feb 2024 10:48:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id C9AFC6B0080 for ; Thu, 8 Feb 2024 10:48:48 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 89A311A0EFC for ; Thu, 8 Feb 2024 15:48:48 +0000 (UTC) X-FDA: 81769069536.30.865EC35 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf23.hostedemail.com (Postfix) with ESMTP id A342C140002 for ; Thu, 8 Feb 2024 15:48:46 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=IkguGe7J; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 33ffEZQkKCBYw74y0DK372AA270.yA8749GJ-886Hwy6.AD2@flex--aliceryhl.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=33ffEZQkKCBYw74y0DK372AA270.yA8749GJ-886Hwy6.AD2@flex--aliceryhl.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707407326; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1MPoST0g8AYkDBhG2VsbV1sslw+0urv81QcaeUn+juw=; b=Jzmy0Guu83K7UDNLue33TE5qP/wHW/Ol5UjdqGD/lPhNEGK/ZSqMTiQ3LXUyeAIZ/Ll1Na AHX920sjUqLWVxzxXIqptWlT2IunlJNuLWNLHim5biIir2uKU+0CN2Og+mwx+1l5D6Qchx 1IiD63KQ0w9GDouiVL0ZrXCjzOivbwQ= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=IkguGe7J; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 33ffEZQkKCBYw74y0DK372AA270.yA8749GJ-886Hwy6.AD2@flex--aliceryhl.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=33ffEZQkKCBYw74y0DK372AA270.yA8749GJ-886Hwy6.AD2@flex--aliceryhl.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707407326; a=rsa-sha256; cv=none; b=uksQSr7P970gETxHw/SnI2qcYfvGFVUil7ZJZbHwF8JcLEAj3ppJhTwtzq8qVhO54xKRka Yc8Jer+53zoQoVMeBTTXzNncvOztbwsvDzrcHwBAZuofp4jZ2oTjMXA+ynbGt6iRgCW2Ru 5AgKz8eVjvzXCAw/7rFb5ckGsM1aglc= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6b269686aso3248283276.1 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=kvack.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=IkguGe7J9Ekbj55ulSzTJq1Itu5gt3LqJHTKgTBHbiFk+oHDWg7br68FkEQdpPPF/M Hp6fN4zH8FttGHfpQOMTb2QOrmoStQZg9odwPlIKjannqN+8DqKLfgLk0rMYahqHT/Oz bUc2qABHSNEWHKZEEh4NkiqGtbuNLBdT9LjYw/+qY+jluJk59vyxc8cS6hZmrts6tqNq 1bwwKTBbcr6kUIPEDFGJ+i1yRM8vpkwkQoB9w719VnSr3uqqbvbgzPKo2oi4O0wfWbch /74rVbJw7RY8s/VyQWteGq6374U67L52Rsr10cPUm/Qyud8LVMpUB+aqgpCwjBnD0vLp Wj4Q== 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=Q1mhMqtQF+SZ9R36B+u7dJoqgRjGoNysekNq9dnp7zJohNgHmVkCwvJyuNFWvMQNjm LSb+qSHunSWLs+p0a4fCVad4qFus/cCLRl7fXsZx800/0kcW1DRPYASM3i7YRpr8uDZQ clGpssLt/zLqhx37O3D2V3myzPCAlnSFUIT5yTWgfHsiKAn5DXAqDzVo3IKNj6zPJTNB sDXp+FotaRx8JDbHWMwn5khQeLgZ3vnnqE+V4BRkk5TNMbHpY1PIUEHEyeo5n1OHco3y CRcbpMvOfVG3cEiQxv7vzAE/6qK3JuGyC/9+IAulB2tfRoQLRh0T1TVeI80lvAkiuEI/ AwVg== X-Forwarded-Encrypted: i=1; AJvYcCWr6O0Uyy5mTec0vaY4GYmRs7j1LhHRRLylocpLA4awrvJ6B+sNmN2Kq2SUl9q1PuotcjfIa69B0XVhIJHWdEtl3Ns= X-Gm-Message-State: AOJu0Yza9Zqinjp2vmHxrQwtQwJUOHYI6StKHYTMWo2mjb5LvJAENigW Qw9OtP24v7YB3BbTIE1VdFPIEeqraM32EENwJRxx4eXXzJklw48bJsaRJbhFVltuNoXAMWIaBCB vyeOBE3yL9Fz6dw== X-Google-Smtp-Source: AGHT+IE8Nan1uCaJHug2RBjceERk4R9+9qTdLW2q08muXcPrNhxeAussQfhnPOBYaHjyNRTtMgh/P0qbCDqR2bA= 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> 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-Rspamd-Queue-Id: A342C140002 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: x55kycnhnndfzwyn7oyjeqj6bgufmkk6 X-HE-Tag: 1707407326-624538 X-HE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Jjs9bUnAoZbzXkSn29VMgvYjtHlCj9lE/0Ft1dWrbrU2kdMyDgKW1mH6yvb9fiVfgE2BuA7zeCahbotcdRtx4klhNYFb2YVbKoFQCAxqeEi1KdAAelen5TJJLWnCNv9JN05tlsGUO6D3GL6qdGqCM1t1JiTrH89SJC6Vd1e5ysMl3H73/iaX7SUwaP0BJDeaAZnxEQFfcGwZI+6DeXJIZFjxEgDb/hYIFGv31Hq2qtfv9kIBBCJGa8D1vcyoBG26/a26V/UnuFEAosalHLeZ3s4ikoC4+x1RmK9LUWWYVk2bwVNESZfMNb148fcC51Cy1bs++9LEVozll4IOdbLxDSRWFJ2qoW8DWHwKIO8LRRNtn2d+M+B1oLD2Tf0iFOOZhDvrfWBSctYzlgzkQDuo3xnNLHrnyPWuc9F7UxLh/IXKavxoTb/YLjiVzQbdbAA7pZmiqUXk/UUrpSmVRkYZoC0PImwwyxbmDQkriedvX/ZwXPJCJCVppAS7lGzfU5pMX9dpnV5aNzgWiNbWA+Ze6Lb9tuTb01U9lN8aQ6VTvfLkEaYaqbJr0WrGqBHZD4sVQKZLeBetWLbdjVCk4BcyAUTJgHaMAlQ1uqicD6JUgDwQSbdrWPAgSGxc5nMEfacgo04CLooVuN15L2jLDYhWSJy/i5lOKAOEamNCQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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