Prevent UB on signed overflow.
commit17458a87d73f605fe6a4bb3c23cf2de135eec99c
authorTobias Stoeckmann <tobias@stoeckmann.org>
Mon, 24 Jun 2019 20:08:49 +0000 (24 22:08 +0200)
committerNick Mathewson <nickm@torproject.org>
Fri, 19 Jul 2019 13:21:32 +0000 (19 09:21 -0400)
tree4c681ff86f7fbde52699596237be1c0e47eee4f1
parent1dd95278970f9f32d83a31fe73e0258a30523539
Prevent UB on signed overflow.

Overflowing a signed integer in C is an undefined behaviour.
It is possible to trigger this undefined behaviour in tor_asprintf on
Windows or systems lacking vasprintf.

On these systems, eiter _vscprintf or vsnprintf is called to retrieve
the required amount of bytes to hold the string. These functions can
return INT_MAX. The easiest way to recreate this is the use of a
specially crafted configuration file, e.g. containing the line:

FirewallPorts AAAAA<in total 2147483610 As>

This line triggers the needed tor_asprintf call which eventually
leads to an INT_MAX return value from _vscprintf or vsnprintf.

The needed byte for \0 is added to the result, triggering the
overflow and therefore the undefined behaviour.

Casting the value to size_t before addition fixes the behaviour.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
src/lib/string/printf.c