Skip to contentSkip to conversation

x86/fsgsbase/64: Fix the base write helper functions

PATCH New Chang S. Bae 2 participants 3 messages Nov 26, 2018 – Dec 18, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 x86/fsgsbase/64: Fix the base write helper functions

The helper functions that purport to write the base should just write it only. It shouldn't have magic optimizations to change the index.

Make the index explicitly changed from the caller, instead of including the code in the helpers.

Subsequently, the task write helpers do not handle for the current task anymore. The range check for a base value is also factored out, to minimize code redundancy from the caller.

Fixes b1378a561fd1 ("x86/fsgsbase/64: Introduce FS/GS base helper functions")

v2: Fix further on the task write functions. Revert the changes on the task read helpers.

v3: Fix putreg(). Edit the changelog.

v4: Update the task write helper functions and do_arch_prctl_64(). Fix the comment in putreg().

v5: Fix preempt_disable() calls in do_arch_prctl_64()

v6: Fix do_arch_prctl_64() to work correctly wrt save_base_legacy() (Fixed by Andy Lutomirski)

Cc Ingo Molnar <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Andi Kleen <[email protected]>
Cc Dave Hansen <[email protected]>
---
 arch/x86/include/asm/fsgsbase.h | 15 +++--
 arch/x86/kernel/process_64.c    | 99 ++++++++++++++++++---------------
 arch/x86/kernel/ptrace.c        |  9 +--
 3 files changed, 71 insertions(+), 52 deletions(-)
arch/x86/include/asm/fsgsbase.h +11 -4
index eb377b6e9eed..bca4c743de77 100644
@@ -16,8 +16,8 @@
  */
 extern unsigned long x86_fsbase_read_task(struct task_struct *task);
 extern unsigned long x86_gsbase_read_task(struct task_struct *task);
-extern int x86_fsbase_write_task(struct task_struct *task, unsigned long fsbase);
-extern int x86_gsbase_write_task(struct task_struct *task, unsigned long gsbase);
+extern void x86_fsbase_write_task(struct task_struct *task, unsigned long fsbase);
+extern void x86_gsbase_write_task(struct task_struct *task, unsigned long gsbase);
 
 /* Helper functions for reading/writing FS/GS base */
 
@@ -39,8 +39,15 @@ static inline unsigned long x86_gsbase_read_cpu_inactive(void)
 	return gsbase;
 }
 
-extern void x86_fsbase_write_cpu(unsigned long fsbase);
-extern void x86_gsbase_write_cpu_inactive(unsigned long gsbase);
+static inline void x86_fsbase_write_cpu(unsigned long fsbase)
+{
+	wrmsrl(MSR_FS_BASE, fsbase);
+}
+
+static inline void x86_gsbase_write_cpu_inactive(unsigned long gsbase)
+{
+	wrmsrl(MSR_KERNEL_GS_BASE, gsbase);
+}
 
 #endif /* CONFIG_X86_64 */
 
arch/x86/kernel/process_64.c +55 -44

128 lines not on this page · raw message

arch/x86/kernel/ptrace.c +5 -4
index ffae9b9740fd..4b8ee05dd6ad 100644
@@ -397,11 +397,12 @@ static int putreg(struct task_struct *child,
 		if (value >= TASK_SIZE_MAX)
 			return -EIO;
 		/*
-		 * When changing the FS base, use the same
-		 * mechanism as for do_arch_prctl_64().
+		 * When changing the FS base, use do_arch_prctl_64()
+		 * to set the index to zero and to set the base
+		 * as requested.
 		 */
 		if (child->thread.fsbase != value)
-			return x86_fsbase_write_task(child, value);
+			return do_arch_prctl_64(child, ARCH_SET_FS, value);
 		return 0;
 	case offsetof(struct user_regs_struct,gs_base):
 		/*
@@ -410,7 +411,7 @@ static int putreg(struct task_struct *child,
 		if (value >= TASK_SIZE_MAX)
 			return -EIO;
 		if (child->thread.gsbase != value)
-			return x86_gsbase_write_task(child, value);
+			return do_arch_prctl_64(child, ARCH_SET_GS, value);
 		return 0;
 #endif
 	}
Signature · 1 line
2.19.1
#1 lore raw
x86/fsgsbase/64: Fix the base write helper functions
Commit-ID:  4771d4f5de52b4e7aa62334d7ba1e5bd22a5f900
Gitweb:     https://git.kernel.org/tip/4771d4f5de52b4e7aa62334d7ba1e5bd22a5f900
Author:     Chang S. Bae <[email protected]>
AuthorDate: Mon, 26 Nov 2018 11:55:24 -0800
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Tue, 18 Dec 2018 12:58:21 +0100

x86/fsgsbase/64: Fix the base write helper functions

The helper functions which write fs/gs base are not just writing the base, they are also changing the index. That's wrong and needs to be separated.

Make the index explicitly changed from the caller, instead of including the code in the helpers.

Subsequently, the task write helpers do not handle for the current task anymore. The range check for a base value is also factored out, to minimize code redundancy from the caller.

Fixes b1378a561fd1 ("x86/fsgsbase/64: Introduce FS/GS base helper functions")
Cc "H . Peter Anvin" <[email protected]>
Cc Andi Kleen <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Ravi Shankar <[email protected]>
Cc H. Peter Anvin <[email protected]>
---
 arch/x86/include/asm/fsgsbase.h | 15 +++++--
 arch/x86/kernel/process_64.c    | 99 +++++++++++++++++++++++------------------
 arch/x86/kernel/ptrace.c        |  9 ++--
 3 files changed, 71 insertions(+), 52 deletions(-)
arch/x86/include/asm/fsgsbase.h +11 -4
index eb377b6e9eed..bca4c743de77 100644
@@ -16,8 +16,8 @@
  */
 extern unsigned long x86_fsbase_read_task(struct task_struct *task);
 extern unsigned long x86_gsbase_read_task(struct task_struct *task);
-extern int x86_fsbase_write_task(struct task_struct *task, unsigned long fsbase);
-extern int x86_gsbase_write_task(struct task_struct *task, unsigned long gsbase);
+extern void x86_fsbase_write_task(struct task_struct *task, unsigned long fsbase);
+extern void x86_gsbase_write_task(struct task_struct *task, unsigned long gsbase);
 
 /* Helper functions for reading/writing FS/GS base */
 
@@ -39,8 +39,15 @@ static inline unsigned long x86_gsbase_read_cpu_inactive(void)
 	return gsbase;
 }
 
-extern void x86_fsbase_write_cpu(unsigned long fsbase);
-extern void x86_gsbase_write_cpu_inactive(unsigned long gsbase);
+static inline void x86_fsbase_write_cpu(unsigned long fsbase)
+{
+	wrmsrl(MSR_FS_BASE, fsbase);
+}
+
+static inline void x86_gsbase_write_cpu_inactive(unsigned long gsbase)
+{
+	wrmsrl(MSR_KERNEL_GS_BASE, gsbase);
+}
 
 #endif /* CONFIG_X86_64 */
 
arch/x86/kernel/process_64.c +55 -44

128 lines not on this page · raw message

arch/x86/kernel/ptrace.c +5 -4
index ffae9b9740fd..4b8ee05dd6ad 100644
@@ -397,11 +397,12 @@ static int putreg(struct task_struct *child,
 		if (value >= TASK_SIZE_MAX)
 			return -EIO;
 		/*
-		 * When changing the FS base, use the same
-		 * mechanism as for do_arch_prctl_64().
+		 * When changing the FS base, use do_arch_prctl_64()
+		 * to set the index to zero and to set the base
+		 * as requested.
 		 */
 		if (child->thread.fsbase != value)
-			return x86_fsbase_write_task(child, value);
+			return do_arch_prctl_64(child, ARCH_SET_FS, value);
 		return 0;
 	case offsetof(struct user_regs_struct,gs_base):
 		/*
@@ -410,7 +411,7 @@ static int putreg(struct task_struct *child,
 		if (value >= TASK_SIZE_MAX)
 			return -EIO;
 		if (child->thread.gsbase != value)
-			return x86_gsbase_write_task(child, value);
+			return do_arch_prctl_64(child, ARCH_SET_GS, value);
 		return 0;
 #endif
 	}
#2 lore raw
In reply to Chang S. Bae
x86/fsgsbase/64: Fix the base write helper functions
Commit-ID:  87ab4689ca6526079ab6f5150219ee88b42000ae
Gitweb:     https://git.kernel.org/tip/87ab4689ca6526079ab6f5150219ee88b42000ae
Author:     Chang S. Bae <[email protected]>
AuthorDate: Mon, 26 Nov 2018 11:55:24 -0800
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Tue, 18 Dec 2018 14:26:09 +0100

x86/fsgsbase/64: Fix the base write helper functions

Andy spotted a regression in the fs/gs base helpers after the patch series was committed. The helper functions which write fs/gs base are not just writing the base, they are also changing the index. That's wrong and needs to be separated because writing the base has not to modify the index.

While the regression is not causing any harm right now because the only caller depends on that behaviour, it's a guarantee for subtle breakage down the road.

Make the index explicitly changed from the caller, instead of including the code in the helpers.

Subsequently, the task write helpers do not handle for the current task anymore. The range check for a base value is also factored out, to minimize code redundancy from the caller.

Fixes b1378a561fd1 ("x86/fsgsbase/64: Introduce FS/GS base helper functions")
Cc "H . Peter Anvin" <[email protected]>
Cc Andi Kleen <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Ravi Shankar <[email protected]>
Cc H. Peter Anvin <[email protected]>
---
 arch/x86/include/asm/fsgsbase.h | 15 +++++--
 arch/x86/kernel/process_64.c    | 99 +++++++++++++++++++++++------------------
 arch/x86/kernel/ptrace.c        |  9 ++--
 3 files changed, 71 insertions(+), 52 deletions(-)
arch/x86/include/asm/fsgsbase.h +11 -4
index eb377b6e9eed..bca4c743de77 100644
@@ -16,8 +16,8 @@
  */
 extern unsigned long x86_fsbase_read_task(struct task_struct *task);
 extern unsigned long x86_gsbase_read_task(struct task_struct *task);
-extern int x86_fsbase_write_task(struct task_struct *task, unsigned long fsbase);
-extern int x86_gsbase_write_task(struct task_struct *task, unsigned long gsbase);
+extern void x86_fsbase_write_task(struct task_struct *task, unsigned long fsbase);
+extern void x86_gsbase_write_task(struct task_struct *task, unsigned long gsbase);
 
 /* Helper functions for reading/writing FS/GS base */
 
@@ -39,8 +39,15 @@ static inline unsigned long x86_gsbase_read_cpu_inactive(void)
 	return gsbase;
 }
 
-extern void x86_fsbase_write_cpu(unsigned long fsbase);
-extern void x86_gsbase_write_cpu_inactive(unsigned long gsbase);
+static inline void x86_fsbase_write_cpu(unsigned long fsbase)
+{
+	wrmsrl(MSR_FS_BASE, fsbase);
+}
+
+static inline void x86_gsbase_write_cpu_inactive(unsigned long gsbase)
+{
+	wrmsrl(MSR_KERNEL_GS_BASE, gsbase);
+}
 
 #endif /* CONFIG_X86_64 */
 
arch/x86/kernel/process_64.c +55 -44

128 lines not on this page · raw message

arch/x86/kernel/ptrace.c +5 -4
index ffae9b9740fd..4b8ee05dd6ad 100644
@@ -397,11 +397,12 @@ static int putreg(struct task_struct *child,
 		if (value >= TASK_SIZE_MAX)
 			return -EIO;
 		/*
-		 * When changing the FS base, use the same
-		 * mechanism as for do_arch_prctl_64().
+		 * When changing the FS base, use do_arch_prctl_64()
+		 * to set the index to zero and to set the base
+		 * as requested.
 		 */
 		if (child->thread.fsbase != value)
-			return x86_fsbase_write_task(child, value);
+			return do_arch_prctl_64(child, ARCH_SET_FS, value);
 		return 0;
 	case offsetof(struct user_regs_struct,gs_base):
 		/*
@@ -410,7 +411,7 @@ static int putreg(struct task_struct *child,
 		if (value >= TASK_SIZE_MAX)
 			return -EIO;
 		if (child->thread.gsbase != value)
-			return x86_gsbase_write_task(child, value);
+			return do_arch_prctl_64(child, ARCH_SET_GS, value);
 		return 0;
 #endif
 	}