1 /*- 2 * Copyright (c) 2023 Dmitry Chagin <dchagin@FreeBSD.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 * 6 * PR: 272585 7 * Test provided by John F. Carr 8 */ 9 10 #include <sys/systm.h> 11 #include <sys/mman.h> 12 #include <vm/vm_param.h> 13 14 #include <atf-c.h> 15 #include <unistd.h> 16 17 18 ATF_TC_WITHOUT_HEAD(mprotect_exec_test); 19 ATF_TC_BODY(mprotect_exec_test, tc) 20 { 21 long pagesize; 22 char *addr, *guard; 23 size_t alloc_size; 24 25 pagesize = sysconf(_SC_PAGESIZE); 26 ATF_REQUIRE(pagesize > 0); 27 28 alloc_size = SGROWSIZ * 2; 29 addr = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, 30 MAP_STACK | MAP_PRIVATE | MAP_ANON, -1, 0); 31 ATF_REQUIRE(addr != MAP_FAILED); 32 33 /* 34 * Change prot of the last page in the mmaped stack area. 35 */ 36 guard = addr + alloc_size - SGROWSIZ; 37 ATF_REQUIRE(mprotect(guard, pagesize, PROT_NONE) == 0); 38 39 ((volatile char *)guard)[-1]; 40 } 41 42 ATF_TP_ADD_TCS(tp) 43 { 44 45 ATF_TP_ADD_TC(tp, mprotect_exec_test); 46 47 return (atf_no_error()); 48 } 49