BUG/MEDIUM: base64: check output boundaries within base64{dec,urldec}
authorDragan Dosen <ddosen@haproxy.com>
Tue, 24 Aug 2021 07:48:04 +0000 (09:48 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 3 Sep 2021 14:24:50 +0000 (16:24 +0200)
commit3df7633eff8034a12d757357035500faa541b657
tree5111bbdabb68abfa61b027c659221135bc79012c
parent01f6b58a67d9c7e81f91eb6437c4699fb10508b0
BUG/MEDIUM: base64: check output boundaries within base64{dec,urldec}

Ensure that no more than olen bytes is written to the output buffer,
otherwise we might experience an unexpected behavior.

While the original code used to validate that the output size was
always large enough before starting to write, this validation was
later broken by the commit below, allowing to 3-byte blocks to
areas whose size is not multiple of 3:

  commit ed697e4856e5ac0b9931fd50fd8ff1b7739e5d88
  Author: Emeric Brun <ebrun@haproxy.com>
  Date:   Mon Jan 14 14:38:39 2019 +0100

    BUG/MINOR: base64: dec func ignores padding for output size checking

    Decode function returns an error even if the ouptut buffer is
    large enought because the padding was not considered. This
    case was never met with current code base.

For base64urldec(), it's basically the same problem except that since
the input format supports arbitrary lengths, the problem has always
been there since its introduction in 2.4.

This should be backported to all stable branches having a backport of
the patch above (i.e. 2.0), with some adjustments depending on the
availability of the base64dec() and base64urldec().

(cherry picked from commit f3899ddbcbf4c445d4a519fb04cc253e60d30e6e)
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit aec6e0244e8b1693c5f78bb5a0483986ef2ac688)
[wt: dropped base64urldec]
Signed-off-by: Willy Tarreau <w@1wt.eu>
src/base64.c