From nobody Sun Aug 27 07:33:15 2023 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RYQQg6Tr8z4rbhQ; Sun, 27 Aug 2023 07:33:15 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RYQQg5zzZz4Yj0; Sun, 27 Aug 2023 07:33:15 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1693121595; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=wnJK6I0zoT/furtGC0B7URodCob31T4BIoJngc/Ux8U=; b=tSM1EG0cHY4Fh6P8UJ7Qm4wMTGujZwNyfjf2hD2YJjaDO49kn0jg+fHr83sixZ+97+KwN9 ca2sugRKoknJ7FW5RA3YQRmh2JcZZVD+e9PHsTlLjNBJ/HemhRHLkAutrdRHpRCkPzHvha +aeikO+sjwVBjAFyN0lbMzcCYHMl7GJWjG3bXNAf3k3JBq3zb0BaSz5uCR6Ds/0LuAnL/E Bq0h4ZAbAalirlyDFN81gMxFQhe3u4DkMWhzzdG3PVu/sUF1XwVNByuhbY3y8cIupuIMli KUKiTfrpjzhoETDjtIjrfGFcsJ+6t8MxfZJXKrHM3pIw51FB/W7L+YqiSGouoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1693121595; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=wnJK6I0zoT/furtGC0B7URodCob31T4BIoJngc/Ux8U=; b=k07J85/670JTxgqPsGh2Bl4T+x7dKvizzsUPLQLLYdjqzRppxbv3da3f5fiXwtnUF+hyId YI98MwAlMDe7vE4DtOUj9Yhz2lYUiqm6UFllLAJRySvfEeMB3dnhMmJYVQUmnLXLY75WOD reoJdz1H7YNWcRUgMhEqzAXCMtb8aCyNhgDliut1OMndvhi5UwU+iLNQ1we8Rw7SA2sgAH 94Cpi+KUjc21USLH6OGD9AtbmYKWgOt68AMO4Isd+W4n7gOfKgOVMtlYVXZ1pCZGGJKlvy IHW7NSa/tLrPZ18BOjm++/PKDin0PzbjyE2XXbZWXMfzrkOHnGnBCTemN/Mlww== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1693121595; a=rsa-sha256; cv=none; b=fhNS2NujMnhRFukJh5QtesJAxQyPOKKFDR9GjW+CSb447QbwCHTiqL4HInnfCcdwkKSLNd M58EXPUnplpH0rrdN8PC4nLxgVocI3DRRKY79f15Tt0dMuwKn2DH6+xIcwjLKZNZ6XthPb uBwlmEwFt7tFRhS9P4HubIiL8JbNBvArDvPV0n8E1zsv9Of42RTdT35g3bQ8cPkbNKi4bY zIDKZM/IQYcn4P1zMB9l1wAE56wVfz2XTQMsdL75KSgwRwW6427WjKHDIaztuFVaBY3Aks +SidCDB9sqSh4Uu6eS0bORTL5NFNpzUSHuDVqVFRptZZGGFDU4VSYPNWCZpj+A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4RYQQg4szJz3Lc; Sun, 27 Aug 2023 07:33:15 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 37R7XFea096016; Sun, 27 Aug 2023 07:33:15 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 37R7XFv1096013; Sun, 27 Aug 2023 07:33:15 GMT (envelope-from git) Date: Sun, 27 Aug 2023 07:33:15 GMT Message-Id: <202308270733.37R7XFv1096013@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dmitry Chagin Subject: git: 0234b6d17f50 - stable/13 - tests: Add stack grows tests List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: dchagin X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 0234b6d17f50c5c3e89e18e1af778548201ec465 Auto-Submitted: auto-generated The branch stable/13 has been updated by dchagin: URL: https://cgit.FreeBSD.org/src/commit/?id=0234b6d17f50c5c3e89e18e1af778548201ec465 commit 0234b6d17f50c5c3e89e18e1af778548201ec465 Author: Dmitry Chagin AuthorDate: 2023-08-08 15:12:09 +0000 Commit: Dmitry Chagin CommitDate: 2023-08-27 07:32:14 +0000 tests: Add stack grows tests Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D41320 MFC after: 2 weeks (cherry picked from commit 8920c5f2a1175c22631780bd236026d15e2d3d72) --- etc/mtree/BSD.tests.dist | 2 + tests/sys/vm/Makefile | 5 +++ tests/sys/vm/soxstack/Makefile | 17 +++++++ tests/sys/vm/soxstack/soxstack.c | 51 +++++++++++++++++++++ tests/sys/vm/stack/Makefile | 15 +++++++ tests/sys/vm/stack/stack_dlopen_exec_test.c | 64 +++++++++++++++++++++++++++ tests/sys/vm/stack/stack_dt_need_exec_test.c | 50 +++++++++++++++++++++ tests/sys/vm/stack/stack_mprotect_exec_test.c | 48 ++++++++++++++++++++ 8 files changed, 252 insertions(+) diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist index 9d3d0e99dd7f..fc04b5b02d23 100644 --- a/etc/mtree/BSD.tests.dist +++ b/etc/mtree/BSD.tests.dist @@ -878,6 +878,8 @@ vfs .. vm + stack + .. .. vmm .. diff --git a/tests/sys/vm/Makefile b/tests/sys/vm/Makefile index ccae71c72932..98e233ce4370 100644 --- a/tests/sys/vm/Makefile +++ b/tests/sys/vm/Makefile @@ -17,4 +17,9 @@ ATF_TESTS_SH+= mmap_map_32bit_test PROGS+= mmap_map_32bit_helper .endif +SUBDIR= soxstack +TESTS_SUBDIRS+= stack + +SUBDIR_DEPENDS_stack=soxstack + .include diff --git a/tests/sys/vm/soxstack/Makefile b/tests/sys/vm/soxstack/Makefile new file mode 100644 index 000000000000..70a22fd278b1 --- /dev/null +++ b/tests/sys/vm/soxstack/Makefile @@ -0,0 +1,17 @@ +# $FreeBSD$ + +SHLIB= soxstack +SHLIB_NAME= libsoxstack.so +SHLIB_MAJOR= 1 + +WITHOUT_STATIC= +WITHOUT_PROFILE= +WITHOUT_PIC= + +SRCS= soxstack.c +LDFLAGS+= -Wl,-z,execstack +LIBADD+= procstat + +LIBDIR= ${TESTSBASE}/sys/vm/stack + +.include diff --git a/tests/sys/vm/soxstack/soxstack.c b/tests/sys/vm/soxstack/soxstack.c new file mode 100644 index 000000000000..fe3eb1c03a29 --- /dev/null +++ b/tests/sys/vm/soxstack/soxstack.c @@ -0,0 +1,51 @@ +/*- + * Copyright (c) 2023 Dmitry Chagin + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +#include +#include +#include +#include +#include + +int checkstack(void); + +#define _STACK_FLAG_GROWS KVME_FLAG_GROWS_UP | KVME_FLAG_GROWS_DOWN +int +checkstack(void) +{ + struct kinfo_vmentry *freep, *kve; + struct kinfo_proc *p; + struct procstat *prstat; + uint64_t stack; + int i, cnt; + + prstat = procstat_open_sysctl(); + assert(prstat != NULL); + p = procstat_getprocs(prstat, KERN_PROC_PID, getpid(), &cnt); + assert(p != NULL); + freep = procstat_getvmmap(prstat, p, &cnt); + assert(freep != NULL); + + stack = (uint64_t)&i; + for (i = 0; i < cnt; i++) { + kve = &freep[i]; + if (stack < kve->kve_start || stack > kve->kve_end) + continue; + if ((kve->kve_flags & _STACK_FLAG_GROWS) != 0 && + (kve->kve_protection & KVME_PROT_EXEC) != 0) + stack = 0; + break; + } + + free(freep); + procstat_freeprocs(prstat, p); + procstat_close(prstat); + return (stack != 0); +} diff --git a/tests/sys/vm/stack/Makefile b/tests/sys/vm/stack/Makefile new file mode 100644 index 000000000000..2d56a6231e24 --- /dev/null +++ b/tests/sys/vm/stack/Makefile @@ -0,0 +1,15 @@ +# $FreeBSD$ + +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 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 + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +#include +#include + +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 + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +#include + +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..d12d99ea39ee --- /dev/null +++ b/tests/sys/vm/stack/stack_mprotect_exec_test.c @@ -0,0 +1,48 @@ +/*- + * Copyright (c) 2023 Dmitry Chagin + * + * SPDX-License-Identifier: BSD-2-Clause + * + * PR: 272585 + * Test provided by John F. Carr + */ + +#include +#include +#include + +#include +#include + + +ATF_TC_WITHOUT_HEAD(mprotect_exec_test); +ATF_TC_BODY(mprotect_exec_test, tc) +{ + long pagesize; + char *addr, *guard; + size_t alloc_size; + + 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()); +}