Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp255220imm; Mon, 2 Jul 2018 10:57:32 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKMOEnoPZlKczD2kC0eoPfsStOaa9t7dY1r//nps1Nc3LWIA1DZkog+0Hy5WB5HB5/23gfL X-Received: by 2002:a63:85c8:: with SMTP id u191-v6mr22572231pgd.36.1530554252851; Mon, 02 Jul 2018 10:57:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530554252; cv=none; d=google.com; s=arc-20160816; b=fu3IfzjAI5n4rwtt2CtqWuU81bHHMGi2xC/42qKJn/QCSAS56/5xVBh3Vi3hpIIwz9 j4yp7alVKjnRtOprOfLK7Eh2RMbCOu1cmSu5XFJGXHjf37lw1SH47UQz1wBLcEv/0j0d sWpj/OLu0HwDbh/fqFraSPjJztKrf0ZfnGLjeAeHwuaxpJWrJ4MgbMg00P1fHT7PC65f 7Xx95trStbHPMSpr1Lfb99AVSr4zaFcoEyFs7RCu4fGeGXUga8lwics5KKYhRCKAIEcE 3vE5n8BR97m6DTve94xR8i3Pd52VFAi5Mn46s+7n8Av4lTtlJXkLfyiW5ntPVjWBhZGe t8Yg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:to:from :arc-authentication-results; bh=CGxLIVXf5MqpH2rxNfjm2+5hC0kjmIncwCVf5epLCgM=; b=bB16gGLR9Zt3qp/eO9u+TUGU3E6+0JBl0g8iWsg2tyR83nF0nKCgAqbSCScj6NGHBE BUVUmZk2lnDhtFXEgCxK8Wyz8bIb80QzNkOUVsr6QuzmvyuaYDIZPX+5jhO4VEPW3QVT 06Cv7n7p9E70okVYWYUrpIrPa0wRLMzueF79Akp3QeEW+97A1isyp7RzDosARcE+NC1C Xk7MriFBnO40BhMbv6dsJuDhUZhg2rGKbdaTgkM+5zTWipmvSuJT1qc8EVWSX+97wOe6 N6K2GYuz/tTUiEcLfi+99etoeQWDO2GYsv5RFSC6PkclFW/50Is7TDu0z/hj/26kQJrI jB6w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s14-v6si4666652pga.21.2018.07.02.10.57.18; Mon, 02 Jul 2018 10:57:32 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753215AbeGBRzt (ORCPT + 99 others); Mon, 2 Jul 2018 13:55:49 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:49528 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752728AbeGBRzs (ORCPT ); Mon, 2 Jul 2018 13:55:48 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A6D924023351; Mon, 2 Jul 2018 17:55:47 +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 C7FE2111AF34; Mon, 2 Jul 2018 17:55:46 +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 v4] add param that allows bootline control of hardened usercopy Date: Mon, 2 Jul 2018 13:55:43 -0400 Message-Id: <1530554143-17218-1-git-send-email-crecklin@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Mon, 02 Jul 2018 17:55:47 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Mon, 02 Jul 2018 17:55:47 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'crecklin@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: root 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. Atomics needed by jump label code are not available on platforms with CONFIG_BROKEN_ON_SMP set, so don't enable this functionality on those platforms. 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..a5f8be5 100644 --- a/mm/usercopy.c +++ b/mm/usercopy.c @@ -20,6 +20,10 @@ #include #include #include +#ifndef CONFIG_BROKEN_ON_SMP +#include +#include +#endif #include /* @@ -240,6 +244,10 @@ static inline void check_heap_object(const void *ptr, unsigned long n, } } +#ifndef CONFIG_BROKEN_ON_SMP +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) { +#ifndef CONFIG_BROKEN_ON_SMP + 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); + +#ifndef CONFIG_BROKEN_ON_SMP +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 -- 1.8.3.1