
Podman lab and PoC reproducing the libwebsockets MQTT PUBACK out-of-bounds write (ZDI-CAN-31039), with a malicious broker, ASan test matrix, and heap unlink/control-flow hijack primitives.
ZDI-CAN-31039, ZDI-26-792, CWE-787, CVSS 8.1
(AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:H). Fixed upstream by
c9e8e1834d7f13038c82963086b90e2ee9c5b44c ("ZDI-CAN-31039: enter wrong
state").
Podman lab that reproduces the bug end to end and shows what the corruption buys: an attacker-run MQTT broker rewrites list pointers and a function-pointer table inside a libwebsockets MQTT client using nothing but PUBACK packets. No client-side misconfiguration needed; whoever runs the broker owns the parse (that's the "authentication is not required" in the advisory).
d0d1c796 (parent of the fix)c9e8e1834 (merged 2026-07-21), v5.0.0 and laterOnly the MQTT client role is affected (-DLWS_ROLE_MQTT=1 or
secure-streams MQTT). libwebsockets has no MQTT server listener, so the
target is the client: IoT stacks connecting out to a broker.
In _lws_mqtt_rx_parser() (lib/roles/mqtt/mqtt.c), when the
properties-length VBI of a PUBACK completes with a non-zero value, the
parser enters the wrong state:
case LMQCPP_PUBACK_PROPERTIES_LEN_VBI:
...
par->props_len = par->vbit.value;
if (!par->props_len)
goto cmd_completion;
lws_mqtt_vbi_init(&par->vbit);
par->state = LMQCPP_PUBACK_VH_PKT_ID; /* WRONG */
The fix changes that last line to
par->state = LMQCPP_EAT_PROPERTIES_AND_COMPLETE;.
par->fixed (the fixed_seen[] slot counter, still 3) is not reset, so
the re-entered state keeps executing
par->fixed_seen[par->fixed++] = *buf++;: one attacker-controlled byte
per received byte, sequentially past fixed_seen[4] into the rest of
struct lws_mqtt_parser_t and then into the enclosing
struct _lws_mqtt_related heap chunk (lws_zalloc(sizeof(*wsi->mqtt))).
Because fixed_seen[] sits mid-struct, the walk overwrites the parser's
own fields. At offsets 16-19 (rel. fixed_seen) sits cpkt_remlen,
which decides the completion check remlen == fixed; at 44-47 sits n,
operand of the bounds check len < par->cpkt_remlen - par->n; at 84
sits fixed, the counter itself. The payload generator in
evil_broker.py mirrors the vulnerable parser byte for byte and steers
these fields: cpkt_remlen=256 never equals the wrapping uint8_t
counter (no spurious completion) and n=0 keeps the bounds check
satisfied, so the write sustains itself for as many bytes as the broker
feels like sending. The uint8_t counter confines the walk to a 256-byte
window sweeping chunk bytes 32-287, which covers the parser
bookkeeping, the lws_mqttc_t tail (heap pointers
id/username/password/will.*, and the qos2_state_ops
function-pointer table on post-v4.5.8 trees), and the first
lws_sorted_usec_list_t timer nodes.
Primitives this gives, and what the PoC does with them:
sul_qos_puback_pubrec_wait.dll2.prev/next: teardown calls
lws_dll2_remove() and executes *(prev+8) = next; *(next) = prev
with attacker values, the usual unlink write-what-whereqos2_state_ops: a follow-up QoS2 PUBLISH makes lws call
qos2_state_ops->rx_add(wsi, cid, pkt_id), an indirect call through
attacker memory.cb pointers (not demonstrated
here)To keep the connection alive across two stages the broker reads the victim's QoS1 publish packet-id off the wire and puts it in the crafted PUBACK, so the parse completes through the normal ACK path instead of hitting the "unsolicited PUBACK" close.
lab/Containerfile self-contained build, clones upstream at pinned commits
lab/build.sh builds vuln/fixed/release trees (ASan and plain)
lab/offsets_probe.c offsetof() map, compiled with each tree's CC flags
lab/victim_client.c lws MQTT client victim (QoS1 publish + subscribe)
lab/evil_broker.py malicious broker + FSM-mirror payload generator
lab/run_poc.sh 6-test matrix with PASS/FAIL verdicts
Makefile podman build/run (--cpus 2 --memory 4g)
make poc
# or manually:
podman build -t cve-2026-114550:lab lab/
podman run --rm --cpus 2 --memory 4g --network none -v $PWD/output:/output \
cve-2026-114550:lab
| test | victim build | broker mode | expected |
|---|---|---|---|
| T1 benign control | vuln+ASan | props_len=0 | clean close |
| T2 OOB write / unlink | vuln+ASan | poison dll2.prev/next = 0x4141414141414141 | teardown deref of attacker pointer |
| T3 A/B fixed control | fix commit+ASan | same as T2 | clean close |
| T4 control-flow hijack | vuln+ASan+NDEBUG | poison qos2_state_ops = 0x0000424242424242 + QoS2 PUBLISH | SEGV at ->rx_add deref (mqtt.c:952) |
| T5 natural crash | vuln, NDEBUG, no ASan | poison client.id = 0x4141414141414141 | SIGSEGV core dump |
| T6 affected release | v4.5.8+ASan | same as T2 | attacker pointer in list node |
Results and log excerpts: RESULTS.md. ASan does not see the writes themselves (they stay inside one allocation), so the evidence is the dereference of attacker values instead of a redzone hit.
P1 is a write-what-where and P2 is a call with argument registers chosen by lws. Turning that into code execution needs a heap leak/groom and no ASan; the walk is deterministic, so the payload only needs the victim build's offsets, which the probe prints per build.
LMQCPP_PROP_ID_VBI off-by-one: if (vbit.value > LWS_ARRAY_SIZE(property_valid)) accepts prop_id == ARRAY_SIZE,
reading one element past property_valid[] and reaching a property
state it should not. Low impact, but a plain bounds bug (present in
v4.5.8; check current main).qos2_rx_list grows without a cap: every unique QoS2 PUBLISH
packet-id allocates a lws_mqtt_qos2_rx_t freed only on PUBCOMP or
close. A broker can grow client memory at will.pub->payload_len is the declared length (cpkt_remlen - overhead),
never clamped to delivered bytes. The shipped minimal-mqtt-client
hexdumps payload_len and over-reads; a declared-huge payload also
swallows subsequent packets (parser stuck in LMQCPP_PAYLOAD).LMQCPP_SUBACK_VH_PKT_ID / LMQCPP_UNSUBACK_VH_PKT_ID return -1
(drop the connection) when fewer than 2 bytes are buffered, so those
packet types are not fragmentation-tolerant despite the comment above
the parser.python3 exploit/exploit.py runs the full chain (exit 1 exploited, 0 safe).