Skip to content

Commit 9475bcb

Browse files
committed
Avoid large eval inputs in fuzzer
While we limit the size of the main compilation input, the size of eval inputs was not limited. This could result in stack overflows, e.g. oss-fuzz #25464.
1 parent f5dbebd commit 9475bcb

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

sapi/fuzzer/fuzzer-execute.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
#include "fuzzer-sapi.h"
2121

2222
#define MAX_STEPS 1000
23+
#define MAX_SIZE (16 * 1024)
2324
static uint32_t steps_left;
2425

2526
/* Because the fuzzer is always compiled with clang,
2627
* we can assume that we don't use global registers / hybrid VM. */
2728
typedef int (ZEND_FASTCALL *opcode_handler_t)(zend_execute_data *);
2829

29-
void fuzzer_execute_ex(zend_execute_data *execute_data) {
30+
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
3031
while (1) {
3132
int ret;
3233
if (--steps_left == 0) {
@@ -46,8 +47,19 @@ void fuzzer_execute_ex(zend_execute_data *execute_data) {
4647
}
4748
}
4849

50+
static zend_op_array *(*orig_compile_string)(zend_string *source_string, const char *filename);
51+
52+
static zend_op_array *fuzzer_compile_string(zend_string *str, const char *filename) {
53+
if (ZSTR_LEN(str) > MAX_SIZE) {
54+
/* Avoid compiling huge inputs via eval(). */
55+
zend_bailout();
56+
}
57+
58+
return orig_compile_string(str, filename);
59+
}
60+
4961
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
50-
if (Size > 16 * 1024) {
62+
if (Size > MAX_SIZE) {
5163
/* Large inputs have a large impact on fuzzer performance,
5264
* but are unlikely to be necessary to reach new codepaths. */
5365
return 0;
@@ -68,7 +80,10 @@ int LLVMFuzzerInitialize(int *argc, char ***argv) {
6880
signal(SIGPIPE, SIG_IGN);
6981

7082
fuzzer_init_php();
83+
7184
zend_execute_ex = fuzzer_execute_ex;
85+
orig_compile_string = zend_compile_string;
86+
zend_compile_string = fuzzer_compile_string;
7287

7388
/* fuzzer_shutdown_php(); */
7489
return 0;

0 commit comments

Comments
 (0)