1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Johannes Schlüter <johanes@php.net> |
| Stanislav Malyshev <stas@php.net> |
+----------------------------------------------------------------------+
*/
#include <main/php.h>
#include <main/php_main.h>
#include <main/SAPI.h>
#include <ext/standard/info.h>
#include <ext/standard/php_var.h>
#include <main/php_variables.h>
#include <zend_exceptions.h>
#ifdef __SANITIZE_ADDRESS__
# include "sanitizer/lsan_interface.h"
#endif
#include "fuzzer.h"
#include "fuzzer-sapi.h"
static const char HARDCODED_INI[] =
"html_errors=0\n"
"implicit_flush=1\n"
"output_buffering=0\n"
"error_reporting=0\n"
/* Let the timeout be enforced by libfuzzer, not PHP. */
"max_execution_time=0\n"
/* Reduce oniguruma limits to speed up fuzzing */
"mbstring.regex_stack_limit=10000\n"
"mbstring.regex_retry_limit=10000\n"
/* For the "execute" fuzzer disable some functions that are likely to have
* undesirable consequences (shell execution, file system writes). */
"allow_url_include=0\n"
"allow_url_fopen=0\n"
"open_basedir=/tmp\n"
"disable_functions=dl,mail,mb_send_mail"
",shell_exec,exec,system,proc_open,popen,passthru,pcntl_exec"
",chdir,chgrp,chmod,chown,copy,file_put_contents,lchgrp,lchown,link,mkdir"
",move_uploaded_file,rename,rmdir,symlink,tempname,touch,unlink,fopen"
/* Networking code likes to wait and wait. */
",fsockopen,pfsockopen"
",stream_socket_pair,stream_socket_client,stream_socket_server"
/* crypt() can be very slow. */
",crypt"
/* openlog() has a known memory-management issue. */
",openlog"
/* Can cause long loops that bypass the executor step limit. */
"\ndisable_classes=InfiniteIterator"
;
static int startup(sapi_module_struct *sapi_module)
{
return php_module_startup(sapi_module, NULL);
}
static size_t ub_write(const char *str, size_t str_length)
{
/* quiet */
return str_length;
}
static void fuzzer_flush(void *server_context)
{
/* quiet */
}
static void send_header(sapi_header_struct *sapi_header, void *server_context)
{
}
static char* read_cookies(void)
{
/* TODO: fuzz these! */
return NULL;
}
static void register_variables(zval *track_vars_array)
{
php_import_environment_variables(track_vars_array);
}
static void log_message(const char *message, int level)
{
}
static sapi_module_struct fuzzer_module = {
"fuzzer", /* name */
"clang fuzzer", /* pretty name */
startup, /* startup */
php_module_shutdown_wrapper, /* shutdown */
NULL, /* activate */
NULL, /* deactivate */
ub_write, /* unbuffered write */
fuzzer_flush, /* flush */
NULL, /* get uid */
NULL, /* getenv */
php_error, /* error handler */
NULL, /* header handler */
NULL, /* send headers handler */
send_header, /* send header handler */
NULL, /* read POST data */
read_cookies, /* read Cookies */
register_variables, /* register server variables */
log_message, /* Log message */
NULL, /* Get request time */
NULL, /* Child terminate */
STANDARD_SAPI_MODULE_PROPERTIES
};
int fuzzer_init_php(const char *extra_ini)
{
#ifdef __SANITIZE_ADDRESS__
/* We're going to leak all the memory allocated during startup,
* so disable lsan temporarily. */
__lsan_disable();
#endif
sapi_startup(&fuzzer_module);
fuzzer_module.phpinfo_as_text = 1;
size_t ini_len = sizeof(HARDCODED_INI);
size_t extra_ini_len = extra_ini ? strlen(extra_ini) : 0;
if (extra_ini) {
ini_len += extra_ini_len + 1;
}
char *p = malloc(ini_len + 1);
fuzzer_module.ini_entries = p;
p = zend_mempcpy(p, HARDCODED_INI, sizeof(HARDCODED_INI) - 1);
if (extra_ini) {
*p++ = '\n';
p = zend_mempcpy(p, extra_ini, extra_ini_len);
}
*p = '\0';
/*
* TODO: we might want to test both Zend and malloc MM, but testing with malloc
* is more likely to find bugs, so use that for now.
*/
putenv("USE_ZEND_ALLOC=0");
if (fuzzer_module.startup(&fuzzer_module)==FAILURE) {
return FAILURE;
}
#ifdef __SANITIZE_ADDRESS__
__lsan_enable();
#endif
return SUCCESS;
}
int fuzzer_request_startup(void)
{
if (php_request_startup() == FAILURE) {
php_module_shutdown();
return FAILURE;
}
#ifdef ZEND_SIGNALS
/* Some signal handlers will be overridden,
* don't complain about them during shutdown. */
SIGG(check) = 0;
#endif
return SUCCESS;
}
void fuzzer_request_shutdown(void)
{
zend_try {
/* Destroy thrown exceptions. This does not happen as part of request shutdown. */
if (EG(exception)) {
zend_object_release(EG(exception));
EG(exception) = NULL;
}
/* Some fuzzers (like unserialize) may create circular structures. Make sure we free them.
* Two calls are performed to handle objects with destructors. */
zend_gc_collect_cycles();
zend_gc_collect_cycles();
} zend_end_try();
php_request_shutdown(NULL);
}
/* Set up a dummy stack frame so that exceptions may be thrown. */
void fuzzer_setup_dummy_frame(void)
{
static zend_execute_data execute_data;
static zend_function func;
memset(&execute_data, 0, sizeof(zend_execute_data));
memset(&func, 0, sizeof(zend_function));
func.type = ZEND_INTERNAL_FUNCTION;
func.common.function_name = ZSTR_EMPTY_ALLOC();
execute_data.func = &func;
EG(current_execute_data) = &execute_data;
}
void fuzzer_set_ini_file(const char *file)
{
if (fuzzer_module.php_ini_path_override) {
free(fuzzer_module.php_ini_path_override);
}
fuzzer_module.php_ini_path_override = strdup(file);
}
int fuzzer_shutdown_php(void)
{
php_module_shutdown();
sapi_shutdown();
free((void *)fuzzer_module.ini_entries);
return SUCCESS;
}
int fuzzer_do_request_from_buffer(
char *filename, const char *data, size_t data_len, bool execute,
void (*before_shutdown)(void))
{
int retval = FAILURE; /* failure by default */
SG(options) |= SAPI_OPTION_NO_CHDIR;
SG(request_info).argc=0;
SG(request_info).argv=NULL;
if (fuzzer_request_startup() == FAILURE) {
return FAILURE;
}
// Commented out to avoid leaking the header callback.
//SG(headers_sent) = 1;
//SG(request_info).no_headers = 1;
php_register_variable("PHP_SELF", filename, NULL);
zend_first_try {
zend_file_handle file_handle;
zend_stream_init_filename(&file_handle, filename);
file_handle.primary_script = 1;
file_handle.buf = emalloc(data_len + ZEND_MMAP_AHEAD);
memcpy(file_handle.buf, data, data_len);
memset(file_handle.buf + data_len, 0, ZEND_MMAP_AHEAD);
file_handle.len = data_len;
/* Avoid ZEND_HANDLE_FILENAME for opcache. */
file_handle.type = ZEND_HANDLE_STREAM;
zend_op_array *op_array = zend_compile_file(&file_handle, ZEND_REQUIRE);
zend_destroy_file_handle(&file_handle);
if (op_array) {
if (execute) {
zend_execute(op_array, NULL);
}
zend_destroy_static_vars(op_array);
destroy_op_array(op_array);
efree(op_array);
}
} zend_end_try();
CG(compiled_filename) = NULL; /* ??? */
if (before_shutdown) {
zend_try {
before_shutdown();
} zend_end_try();
}
fuzzer_request_shutdown();
return (retval == SUCCESS) ? SUCCESS : FAILURE;
}
// Call named PHP function with N zval arguments
void fuzzer_call_php_func_zval(const char *func_name, int nargs, zval *args) {
zval retval, func;
ZVAL_STRING(&func, func_name);
ZVAL_UNDEF(&retval);
call_user_function(CG(function_table), NULL, &func, &retval, nargs, args);
// TODO: check result?
/* to ensure retval is not broken */
php_var_dump(&retval, 0);
/* cleanup */
zval_ptr_dtor(&retval);
zval_ptr_dtor(&func);
}
// Call named PHP function with N string arguments
void fuzzer_call_php_func(const char *func_name, int nargs, char **params) {
zval args[nargs];
int i;
for(i=0;i<nargs;i++) {
ZVAL_STRING(&args[i], params[i]);
}
fuzzer_call_php_func_zval(func_name, nargs, args);
for(i=0;i<nargs;i++) {
zval_ptr_dtor(&args[i]);
ZVAL_UNDEF(&args[i]);
}
}
|