Add use of stdin to fuzzer_tool_flac, and redirect stdout

This commit is contained in:
Martijn van Beurden 2023-03-18 19:53:00 +01:00
parent 0e1535ccce
commit e683286bb4
2 changed files with 32 additions and 13 deletions

View File

@ -46,8 +46,11 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
char * argv[67];
char exename[] = "flac";
char filename[] = "/tmp/fuzzXXXXXX";
int numarg = 0, maxarg, pad;
int numarg = 0, maxarg;
int file_to_fuzz;
int tmp_stdout, tmp_stdin;
fpos_t pos_stdout;
bool use_stdin = false;
/* reset global vars */
flac__utils_verbosity_ = 0;
@ -59,7 +62,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
return 0;
maxarg = data[0] & 63;
pad = data[0] & 64;
use_stdin = data[0] & 64;
size_left--;
argv[0] = exename;
@ -76,14 +79,38 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
if (file_to_fuzz < 0)
abort();
write(file_to_fuzz,data+(size-size_left),size_left);
if(pad)
write(file_to_fuzz,"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",12);
close(file_to_fuzz);
argv[numarg++] = filename;
/* redirect stdout */
fflush(stdout);
fgetpos(stdout,&pos_stdout);
tmp_stdout = dup(fileno(stdout));
freopen("/dev/null","w",stdout);
/* redirect stdin */
tmp_stdin = dup(fileno(stdin));
if(use_stdin)
freopen(filename,"r",stdin);
else {
freopen("/dev/null","r",stdin);
argv[numarg++] = filename;
}
main_to_fuzz(numarg,argv);
/* restore stdout */
fflush(stdout);
dup2(tmp_stdout, fileno(stdout));
close(tmp_stdout);
clearerr(stdout);
fsetpos(stdout,&pos_stdout);
/* restore stdin */
dup2(tmp_stdin, fileno(stdin));
close(tmp_stdin);
clearerr(stdin);
unlink(filename);
return 0;

View File

@ -1804,13 +1804,11 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
return 1;
}
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
if(0 == strcmp(infilename, "-")) {
infilesize = (FLAC__off_t)(-1);
encode_infile = grabbag__file_get_binary_stdin();
}
else
#endif
{
infilesize = grabbag__file_get_filesize(infilename);
if(0 == (encode_infile = flac_fopen(infilename, "rb"))) {
@ -2363,12 +2361,6 @@ int decode_file(const char *infilename)
decode_options.channel_map_none = option_values.channel_map_none;
decode_options.format = output_format;
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
/* Can't fuzz from stdin */
if(0 == strcmp(infilename, "-") || 0 == strcmp(outfilename, "-"))
return 1;
#endif
if(output_format == FORMAT_RAW) {
decode_options.format_options.raw.is_big_endian = option_values.format_is_big_endian;
decode_options.format_options.raw.is_unsigned_samples = option_values.format_is_unsigned_samples;