lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Thu, 27 Sep 2018 16:18:00 -0400
From:   Stephen Smalley <sds@...ho.nsa.gov>
To:     Jiri Kosina <jikos@...nel.org>,
        Thomas Gleixner <tglx@...utronix.de>,
        Ingo Molnar <mingo@...hat.com>,
        Peter Zijlstra <peterz@...radead.org>,
        Josh Poimboeuf <jpoimboe@...hat.com>,
        Andrea Arcangeli <aarcange@...hat.com>,
        "Woodhouse, David" <dwmw@...zon.co.uk>,
        Andi Kleen <ak@...ux.intel.com>,
        Tim Chen <tim.c.chen@...ux.intel.com>,
        "Schaufler, Casey" <casey.schaufler@...el.com>
Cc:     linux-kernel@...r.kernel.org, x86@...nel.org
Subject: Re: [PATCH v7 1/3] x86/speculation: apply IBPB more strictly to avoid
 cross-process data leak

On 09/25/2018 08:38 AM, Jiri Kosina wrote:
> From: Jiri Kosina <jkosina@...e.cz>
> 
> 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).
> 
> [ tglx@...utronix.de: split up PTRACE_MODE_NOACCESS_CHK into PTRACE_MODE_SCHED and
>    PTRACE_MODE_IBPB to be able to do ptrace() context tracking reasonably
>    fine-grained ]
> 
> Fixes: 18bf3c3ea8 ("x86/speculation: Use Indirect Branch Prediction Barrier in context switch")
> Originally-by: Tim Chen <tim.c.chen@...ux.intel.com>
> Signed-off-by: Jiri Kosina <jkosina@...e.cz>
> ---
>   arch/x86/mm/tlb.c      | 31 ++++++++++++++++++++-----------
>   include/linux/ptrace.h | 21 +++++++++++++++++++--
>   kernel/ptrace.c        | 10 ++++++++++
>   3 files changed, 49 insertions(+), 13 deletions(-)
> 
> diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c
> index e96b99eb800c..073b8df349a0 100644
> --- a/arch/x86/mm/tlb.c
> +++ b/arch/x86/mm/tlb.c
> @@ -7,6 +7,7 @@
>   #include <linux/export.h>
>   #include <linux/cpu.h>
>   #include <linux/debugfs.h>
> +#include <linux/ptrace.h>
>   
>   #include <asm/tlbflush.h>
>   #include <asm/mmu_context.h>
> @@ -180,6 +181,19 @@ static void sync_current_stack_to_mm(struct mm_struct *mm)
>   	}
>   }
>   
> +static bool ibpb_needed(struct task_struct *tsk, u64 last_ctx_id)
> +{
> +	/*
> +	 * Check if the current (previous) task has access to the memory
> +	 * of the @tsk (next) task. If access is denied, make sure to
> +	 * issue a IBPB to stop user->user Spectre-v2 attacks.
> +	 *
> +	 * Note: __ptrace_may_access() returns 0 or -ERRNO.
> +	 */
> +	return (tsk && tsk->mm && tsk->mm->context.ctx_id != last_ctx_id &&
> +		ptrace_may_access_sched(tsk, PTRACE_MODE_SPEC_IBPB));

Would there be any safe way to perform the ptrace check earlier at a 
point where the locking constraints are less severe, and just pass down 
the result to this code?  Possibly just defaulting to enabling IBPB for 
safety if something changed in the interim that would invalidate the 
earlier ptrace check?  Probably not possible, but I thought I'd ask as 
it would avoid the need to skip both the ptrace_has_cap check and the 
LSM hook, and would reduce the critical section.

> +}
> +
>   void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
>   			struct task_struct *tsk)
>   {
> @@ -262,18 +276,13 @@ 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)
> +		if (static_cpu_has(X86_FEATURE_USE_IBPB) &&
> +				ibpb_needed(tsk, last_ctx_id))
>   			indirect_branch_prediction_barrier();
>   
>   		if (IS_ENABLED(CONFIG_VMAP_STACK)) {
> diff --git a/include/linux/ptrace.h b/include/linux/ptrace.h
> index 4f36431c380b..e5e5ef513df3 100644
> --- a/include/linux/ptrace.h
> +++ b/include/linux/ptrace.h
> @@ -62,14 +62,17 @@ extern void exit_ptrace(struct task_struct *tracer, struct list_head *dead);
>   #define PTRACE_MODE_READ	0x01
>   #define PTRACE_MODE_ATTACH	0x02
>   #define PTRACE_MODE_NOAUDIT	0x04
> -#define PTRACE_MODE_FSCREDS 0x08
> -#define PTRACE_MODE_REALCREDS 0x10
> +#define PTRACE_MODE_FSCREDS	0x08
> +#define PTRACE_MODE_REALCREDS	0x10
> +#define PTRACE_MODE_SCHED	0x20
> +#define PTRACE_MODE_IBPB	0x40
>   
>   /* 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_SPEC_IBPB (PTRACE_MODE_ATTACH_REALCREDS | PTRACE_MODE_IBPB)
>   
>   /**
>    * ptrace_may_access - check whether the caller is permitted to access
> @@ -87,6 +90,20 @@ extern void exit_ptrace(struct task_struct *tracer, struct list_head *dead);
>    */
>   extern bool ptrace_may_access(struct task_struct *task, unsigned int mode);
>   
> +/**
> + * ptrace_may_access - check whether the caller is permitted to access

s/ptrace_may_access/ptrace_may_access_sched/

> + * a target task.
> + * @task: target task
> + * @mode: selects type of access and caller credentials
> + *
> + * Returns true on success, false on denial.
> + *
> + * Similar to ptrace_may_access(). Only to be called from context switch
> + * code. Does not call into audit and the regular LSM hooks due to locking
> + * constraints.

Pardon my ignorance, but can you clarify exactly what are the locking 
constraints for any code that might be called now or in the future from 
ptrace_may_access_sched().  What's permissible?  rcu_read_lock()?

> + */
> +extern bool ptrace_may_access_sched(struct task_struct *task, unsigned int mode);
> +
>   static inline int ptrace_reparented(struct task_struct *child)
>   {
>   	return !same_thread_group(child->real_parent, child->parent);
> diff --git a/kernel/ptrace.c b/kernel/ptrace.c
> index 21fec73d45d4..99cfddde6a55 100644
> --- a/kernel/ptrace.c
> +++ b/kernel/ptrace.c
> @@ -261,6 +261,9 @@ static int ptrace_check_attach(struct task_struct *child, bool ignore_state)
>   
>   static int ptrace_has_cap(struct user_namespace *ns, unsigned int mode)
>   {
> +	if (mode & PTRACE_MODE_SCHED)
> +		return false;
> +
>   	if (mode & PTRACE_MODE_NOAUDIT)
>   		return has_ns_capability_noaudit(current, ns, CAP_SYS_PTRACE);
>   	else
> @@ -328,9 +331,16 @@ static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
>   	     !ptrace_has_cap(mm->user_ns, mode)))
>   	    return -EPERM;
>   
> +	if (mode & PTRACE_MODE_SCHED)
> +		return 0;
>   	return security_ptrace_access_check(task, mode);
>   }
>   
> +bool ptrace_may_access_sched(struct task_struct *task, unsigned int mode)
> +{
> +	return __ptrace_may_access(task, mode | PTRACE_MODE_SCHED);
> +}
> +
>   bool ptrace_may_access(struct task_struct *task, unsigned int mode)
>   {
>   	int err;
> 

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ