x86/fsgsbase/64: Fix the base write helper functions
Subsystems (2)
PATCH v3 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.
v2: Fix further on the task write functions. Revert the changes on the task read helpers.
v3: Fix putreg(). Edit the changelog.
--- arch/x86/kernel/process_64.c | 48 ++++++++++++++++++++++-------------- arch/x86/kernel/ptrace.c | 8 +++--- 2 files changed, 34 insertions(+), 22 deletions(-)
arch/x86/kernel/process_64.c +30 -18
@@ -339,19 +339,11 @@ static unsigned long x86_fsgsbase_read_task(struct task_struct *task,void x86_fsbase_write_cpu (unsigned long fsbase) { -/* -* Set the selector to 0 as a notion, that the segment base is -* overwritten, which will be checked for skipping the segment load -* during context switch. -*/ -loadseg (FS ,0 );wrmsrl (MSR_FS_BASE , fsbase); }void x86_gsbase_write_cpu_inactive (unsigned long gsbase) { -/* Set the selector to 0 for the same reason as %fs above. */ -loadseg (GS ,0 );wrmsrl (MSR_KERNEL_GS_BASE , gsbase); }
@@ -392,12 +384,7 @@ int x86_fsbase_write_task(struct task_struct *task, unsigned long fsbase)if (unlikely (fsbase >=TASK_SIZE_MAX ))return -EPERM ; -preempt_disable (); task->thread.fsbase = fsbase; -if (task == current) -x86_fsbase_write_cpu (fsbase); - task->thread.fsindex =0 ; -preempt_enable ();return 0 ; }
@@ -407,12 +394,7 @@ int x86_gsbase_write_task(struct task_struct *task, unsigned long gsbase)if (unlikely (gsbase >=TASK_SIZE_MAX ))return -EPERM ; -preempt_disable (); task->thread.gsbase = gsbase; -if (task == current) -x86_gsbase_write_cpu_inactive (gsbase); - task->thread.gsindex =0 ; -preempt_enable ();return 0 ; }
@@ -758,11 +740,41 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2)switch (option) {case ARCH_SET_GS : { +preempt_disable (); ret =x86_gsbase_write_task (task, arg2); +if (ret ==0 ) { +/* +* ARCH_SET_GS has always overwritten the index +* and the base. Zero is the most sensible value +* to put in the index, and is the only value that +* makes any sense if FSGSBASE is unavailable. +*/ +if (task == current) { +loadseg (GS ,0 ); +x86_gsbase_write_cpu_inactive (arg2); + }else { + task->thread.gsindex =0 ; + } + } +preempt_enable ();break ; }case ARCH_SET_FS : { +preempt_disable (); ret =x86_fsbase_write_task (task, arg2); +if (ret ==0 ) { +/* +* Set the selector to 0 for the same reason +* as %gs above. +*/ +if (task == current) { +loadseg (FS ,0 ); +x86_fsbase_write_cpu (arg2); + }else { + task->thread.fsindex =0 ; + } + } +preempt_enable ();break ; }case ARCH_GET_FS : {
arch/x86/kernel/ptrace.c +4 -4
@@ -397,11 +397,11 @@ 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 and the base. */ 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 +410,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
Quoted 51 lines · Chang S. Bae
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.
v2: Fix further on the task write functions. Revert the changes on the task read helpers.
v3: Fix
putreg(). Edit the changelog.Suggested-by Andy Lutomirski <[email protected]>Signed-off-by Chang S. Bae <[email protected]>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/kernel/process_64.c | 48 ++++++++++++++++++++++-------------- arch/x86/kernel/ptrace.c | 8 +++--- 2 files changed, 34 insertions(+), 22 deletions(-)arch/x86/kernel/process_64.c +0
-8@@ -339,19 +339,11 @@ static unsigned long x86_fsgsbase_read_task(struct task_struct *task,void x86_fsbase_write_cpu (unsigned long fsbase) { -/* -* Set the selector to 0 as a notion, that the segment base is -* overwritten, which will be checked for skipping the segment load -* during context switch. -*/ -loadseg (FS ,0 );wrmsrl (MSR_FS_BASE , fsbase); }void x86_gsbase_write_cpu_inactive (unsigned long gsbase) { -/* Set the selector to 0 for the same reason as %fs above. */ -loadseg (GS ,0 );wrmsrl (MSR_KERNEL_GS_BASE , gsbase); }@@ -392,12 +384,7 @@ int x86_fsbase_write_task(struct task_struct *task, unsigned long fsbase)if (unlikely (fsbase >=TASK_SIZE_MAX ))return -EPERM ;
arch/x86/kernel/process_64.c int x86_fsbase_write_task(struct task_struct *task, unsigned long fsbase) · line 390
Quoted 10 lines
arch/x86/kernel/process_64.c +0
-5-preempt_disable (); task->thread.fsbase = fsbase; -if (task == current) -x86_fsbase_write_cpu (fsbase); - task->thread.fsindex =0 ; -preempt_enable ();return 0 ; }
Similar objection to last time. If you get rid of the task == current check, you should WARN_ON_ONCE(task != current).
Quoted 24 lines
arch/x86/kernel/process_64.c +6
-5@@ -407,12 +394,7 @@ int x86_gsbase_write_task(struct task_struct *task, unsigned long gsbase)if (unlikely (gsbase >=TASK_SIZE_MAX ))return -EPERM ; -preempt_disable (); task->thread.gsbase = gsbase; -if (task == current) -x86_gsbase_write_cpu_inactive (gsbase); - task->thread.gsindex =0 ; -preempt_enable ();return 0 ; }@@ -758,11 +740,41 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2)switch (option) {case ARCH_SET_GS : { +preempt_disable (); ret =x86_gsbase_write_task (task, arg2); +if (ret ==0 ) { +/* +* ARCH_SET_GS has always overwritten the index +* and the base. Zero is the most sensible value +* to put in the index, and is the only value that
arch/x86/kernel/process_64.c long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2) · line 759
Quoted 10 lines
arch/x86/kernel/process_64.c +10
-0+* makes any sense if FSGSBASE is unavailable. +*/ +if (task == current) { +loadseg (GS ,0 ); +x86_gsbase_write_cpu_inactive (arg2); + }else { + task->thread.gsindex =0 ; + } + } +preempt_enable ();
And you should drop the redundant ...write_task if task == current.
Quoted 26 lines
arch/x86/kernel/process_64.c +14
-0break ; }case ARCH_SET_FS : { +preempt_disable (); ret =x86_fsbase_write_task (task, arg2); +if (ret ==0 ) { +/* +* Set the selector to 0 for the same reason +* as %gs above. +*/ +if (task == current) { +loadseg (FS ,0 ); +x86_fsbase_write_cpu (arg2); + }else { + task->thread.fsindex =0 ; + } + } +preempt_enable ();break ; }case ARCH_GET_FS : {arch/x86/kernel/ptrace.c +0
-0
arch/x86/kernel/ptrace.c static int putreg(struct task_struct *child, · line 401
Quoted 7 lines
arch/x86/kernel/ptrace.c +2
-2@@ -397,11 +397,11 @@ 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 and the base.
to set the index to zero and to set the base as requested.
Quoted 19 lines
arch/x86/kernel/ptrace.c +2
-3*/ 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 +410,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.19.1
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 14 lines · Andy Lutomirski
Quoted 12 lines · Chang S. Bae
arch/x86/kernel/process_64.c +0
-5@@ -392,12 +384,7 @@ int x86_fsbase_write_task(struct task_struct *task, unsigned long fsbase)if (unlikely (fsbase >=TASK_SIZE_MAX ))return -EPERM ; -preempt_disable (); task->thread.fsbase = fsbase; -if (task == current) -x86_fsbase_write_cpu (fsbase); - task->thread.fsindex =0 ; -preempt_enable ();
Quoted 6 lines
Review int x86_fsbase_write_task(struct task_struct *task, unsigned long fsbase) · line 390
Quoted 2 lines
(quoted diff) +0
-0return 0; }Similar objection to last time. If you get rid of the task == current check, you should
WARN_ON_ONCE(task != current).
From your previous comments, I was a bit confused by a mixture of options. It looks like you’d intended to suggest two different ways (by our own words):
1. The _write_task() functions must not be called on current. Then there should be a warning, and you shouldn't call them on current.
2. The _write_task() functions should work correctly on current. In this case, you shouldn't need to *also* called _write_cpu / _write_cpu_inactive. You'll still need the special loadseg handling in do_arch_prctl_64().
The option 1 looks to be simple and straight forward. So, let me go with that.
Nit. I think it should be WARN_ON_ONCE(task == current).
Quoted 15 lines
Quoted 15 lines
arch/x86/kernel/process_64.c +10
-0@@ -758,11 +740,41 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2)switch (option) {case ARCH_SET_GS : { +preempt_disable (); ret =x86_gsbase_write_task (task, arg2); +if (ret ==0 ) { +/* +* ARCH_SET_GS has always overwritten the index +* and the base. Zero is the most sensible value +* to put in the index, and is the only value that +* makes any sense if FSGSBASE is unavailable. +*/ +if (task == current) { +loadseg (GS ,0 );
Quoted 8 lines
arch/x86/kernel/process_64.c long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2) · line 759
Quoted 6 lines
arch/x86/kernel/process_64.c +6
-0+x86_gsbase_write_cpu_inactive (arg2); + }else { + task->thread.gsindex =0 ; + } + } +preempt_enable ();And you should drop the redundant ...write_task if task == current.
To avoid the redundancy, the sanity check for a base value can be moved out here.
Quoted 15 lines
arch/x86/kernel/ptrace.c static int putreg(struct task_struct *child, · line 401
Quoted 13 lines
arch/x86/kernel/ptrace.c +2
-2@@ -397,11 +397,11 @@ 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 and the base. to set the index to zero and to set the base as requested.
Thanks for the edit.
Chang