Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PHAR fuzzer #5424

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions sapi/fuzzer/Makefile.frag
Expand Up @@ -16,3 +16,6 @@ $(SAPI_FUZZER_PATH)/php-fuzz-exif: $(PHP_GLOBAL_OBJS) $(PHP_SAPI_OBJS) $(PHP_FUZ

$(SAPI_FUZZER_PATH)/php-fuzz-mbstring: $(PHP_GLOBAL_OBJS) $(PHP_SAPI_OBJS) $(PHP_FUZZER_MBSTRING_OBJS)
$(FUZZER_BUILD) $(PHP_FUZZER_MBSTRING_OBJS) -o $@

$(SAPI_FUZZER_PATH)/php-fuzz-phar: $(PHP_GLOBAL_OBJS) $(PHP_SAPI_OBJS) $(PHP_FUZZER_PHAR_OBJS)
$(FUZZER_BUILD) $(PHP_FUZZER_PHAR_OBJS) -o $@
3 changes: 3 additions & 0 deletions sapi/fuzzer/config.m4
Expand Up @@ -87,6 +87,9 @@ if test "$PHP_FUZZER" != "no"; then
if test -n "$enable_mbstring" && test "$enable_mbstring" != "no"; then
PHP_FUZZER_TARGET([mbstring], PHP_FUZZER_MBSTRING_OBJS)
fi
if test -n "$enable_phar" && test "$enable_phar" != "no"; then
PHP_FUZZER_TARGET([phar], PHP_FUZZER_PHAR_OBJS)
fi

PHP_SUBST(PHP_FUZZER_BINARIES)
fi
Expand Down
Binary file added sapi/fuzzer/corpus/phar/72321_1.zip
Binary file not shown.
Binary file added sapi/fuzzer/corpus/phar/72321_2.zip
Binary file not shown.
Binary file added sapi/fuzzer/corpus/phar/bug69324.phar
Binary file not shown.
Binary file added sapi/fuzzer/corpus/phar/bug69441.phar
Binary file not shown.
Binary file added sapi/fuzzer/corpus/phar/bug69453.tar.phar
Binary file not shown.
Binary file added sapi/fuzzer/corpus/phar/bug69720.phar
Binary file not shown.
Binary file added sapi/fuzzer/corpus/phar/bug69958.tar
Binary file not shown.
Binary file added sapi/fuzzer/corpus/phar/bug70019.zip
Binary file not shown.
Binary file added sapi/fuzzer/corpus/phar/bug70433.zip
Binary file not shown.
Binary file added sapi/fuzzer/corpus/phar/bug71331.tar
Binary file not shown.
Binary file added sapi/fuzzer/corpus/phar/bug71354.tar
Binary file not shown.
Binary file added sapi/fuzzer/corpus/phar/bug71391.tar
Binary file not shown.
Binary file added sapi/fuzzer/corpus/phar/bug71488.tar
Binary file not shown.
Binary file added sapi/fuzzer/corpus/phar/bug71498.zip
Binary file not shown.
Binary file added sapi/fuzzer/corpus/phar/bug72928.zip
Binary file not shown.
Binary file added sapi/fuzzer/corpus/phar/bug73035.tar
Binary file not shown.
Binary file added sapi/fuzzer/corpus/phar/bug73764.phar
Binary file not shown.
Binary file added sapi/fuzzer/corpus/phar/bug73768.phar
Binary file not shown.
Binary file added sapi/fuzzer/corpus/phar/bug77143.phar
Binary file not shown.
77 changes: 77 additions & 0 deletions sapi/fuzzer/fuzzer-phar.c
@@ -0,0 +1,77 @@
/*
+----------------------------------------------------------------------+
| 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: |
| http://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: Stanislav Malyshev <stas@php.net> |
+----------------------------------------------------------------------+
*/

#include "fuzzer.h"

#include "Zend/zend.h"
#include "main/php_config.h"
#include "main/php_main.h"

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "fuzzer-sapi.h"

int phar_create_or_parse_filename(char *fname, size_t fname_len, char *alias, size_t alias_len, zend_bool is_data, uint32_t options, void** pphar, char **error);
void phar_request_initialize(void);

int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
void *phar_data;
char *tmp_filename;
int tmpfd;

if (Size > 256 * 1024) {
/* Large inputs have a large impact on fuzzer performance,
* but are unlikely to be necessary to reach new codepaths. */
return 0;
}

if (fuzzer_request_startup() == FAILURE) {
return 0;
}
phar_request_initialize();

tmp_filename = estrdup("/tmp/phar-fuzz.temp.XXXXXX");
tmpfd = mkstemp(tmp_filename);
write(tmpfd, Data, Size);
close(tmpfd);

zend_first_try {
phar_create_or_parse_filename(tmp_filename, strlen(tmp_filename), NULL, 0, 1, REPORT_ERRORS, &phar_data, NULL);
} zend_end_try();

/* cleanup */
unlink(tmp_filename);
efree(tmp_filename);
php_request_shutdown(NULL);

return 0;
}

int LLVMFuzzerInitialize(int *argc, char ***argv) {
/* Gracefully handle bailouts. */
putenv("USE_TRACKED_ALLOC=1");

fuzzer_init_php();

/* fuzzer_shutdown_php(); */
return 0;
}