linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers3@gmail.com>
To: x86@kernel.org
Cc: linux-kernel@vger.kernel.org,
	kernel-hardening@lists.openwall.com,
	Andy Lutomirski <luto@kernel.org>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	Dmitry Vyukov <dvyukov@google.com>,
	Fenghua Yu <fenghua.yu@intel.com>, Ingo Molnar <mingo@kernel.org>,
	Kevin Hao <haokexin@gmail.com>, Oleg Nesterov <oleg@redhat.com>,
	Wanpeng Li <wanpeng.li@hotmail.com>,
	Yu-cheng Yu <yu-cheng.yu@intel.com>,
	Michael Halcrow <mhalcrow@google.com>,
	Eric Biggers <ebiggers@google.com>,
	stable@vger.kernel.org
Subject: [PATCH v4 1/3] x86/fpu: don't let userspace set bogus xcomp_bv
Date: Fri, 22 Sep 2017 10:41:54 -0700	[thread overview]
Message-ID: <20170922174156.16780-2-ebiggers3@gmail.com> (raw)
In-Reply-To: <20170922174156.16780-1-ebiggers3@gmail.com>

From: Eric Biggers <ebiggers@google.com>

On x86, userspace can use the ptrace() or rt_sigreturn() system calls to
set a task's extended state (xstate) or "FPU" registers.  ptrace() can
set them for another task using the PTRACE_SETREGSET request with
NT_X86_XSTATE, while rt_sigreturn() can set them for the current task.
In either case, registers can be set to any value, but the kernel
assumes that the XSAVE area itself remains valid in the sense that the
CPU can restore it.

However, in the case where the kernel is using the uncompacted xstate
format (which it does whenever the XSAVES instruction is unavailable),
it was possible for userspace to set the xcomp_bv field in the
xstate_header to an arbitrary value.  However, all bits in that field
are reserved in the uncompacted case, so when switching to a task with
nonzero xcomp_bv, the XRSTOR instruction failed with a #GP fault.  This
caused the WARN_ON_FPU(err) in copy_kernel_to_xregs() to be hit.  In
addition, since the error is otherwise ignored, the FPU registers from
the task previously executing on the CPU were leaked.

Fix the bug by checking that the user-supplied value of xcomp_bv is 0 in
the uncompacted case, and returning an error otherwise.

The reason for validating xcomp_bv rather than simply overwriting it
with 0 is that we want userspace to see an error if it (incorrectly)
provides an XSAVE area in compacted format rather than in uncompacted
format.

Note that as before, in case of error we clear the task's FPU state.
This is perhaps non-ideal, especially for PTRACE_SETREGSET; it might be
better to return an error before changing anything.  But it seems the
"clear on error" behavior is fine for now, and it's a little tricky to
do otherwise because it would mean we couldn't simply copy the full
userspace state into kernel memory in one __copy_from_user().

This bug was found by syzkaller, which hit the above-mentioned
WARN_ON_FPU():

    WARNING: CPU: 1 PID: 0 at ./arch/x86/include/asm/fpu/internal.h:373 __switch_to+0x5b5/0x5d0
    CPU: 1 PID: 0 Comm: swapper/1 Not tainted 4.13.0 #453
    Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
    task: ffff9ba2bc8e42c0 task.stack: ffffa78cc036c000
    RIP: 0010:__switch_to+0x5b5/0x5d0
    RSP: 0000:ffffa78cc08bbb88 EFLAGS: 00010082
    RAX: 00000000fffffffe RBX: ffff9ba2b8bf2180 RCX: 00000000c0000100
    RDX: 00000000ffffffff RSI: 000000005cb10700 RDI: ffff9ba2b8bf36c0
    RBP: ffffa78cc08bbbd0 R08: 00000000929fdf46 R09: 0000000000000001
    R10: 0000000000000000 R11: 0000000000000000 R12: ffff9ba2bc8e42c0
    R13: 0000000000000000 R14: ffff9ba2b8bf3680 R15: ffff9ba2bf5d7b40
    FS:  00007f7e5cb10700(0000) GS:ffff9ba2bf400000(0000) knlGS:0000000000000000
    CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
    CR2: 00000000004005cc CR3: 0000000079fd5000 CR4: 00000000001406e0
    Call Trace:
    Code: 84 00 00 00 00 00 e9 11 fd ff ff 0f ff 66 0f 1f 84 00 00 00 00 00 e9 e7 fa ff ff 0f ff 66 0f 1f 84 00 00 00 00 00 e9 c2 fa ff ff <0f> ff 66 0f 1f 84 00 00 00 00 00 e9 d4 fc ff ff 66 66 2e 0f 1f

Here is a C reproducer.  The expected behavior is that the program spin
forever with no output.  However, on a buggy kernel running on a
processor with the "xsave" feature but without the "xsaves" feature
(e.g. Sandy Bridge through Broadwell for Intel), within a second or two
the program reports that the xmm registers were corrupted, i.e. were not
restored correctly.  With CONFIG_X86_DEBUG_FPU=y it also hits the above
kernel warning.

    #define _GNU_SOURCE
    #include <stdbool.h>
    #include <inttypes.h>
    #include <linux/elf.h>
    #include <stdio.h>
    #include <sys/ptrace.h>
    #include <sys/uio.h>
    #include <sys/wait.h>
    #include <unistd.h>

    int main(void)
    {
        int pid = fork();
        uint64_t xstate[512];
        struct iovec iov = { .iov_base = xstate, .iov_len = sizeof(xstate) };

        if (pid == 0) {
            bool tracee = true;
            for (int i = 0; i < sysconf(_SC_NPROCESSORS_ONLN) && tracee; i++)
                tracee = (fork() != 0);
            uint32_t xmm0[4] = { [0 ... 3] = tracee ? 0x00000000 : 0xDEADBEEF };
            asm volatile("   movdqu %0, %%xmm0\n"
                         "   mov %0, %%rbx\n"
                         "1: movdqu %%xmm0, %0\n"
                         "   mov %0, %%rax\n"
                         "   cmp %%rax, %%rbx\n"
                         "   je 1b\n"
                         : "+m" (xmm0) : : "rax", "rbx", "xmm0");
            printf("BUG: xmm registers corrupted!  tracee=%d, xmm0=%08X%08X%08X%08X\n",
                   tracee, xmm0[0], xmm0[1], xmm0[2], xmm0[3]);
        } else {
            usleep(100000);
            ptrace(PTRACE_ATTACH, pid, 0, 0);
            wait(NULL);
            ptrace(PTRACE_GETREGSET, pid, NT_X86_XSTATE, &iov);
            xstate[65] = -1;
            ptrace(PTRACE_SETREGSET, pid, NT_X86_XSTATE, &iov);
            ptrace(PTRACE_CONT, pid, 0, 0);
            wait(NULL);
        }
        return 1;
    }

Note: the program only tests for the bug using the ptrace() system call.
The bug can also be reproduced using the rt_sigreturn() system call, but
only when called from a 32-bit program, since for 64-bit programs the
kernel restores the FPU state from the signal frame by doing XRSTOR
directly from userspace memory (with proper error checking).

Fixes: 0b29643a5843 ("x86/xsaves: Change compacted format xsave area header")
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Rik van Riel <riel@redhat.com>
Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Kevin Hao <haokexin@gmail.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Wanpeng Li <wanpeng.li@hotmail.com>
Cc: Yu-cheng Yu <yu-cheng.yu@intel.com>
Cc: <stable@vger.kernel.org>    [v3.17+]
Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 arch/x86/kernel/fpu/regset.c | 4 ++++
 arch/x86/kernel/fpu/signal.c | 9 +++++++--
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kernel/fpu/regset.c b/arch/x86/kernel/fpu/regset.c
index 19a7385a912c..c764f7405322 100644
--- a/arch/x86/kernel/fpu/regset.c
+++ b/arch/x86/kernel/fpu/regset.c
@@ -141,6 +141,10 @@ int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
 			ret = copy_user_to_xstate(xsave, ubuf);
 	} else {
 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
+
+		/* xcomp_bv must be 0 when using uncompacted format */
+		if (!ret && xsave->header.xcomp_bv)
+			ret = -EINVAL;
 	}
 
 	/*
diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c
index 629106e51a29..d34349934702 100644
--- a/arch/x86/kernel/fpu/signal.c
+++ b/arch/x86/kernel/fpu/signal.c
@@ -324,11 +324,16 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)
 		 */
 		fpu__drop(fpu);
 
-		if (using_compacted_format())
+		if (using_compacted_format()) {
 			err = copy_user_to_xstate(&fpu->state.xsave, buf_fx);
-		else
+		} else {
 			err = __copy_from_user(&fpu->state.xsave, buf_fx, state_size);
 
+			/* xcomp_bv must be 0 when using uncompacted format */
+			if (!err && fpu->state.xsave.header.xcomp_bv)
+				err = -EINVAL;
+		}
+
 		if (err || __copy_from_user(&env, buf, sizeof(env))) {
 			fpstate_init(&fpu->state);
 			trace_x86_fpu_init_state(fpu);
-- 
2.14.1.821.g8fa685d3b7-goog

  reply	other threads:[~2017-09-22 17:44 UTC|newest]

Thread overview: 94+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-22 17:41 [PATCH v4 0/3] x86/fpu: prevent leaking FPU registers via invalid FPU state Eric Biggers
2017-09-22 17:41 ` Eric Biggers [this message]
2017-09-22 17:41 ` [PATCH v4 2/3] x86/fpu: tighten validation of user-supplied xstate_header Eric Biggers
2017-09-22 17:41 ` [PATCH v4 3/3] x86/fpu: reinitialize FPU registers if restoring FPU state fails Eric Biggers
2017-09-23  9:09 ` [PATCH v4 0/3] x86/fpu: prevent leaking FPU registers via invalid FPU state Ingo Molnar
  -- strict thread matches above, loose matches on Subject: below --
2017-09-23 12:59 [PATCH 00/33] x86 FPU fixes and cleanups for v4.14 Ingo Molnar
2017-09-23 12:59 ` [PATCH 01/33] x86/fpu: Rename copyin_to_xsaves()/copyout_from_xsaves() to copy_user_to_xstate()/copy_xstate_to_user() Ingo Molnar
2017-09-26  8:21   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 12:59 ` [PATCH 02/33] x86/fpu: Split copy_xstate_to_user() into copy_xstate_to_kernel() & copy_xstate_to_user() Ingo Molnar
2017-09-26  8:22   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 12:59 ` [PATCH 03/33] x86/fpu: Remove 'ubuf' parameter from the copy_xstate_to_kernel() APIs Ingo Molnar
2017-09-26  8:22   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 12:59 ` [PATCH 04/33] x86/fpu: Remove 'kbuf' parameter from the copy_xstate_to_user() APIs Ingo Molnar
2017-09-26  8:23   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 12:59 ` [PATCH 05/33] x86/fpu: Clean up parameter order in the copy_xstate_to_*() APIs Ingo Molnar
2017-09-26  8:23   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 12:59 ` [PATCH 06/33] x86/fpu: Clean up the parameter definitions of copy_xstate_to_*() Ingo Molnar
2017-09-26  8:23   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 12:59 ` [PATCH 07/33] x86/fpu: Remove the 'start_pos' parameter from the __copy_xstate_to_*() functions Ingo Molnar
2017-09-26  8:24   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 12:59 ` [PATCH 08/33] x86/fpu: Clarify parameter names in the copy_xstate_to_*() methods Ingo Molnar
2017-09-25 19:56   ` Thomas Gleixner
2017-09-25 20:01     ` Thomas Gleixner
2017-09-26  8:24   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 12:59 ` [PATCH 09/33] x86/fpu: Change 'size_total' parameter to unsigned and standardize the size checks in copy_xstate_to_*() Ingo Molnar
2017-09-26  8:25   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 12:59 ` [PATCH 10/33] x86/fpu: Simplify __copy_xstate_to_kernel() return values Ingo Molnar
2017-09-26  8:25   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 12:59 ` [PATCH 11/33] x86/fpu: Split copy_user_to_xstate() into copy_kernel_to_xstate() & copy_user_to_xstate() Ingo Molnar
2017-09-26  8:25   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 12:59 ` [PATCH 12/33] x86/fpu: Remove 'ubuf' parameter from the copy_kernel_to_xstate() API Ingo Molnar
2017-09-26  8:26   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 12:59 ` [PATCH 13/33] x86/fpu: Remove 'kbuf' parameter from the copy_user_to_xstate() API Ingo Molnar
2017-09-26  8:26   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 12:59 ` [PATCH 14/33] x86/fpu: Flip the parameter order in copy_*_to_xstate() Ingo Molnar
2017-09-26  8:27   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 12:59 ` [PATCH 15/33] x86/fpu: Simplify fpu->fpregs_active use Ingo Molnar
2017-09-26  8:27   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 12:59 ` [PATCH 16/33] x86/fpu: Make the fpu state change in fpu__clear() scheduler-atomic Ingo Molnar
2017-09-26  8:27   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 13:00 ` [PATCH 17/33] x86/fpu: Split the state handling in fpu__drop() Ingo Molnar
2017-09-26  8:28   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 13:00 ` [PATCH 18/33] x86/fpu: Change fpu->fpregs_active users to fpu->fpstate_active Ingo Molnar
2017-09-26  8:28   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 13:00 ` [PATCH 19/33] x86/fpu: Decouple fpregs_activate()/fpregs_deactivate() from fpu->fpregs_active Ingo Molnar
2017-09-26  8:28   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 13:00 ` [PATCH 20/33] x86/fpu: Remove struct fpu::fpregs_active Ingo Molnar
2017-09-26  8:29   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 13:00 ` [PATCH 21/33] x86/fpu: Add FPU state copying quirk to handle XRSTOR failure on Intel Skylake CPUs Ingo Molnar
2017-09-26  8:29   ` [tip:x86/fpu] " tip-bot for Rik van Riel
2017-09-23 13:00 ` [PATCH 22/33] x86/fpu: Fix boolreturn.cocci warnings Ingo Molnar
2017-09-26  8:30   ` [tip:x86/fpu] " tip-bot for kbuild test robot
2017-09-23 13:00 ` [PATCH 23/33] x86/fpu: Turn WARN_ON() in context switch into WARN_ON_FPU() Ingo Molnar
2017-09-26  8:30   ` [tip:x86/fpu] " tip-bot for Andi Kleen
2017-09-23 13:00 ` [PATCH 24/33] x86/fpu: Don't let userspace set bogus xcomp_bv Ingo Molnar
2017-09-26  8:30   ` [tip:x86/fpu] " tip-bot for Eric Biggers
2017-09-23 13:00 ` [PATCH 25/33] x86/fpu: Tighten validation of user-supplied xstate_header Ingo Molnar
2017-09-23 13:00 ` [PATCH 26/33] x86/fpu: Reinitialize FPU registers if restoring FPU state fails Ingo Molnar
2017-09-26  8:31   ` [tip:x86/fpu] " tip-bot for Eric Biggers
2017-09-23 13:00 ` [PATCH 27/33] x86/fpu: Simplify fpu__activate_fpstate_read() Ingo Molnar
2017-09-26  8:31   ` [tip:x86/fpu] x86/fpu: Fix fpu__activate_fpstate_read() and update comments tip-bot for Ingo Molnar
2017-09-23 13:00 ` [PATCH 28/33] x86/fpu: Remove fpu__current_fpstate_write_begin/end() Ingo Molnar
2017-09-26  8:32   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 13:00 ` [PATCH 29/33] x86/fpu: Rename fpu::fpstate_active to fpu::initialized Ingo Molnar
2017-09-26  8:32   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 13:00 ` [PATCH 30/33] x86/fpu: Fix stale comments about lazy FPU logic Ingo Molnar
2017-09-26  8:32   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 13:00 ` [PATCH 31/33] x86/fpu: Simplify and speed up fpu__copy() Ingo Molnar
2017-09-26  8:33   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 13:00 ` [PATCH 32/33] x86/fpu: Rename fpu__activate_curr() to fpu__initialize() Ingo Molnar
2017-09-26  8:33   ` [tip:x86/fpu] " tip-bot for Ingo Molnar
2017-09-23 13:00 ` [PATCH 33/33] x86/fpu: Rename fpu__activate_fpstate_read/write() to fpu__read/write() Ingo Molnar
2017-09-23 13:02 ` [PATCH 00/33] x86 FPU fixes and cleanups for v4.14 Ingo Molnar
2017-09-23 15:03   ` Juergen Gross
2017-09-23 23:27     ` Ingo Molnar
2017-03-29  6:26 [PATCH] x86/fpu: Turn WARN_ON in context switch into WARN_ON_FPU Andi Kleen
2017-04-24 21:00 ` [tip:perf/core] x86/fpu: Turn WARN_ON() in context switch into WARN_ON_FPU() tip-bot for Andi Kleen
2017-03-06  0:45 [tip:WIP.x86/fpu 31/31] arch/x86/kernel/fpu/xstate.c:931:9-10: WARNING: return of 0/1 in function 'xfeatures_mxcsr_quirk' with return type bool kbuild test robot
2017-03-06  0:45 ` [PATCH] x86/fpu: fix boolreturn.cocci warnings kbuild test robot
2017-03-07  7:23   ` Ingo Molnar
2017-03-07  8:33     ` Thomas Gleixner
2017-03-07  9:01       ` Ingo Molnar
2017-03-07 12:01         ` Joe Perches
2017-04-24 20:54   ` [tip:perf/core] x86/fpu: Fix " tip-bot for kbuild test robot
2017-02-09 23:43 [PATCH v2] x86/fpu: copy MXCSR & MXCSR_FLAGS with SSE/YMM state Rik van Riel
2017-02-10  0:02 ` Borislav Petkov
2017-02-10  0:51   ` Rik van Riel
2017-02-10  8:00     ` Ingo Molnar
2017-02-10 13:54       ` [PATCH v3] " Rik van Riel
2017-02-11 10:02         ` Ingo Molnar
2017-04-24 20:54         ` [tip:perf/core] x86/fpu: Add FPU state copying quirk to handle XRSTOR failure on Intel Skylake CPUs tip-bot for Rik van Riel
2017-02-10  0:45 ` [PATCH v2] x86/fpu: copy MXCSR & MXCSR_FLAGS with SSE/YMM state Yu-cheng Yu
2017-02-10  1:00   ` Rik van Riel
2017-02-10 17:18     ` Yu-cheng Yu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170922174156.16780-2-ebiggers3@gmail.com \
    --to=ebiggers3@gmail.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=dvyukov@google.com \
    --cc=ebiggers@google.com \
    --cc=fenghua.yu@intel.com \
    --cc=haokexin@gmail.com \
    --cc=kernel-hardening@lists.openwall.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mhalcrow@google.com \
    --cc=mingo@kernel.org \
    --cc=oleg@redhat.com \
    --cc=stable@vger.kernel.org \
    --cc=wanpeng.li@hotmail.com \
    --cc=x86@kernel.org \
    --cc=yu-cheng.yu@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).