Skip to content

Add buffer with args fuzzer #4103

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

Merged
merged 6 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
39 changes: 39 additions & 0 deletions fuzz/common_fuzzer_corpus/generic_buffer_with_args_pdf_to_png
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[n=-1,page=0,dpi=72,scale=0.1,password=secret]
.png[filter=none]
%PDF-1.1
%âãÏÓ
1 0 obj
<<
/Pages 2 0 R
/Type /Catalog
>>
endobj
2 0 obj
<<
/MediaBox [0 0 595 842]
/Kids [3 0 R]
/Count 1
/Type /Pages
>>
endobj
3 0 obj
<<
/Parent 2 0 R
/MediaBox [0 0 595 842]
/Type /Page
>>
endobj xref
0 4
0000000000 65535 f
0000000015 00000 n
0000000066 00000 n
0000000149 00000 n
trailer

<<
/Root 1 0 R
/Size 4
>>
startxref
221
%%EOF
Binary file not shown.
81 changes: 81 additions & 0 deletions fuzz/generic_buffer_with_args_fuzzer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <vips/vips.h>

#define MAX_ARG_LEN 4096 // =VIPS_PATH_MAX

extern "C" int
LLVMFuzzerInitialize(int *argc, char ***argv)
{
if (VIPS_INIT(*argv[0]))
return -1;

vips_concurrency_set(1);
return 0;
}

static char *
ExtractLine(const guint8 *data, size_t size, size_t *n)
{
const guint8 *end;

end = static_cast<const guint8 *>(
memchr(data, '\n', VIPS_MIN(size, MAX_ARG_LEN)));
if (end == nullptr)
return nullptr;

*n = end - data;
return g_strndup(reinterpret_cast<const char *>(data), *n);
}

extern "C" int
LLVMFuzzerTestOneInput(const guint8 *data, size_t size)
{
VipsImage *image;
void *buf;
char *option_string, *suffix;
size_t len, n;

option_string = ExtractLine(data, size, &n);
if (option_string == nullptr)
return 0;

data += n + 1;
size -= n + 1;

suffix = ExtractLine(data, size, &n);
if (suffix == nullptr) {
g_free(option_string);
return 0;
}

data += n + 1;
size -= n + 1;

if (!(image = vips_image_new_from_buffer(data, size, option_string, nullptr))) {
g_free(option_string);
g_free(suffix);
return 0;
}

// We're done with option_string, free early.
g_free(option_string);

if (image->Xsize > 100 ||
image->Ysize > 100 ||
image->Bands > 4) {
g_object_unref(image);
g_free(suffix);
return 0;
}

if (vips_image_write_to_buffer(image, suffix, &buf, &len, nullptr)) {
g_object_unref(image);
g_free(suffix);
return 0;
}

g_free(buf);
g_free(suffix);
g_object_unref(image);

return 0;
}
7 changes: 7 additions & 0 deletions fuzz/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ foreach fuzz_basename, fuzz_save_suffix : fuzz_save_buffer_progs
)
endforeach


fuzz_execs += executable('generic_buffer_with_args_fuzzer',
'generic_buffer_with_args_fuzzer.cc',
dependencies: [libvips_dep, fuzz_deps],
link_args: fuzz_ldflags,
)

# If the fuzzing engine is not OSS-Fuzz, build the unit tests to be run on CI
if fuzzing_engine != 'oss-fuzz'
test_fuzz = configure_file(
Expand Down
Loading