linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Chang S. Bae" <chang.seok.bae@intel.com>
To: x86@kernel.org
Cc: luto@kernel.org, ak@linux.intel.com, hpa@zytor.com,
	markus.t.metzger@intel.com, tony.luck@intel.com,
	ravi.v.shankar@intel.com, linux-kernel@vger.kernel.org,
	chang.seok.bae@intel.com
Subject: [PATCH 14/15] x86/fsgsbase/64: Support legacy behavior when FS/GS updated by ptracer
Date: Mon, 19 Mar 2018 10:49:26 -0700	[thread overview]
Message-ID: <1521481767-22113-15-git-send-email-chang.seok.bae@intel.com> (raw)
In-Reply-To: <1521481767-22113-1-git-send-email-chang.seok.bae@intel.com>

When FSGSBASE enabled, ptracer's FS/GS selector update
fetches FS/GS base from GDT/LDT. This emulation of FS/GS
segment loading provides backward compatibility for the
legacy ptracers.

When ptracer sets FS/GS selector, its base is going to be
(accordingly) reloaded as the tracee resumes. This is without
FSGSBASE. With FSGSBASE, FS/GS base is preserved regardless
of its selector. Thus, emulating FS/GS load in ptrace is
requested to keep compatible with what has been with FS/GS
setting.

Additionally, whenever a new base value is written, the
FSGSBASE-enabled kernel allows the tracee effectively carry
on. This also means that when both selector and base are
changed, the base is not fetched from GDT/LDT, but
preserved as given.

In a summary, ptracer's update on FS/GS selector and base
yields such results on tracee's base:
- When FS/GS selector only changed (to nonzero), fetch base
from GDT/LDT (legacy behavior)
- When FS/GS base (regardless of selector) changed, tracee
will have the base

Suggested-by: Markus T. Metzger <markus.t.metzger@intel.com>
Suggested-by: H. Peter Anvin <hpa@zytor.com>
Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Andy Lutomirski <luto@kernel.org>
---
 arch/x86/include/asm/fsgsbase.h |  4 +++
 arch/x86/kernel/process_64.c    |  4 +--
 arch/x86/kernel/ptrace.c        | 68 +++++++++++++++++++++++++++++++++++------
 3 files changed, 65 insertions(+), 11 deletions(-)

diff --git a/arch/x86/include/asm/fsgsbase.h b/arch/x86/include/asm/fsgsbase.h
index 76d3457..430ae40 100644
--- a/arch/x86/include/asm/fsgsbase.h
+++ b/arch/x86/include/asm/fsgsbase.h
@@ -17,6 +17,10 @@ unsigned long read_task_gsbase(struct task_struct *task);
 int write_task_fsbase(struct task_struct *task, unsigned long fsbase);
 int write_task_gsbase(struct task_struct *task, unsigned long gsbase);
 
+/* Read (FS/GS) base from GDT/LDT */
+unsigned long task_seg_base(struct task_struct *task,
+			    unsigned short selector);
+
 /*
  * Must be protected by X86_FEATURE_FSGSBASE check.
  */
diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
index 5aae132..ef32f75 100644
--- a/arch/x86/kernel/process_64.c
+++ b/arch/x86/kernel/process_64.c
@@ -328,8 +328,8 @@ static __always_inline void load_fsgs(struct thread_struct *prev,
 	}
 }
 
-static unsigned long task_seg_base(struct task_struct *task,
-				   unsigned short selector)
+unsigned long task_seg_base(struct task_struct *task,
+			    unsigned short selector)
 {
 	unsigned short idx = selector >> 3;
 	unsigned long base;
diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
index ee37e28..be3e022 100644
--- a/arch/x86/kernel/ptrace.c
+++ b/arch/x86/kernel/ptrace.c
@@ -307,14 +307,26 @@ static int set_segment_reg(struct task_struct *task,
 
 	switch (offset) {
 	case USER_REGS_OFFSET(fs):
-		task->thread.fsindex = value;
 		if (task == current)
-			loadsegment(fs, task->thread.fsindex);
+			loadsegment(fs, value);
+		/*
+		 * %fs setting goes to reload its base, when tracee
+		 * resumes without FSGSBASE (legacy). Here with FSGSBASE
+		 * FS base is (manually) fetched from GDT/LDT when needed.
+		 */
+		else if (static_cpu_has(X86_FEATURE_FSGSBASE) &&
+			 (value != 0) && (task->thread.fsindex != value))
+			task->thread.fsbase = task_seg_base(task, value);
+		task->thread.fsindex = value;
 		break;
 	case USER_REGS_OFFSET(gs):
-		task->thread.gsindex = value;
 		if (task == current)
-			load_gs_index(task->thread.gsindex);
+			load_gs_index(value);
+		/* Same as %fs handling above */
+		else if (static_cpu_has(X86_FEATURE_FSGSBASE) &&
+			 (value != 0) && (task->thread.gsindex != value))
+			task->thread.gsbase = task_seg_base(task, value);
+		task->thread.gsindex = value;
 		break;
 	case USER_REGS_OFFSET(ds):
 		task->thread.ds = value;
@@ -433,14 +445,31 @@ static int putregs(struct task_struct *child,
 			((offset + count) >= USER_REGS_OFFSET(fs));
 	bool gs_fully_covered = (offset <= USER_REGS_OFFSET(gs_base)) &&
 			((offset + count) >= USER_REGS_OFFSET(gs));
+	bool fs_updated = false, gs_updated = false;
 
 	offset += count - sizeof(*v);
 
 	while (count >= sizeof(*v) && !ret) {
 		v--;
 		switch (offset) {
+		case USER_REGS_OFFSET(fs):
+			if (fs_fully_covered &&
+			    static_cpu_has(X86_FEATURE_FSGSBASE)) {
+				if (invalid_selector(*v))
+					return -EIO;
+				/*
+				 * Set the flag to fetch fsbase from GDT/LDT
+				 * with FSGSBASE
+				 */
+				fs_updated = (*v != 0) &&
+					(child->thread.fsindex != *v);
+				child->thread.fsindex = *v;
+				break;
+			}
 		case USER_REGS_OFFSET(fs_base):
 			if (fs_fully_covered) {
+				struct thread_struct *thread = &child->thread;
+
 				if (unlikely(*v >= TASK_SIZE_MAX))
 					return -EIO;
 				/*
@@ -448,17 +477,38 @@ static int putregs(struct task_struct *child,
 				 * write_task_fsbase() tends to overwrite
 				 * task's %fs. Simply setting base only here.
 				 */
-				if (child->thread.fsbase != *v)
-					child->thread.fsbase = *v;
+				if (thread->fsbase != *v)
+					thread->fsbase = *v;
+				else if (fs_updated)
+					thread->fsbase =
+						task_seg_base(child,
+							      thread->fsindex);
+				break;
+			}
+		case USER_REGS_OFFSET(gs):
+			if (gs_fully_covered &&
+			    static_cpu_has(X86_FEATURE_FSGSBASE)) {
+				if (invalid_selector(*v))
+					return -EIO;
+				/* Same here as the %fs handling above */
+				gs_updated = (*v != 0) &&
+					(child->thread.gsindex != *v);
+				child->thread.gsindex = *v;
 				break;
 			}
 		case USER_REGS_OFFSET(gs_base):
 			if (gs_fully_covered) {
+				struct thread_struct *thread = &child->thread;
+
 				if (unlikely(*v >= TASK_SIZE_MAX))
 					return -EIO;
-				/* Same here as the %fs handling above */
-				if (child->thread.gsbase != *v)
-					child->thread.gsbase = *v;
+				/* Same here as the %fs_base handling above */
+				if (thread->gsbase != *v)
+					thread->gsbase = *v;
+				else if (gs_updated)
+					thread->gsbase =
+						task_seg_base(child,
+							      thread->gsindex);
 				break;
 			}
 		default:
-- 
2.7.4

  parent reply	other threads:[~2018-03-19 20:55 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-19 17:49 [PATCH 00/15] x86: Enable FSGSBASE instructions Chang S. Bae
2018-03-19 17:49 ` [PATCH 01/15] x86/fsgsbase/64: Introduce FS/GS base helper functions Chang S. Bae
2018-03-19 17:49 ` [PATCH 02/15] x86/fsgsbase/64: Make ptrace read FS/GS base accurately Chang S. Bae
2018-03-19 17:49 ` [PATCH 03/15] x86/fsgsbase/64: Use FS/GS base helpers in core dump Chang S. Bae
2018-03-19 17:49 ` [PATCH 04/15] x86/fsgsbase/64: Factor out load FS/GS segments from __switch_to Chang S. Bae
2018-03-19 17:49 ` [PATCH 05/15] x86/ptrace: A new macro to get an offset of user_regs_struct Chang S. Bae
2018-03-19 17:49 ` [PATCH 06/15] x86/fsgsbase/64: Add putregs() to handle multiple elements' setting Chang S. Bae
2018-03-19 17:49 ` [PATCH 07/15] x86/fsgsbase/64: putregs() in a reverse order Chang S. Bae
2018-03-19 17:49 ` [PATCH 08/15] x86/fsgsbase/64: Add 'unsafe_fsgsbase' to enable CR4.FSGSBASE Chang S. Bae
2018-03-19 17:49 ` [PATCH 09/15] x86/fsgsbase/64: Add intrinsics/macros for FSGSBASE instructions Chang S. Bae
2018-03-19 17:49 ` [PATCH 10/15] x86/fsgsbase/64: Enable FSGSBASE instructions in helper functions Chang S. Bae
2018-03-19 17:49 ` [PATCH 11/15] x86/fsgsbase/64: Preserve FS/GS state in __switch_to if FSGSBASE is on Chang S. Bae
2018-03-19 17:49 ` [PATCH 12/15] x86/fsgsbase/64: When copying a thread, use FSGSBASE if enabled Chang S. Bae
2018-03-19 17:49 ` [PATCH 13/15] x86/fsgsbase/64: With FSGSBASE, compare GS bases on paranoid_entry Chang S. Bae
2018-03-19 20:05   ` Dave Hansen
2018-03-19 21:12     ` Bae, Chang Seok
2018-03-20 10:16   ` David Laight
2018-03-20 20:07     ` H. Peter Anvin
2018-03-20 20:36       ` Bae, Chang Seok
2018-03-21  0:36       ` Andy Lutomirski
2018-03-21 21:56         ` H. Peter Anvin
2018-03-20 14:58   ` Andy Lutomirski
2018-03-20 16:33     ` Bae, Chang Seok
2018-03-20 17:03       ` Bae, Chang Seok
2018-03-21 22:03     ` H. Peter Anvin
2018-03-22  1:37       ` Andy Lutomirski
2018-03-19 17:49 ` Chang S. Bae [this message]
2018-03-20 15:04   ` [PATCH 14/15] x86/fsgsbase/64: Support legacy behavior when FS/GS updated by ptracer Andy Lutomirski
2018-03-20 16:33     ` Bae, Chang Seok
2018-03-21  0:47       ` Andy Lutomirski
2018-03-21  7:01         ` Metzger, Markus T
2018-03-22  1:39           ` Andy Lutomirski
2018-03-21 15:11         ` Bae, Chang Seok
2018-03-22  1:40           ` Andy Lutomirski
2018-03-22 15:45             ` Bae, Chang Seok
2018-03-22 16:07               ` Andy Lutomirski
2018-03-22 16:46                 ` Bae, Chang Seok
2018-03-22 16:53                   ` Andy Lutomirski
2018-03-22 21:17                     ` Bae, Chang Seok
2018-03-22 21:28                       ` Andy Lutomirski
2018-03-19 17:49 ` [PATCH 15/15] x86/fsgsbase/64: Enable FSGSBASE by default and add a chicken bit Chang S. Bae
2018-03-20 15:05 ` [PATCH 00/15] x86: Enable FSGSBASE instructions Andy Lutomirski
2018-03-20 15:06   ` Andy Lutomirski
2018-03-20 16:33     ` Bae, Chang Seok
2018-03-21  0:40       ` Andy Lutomirski
2018-03-21 15:15         ` Bae, Chang Seok

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=1521481767-22113-15-git-send-email-chang.seok.bae@intel.com \
    --to=chang.seok.bae@intel.com \
    --cc=ak@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=markus.t.metzger@intel.com \
    --cc=ravi.v.shankar@intel.com \
    --cc=tony.luck@intel.com \
    --cc=x86@kernel.org \
    --subject='Re: [PATCH 14/15] x86/fsgsbase/64: Support legacy behavior when FS/GS updated by ptracer' \
    /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

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).