Skip to content

Commit

Permalink
base64: Properly ignore invalid characters
Browse files Browse the repository at this point in the history
Not all invalid characters were ignored by base64 decoder
causing data corruption and reads beyond decode table
(faults under ASAN).

Added corresponding check into base64 unit test.

Fixes: #5627
(cherry picked from commit 726b96f)
  • Loading branch information
Sergey Nikiforov authored and Totktonada committed Dec 30, 2020
1 parent 76f7d3d commit f93e480
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
21 changes: 20 additions & 1 deletion test/unit/base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,26 @@ base64_nowrap_test(const char *str)
base64_test(str, BASE64_NOWRAP, symbols, lengthof(symbols));
}

static void
base64_invalid_chars_test(void)
{
plan(1);

/* Upper bit must be cleared */
const char invalid_data[] = { '\x7b', '\x7c', '\x7d', '\x7e' };
char outbuf[8];

/* Invalid chars should be ignored, not decoded into garbage */
is(base64_decode(invalid_data, sizeof(invalid_data),
outbuf, sizeof(outbuf)),
0, "ignoring invalid chars");

check_plan();
}

int main(int argc, char *argv[])
{
plan(28);
plan(29);
header();

const char *option_tests[] = {
Expand All @@ -78,6 +95,8 @@ int main(int argc, char *argv[])
base64_nowrap_test(option_tests[i]);
}

base64_invalid_chars_test();

footer();
return check_plan();
}
5 changes: 4 additions & 1 deletion test/unit/base64.result
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
1..28
1..29
*** main ***
1..3
ok 1 - length
Expand Down Expand Up @@ -175,4 +175,7 @@ ok 27 - subtests
ok 3 - decode length ok
ok 4 - encode/decode
ok 28 - subtests
1..1
ok 1 - ignoring invalid chars
ok 29 - subtests
*** main: done ***
3 changes: 2 additions & 1 deletion third_party/base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ base64_decode_value(int value)
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49, 50, 51
};
static const int decoding_size = sizeof(decoding);
static const int decoding_size =
sizeof(decoding) / sizeof(decoding[0]);
int codepos = value;
codepos -= 43;
if (codepos < 0 || codepos >= decoding_size)
Expand Down

0 comments on commit f93e480

Please sign in to comment.