From e3b6872b51fd093c47e06b336d6c157533519090 Mon Sep 17 00:00:00 2001 From: David CARLIER Date: Mon, 4 Mar 2024 19:06:37 +0000 Subject: [PATCH] zend call stack adjust case for freebsd to calculate the guard size. (#13586) it was not wrong but there is a sysctl oid storing the number of guard pages, which is 1 by default but is modifiable at runtime. --- Zend/zend_call_stack.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Zend/zend_call_stack.c b/Zend/zend_call_stack.c index ad6c1932553..97cbed90414 100644 --- a/Zend/zend_call_stack.c +++ b/Zend/zend_call_stack.c @@ -313,6 +313,7 @@ static bool zend_call_stack_get_freebsd_sysctl(zend_call_stack *stack) int mib[2] = {CTL_KERN, KERN_USRSTACK}; size_t len = sizeof(stack_base); struct rlimit rlim; + size_t numguards = 0; /* This method is relevant only for the main thread */ ZEND_ASSERT(zend_call_stack_is_main_thread()); @@ -329,7 +330,13 @@ static bool zend_call_stack_get_freebsd_sysctl(zend_call_stack *stack) return false; } - size_t guard_size = getpagesize(); + len = sizeof(numguards); + /* For most of the cases, we do not necessarily need to do so as, by default, it is `1` page, but is user writable */ + if (sysctlbyname("security.bsd.stack_guard_page", &numguards, &len, NULL, 0) != 0) { + return false; + } + + size_t guard_size = numguards * getpagesize(); stack->base = stack_base; stack->max_size = rlim.rlim_cur - guard_size;