Bug #9055
closedlua/base64: decode truncates binary output at the first NUL (lua_pushstring on binary data)
Description
- Status: present in
main(9b7938aec); code-confirmed - Class: silent data corruption (binary data truncated at first NUL)
- Component:
src/util-lua-base64lib.c(LuaBase64Decode)
Description¶
LuaBase64Decode() decodes into a heap buffer and knows the true decoded length n, but returns the result with C-string semantics (src/util-lua-base64lib.c:69):
uint32_t n = SCBase64Decode((uint8_t *)input, (uintptr_t)input_len, mode, (uint8_t *)output);
if (n == 0) { ... error ... }
DEBUG_VALIDATE_BUG_ON(n > input_len);
output[n] = '\0';
lua_pushstring(L, (const char *)output); /* truncates at the first NUL */
Any decoded NUL byte truncates the Lua string, so binary payloads cannot round-trip:
base64.decode("AA==")returns""instead of one"\0"byte;base64.decode("Zm9vAGJhcg==")returns"foo"instead of the 7-byte"foo\0bar".
The same applies to decode_nopad, decode_padopt, decode_rfc2045 and decode_rfc4648. Reachable from rule, transform and output scripts; the encoded input can be packet-derived, so script-side allowlists/comparisons on decoded data can be defeated.
(The n > input_len guard/direction is a separate defense-in-depth nit: n input_len is provably impossible for all five modes, so the write is in-bounds today.)
Fix direction¶
Consider also returning the decoded length to the script (useful and makes the binary semantics explicit).
Tests¶
lua-base64-binary: decode("AA") has length 1 and byte 1 0; interior-NUL vector "Zm9vAGJhcg" returns 7 bytes; repeat for one no-pad and one RFC mode; encode/decode round-trip of a buffer with NULs.