The shadow stack selftests do not currently verify that locking a feature prevents subsequent changes to its enabled state. Add a test that locks the WRSS feature while enabled and verifies that attempting to disable it fails with -EPERM. Despite WRSS being locked, this allows the existing code in main() to disable shadow stack before returning to prevent a SIGSEGV from a shadow stack underflow. This is why shadow stack enable is not locked. Signed-off-by: Bill Roberts --- .../testing/selftests/x86/test_shadow_stack.c | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tools/testing/selftests/x86/test_shadow_stack.c b/tools/testing/selftests/x86/test_shadow_stack.c index b52c5420c137..e047f1ceae0a 100644 --- a/tools/testing/selftests/x86/test_shadow_stack.c +++ b/tools/testing/selftests/x86/test_shadow_stack.c @@ -1060,9 +1060,28 @@ int test_ptrace(void) return 1; } +static int test_locking(void) +{ + + if (ARCH_PRCTL(ARCH_SHSTK_LOCK, ARCH_SHSTK_WRSS)) { + printf("[FAIL]\tCould not lock Shadow stack write\n"); + return 1; + } + + if (ARCH_PRCTL(ARCH_SHSTK_DISABLE, ARCH_SHSTK_WRSS) != -EPERM) { + printf("[FAIL]\tCould change Shadow stack write after lock\n"); + return 1; + } + + printf("[OK]\tShadow stack locking\n"); + + return 0; +} + int main(int argc, char *argv[]) { int ret = 0; + unsigned long status = 0; if (ARCH_PRCTL(ARCH_SHSTK_ENABLE, ARCH_SHSTK_SHSTK)) { printf("[SKIP]\tCould not enable Shadow stack\n"); @@ -1079,12 +1098,31 @@ int main(int argc, char *argv[]) return 1; } + /* Note: test_shadow_stack_lock() needs write enabled */ if (ARCH_PRCTL(ARCH_SHSTK_ENABLE, ARCH_SHSTK_WRSS)) { printf("[SKIP]\tCould not enable WRSS\n"); ret = 1; goto out; } + if (ARCH_PRCTL(ARCH_SHSTK_STATUS, &status)) { + printf("[FAIL]\tCould not get Shadow stack status\n"); + ret = 1; + goto out; + } + + if (status != (ARCH_SHSTK_WRSS|ARCH_SHSTK_SHSTK)) { + printf("[FAIL]\tStatus not as expected, got 0x%lx status, wanted: 0x%llx\n", + status, (ARCH_SHSTK_WRSS|ARCH_SHSTK_SHSTK)); + ret = 1; + goto out; + } + + if (test_locking()) { + ret = 1; + goto out; + } + /* Should have succeeded if here, but this is a test, so double check. */ if (!get_ssp()) { printf("[FAIL]\tShadow stack disabled\n"); -- 2.55.0