-
Notifications
You must be signed in to change notification settings - Fork 74
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
NULL pointer derefence and assertion failure with invalid inputs #36
Comments
Thanks for reporting these issues. I have read about these kind of tools, but never have found the time to set them up for charls. JpegLsDecode should of course remain stable for invalid encoded data. |
I am trying to setup, american fuzzy lop, to be able to validate the fixes I am planing to make. The documentation of american fuzzy lop describes something of a minimum input file. Are you using a special file to start the fuzzy verification process? |
For the fuzzing results above I used all the sample files that come with charls and asked To produce a minimum input file, I changed my #include "src/charls.h"
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
static char result[1024 * 1024];
static char input[1024 * 1024];
static void generate_once(size_t *bytesWritten)
{
JlsParameters params;
memset(&input[0], 0, sizeof(input));
memset(¶ms, 0, sizeof(params));
params.width = 1;
params.height = 1;
params.bitsPerSample = 8;
params.stride = 0;
params.components = 3;
CharlsApiResultType i = JpegLsEncode(result, sizeof(result), bytesWritten, input, sizeof(input), ¶ms, nullptr);
if (i != charls::ApiResult::OK)
fprintf(stderr, "Error while encoding: %d\n", static_cast<int>(i));
i = JpegLsDecode(input, sizeof(input), result, *bytesWritten, nullptr, nullptr);
if (i != charls::ApiResult::OK)
fprintf(stderr, "Error while decoding: %d\n", static_cast<int>(i));
}
int main(int argc, char *argv[])
{
size_t bytesWritten;
int fd = 0;
if (argc == 2)
{
if (argv[1][0] == '\0') {
// Write some small-ish JPEG-LS file to stdout
generate_once(&bytesWritten);
write(1, result, bytesWritten);
return 0;
}
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Failed to open '%s': %s\n",
argv[1], strerror(errno));
return 1;
}
}
generate_once(&bytesWritten);
__AFL_INIT();
while (__AFL_LOOP(100))
{
memset(&input[0], 0, sizeof(input));
size_t input_length = read(fd, input, sizeof(input));
JpegLsDecode(result, sizeof(result), input, input_length, nullptr, nullptr);
}
return 0;
} When called with an empty argument, this writes a small JPEG-LS file to stdout. (It might make sense to also produce some input files with different parameters; dunno how much of a difference this really makes to charls; perhaps feeding afl with files with different interleave modes makes sense?)
Another (unrelated) change compared to my original program is the use of the |
The NEAR parameter needs to be in the range [0, min(255, max-sample-value)]. This needs to be verified to ensure other pre-conditions are not broken. Note: found with the american fuzzy lop tool
The NEAR parameter needs to be in the range [0, min(255, max-sample-value)]. This needs to be verified to ensure other pre-conditions are not broken. Note: found with the american fuzzy lop tool
To make it possible to use the american fuzzy lop (afl) fuzzing framework, add a test application that can be used to fuzz the decoding process.
It is not valid if a Start Of Scan (SOS) marker is found before a Start of Frame (SOF) Rename error code 16 to make it possible to use the error in more places.
A valid JPEG-LS stream can have only 1 Start of Frame (SOF) marker segment. Add a unit test + fix to ensure duplicate sofs are detected.
With 6a49aea in place, american fuzzy lop 2.59d doesn't find any crashes (8 hours running) |
Hi,
I just started experimenting with american fuzzy lop and picked charls as my target. I wrote the following code to feed data to CharLS:
Within seconds, afl managed to crash this program.
Valgrind result for first file https://filebin.ca/3j4hDmAVjSvI:
The above seems to be
JlsCodecFactory::CreateCodec
returning a nullptr, which is something thatJpegStreamReader::Read
does not check for before dereferencingqcodec
in line 124.Plus, there are lots (currently 9) of files which cause the following assertion failure, one of them is https://filebin.ca/3j4hfYzIdJMm:
Edit: Here is another failed assertion:
https://filebin.ca/3j4yTkNxVuQb
Edit: https://filebin.ca/3j57Vrm3jPkZ
Edit: The above as a check-list since I see that some of these were already fixed (by the way, assuming I find more such "evil inputs", should I open one issue per file or continue grouping them like this? Should I open a new issue or keep updating this one?):
The text was updated successfully, but these errors were encountered: