diff options
Diffstat (limited to 'tests/sys/vm/stack')
-rw-r--r-- | tests/sys/vm/stack/Makefile | 13 | ||||
-rw-r--r-- | tests/sys/vm/stack/stack_dlopen_exec_test.c | 64 | ||||
-rw-r--r-- | tests/sys/vm/stack/stack_dt_need_exec_test.c | 50 | ||||
-rw-r--r-- | tests/sys/vm/stack/stack_mprotect_exec_test.c | 57 |
4 files changed, 184 insertions, 0 deletions
diff --git a/tests/sys/vm/stack/Makefile b/tests/sys/vm/stack/Makefile new file mode 100644 index 000000000000..06ffcf4d4b4a --- /dev/null +++ b/tests/sys/vm/stack/Makefile @@ -0,0 +1,13 @@ +PACKAGE= tests + +TESTSDIR= ${TESTSBASE}/sys/vm/stack + +ATF_TESTS_C+= stack_dt_need_exec_test +ATF_TESTS_C+= stack_dlopen_exec_test +ATF_TESTS_C+= stack_mprotect_exec_test + +LDFLAGS.stack_dt_need_exec_test+= -Wl,-rpath,${TESTSDIR} -L${.OBJDIR:H}/soxstack +LDADD.stack_dt_need_exec_test+= -lsoxstack +LDFLAGS.stack_dlopen_exec_test+= -Wl,-rpath,${TESTSDIR} + +.include <bsd.test.mk> diff --git a/tests/sys/vm/stack/stack_dlopen_exec_test.c b/tests/sys/vm/stack/stack_dlopen_exec_test.c new file mode 100644 index 000000000000..d4d5fc7c5cf0 --- /dev/null +++ b/tests/sys/vm/stack/stack_dlopen_exec_test.c @@ -0,0 +1,64 @@ +/*- + * Copyright (c) 2023 Dmitry Chagin <dchagin@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <sys/systm.h> +#include <vm/vm_param.h> + +#include <atf-c.h> +#include <dlfcn.h> + +static int jumpstack0(void) __noinline; +static int jumpstack1(void) __noinline; + +static int (*socheckstack)(void) = NULL; + +static int +checkstack(void) +{ + void *fh; + + if (socheckstack == NULL) { + fh = dlopen("libsoxstack.so", RTLD_LAZY); + ATF_REQUIRE(fh != NULL); + socheckstack = dlsym(fh, "checkstack"); + ATF_REQUIRE(socheckstack != NULL); + } + return (socheckstack()); +} + +static int +jumpstack0(void) +{ + char stack[SGROWSIZ]; + + explicit_bzero(stack, sizeof(stack)); + return (checkstack()); +} + +static int +jumpstack1(void) +{ + char stack[SGROWSIZ * 2]; + + explicit_bzero(stack, sizeof(stack)); + return (checkstack()); +} + +ATF_TC_WITHOUT_HEAD(dlopen_test); +ATF_TC_BODY(dlopen_test, tc) +{ + + ATF_REQUIRE(jumpstack0() == 0); + ATF_REQUIRE(jumpstack1() == 0); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, dlopen_test); + + return (atf_no_error()); +} diff --git a/tests/sys/vm/stack/stack_dt_need_exec_test.c b/tests/sys/vm/stack/stack_dt_need_exec_test.c new file mode 100644 index 000000000000..8a234abe3da5 --- /dev/null +++ b/tests/sys/vm/stack/stack_dt_need_exec_test.c @@ -0,0 +1,50 @@ +/*- + * Copyright (c) 2023 Dmitry Chagin <dchagin@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <sys/systm.h> +#include <vm/vm_param.h> + +#include <atf-c.h> + +extern int checkstack(void); + +static int jumpstack0(void) __noinline; +static int jumpstack1(void) __noinline; + + +static int +jumpstack0(void) +{ + char stack[SGROWSIZ]; + + explicit_bzero(stack, sizeof(stack)); + return (checkstack()); +} + +static int +jumpstack1(void) +{ + char stack[SGROWSIZ * 2]; + + explicit_bzero(stack, sizeof(stack)); + return (checkstack()); +} + +ATF_TC_WITHOUT_HEAD(dt_need_test); +ATF_TC_BODY(dt_need_test, tc) +{ + + ATF_REQUIRE(jumpstack0() == 0); + ATF_REQUIRE(jumpstack1() == 0); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, dt_need_test); + + return (atf_no_error()); +} diff --git a/tests/sys/vm/stack/stack_mprotect_exec_test.c b/tests/sys/vm/stack/stack_mprotect_exec_test.c new file mode 100644 index 000000000000..ba5a1d5cb859 --- /dev/null +++ b/tests/sys/vm/stack/stack_mprotect_exec_test.c @@ -0,0 +1,57 @@ +/*- + * Copyright (c) 2023 Dmitry Chagin <dchagin@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + * + * PR: 272585 + * Test provided by John F. Carr + */ + +#include <sys/systm.h> +#include <sys/mman.h> +#include <vm/vm_param.h> + +#include <atf-c.h> +#include <signal.h> +#include <unistd.h> + +static void +sigsegv_handler(int sig __unused) +{ + + atf_tc_fail("Invalid stack protection mode after grows"); +} + +ATF_TC_WITHOUT_HEAD(mprotect_exec_test); +ATF_TC_BODY(mprotect_exec_test, tc) +{ + long pagesize; + char *addr, *guard; + size_t alloc_size; + + signal(SIGSEGV, sigsegv_handler); + + pagesize = sysconf(_SC_PAGESIZE); + ATF_REQUIRE(pagesize > 0); + + alloc_size = SGROWSIZ * 2; + addr = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, + MAP_STACK | MAP_PRIVATE | MAP_ANON, -1, 0); + ATF_REQUIRE(addr != MAP_FAILED); + + /* + * Change prot of the last page in the mmaped stack area. + */ + guard = addr + alloc_size - SGROWSIZ; + ATF_REQUIRE(mprotect(guard, pagesize, PROT_NONE) == 0); + + ((volatile char *)guard)[-1]; +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, mprotect_exec_test); + + return (atf_no_error()); +} |