BUG/MEDIUM: payload: reject out-of-range payload and payload_lv arguments
The offsets and lengths taken by the payload and payload_lv sample fetch
families are parsed as 64-bit ARGT_SINT, but smp_fetch_payload() and
smp_fetch_payload_lv() store them into unsigned ints and perform their
bounds checks in 32 bits:
if (buf_size > max || buf_offset + buf_size > max)
The payload, req.payload and res.payload keywords have no argument
validation at all, and val_payload_lv() only rejects a negative offset1
and a null length. A negative or oversized argument therefore becomes a
value close to 4G once truncated, the sum wraps, passes the checks, and
the fetch either returns a sample pointing about 4 GB past the buffer
(payload) or makes the length loop read past it (payload_lv, where a
negative length turns into a ~4G byte count). Each of these expressions
crashes the process the first time it is evaluated, for example from a
"tcp-request content" rule:
req.payload(-1,1)
req.payload(
4294967295,1)
req.payload_lv(1,-1) (heap-buffer-overflow read)
req.payload_lv(
4294967295,1)
req.payload_lv(0,1,+
4294967290) (once a converter reads the sample)
Lua is affected as well (e.g. txn.f:req_payload(-1,1)), since it relies
on the same validation callbacks. HTX streams are not affected because
the fetches return early there. The tcp-check path performs the same
arithmetic on check->bi.
Such an expression has to be written in the configuration, so this is not
a vulnerability per the threat model, but nothing warns about it, and once
it is present the first connection that reaches the rule crashes the
process.
This patch adds val_payload() for the payload family, which limits both
the offset and the length to [0, INT_MAX] (a null length still means "the
rest of the buffer"). val_payload_lv() now limits offset1 to [0, INT_MAX],
the length to [1, INT_MAX], the raw offset2 to [-2*INT_MAX, INT_MAX], and
the resulting relative offset to [0, INT_MAX]. INT_MAX is far beyond any
buffer size so no working configuration is affected, and with each term
bounded this way the sums computed by the fetch functions can no longer
wrap, so these functions are left untouched. Configurations that used
out-of-range values already crashed or never matched; they now fail to
load with an explicit error instead:
invalid args in fetch method 'req.payload' : payload offset must be
between 0 and
2147483647
Lua calls now raise an error instead of crashing.
This should be backported to all supported versions.