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

Fix heap-buffer-overflow issue in redisvFormatCommad #1097

Merged
merged 2 commits into from
Sep 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions fuzzing/format_command_fuzzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
memcpy(new_str, data, size);
new_str[size] = '\0';

redisFormatCommand(&cmd, new_str);

if (cmd != NULL)
if (redisFormatCommand(&cmd, new_str) != -1)
hi_free(cmd);

free(new_str);
return 0;
}
5 changes: 5 additions & 0 deletions hiredis.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,11 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {
/* Copy va_list before consuming with va_arg */
va_copy(_cpy,ap);

/* Make sure we have more characters otherwise strchr() accepts
* '\0' as an integer specifier. This is checked after above
* va_copy() to avoid UB in fmt_invalid's call to va_end(). */
if (*_p == '\0') goto fmt_invalid;

/* Integer conversion (without modifiers) */
if (strchr(intfmts,*_p) != NULL) {
va_arg(ap,int);
Expand Down
6 changes: 5 additions & 1 deletion test.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,14 @@ static void test_format_commands(void) {
FLOAT_WIDTH_TEST(float);
FLOAT_WIDTH_TEST(double);

test("Format command with invalid printf format: ");
test("Format command with unhandled printf format (specifier 'p' not supported): ");
len = redisFormatCommand(&cmd,"key:%08p %b",(void*)1234,"foo",(size_t)3);
test_cond(len == -1);

test("Format command with invalid printf format (specifier missing): ");
len = redisFormatCommand(&cmd,"%-");
test_cond(len == -1);

const char *argv[3];
argv[0] = "SET";
argv[1] = "foo\0xxx";
Expand Down