Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp2653738imm; Thu, 11 Oct 2018 13:56:46 -0700 (PDT) X-Google-Smtp-Source: ACcGV61a5snsE4YhD+rhxBIyJ/UTw+C6jtrUFwIvAf9azaFjGZYyEPoivyNZEE3DHJKBgA3VngAY X-Received: by 2002:a65:6295:: with SMTP id f21-v6mr2847036pgv.167.1539291406556; Thu, 11 Oct 2018 13:56:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539291406; cv=none; d=google.com; s=arc-20160816; b=CaIDmlrkqV6IVxgGK5DATetYFmfORtHnP6fHSQpCG6HnS/sYvWAfeOmA1spAKp2GK9 0rTXbNp7LhZJKy0CD001qgKkTj8HzuyRru1wuftcGTPSRbDaBJer/bDK0X4GQN3S2CpK ary4vd77DYkSs7taQbNuU7mkVMnTNr30S1lWkU42mSnJLnTiv5B40G6xFS/K26339Ol5 sg+lMMjoG3UgMODU/4m0xAi0q4MrAmlqs6fDehBirhY7Ab+y9Oh06qAgw3LaM+TuiRYV R4E5tzux/HT3MCHi3Ze1wH+ZTx5+kmZagQ3gCxa4eIjNV4M4/dttfz0wkQNsem3UNEuy WzCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature; bh=xhdlW9MnzJZrJ1pC8eL3uOGXJT7PaJJkKhL6cjwk868=; b=CgrrIn4vj7sETYZDR+MBYvAeNaVa+xGLMb20q2UWD6Wzyjz2bJu+Zr0JS/CMYdUX5t 4b9Pdsd4eg//JaWrYkBNDdbYnzFJhizmrKG5gLEL/VpSUUJXPHogpriBNdbYiJqgTdbO IL8MkBMgWyMiy3p9lXy98ZjiGJButZf6Fro/Ii4t4iQpaRqMJljAzGuosymg5qzf6g6D uyLkejJrRy6+A6ybQkiVQH/SYJmiOtxhpoybLSpO1ndb1g3kMM0iByay5ZXNF7KmgRb6 XgMjFvQ+qxPm9W/c5pP9k7cuZFaOZ84B4uUAnD5BCFGZ6RYvp3ZAb//BQMMgzQDEyVvL 7giA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=IRnY2REQ; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j16-v6si27208018pgm.501.2018.10.11.13.56.31; Thu, 11 Oct 2018 13:56:46 -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; dkim=pass header.i=@chromium.org header.s=google header.b=IRnY2REQ; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726890AbeJLEY4 (ORCPT + 99 others); Fri, 12 Oct 2018 00:24:56 -0400 Received: from mail-yb1-f195.google.com ([209.85.219.195]:40593 "EHLO mail-yb1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726227AbeJLEY4 (ORCPT ); Fri, 12 Oct 2018 00:24:56 -0400 Received: by mail-yb1-f195.google.com with SMTP id w7-v6so4159106ybm.7 for ; Thu, 11 Oct 2018 13:55:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=xhdlW9MnzJZrJ1pC8eL3uOGXJT7PaJJkKhL6cjwk868=; b=IRnY2REQEVvHG0GF+NWwi2s7Qi+NGGoNLq6dLtl23zuk5KMUBHbbtBzVSqdPkGI50y px3fz7ZtTPHWn9E8QmJO+zDQ95/u+8IvenOJTwBApdlxrTwERRCAMtabYD9eWMKDjhrx xc93H72UF3KaU/Aec9NWOHAvZdMZu65t67xGk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=xhdlW9MnzJZrJ1pC8eL3uOGXJT7PaJJkKhL6cjwk868=; b=jzkoxiklZmfhmLsaGqcDK13PCgYvpmZlGcmJpD2cg37vshG5lfzbhYvDPxA5068PZ6 oz6aks6b5xR3YnWmp8jL4sAsiIT5ws2hKnIyjLymCumNkEkdCOATtnsQ47Yw7ck8Z1Ev vXzl+p8AR6h25nqi3lcRGiMOIRXuTTcO7ldjvf+l0Dl8pwmuBmtjegIc1yYllXXtJfuy AHW3+gVGQe+KOQ9H106SSlSNOuWnucd8ucIPtLZMec7VHgPxDZcHaGcGBVpKRGy7DtLP OXIKemte4PW5kZLwI9GvdEYHwnMWH/djk/0N8/G3meUwKHaPHQdaVhuL/9gxPcQvGm/E 2TRA== X-Gm-Message-State: ABuFfojqD82aqZ4iyurBNwytl4RGuzne99jz35uvV9kXSaZFgSN3nnCL xbxa4M4/ElE+HuQ8RdWdSSTkx9ce/fY= X-Received: by 2002:a25:b306:: with SMTP id l6-v6mr1805157ybj.8.1539291355858; Thu, 11 Oct 2018 13:55:55 -0700 (PDT) Received: from mail-yb1-f182.google.com (mail-yb1-f182.google.com. [209.85.219.182]) by smtp.gmail.com with ESMTPSA id q6-v6sm5249860ywh.41.2018.10.11.13.55.54 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Oct 2018 13:55:54 -0700 (PDT) Received: by mail-yb1-f182.google.com with SMTP id 5-v6so4168391ybf.3 for ; Thu, 11 Oct 2018 13:55:54 -0700 (PDT) X-Received: by 2002:a25:2395:: with SMTP id j143-v6mr1823975ybj.137.1539291353539; Thu, 11 Oct 2018 13:55:53 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a25:d116:0:0:0:0:0 with HTTP; Thu, 11 Oct 2018 13:55:51 -0700 (PDT) In-Reply-To: References: <20181011185458.10186-1-kristen@linux.intel.com> From: Kees Cook Date: Thu, 11 Oct 2018 13:55:51 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] x86: entry: flush the cache if syscall error To: Andy Lutomirski Cc: Kristen Carlson Accardi , Kernel Hardening , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , X86 ML , LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 11, 2018 at 1:48 PM, Andy Lutomirski wrote: > On Thu, Oct 11, 2018 at 11:55 AM Kristen Carlson Accardi > wrote: >> >> 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) > > What about ax > 0? (Or more generally, any ax outside the range of -1 > .. -4095 or whatever the error range is.) As it stands, it looks like > you'll flush on successful read(), write(), recv(), etc, and that > could seriously hurt performance on real workloads. Seems like just changing this with "ax == 0" into "ax >= 0" would solve that? I think this looks like a good idea. It might be worth adding a comment about the checks to explain why those errors are whitelisted. It's a cheap and effective mitigation for "unknown future problems" that doesn't degrade normal workloads. >> + return; >> + >> + wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH); What about CPUs without FLUSH_L1D? Could it be done manually with a memcpy or something? -Kees >> + } >> +} >> + >> #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 >> -- Kees Cook Pixel Security