Skip to contentSkip to conversation

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

PATCH Superseded Chang S. Bae 1 participant 2 messages Nov 14, 2018 – Nov 16, 2018

Subsystems (2)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 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.

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

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    | 84 ++++++++++++++++-----------------
 arch/x86/kernel/ptrace.c        |  9 ++--
 3 files changed, 56 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 +40 -44

113 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

arch/x86/kernel/process_64.c long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2) · line 725

Quoted 11 lines · Chang Seok

int copy_thread_tls(unsigned long clone_flags, unsigned long sp,

arch/x86/kernel/process_64.c +3 -1
@@ -758,11 +720,45 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2)
 
 	switch (option) {
 	case ARCH_SET_GS: {
-		ret = x86_gsbase_write_task(task, arg2);
+		preempt_disable();
+		if (unlikely(arg2 >= TASK_SIZE_MAX))
+			return -EPERM;

Sorry, preempt_disabled() should go after this.
Chang