Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp2583397imm; Thu, 11 Oct 2018 12:38:19 -0700 (PDT) X-Google-Smtp-Source: ACcGV60nvaV+JrP4SF9PzXyobr4jVWjsgm4e2ZI7MI9hXlHF5Hl2WdA85Rm/GEWbzbAMo4CwmODQ X-Received: by 2002:a62:968a:: with SMTP id s10-v6mr2830159pfk.191.1539286699445; Thu, 11 Oct 2018 12:38:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539286699; cv=none; d=google.com; s=arc-20160816; b=X3F2eCrXxDcj1jPGEz0caBXEk/qm/j62rOQB9q/qxSMl+cJuJZlOyi+/tSUhmmhOrd WjCVISibZE+MBxgqPs1BZQn7cDN8PooW7p7jE4A/Ur0BjvHtHep/gDCZATeKmYAPtDKE hwQd3VUZ3MCUe4zrteSRS/nzurTXm0CbfljvVt33x6qF/jtIO7Jf7PabO8IStRxauuQs MeF2J3FKVI1L112PbKJOOPY/kL2mStWNH8BmSAaIxdf0ZAto4CtTQ3YJfkf/HomkJ7k+ FMuXoqIztMIMG/otNr7sXm3SNsWxl7Zknmsjqg6ZN980uVsTs9pr9jZnqGGY2vHePxgS ZShw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:message-id:date:subject:cc:from; bh=2GeAR7t4tqwXSPcaMH/BgmcKlA69TDNYocGlxlYOdvU=; b=OLmSQgXxf8fWr9b3MDMX6CZZnf2UJgJ9U9MdNxK2EWpG25i09FKGRLmjob8kgK2AId HtVFnuQK19NuXQ+1q138mrgJypFTsbQb3E1SNafMw2zYXamNqC9pXlaKgx+btZiZzVD8 grPuBj1cThlC2+F0ZD2lYG1fG8g53WN3oL/WgJQLx1KBeob3sNvSt3Xze1f3cARKVtki 9utbEl16j+2upT5mZT0NmvO6prC/2qda1XqBSB0ybWCTNmSI4pjn+QrsZjQBCGKBBDcx LoD8azNN9NOFYjnLCmLef40kIEaGqyj0QOOo41TIfE/695R+46akwFW4MlWiMJTQOihu vPUA== 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=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 34-v6si7880534pgm.544.2018.10.11.12.38.04; Thu, 11 Oct 2018 12:38:19 -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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729774AbeJLCX2 (ORCPT + 99 others); Thu, 11 Oct 2018 22:23:28 -0400 Received: from mga09.intel.com ([134.134.136.24]:52384 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728239AbeJLCX2 (ORCPT ); Thu, 11 Oct 2018 22:23:28 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 11 Oct 2018 11:54:59 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,369,1534834800"; d="scan'208";a="91214723" Received: from kcaccard-mobl3.jf.intel.com ([10.24.8.209]) by orsmga003.jf.intel.com with ESMTP; 11 Oct 2018 11:54:58 -0700 From: Kristen Carlson Accardi Cc: kernel-hardening@lists.openwall.com, Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , x86@kernel.org, Andy Lutomirski , linux-kernel@vger.kernel.org Subject: [PATCH] x86: entry: flush the cache if syscall error Date: Thu, 11 Oct 2018 11:54:58 -0700 Message-Id: <20181011185458.10186-1-kristen@linux.intel.com> X-Mailer: git-send-email 2.14.4 To: unlisted-recipients:; (no To-header on input) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch aims to make it harder to perform cache timing attacks on data left behind by system calls. If we have an error returned from a syscall, flush the L1 cache. It's important to note that this patch is not addressing any specific exploit, nor is it intended to be a complete defense against anything. It is intended to be a low cost way of eliminating some of side effects of a failed system call. A performance test using sysbench on one hyperthread and a script which attempts to repeatedly access files it does not have permission to access on the other hyperthread found no significant performance impact. Suggested-by: Alan Cox Signed-off-by: Kristen Carlson Accardi --- arch/x86/Kconfig | 9 +++++++++ arch/x86/entry/common.c | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 1a0be022f91d..bde978eb3b4e 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -445,6 +445,15 @@ config RETPOLINE code are eliminated. Since this includes the syscall entry path, it is not entirely pointless. +config SYSCALL_FLUSH + bool "Clear L1 Cache on syscall errors" + default n + help + Selecting 'y' allows the L1 cache to be cleared upon return of + an error code from a syscall if the CPU supports "flush_l1d". + This may reduce the likelyhood of speculative execution style + attacks on syscalls. + config INTEL_RDT bool "Intel Resource Director Technology support" default n diff --git a/arch/x86/entry/common.c b/arch/x86/entry/common.c index 3b2490b81918..26de8ea71293 100644 --- a/arch/x86/entry/common.c +++ b/arch/x86/entry/common.c @@ -268,6 +268,20 @@ __visible inline void syscall_return_slowpath(struct pt_regs *regs) prepare_exit_to_usermode(regs); } +__visible inline void l1_cache_flush(struct pt_regs *regs) +{ + if (IS_ENABLED(CONFIG_SYSCALL_FLUSH) && + static_cpu_has(X86_FEATURE_FLUSH_L1D)) { + if (regs->ax == 0 || regs->ax == -EAGAIN || + regs->ax == -EEXIST || regs->ax == -ENOENT || + regs->ax == -EXDEV || regs->ax == -ETIMEDOUT || + regs->ax == -ENOTCONN || regs->ax == -EINPROGRESS) + return; + + wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH); + } +} + #ifdef CONFIG_X86_64 __visible void do_syscall_64(unsigned long nr, struct pt_regs *regs) { @@ -290,6 +304,8 @@ __visible void do_syscall_64(unsigned long nr, struct pt_regs *regs) regs->ax = sys_call_table[nr](regs); } + l1_cache_flush(regs); + syscall_return_slowpath(regs); } #endif @@ -338,6 +354,8 @@ static __always_inline void do_syscall_32_irqs_on(struct pt_regs *regs) #endif /* CONFIG_IA32_EMULATION */ } + l1_cache_flush(regs); + syscall_return_slowpath(regs); } -- 2.14.4