From patchwork Mon Jul 2 20:07:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris von Recklinghausen X-Patchwork-Id: 10502331 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3C5F760362 for ; Mon, 2 Jul 2018 20:07:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2D75E28DC5 for ; Mon, 2 Jul 2018 20:07:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1FEB528DC7; Mon, 2 Jul 2018 20:07:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ECCA028DC5 for ; Mon, 2 Jul 2018 20:07:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E38846B026B; Mon, 2 Jul 2018 16:07:25 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id DE8136B026C; Mon, 2 Jul 2018 16:07:25 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CD8516B026D; Mon, 2 Jul 2018 16:07:25 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt0-f197.google.com (mail-qt0-f197.google.com [209.85.216.197]) by kanga.kvack.org (Postfix) with ESMTP id 9E60F6B026B for ; Mon, 2 Jul 2018 16:07:25 -0400 (EDT) Received: by mail-qt0-f197.google.com with SMTP id t19-v6so9983573qtn.5 for ; Mon, 02 Jul 2018 13:07:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to :subject:date:message-id; bh=I1ljTj+N4rSW2RxrBind6iAAJWXeXq4RWlpIbIAv+eg=; b=ulOWxf71lDKw2wnOfzK10i9rr1VP/jP3GEXnRbC9u8CV/LKD5N+oguOOkfI2TPh6pw szLDamNzj7A0TEYPHQIYoO65NM6+sEG52VGrei4VBVJ1QM6ijQErpi8hH9bUhdqfScQb m3nX7jSnd34MYNJX2apGOoShvbgWtUfalf3UThHecenyui5t1z5l58EYOebuFwK/NqQh 3HDczkazl3Pk3H6aJ+rDHap/n9n36/9GFq4dtZtwTL0i+7erIZQLQIHxRLAFsfrsylK/ YBR9s9l8Nx8+qRSp2s34c2ZkVsrWJGg5sgjGL3j7AKwbAaQ4rt3596JobC4LOwbWYGVw Np5w== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of crecklin@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=crecklin@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: APt69E0U1mRK7b9yMnSxwnCT5Mvt7SFkH//d4DZa4CoOuS7p+Qm5yezK 87MPBsItG6nuIoS99BBZS+VYibZLFHTitWq5/mko9ajG/9A60XNTglpJPqkkA1OZLJkJK8pqEGH hVztlagLfjeALthWxSyYql7bI5a6F5rnp/JnE3HPwBMl3MADprAGznBg0NdzXSvfE/Q== X-Received: by 2002:a37:151f:: with SMTP id f31-v6mr22665435qkh.388.1530562045375; Mon, 02 Jul 2018 13:07:25 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdQk0GFq6JILarTiDZCVnrTVjZAT0Pyu50xziZlVy53U9wZqJPqXTHqIwSRZBkEVooqpYdo X-Received: by 2002:a37:151f:: with SMTP id f31-v6mr22665381qkh.388.1530562044436; Mon, 02 Jul 2018 13:07:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530562044; cv=none; d=google.com; s=arc-20160816; b=FE3tGb7rH6/qW9Jog7cBb9rVMKWLb8yumuOwO7wlxeS3KPyQ/iT1lU6OlqDekI6t+Q xENdCk2tTtRB3+DCOUZHidIdRDnvQLrFfx21sCcJYTJ21k8Mntw+sM8IxtzDHc9AAqcW z4RwD3FD2ctFrsQ/oyymw8H/6gSmJsVM+hMlGSRdNWI8QOd8Zh39CbSP11ic2T2bwUJ/ Lc2hC8CLoEBzeNMqmMSa2OqhzWFuMUx6FILIX+6IG52OcuhiepDyRMNAp0U0XIgS/3tA Ikh3p5KmAkbxqVLe+jksINTpYF/PZtNVMfbO+YgylhdQQgkVcaHg+f7mPI9ainWYZgGj ohoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:to:from:arc-authentication-results; bh=I1ljTj+N4rSW2RxrBind6iAAJWXeXq4RWlpIbIAv+eg=; b=W61CHcKsN0oimNmD1U2AkAQqTwRVnETpHTaNAGaxDc6+n1JZs+ufXvzKb+TGrQXF2q sGsVaVYf/4v2wRKrOmZNOy+oxm52q4RDe/W6zI+cZg2cHYH8/HgI4pd/u0oxgNEZ94JO BWSGlFDrJo5V1ffjiGTU730o7cEWggQEFpv2RYMQaYjpY4u4fyKB6w3V/fyR+7MZjXR1 YbVFQ/nj7E6drafe9lU9OoxH27haLmKsF/qIIpN5NwOvCbrzXEiCOwdpfBDAydgqeCmQ zatuy8OqzhSL5dfpcbryj25yHtiHNVKGQiqio8qqCNeNeG/+J0i/ixRh2wlL2c8xRe6q 37YA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of crecklin@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=crecklin@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx3-rdu2.redhat.com. [66.187.233.73]) by mx.google.com with ESMTPS id t63-v6si5112563qke.282.2018.07.02.13.07.24 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 02 Jul 2018 13:07:24 -0700 (PDT) Received-SPF: pass (google.com: domain of crecklin@redhat.com designates 66.187.233.73 as permitted sender) client-ip=66.187.233.73; Authentication-Results: mx.google.com; spf=pass (google.com: domain of crecklin@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=crecklin@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AE663402211D; Mon, 2 Jul 2018 20:07:23 +0000 (UTC) Received: from crecklin.bos.com (ovpn-120-13.rdu2.redhat.com [10.10.120.13]) by smtp.corp.redhat.com (Postfix) with ESMTP id D1F522166B5D; Mon, 2 Jul 2018 20:07:22 +0000 (UTC) From: Chris von Recklinghausen To: keescook@chromium.org, labbott@redhat.com, pabeni@redhat.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kernel-hardening@lists.openwall.com Subject: [PATCH v5] add param that allows bootline control of hardened usercopy Date: Mon, 2 Jul 2018 16:07:20 -0400 Message-Id: <1530562040-16158-1-git-send-email-crecklin@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Mon, 02 Jul 2018 20:07:23 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Mon, 02 Jul 2018 20:07:23 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'crecklin@redhat.com' RCPT:'' 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: X-Virus-Scanned: ClamAV using ClamSMTP From: Chris von Recklinghausen Enabling HARDENED_USERCOPY causes measurable regressions in networking performance, up to 8% under UDP flood. I'm running an a small packet UDP flood using pktgen vs. a host b2b connected. On the receiver side the UDP packets are processed by a simple user space process that just reads and drops them: https://github.com/netoptimizer/network-testing/blob/master/src/udp_sink.c Not very useful from a functional PoV, but it helps to pin-point bottlenecks in the networking stack. When running a kernel with CONFIG_HARDENED_USERCOPY=y, I see a 5-8% regression in the receive tput, compared to the same kernel without this option enabled. With CONFIG_HARDENED_USERCOPY=y, perf shows ~6% of CPU time spent cumulatively in __check_object_size (~4%) and __virt_addr_valid (~2%). The call-chain is: __GI___libc_recvfrom entry_SYSCALL_64_after_hwframe do_syscall_64 __x64_sys_recvfrom __sys_recvfrom inet_recvmsg udp_recvmsg __check_object_size udp_recvmsg() actually calls copy_to_iter() (inlined) and the latters calls check_copy_size() (again, inlined). A generic distro may want to enable HARDENED_USERCOPY in their default kernel config, but at the same time, such distro may want to be able to avoid the performance penalties in with the default configuration and disable the stricter check on a per-boot basis. This change adds a boot parameter that conditionally disables HARDENED_USERCOPY at boot time. This feature is not available on platforms that don't have CONFIG_JUMP_LABEL set. v4->v5: key off of CONFIG_JUMP_LABEL, not CONFIG_SMP_BROKEN. v3->v4: fix a couple of nits in commit comments declaration of bypass_usercopy_checks moved inside mm/usercopy.c and made static add blurb to commit comments about not enabling this functionality on platforms with CONFIG_BROKEN_ON_SMP set. v2->v3: add benchmark details to commit comments Don't add new item to Documentation/admin-guide/kernel-parameters.rst rename boot param to "hardened_usercopy=" update description in Documentation/admin-guide/kernel-parameters.txt static_branch_likely -> static_branch_unlikely add __ro_after_init versions of DEFINE_STATIC_KEY_FALSE, DEFINE_STATIC_KEY_TRUE disable_huc_atboot -> enable_checks (strtobool "on" == true) v1->v2: remove CONFIG_HUC_DEFAULT_OFF default is now enabled, boot param disables move check to __check_object_size so as to not break optimization of __builtin_constant_p() include linux/atomic.h before linux/jump_label.h Signed-off-by: Chris von Recklinghausen --- Documentation/admin-guide/kernel-parameters.txt | 11 ++++++++ include/linux/jump_label.h | 6 +++++ mm/usercopy.c | 35 +++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index efc7aa7..560d4dc 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -816,6 +816,17 @@ disable= [IPV6] See Documentation/networking/ipv6.txt. + hardened_usercopy= + [KNL] Under CONFIG_HARDENED_USERCOPY, whether + hardening is enabled for this boot. Hardened + usercopy checking is used to protect the kernel + from reading or writing beyond known memory + allocation boundaries as a proactive defense + against bounds-checking flaws in the kernel's + copy_to_user()/copy_from_user() interface. + on Perform hardened usercopy checks (default). + off Disable hardened usercopy checks. + disable_radix [PPC] Disable RADIX MMU mode on POWER9 diff --git a/include/linux/jump_label.h b/include/linux/jump_label.h index b46b541..1a0b6f1 100644 --- a/include/linux/jump_label.h +++ b/include/linux/jump_label.h @@ -299,12 +299,18 @@ struct static_key_false { #define DEFINE_STATIC_KEY_TRUE(name) \ struct static_key_true name = STATIC_KEY_TRUE_INIT +#define DEFINE_STATIC_KEY_TRUE_RO(name) \ + struct static_key_true name __ro_after_init = STATIC_KEY_TRUE_INIT + #define DECLARE_STATIC_KEY_TRUE(name) \ extern struct static_key_true name #define DEFINE_STATIC_KEY_FALSE(name) \ struct static_key_false name = STATIC_KEY_FALSE_INIT +#define DEFINE_STATIC_KEY_FALSE_RO(name) \ + struct static_key_false name __ro_after_init = STATIC_KEY_FALSE_INIT + #define DECLARE_STATIC_KEY_FALSE(name) \ extern struct static_key_false name diff --git a/mm/usercopy.c b/mm/usercopy.c index e9e9325..baa0193 100644 --- a/mm/usercopy.c +++ b/mm/usercopy.c @@ -20,6 +20,10 @@ #include #include #include +#ifdef CONFIG_JUMP_LABEL +#include +#include +#endif #include /* @@ -240,6 +244,10 @@ static inline void check_heap_object(const void *ptr, unsigned long n, } } +#ifdef CONFIG_JUMP_LABEL +static DEFINE_STATIC_KEY_FALSE_RO(bypass_usercopy_checks); +#endif + /* * Validates that the given object is: * - not bogus address @@ -248,6 +256,11 @@ static inline void check_heap_object(const void *ptr, unsigned long n, */ void __check_object_size(const void *ptr, unsigned long n, bool to_user) { +#ifdef CONFIG_JUMP_LABEL + if (static_branch_unlikely(&bypass_usercopy_checks)) + return; +#endif + /* Skip all tests if size is zero. */ if (!n) return; @@ -279,3 +292,25 @@ void __check_object_size(const void *ptr, unsigned long n, bool to_user) check_kernel_text_object((const unsigned long)ptr, n, to_user); } EXPORT_SYMBOL(__check_object_size); + +#ifdef CONFIG_JUMP_LABEL +EXPORT_SYMBOL(bypass_usercopy_checks); + +static bool enable_checks __initdata = true; + +static int __init parse_hardened_usercopy(char *str) +{ + return strtobool(str, &enable_checks); +} + +__setup("hardened_usercopy=", parse_hardened_usercopy); + +static int __init set_hardened_usercopy(void) +{ + if (enable_checks == false) + static_branch_enable(&bypass_usercopy_checks); + return 1; +} + +late_initcall(set_hardened_usercopy); +#endif