arch/x86: do not allow unlock to set bits
PATCH 1/2 arch/x86: do not allow unlock to set bits
Currently, the code for handling arch_prctl for shadow stack operations is written to exit early on all operations but enable. However, the check for ARCH_SHSTK_UNLOCK is gated on a check for task != current, which means that if current == task, ARCH_SHSTK_UNLOCK can be used to set feature bits by virtue of skipping that check.
This seems not as intended, and the check should first check that the operation is ARCH_SHSTK_UNLOCK and then check the task status to determine the error code.
--- arch/x86/kernel/shstk.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
arch/x86/kernel/shstk.c +7 -6
@@ -583,12 +583,13 @@ long shstk_prctl(struct task_struct *task, int option, unsigned long arg2) }/* Only allow via ptrace */ -if (task != current) { -if (option ==ARCH_SHSTK_UNLOCK &&IS_ENABLED (CONFIG_CHECKPOINT_RESTORE )) { - task->thread.features_locked &= ~features; -return 0 ; - } -return -EINVAL ; +if (option ==ARCH_SHSTK_UNLOCK ) { +if (task == current) +return -EPERM ; +if (!IS_ENABLED (CONFIG_CHECKPOINT_RESTORE )) +return -EINVAL ; + task->thread.features_locked &= ~features; +return 0 ; }/* Do not allow to change locked features */
Signature · 1 line
2.54.0
PATCH 2/2 selftest/x86: test ARCH_SHSTK_UNLOCK
One should not be able to use ARCH_SHSTK_UNLOCK via arch_prctl to twiddle feature bits, test that this is the case.
--- tools/testing/selftests/x86/test_shadow_stack.c | 8 ++++++++ 1 file changed, 8 insertions(+)
tools/testing/selftests/x86/test_shadow_stack.c +8 -0
@@ -979,6 +979,14 @@ int main(int argc, char *argv[]) {int ret =0 ; +/* test that we can't use unlock to set shadow stack */ +if (!ARCH_PRCTL (ARCH_SHSTK_UNLOCK ,ARCH_SHSTK_SHSTK )) { +printf ("[SKIP]\tCould enable Shadow stack via UNLOCK\n" ); +return 1 ; + } + +printf ("[OK]\tCouldn't enable Shadow stack via UNLOCK\n" ); +if (ARCH_PRCTL (ARCH_SHSTK_ENABLE ,ARCH_SHSTK_SHSTK )) {printf ("[SKIP]\tCould not enable Shadow stack\n" );return 1 ;
Signature · 1 line
2.54.0
Re: [PATCH 2/2] selftest/x86: test ARCH_SHSTK_UNLOCK
Quoted 13 lines · Bill Roberts
One should not be able to use
ARCH_SHSTK_UNLOCKviaarch_prctlto twiddle feature bits, test that this is the case.Signed-off-by Bill Roberts <[email protected]>--- tools/testing/selftests/x86/test_shadow_stack.c | 8 ++++++++ 1 file changed, 8 insertions(+)tools/testing/selftests/x86/test_shadow_stack.c +0
-0
tools/testing/selftests/x86/test_shadow_stack.c int main(int argc, char *argv[]) · line 986
Quoted 8 lines
tools/testing/selftests/x86/test_shadow_stack.c +5
-0@@ -979,6 +979,14 @@ int main(int argc, char *argv[]) {int ret =0 ; +/* test that we can't use unlock to set shadow stack */ +if (!ARCH_PRCTL (ARCH_SHSTK_UNLOCK ,ARCH_SHSTK_SHSTK )) { +printf ("[SKIP]\tCould enable Shadow stack via UNLOCK\n" ); +return 1 ; + }
More generally, we should not be able to use ARCH_SHSTK_UNLOCK here. Can we make the comment generic and not checking for a specific bug? Because it does more than that.
Quoted 6 lines
tools/testing/selftests/x86/test_shadow_stack.c +3
-0+ +printf ("[OK]\tCouldn't enable Shadow stack via UNLOCK\n" ); +if (ARCH_PRCTL (ARCH_SHSTK_ENABLE ,ARCH_SHSTK_SHSTK )) {printf ("[SKIP]\tCould not enable Shadow stack\n" );return 1 ;
Re: [PATCH 2/2] selftest/x86: test ARCH_SHSTK_UNLOCK
Quoted 17 lines · Rick P
Quoted 16 lines · Bill Roberts
One should not be able to use
ARCH_SHSTK_UNLOCKviaarch_prctlto twiddle feature bits, test that this is the case.Signed-off-by Bill Roberts <[email protected]>--- tools/testing/selftests/x86/test_shadow_stack.c | 8 ++++++++ 1 file changed, 8 insertions(+)tools/testing/selftests/x86/test_shadow_stack.c +0
-0@@ -979,6 +979,14 @@ int main(int argc, char *argv[]) {int ret =0 ;
Quoted 8 lines
tools/testing/selftests/x86/test_shadow_stack.c int main(int argc, char *argv[]) · line 986
Quoted 5 lines
tools/testing/selftests/x86/test_shadow_stack.c +5
-0+/* test that we can't use unlock to set shadow stack */ +if (!ARCH_PRCTL (ARCH_SHSTK_UNLOCK ,ARCH_SHSTK_SHSTK )) { +printf ("[SKIP]\tCould enable Shadow stack via UNLOCK\n" ); +return 1 ; + }More generally, we should not be able to use
ARCH_SHSTK_UNLOCKhere. Can we make the comment generic and not checking for a specific bug? Because it does more than that.
Hey Rick, not really sure how I could make the git commit message and the comment more generic, as they seem really generic, can you please be more specific or give me a blurb you're looking for here?
Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/x86/test_shadow_stack.c +3
-0+ +printf ("[OK]\tCouldn't enable Shadow stack via UNLOCK\n" ); +if (ARCH_PRCTL (ARCH_SHSTK_ENABLE ,ARCH_SHSTK_SHSTK )) {printf ("[SKIP]\tCould not enable Shadow stack\n" );return 1 ;
Re: [PATCH 2/2] selftest/x86: test ARCH_SHSTK_UNLOCK
Quoted 11 lines · Bill Roberts
Quoted 10 lines
tools/testing/selftests/x86/test_shadow_stack.c int main(int argc, char *argv[]) · line 986
Quoted 5 lines
tools/testing/selftests/x86/test_shadow_stack.c +5
-0+/* test that we can't use unlock to set shadow stack */ +if (!ARCH_PRCTL (ARCH_SHSTK_UNLOCK ,ARCH_SHSTK_SHSTK )) { +printf ("[SKIP]\tCould enable Shadow stack via UNLOCK\n" ); +return 1 ; + }More generally, we should not be able to use
ARCH_SHSTK_UNLOCKhere. Can we make
the comment generic and not checking for a specific bug? Because it does more
than that.
Quoted 5 lines
Hey Rick, not really sure how I could make the git commit message and the comment
more generic, as they seem really generic, can you please be more specific or give me
a blurb you're looking for here?
I was thinking that the comment was specifically about ARCH_SHSTK_UNLOCK being able to set shadow stack. But it is really testing whether ARCH_SHSTK_UNLOCK can be called at all. So maybe:
/*
* Test that ARCH_SHSTK_UNLOCK can't be called
* from normal (non-ptrace) context.
*/
Re: [PATCH 2/2] selftest/x86: test ARCH_SHSTK_UNLOCK
In reply to Bill Robertstools/testing/selftests/x86/test_shadow_stack.c int main(int argc, char *argv[]) · line 984
Quoted 3 lines · Bill Roberts
tools/testing/selftests/x86/test_shadow_stack.c +3
-0+/* test that we can't use unlock to set shadow stack */ +if (!ARCH_PRCTL (ARCH_SHSTK_UNLOCK ,ARCH_SHSTK_SHSTK )) { +printf ("[SKIP]\tCould enable Shadow stack via UNLOCK\n" );
Should it be a FAIL? And if this path is taken the test will crash because shadow stack got accidentally enabled and we can't return from this function. It can "goto out" and disable shadow stack before returning.
Quoted 5 lines
tools/testing/selftests/x86/test_shadow_stack.c +5
-0+return 1 ; + } + +printf ("[OK]\tCouldn't enable Shadow stack via UNLOCK\n" ); +
Re: [PATCH 2/2] selftest/x86: test ARCH_SHSTK_UNLOCK
Quoted 7 lines · Rick P
tools/testing/selftests/x86/test_shadow_stack.c int main(int argc, char *argv[]) · line 984
Quoted 3 lines · Bill Roberts
tools/testing/selftests/x86/test_shadow_stack.c +3
-0+/* test that we can't use unlock to set shadow stack */ +if (!ARCH_PRCTL (ARCH_SHSTK_UNLOCK ,ARCH_SHSTK_SHSTK )) { +printf ("[SKIP]\tCould enable Shadow stack via UNLOCK\n" );Should it be a FAIL? And if this path is taken the test will crash because shadow stack got accidentally enabled and we can't return from this function. It can "goto out" and disable shadow stack before returning.
Yeah Rick, we can do that, not sure why I didn't do that in V1 :-p
Quoted 5 lines
Quoted 5 lines
tools/testing/selftests/x86/test_shadow_stack.c +5
-0+return 1 ; + } + +printf ("[OK]\tCouldn't enable Shadow stack via UNLOCK\n" ); +
Quoted 9 lines · Bill Roberts
Currently, the code for handling
arch_prctlfor shadow stack operations is written to exit early on all operations but enable. However, the check forARCH_SHSTK_UNLOCKis gated on a check for task != current, which means that if current == task,ARCH_SHSTK_UNLOCKcan be used to set feature bits by virtue of skipping that check.This seems not as intended, and the check should first check that the operation is
ARCH_SHSTK_UNLOCKand then check the task status to determine the error code.
I found this log kind of confusing. The problem being fixed is not that ARCH_SHSTK_UNLOCK can be used when current == task, but rather that it can be misinterpreted as ARCH_SHSTK_ENABLE. Which is allowed when current == task. So this is a very strange ABI, but nothing is exposed.
Quoted 17 lines
Signed-off-by Bill Roberts <[email protected]>--- arch/x86/kernel/shstk.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)arch/x86/kernel/shstk.c +0
-3@@ -583,12 +583,13 @@ long shstk_prctl(struct task_struct *task, int option, unsigned long arg2) }/* Only allow via ptrace */ -if (task != current) { -if (option ==ARCH_SHSTK_UNLOCK &&IS_ENABLED (CONFIG_CHECKPOINT_RESTORE )) { - task->thread.features_locked &= ~features;
arch/x86/kernel/shstk.c long shstk_prctl(struct task_struct *task, int option, unsigned long arg2) · line 592
Quoted 10 lines
arch/x86/kernel/shstk.c +7
-3-return 0 ; - } -return -EINVAL ; +if (option ==ARCH_SHSTK_UNLOCK ) { +if (task == current) +return -EPERM ; +if (!IS_ENABLED (CONFIG_CHECKPOINT_RESTORE )) +return -EINVAL ; + task->thread.features_locked &= ~features; +return 0 ;
I think the root cause of this is the slightly odd default treatment of ARCH_SHSTK_ENABLE. We should refactor it to be a normal case statement that explicitly has behavior for each ARCH_SHSTK_FOO. This moves in that direction, but leaves the code brittle.
Quoted 3 lines
arch/x86/kernel/shstk.c +0
-0}/* Do not allow to change locked features */
Quoted 7 lines · Rick P
Quoted 6 lines · Bill Roberts
Currently, the code for handling
arch_prctlfor shadow stack operations is written to exit early on all operations but enable. However, the check forARCH_SHSTK_UNLOCKis gated on a check for task != current, which means that if current == task,ARCH_SHSTK_UNLOCKcan be used to set feature bits by virtue of skipping that check.
Quoted 7 lines
Quoted 3 lines
This seems not as intended, and the check should first check that the operation is
ARCH_SHSTK_UNLOCKand then check the task status to determine the error code.I found this log kind of confusing. The problem being fixed is not that
ARCH_SHSTK_UNLOCKcan be used when current == task, but rather that it can be misinterpreted asARCH_SHSTK_ENABLE. Which is allowed when current == task. So this is a very strange ABI, but nothing is exposed.
I'll make the message more clear on the v2 respin.
Quoted 39 lines
Quoted 17 lines
Signed-off-by Bill Roberts <[email protected]>--- arch/x86/kernel/shstk.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)arch/x86/kernel/shstk.c +0
-3@@ -583,12 +583,13 @@ long shstk_prctl(struct task_struct *task, int option, unsigned long arg2) }/* Only allow via ptrace */ -if (task != current) { -if (option ==ARCH_SHSTK_UNLOCK &&IS_ENABLED (CONFIG_CHECKPOINT_RESTORE )) { - task->thread.features_locked &= ~features;arch/x86/kernel/shstk.c long shstk_prctl(struct task_struct *task, int option, unsigned long arg2) · line 592
Quoted 10 lines
arch/x86/kernel/shstk.c +7
-3-return 0 ; - } -return -EINVAL ; +if (option ==ARCH_SHSTK_UNLOCK ) { +if (task == current) +return -EPERM ; +if (!IS_ENABLED (CONFIG_CHECKPOINT_RESTORE )) +return -EINVAL ; + task->thread.features_locked &= ~features; +return 0 ;I think the root cause of this is the slightly odd default treatment of
ARCH_SHSTK_ENABLE. We should refactor it to be a normal case statement that explicitly has behavior for eachARCH_SHSTK_FOO. This moves in that direction, but leaves the code brittle.Ha, I had that initially, but kept the lowest line count change that fixed the issue. Ill add that to v2.
Quoted 3 lines
arch/x86/kernel/shstk.c +0
-0}/* Do not allow to change locked features */