Re: [PATCH] x86_64: test that userspace stack is in fact NX

From: Ingo Molnar
Date: Mon Oct 02 2023 - 09:12:46 EST



* Alexey Dobriyan <adobriyan@xxxxxxxxx> wrote:

> Here is how it works:
>
> * fault and fill the stack from rsp with int3 down until rlimit allows,
> * fill upwards with int3 too, overwrite libc stuff, argv, envp,
> * try to exec int3 on each page and catch it with either SIGSEGV or
> SIGTRAP handler.
>
> Note: trying to execute _every_ int3 takes 30-40 seconds even on fast
> machine, so only 1 int3 per page is tried.
>
> Tested on F37 kernel and on custom kernel which did
>
> vm_flags |= VM_EXEC;
>
> to stack VMA.
>
> Signed-off-by: Alexey Dobriyan <adobriyan@xxxxxxxxx>
> ---
>
> tools/testing/selftests/x86/Makefile | 3
> tools/testing/selftests/x86/nx_stack.c | 167 +++++++++++++++++++++++++++++++++
> 2 files changed, 170 insertions(+)

Ok, that's a good idea, but could the test case please output something
human-readable that indicates whether the test was a success or not?

A typical testcase output is like:

kepler:~/tip/tools/testing/selftests/x86> ./sigaltstack_32
[RUN] Test an alternate signal stack of sufficient size.
Raise SIGALRM. It is expected to be delivered.
[OK] SIGALRM signal delivered.

Or:

kepler:~/tip/tools/testing/selftests/x86> ./test_vsyscall_64
...
[OK] vsyscalls are emulated (1 instructions in vsyscall page)
...

... where the 'OK' denotes success of a test.

The nx_stack testcase only outputs:

stack min 00007fffd75b5000
stack max 00007fffd7db5000

... with only the exit code denoting success/failure.

Thanks,

Ingo