Project

General

Profile

Actions

Bug #9055

closed
VJ VJ

lua/base64: decode truncates binary output at the first NUL (lua_pushstring on binary data)

Bug #9055: lua/base64: decode truncates binary output at the first NUL (lua_pushstring on binary data)

Added by Victor Julien 2 months ago. Updated 8 days ago.

Status:
Closed
Priority:
Normal
Assignee:
Target version:
Affected Versions:
Effort:
Difficulty:
Label:

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

lua_pushlstring(L, (const char *)output, n);

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.


Subtasks 1 (0 open — 1 closed)

Bug #9305: lua/base64: decode truncates binary output at the first NUL (lua_pushstring on binary data) (8.0.x backport)ClosedVictor JulienActions

VJ Updated by Victor Julien about 2 months ago Actions #1

  • Status changed from New to In Progress
  • Assignee changed from OISF Dev to Victor Julien

JI Updated by Jason Ish about 1 month ago Actions #2

  • Status changed from In Progress to In Review

MR1382

JI Updated by Jason Ish 27 days ago Actions #3

  • Subtask #9305 added

JI Updated by Jason Ish 27 days ago Actions #4

  • Severity set to LOW

JI Updated by Jason Ish 27 days ago Actions #5

  • Tracker changed from Security to Bug
  • Severity deleted (LOW)

JI Updated by Jason Ish 26 days ago Actions #6

  • Status changed from In Review to Closed

JI Updated by Jason Ish 8 days ago Actions #7

  • Private changed from Yes to No
Actions

Also available in: PDF Atom