Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp25927imm; Thu, 11 Oct 2018 14:45:16 -0700 (PDT) X-Google-Smtp-Source: ACcGV62RKcDRdx9fhbaTH33IeTq7OiRyJvF1Xgl9wM5AE7k2ONQZTTsYtmCY4S5AgjSJL71qEd8H X-Received: by 2002:a63:ce56:: with SMTP id r22-v6mr2905679pgi.217.1539294316911; Thu, 11 Oct 2018 14:45:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539294316; cv=none; d=google.com; s=arc-20160816; b=LwHUkSMB8YSSk2sK3UUHvlu0eOYVERg3xBCAlT16dhJ5nBOTvuCBRC+uTUeQOVeDTQ joQgIfzteT4IfeWtuAwB7Y77BbGLkRWzk6TS3iIKpurQWFKBQi9m5rKlmKhun3Zgf7GV x0bOkaIjYXtPx/mVbHtxLKc0MEyaFvDh3oGWaqgvBQqQVDIHOgYpXcuqEs1NE+2GC0mu TIpgubInytxHGgawu8HCEsP6HnU78X7IVw2lg4Vbdq6Y0Uy/ar06qf3nMiCBBa2pPnpW h9f7es4St9XVmLs1T45A/BwNytDtVIvKmyt8BtFkpZXcmI2EOqduHVhO7d0bPB3bs3d0 PNXQ== 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 :in-reply-to:references:mime-version:dkim-signature; bh=PsLJxfor8wh4MP1uHMCLdtfsRIyiC2Ui82itUm8oErM=; b=GmOowXi3gtX6vJau5is1DYRxHsQY5CsbzQs5v/VuU1HkmNWwWae2IM5sGYueJMaahY HQUxVgkHOGpIh5Gu/FMOBGcH3keqkItiYhJyK7J8a0S4StAb5h1nX6+dCcXG1jmcH4o7 drIz3P8c8kl6EC6OQlvsfkOENHIxb1OBiphrMCHeRe7u2+CcS78J2L8r7e3JIoLuiT9N Ca/+f52S82wcpdO0mnlrPZqhcfwOTXS8SEy3tNHUFycsmYkfvlIFQM/VqqNY7l3tiOSA 3ScMGLTrffFdDhGPwqoZauILtoJgBTxCXuDNLx3auk8lNlYSd0lG3vu3THJqGRCgrk/p 2kvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=k6uYP8Iq; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f63-v6si9351124pfb.0.2018.10.11.14.45.00; Thu, 11 Oct 2018 14:45:16 -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=@google.com header.s=20161025 header.b=k6uYP8Iq; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726647AbeJLFMX (ORCPT + 99 others); Fri, 12 Oct 2018 01:12:23 -0400 Received: from mail-oi1-f194.google.com ([209.85.167.194]:42376 "EHLO mail-oi1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725956AbeJLFMX (ORCPT ); Fri, 12 Oct 2018 01:12:23 -0400 Received: by mail-oi1-f194.google.com with SMTP id w81-v6so8314998oiw.9 for ; Thu, 11 Oct 2018 14:43:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=PsLJxfor8wh4MP1uHMCLdtfsRIyiC2Ui82itUm8oErM=; b=k6uYP8IqARiSLR8v24cY7FEFXlHVydL7JOqgbY4sHM0Z5WMwXeKvEqR61P7gbYfMX/ eAFwGTa2OlrlFSmeOHTo+GsCG8W02KsGTTXxlPdAzY1G4CNDBQQNSWe7Rgge5YtOV+RG X/KXe2QlOsRxxzshrpcipciuHKVbB5Qlf7ugX17Dd7VbJ5dFU5SW9e5PY/y+PnOH+lq3 726jQiUFsJlvoIeKhaBcYrIax/WCwvnYScESzyAZhL8N3VleHbm1IlMGhV1RXTChAsSp x6D0ovDgMjZylpQuNLJqzhnzgypX3ZOSIvZ2GrpjUkz6huNoVukYMmQeBfOXKOH30d4X ouJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=PsLJxfor8wh4MP1uHMCLdtfsRIyiC2Ui82itUm8oErM=; b=RmPxpLHmq0ULkXnzkQRJQ9FMsr52+EYgmHMdQ+gXj/0aT2EXxg0hi1eHQgXKlsyeXI hvjTcTKG2j1Dcp5PZQPJXMlanOcWDl2eGTvx7KTpbcx2U7nRa3LWPeJ35XyZyWpPR4p+ x7qzNys+1KvBo2P2Z2Y6yR0gEKlnWNk2BFed16cV1bmIaqNF43c5FGsU7H71ZJlP/nuo 0NryQyLoOeVMZlrAH4IyyzrYxsLGrr/XqzwkBJj1mN+Xpkgrqxyy8IWmmvKe4d+Pp8E4 hgi/zDXW4CCBJ85hgUAQsbZtxDeQnv9HELASQ3s1v76kPfG2xEv44TiyXPAwfqBD+pby dIHw== X-Gm-Message-State: ABuFfohVpMpax6Yqn16IJ+eLcFSAqEReRVeRrrhAtDNg0l39xr/ip8mX 55rh76AKKAeymm7hUphtHUW6EpIGkwmnR05DQtPqdA== X-Received: by 2002:aca:c045:: with SMTP id q66-v6mr1950543oif.22.1539294192530; Thu, 11 Oct 2018 14:43:12 -0700 (PDT) MIME-Version: 1.0 References: <20181011185458.10186-1-kristen@linux.intel.com> In-Reply-To: From: Jann Horn Date: Thu, 11 Oct 2018 23:42:46 +0200 Message-ID: Subject: Re: [PATCH] x86: entry: flush the cache if syscall error To: Kees Cook Cc: Andy Lutomirski , kristen@linux.intel.com, Kernel Hardening , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H . Peter Anvin" , "the arch/x86 maintainers" , kernel list 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 10:56 PM Kees Cook wrote: > 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? As spender points out on twitter (https://twitter.com/grsecurity/status/1050497259937370118 - thanks, spender!), struct pt_regs stores register values as "unsigned long", and so you'll need to use something like IS_ERR_VALUE().