Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2638668imm; Tue, 4 Sep 2018 07:44:32 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYOu5qHBIhQNnl6YIs/IjX99DxcMMb4QjUqVKZXnv9qkRl9xnycbaMdGXRNM4kCBV/wNLwp X-Received: by 2002:a17:902:8215:: with SMTP id x21-v6mr33169352pln.175.1536072272357; Tue, 04 Sep 2018 07:44:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536072272; cv=none; d=google.com; s=arc-20160816; b=o8Ymf/MBuxyosk2QQfiibKc23WJAYKwCP/ux9utT0H+AgfGoYk30TnvsEw/wuJ1AyW qMF3j1CTA45w5yUDVbhm2CDG1TyXwBW+i+ifRmkafPVJZMtQhPMPN994uDw3ex8o9x9G xp1MFf2M/3Ky36pmm1EB2tLdnP1argE7eysNCstUBIV7a6Q4J0sXPvS65YO8p8N53KzM HXg0y6+3yPKEc5ZLp9S6492a/Z5tKVoSsHBlqQvXLr0Q/SM8BaVqZtQfvxiIYXyHe7PW O2wCH7827IXR3BE+2okfLf4wEJwXuB2lMNTHxH8AeOYs7NgMw9HVuxRYRSfwDKp2HTrR COZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date :arc-authentication-results; bh=z3WFhAJvS53jX71ZYbl6zday8IyXPyUn5IsrRXU2S+o=; b=eNFvcmCZTR5HEiBjSFiZARhCz17SbDstzkqRgL+rR03FKPU+eXlIcKXKp/TyNMB2Cz 53blOOak/THG0RgEo1lxuFr0E7s/nJT08Bf4vlesYu1yPWcTbecaosbBTXvWo+9Ut8ZX ubrlpJD2yT2fSvl5EPtab6nstPf0tP9Khut3jt+sQdpFXMVY1P6k+SCiobpt2EIyx/wR npACdoyRdBJvOhMjPPCLEW2lr+atmecVIPM7rJCNCRSDTcB8Kq9fo1ycZ1r02e+uEdky 099hFbCVxfjAyridhlPeJQfp7znLEjLdYJQnyYtAw/wnUD60IG13sx8lAtC5n6/EqH/u PFKA== 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a68-v6si19815065pla.361.2018.09.04.07.44.16; Tue, 04 Sep 2018 07:44: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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727665AbeIDTH5 (ORCPT + 99 others); Tue, 4 Sep 2018 15:07:57 -0400 Received: from twin.jikos.cz ([91.219.245.39]:36320 "EHLO twin.jikos.cz" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727621AbeIDTH4 (ORCPT ); Tue, 4 Sep 2018 15:07:56 -0400 Received: from twin.jikos.cz (jikos@[127.0.0.1]) by twin.jikos.cz (8.13.6/8.13.6) with ESMTP id w84Eg3fO003850 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 4 Sep 2018 16:42:04 +0200 Received: from localhost (jikos@localhost) by twin.jikos.cz (8.13.6/8.13.6/Submit) with ESMTP id w84Eg2NE003573; Tue, 4 Sep 2018 16:42:02 +0200 X-Authentication-Warning: twin.jikos.cz: jikos owned process doing -bs Date: Tue, 4 Sep 2018 16:42:02 +0200 (CEST) From: Jiri Kosina To: Thomas Gleixner , Ingo Molnar , Peter Zijlstra , Josh Poimboeuf , Andrea Arcangeli , "Woodhouse, David" , Oleg Nesterov , Tim Chen cc: linux-kernel@vger.kernel.org, x86@kernel.org Subject: [PATCH v3 2/3] x86/speculation: apply IBPB more strictly to avoid cross-process data leak In-Reply-To: Message-ID: References: User-Agent: Alpine 2.00 (LRH 1167 2008-08-23) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jiri Kosina Currently, we are issuing IBPB only in cases when switching into a non-dumpable process, the rationale being to protect such 'important and security sensitive' processess (such as GPG) from data leak into a different userspace process via spectre v2. This is however completely insufficient to provide proper userspace-to-userpace spectrev2 protection, as any process can poison branch buffers before being scheduled out, and the newly scheduled process immediately becomes spectrev2 victim. In order to minimize the performance impact (for usecases that do require spectrev2 protection), issue the barrier only in cases when switching between processess where the victim can't be ptraced by the potential attacker (as in such cases, the attacker doesn't have to bother with branch buffers at all). Fixes: 18bf3c3ea8 ("x86/speculation: Use Indirect Branch Prediction Barrier in context switch") Originally-by: Tim Chen Signed-off-by: Jiri Kosina --- Sorry for the resend, my pine is buggered and broke threading. arch/x86/mm/tlb.c | 15 ++++++--------- include/linux/ptrace.h | 3 +++ kernel/ptrace.c | 4 +++- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index e96b99eb800c..ce33e58b9327 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -262,18 +263,14 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, * one process from doing Spectre-v2 attacks on another. * * As an optimization, flush indirect branches only when - * switching into processes that disable dumping. This - * protects high value processes like gpg, without having - * too high performance overhead. IBPB is *expensive*! - * - * This will not flush branches when switching into kernel - * threads. It will also not flush if we switch to idle - * thread and back to the same process. It will flush if we - * switch to a different non-dumpable process. + * switching into a processes that can't be ptrace by the + * current one (as in such case, attacker has much more + * convenient way how to tamper with the next process than + * branch buffer poisoning). */ if (tsk && tsk->mm && tsk->mm->context.ctx_id != last_ctx_id && - get_dumpable(tsk->mm) != SUID_DUMP_USER) + ___ptrace_may_access(current, tsk, PTRACE_MODE_IBPB)) indirect_branch_prediction_barrier(); if (IS_ENABLED(CONFIG_VMAP_STACK)) { diff --git a/include/linux/ptrace.h b/include/linux/ptrace.h index 09744d4113fb..adab379b5456 100644 --- a/include/linux/ptrace.h +++ b/include/linux/ptrace.h @@ -64,12 +64,15 @@ extern void exit_ptrace(struct task_struct *tracer, struct list_head *dead); #define PTRACE_MODE_NOAUDIT 0x04 #define PTRACE_MODE_FSCREDS 0x08 #define PTRACE_MODE_REALCREDS 0x10 +#define PTRACE_MODE_NOACCESS_CHK 0x20 /* shorthands for READ/ATTACH and FSCREDS/REALCREDS combinations */ #define PTRACE_MODE_READ_FSCREDS (PTRACE_MODE_READ | PTRACE_MODE_FSCREDS) #define PTRACE_MODE_READ_REALCREDS (PTRACE_MODE_READ | PTRACE_MODE_REALCREDS) #define PTRACE_MODE_ATTACH_FSCREDS (PTRACE_MODE_ATTACH | PTRACE_MODE_FSCREDS) #define PTRACE_MODE_ATTACH_REALCREDS (PTRACE_MODE_ATTACH | PTRACE_MODE_REALCREDS) +#define PTRACE_MODE_IBPB (PTRACE_MODE_ATTACH | PTRACE_MODE_NOAUDIT \ + | PTRACE_MODE_NOACCESS_CHK | PTRACE_MODE_REALCREDS) /** * ptrace_may_access - check whether the caller is permitted to access diff --git a/kernel/ptrace.c b/kernel/ptrace.c index 07ff6685ebed..b41c37f44c32 100644 --- a/kernel/ptrace.c +++ b/kernel/ptrace.c @@ -330,7 +330,9 @@ int ___ptrace_may_access(struct task_struct *curr, struct task_struct *task, !ptrace_has_cap(mm->user_ns, mode))) return -EPERM; - return security_ptrace_access_check(task, mode); + if (!(mode & PTRACE_MODE_NOACCESS_CHK)) + return security_ptrace_access_check(task, mode); + return 0; } static int __ptrace_may_access(struct task_struct *task, unsigned int mode) -- Jiri Kosina SUSE Labs