Skip to content

Short UDP sync packets can read beyond segment data #5942

Open

Description

@K-ANOY

What happened?

In wled00/udp.cpp, handleNotifications() stores a notifier packet in udpIn[packetSize + 1], then calls parseNotifyPacket(udpIn) without passing the received length. The segment loop takes its segment count from udpIn[39] and stride from udpIn[40]:

unsigned ofs = 41 + i*udpIn[40];
if (ofs + 36 > UDP_IN_MAXSIZE) break;

UDP_IN_MAXSIZE is 1472, the maximum allowed datagram size, not the size of this packet. A short packet declaring a segment can therefore pass the check at ofs = 41 while the parser reads beyond the stack buffer. With version 12 and segment options enabled, it may read through udpIn[ofs+35]; with bounds only and version 11, it reads fewer fields but still crosses the buffer. A larger stride can move later reads farther out. The ESP-NOW caller passes a buffer to the same parser without its assembled length as well.

This path requires receiveSegmentOptions or receiveSegmentBounds; both are off by default. It is an out-of-bounds read. Adjacent stack bytes may influence segment state or LED output, and a fault is possible. Incoming notifications do not trigger an automatic UDP rebroadcast, so a practical information leak has not been demonstrated. There is no out-of-bounds write shown here. The included ASan reproducer is standalone, not a hardware test.

To Reproduce Bug

  1. Enable receive segment bounds and segment options, enable UDP sync, and keep the node outside realtime mode. Wait at least one second after WLED sends its own notification.
  2. Send a 42-byte notifier packet to udpPort from the LAN: byte 0 = 0, byte 1 = 0, byte 11 = 12, byte 36 matches receiveGroups, byte 39 = 1 segment, byte 40 = 36, and byte 41 = segment ID 0.
  3. The check accepts offset 41, then the parser reads segment fields beyond the received bytes. This path is confirmed from source; device behavior has not been tested.

Expected Behavior

WLED should ignore incomplete segment data. Pass the actual received length to parseNotifyPacket() and check ofs + 36 <= length before reading a segment; the ESP-NOW caller also needs to pass its assembled length.

Install Method

Binary from WLED.me

What version of WLED?

296eec3

Which microcontroller/board are you seeing the problem on?

ESP8266

Relevant log/trace output

Anything else?

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. DedeHai commented on Oct 31, 2026

    @DedeHai
    Collaborator

    @coderabbitai is this an issue with internally generated packets? when can this issue arise?

  2. coderabbitai commented on Oct 31, 2026

    @coderabbitai
    Contributor
    Analysis chain

    Script executed:

    #!/bin/bash
    gh issue view 5942 --repo wled/WLED --json title,body,comments,url

    Length of output: 3322


    Script executed:

    #!/bin/bash
    git rev-parse --short HEAD
    fd -i '^(udp\.cpp|.*espnow.*|.*esp_now.*)$'
    ast-grep outline wled00/udp.cpp --items all
    rg -n -C3 'parseNotifyPacket\s*\(|UDP_IN_MAXSIZE|udpOut\[39\]|udpOut\[40\]' .

    Length of output: 5104


    Script executed:

    #!/bin/bash
    sed -n '1,205p' wled00/udp.cpp
    sed -n '206,345p' wled00/udp.cpp
    sed -n '466,565p' wled00/udp.cpp
    sed -n '895,995p' wled00/udp.cpp

    Length of output: 22771


    @DedeHai Normal WLED-generated UDP sync packets should not trigger this issue. In wled00/udp.cpp, notify() writes a complete 36-byte record for each advertised active segment. It sends the full WLEDPACKETSIZE buffer. The receiver also ignores its own broadcasts on the primary notifier port.

    The reported issue arises when a received packet advertises segment data that it does not contain. For example:

    • A third-party sync sender constructs an incomplete packet.
    • A sender has a packet-length or segment-count bug.
    • Someone deliberately sends a malformed packet to the WLED UDP port.

    For the segment-read case, the receiver must accept the sync group, have receiveSegmentOptions or receiveSegmentBounds enabled, and be outside realtime mode. The one-second suppression period after sending its own notification must also have expired.

    A concrete example is a 42-byte packet that declares version 12, one segment, and a 36-byte segment stride. The segment starts at offset 41. The check accepts it because 41 + 36 <= 1472, although the received packet does not contain that segment record. The parser can then read beyond the 43-byte stack buffer.

    Ordinary UDP packet loss does not cause this. A lost datagram or an incomplete IP fragment set does not normally arrive as a shorter UDP datagram.

    ESP-NOW also calls the parser without a length, but its buffer and assembly path differ. That path needs separate validation; it is not evidence that normal WLED-generated traffic triggers this specific stack-buffer read.

    This is a malformed-input handling issue, not a demonstrated failure during normal WLED-to-WLED UDP sync. The source confirms the invalid read path; a device crash has not been verified.


    I couldn't check this conversation for new learnings, so no new learning was saved.


    Learnings used
    Learnt from: DedeHai
    Repo: wled/WLED PR: 5547
    File: wled00/e131.cpp:65-69
    Timestamp: 2026-05-01T10:24:37.448Z
    Learning: In WLED DDP packet handling (wled00/e131.cpp, PR #5547), `dataLen` from the DDP header counts ONLY the color data bytes — it does NOT include the 4-byte timecode field when DDP_FLAGS_TIME is set. The packet layout is: [fixed header (DDP_HEADER_LEN bytes)] + [optional timecode (c bytes, c=4 when DDP_FLAGS_TIME)] + [color data (dataLen bytes)]. Therefore:
    - The packet-length completeness guard is correctly: `packetLen < DDP_HEADER_LEN + c + dataLen`
    - The data-bounds guard must compare color-byte count (NOT `c + color-byte count`) against `dataLen`: `numLeds * ddpChannelsPerLed > dataLen` is the correct rejection condition. Using `c + numLeds*ddpChannelsPerLed > dataLen` is a bug that incorrectly rejects valid TIME-flagged packets.
    
    Learnt from: willmmiles
    Repo: wled/WLED PR: 4859
    File: usermods/udp_name_sync/udp_name_sync.cpp:62-70
    Timestamp: 2025-08-21T00:00:17.196Z
    Learning: In WLED codebase, UDP packet processing code must never rely on null termination being present on the wire and must always validate packet size first. The codebase already has some vulnerabilities where malformed data can crash the software, so avoiding introduction of new ones is a priority.
    
    Learnt from: DedeHai
    Repo: wled/WLED PR: 4997
    File: wled00/e131.cpp:33-44
    Timestamp: 2025-10-10T18:34:06.550Z
    Learning: In WLED's DDP packet handling (ws.cpp and e131.cpp), only prevent out-of-bounds memory access rather than enforcing DDP spec compliance. Don't check the 1440-byte spec limit—accept out-of-spec packets assuming correct encoding. The bounds check `maxDataIndex = c + numLeds * ddpChannelsPerLed; if (maxDataIndex > dataLen) reject` is sufficient and already covers the timecode flag case (when c=4) without needing separate validation.
    

    You are interacting with an AI system.

  3. softhack007 commented on Oct 31, 2026

    @softhack007
    Member

    I think this is only a problem with rogue/fabricated packages. We should actually decide if we want to go all the way and harden WLED against fabricated packages in all places - including DMX messages, node list, udp sync, json api, http api, esp-now, etc.

    Even when we fix all these potential buffer overread scenarios, the next threat is fabricated content, even with correct packet sizes.

    In this special case, parseNotifyPacket() handles 12 versions of the sync format, and to really make it bullet-proof the function needs the received packet size as parameter, and compare against the size before each processing step.

    I've already added a safeguard for minimal packet size 12 in #5932 (comment).

  4. softhack007 commented on Oct 31, 2026

    @softhack007
    Member

    @K-ANOY do you have a POC realistic exploit based on the buffer overread you found? If yes, what are the exploit consequences related to traditional security areas of concern: confidentiality, integrity, availability, and accountability?

  5. softhack007 commented on Oct 31, 2026

    @softhack007
    Member

    @K-ANOY as a side note: please make sure to declare AI support when posting bug reports. Your description has certain phrases and technical details that smell like AI - especially "the included ASan reproducer is standalone" (so where is it?).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址