Skip to content

[Bug] RT-Thread serial v1: Reopening a device configured for INT_TX in DMA_TX mode causes the 4-byte tx_fifo to be treated as an rt_serial_tx_dma structure, resulting in out-of-bounds heap access and invalid function pointer calls. #12072

Closed

Description

@Finder16

RT-Thread Version

5.3.0

Affected area

Device drivers

Hardware/BSP vendor

Not applicable / Other

Architecture

Not applicable / Other

Board and hardware details

QEMU vexpress-a9 (bsp/qemu-vexpress-a9), UART via serial v1 with DMA_TX. Not board-specific

Develop Toolchain

Other

Describe the bug

Bug Description

In the serial v1 framework (components/drivers/serial/dev_serial.c), a vulnerability occurs when a device initially opened in interrupt transmission mode (RT_DEVICE_FLAG_INT_TX) is reopened in DMA transmission mode (RT_DEVICE_FLAG_DMA_TX).

The previously allocated 4-byte struct rt_serial_tx_fifo remains unchanged, but the DMA transmission path treats it as a 48-byte struct rt_serial_tx_dma. This results in out-of-bounds heap reads and writes.

Root Cause (based on master commit 6fdfe70)

  • rt_device_open() (components/drivers/core/device.c) invokes device_open again when the same device is opened with different flags. Since RT_DEVICE_OFLAG_MASK is 0xf0f, which includes both INT_TX (0x400) and DMA_TX (0x800), rt_serial_open() is called again even when only the transmission mode changes.

  • rt_serial_open() allocates a transmission object only when serial->serial_tx == RT_NULL (dev_serial.c:858). If an object has already been allocated, it simply enables RT_DEVICE_FLAG_DMA_TX in open_flag (line 902), leaving the existing rt_serial_tx_fifo unchanged.

  • The DMA transmission path subsequently casts serial->serial_tx to struct rt_serial_tx_dma * and accesses its data_queue member.

    • _serial_dma_tx() (lines 712, 714)
    • DMA completion handler (line 1657)
    • Close handler (line 991)
  • The data_queue member starts at offset 4, which is outside the bounds of the original 4-byte allocation.

Steps to Reproduce

  1. Open a serial device that supports both INT_TX and DMA_TX using:

    rt_device_open(dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_TX);

    This allocates a 4-byte rt_serial_tx_fifo object for serial_tx.

  2. Reopen the same device using:

    rt_device_open(dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_DMA_TX);

    The serial_tx pointer still refers to the original object, while open_flag becomes 0x80b.

  3. Call:

    rt_device_write(dev, 0, buf, len);

    This triggers the following execution path:

    _serial_dma_tx() -> rt_data_queue_push(&tx_dma->data_queue, ...)

Observed Results (QEMU vexpress-a9)

  • With RT_DEBUGING_ASSERT enabled, the assertion (queue->magic == DATAQUEUE_MAGIC) fails inside rt_data_queue_push().

  • With RT_DEBUGING_ASSERT disabled, a data abort occurs at dataqueue.c:170:

    queue->queue[queue->put_index].data_ptr = data_ptr;

    This happens because the code attempts to write through a pointer read from heap memory outside the bounds of the original 4-byte allocation.

    Call stack:
    rt_device_write
    -> rt_serial_write
    -> _serial_dma_tx
    -> rt_data_queue_push

  • In the earlier commit 23c05e4, the same vulnerability caused execution to jump to a heap address through the queue->evt_notify function pointer, resulting in a crash.

Conditions Required

  • The UART driver must register both RT_DEVICE_FLAG_INT_TX and RT_DEVICE_FLAG_DMA_TX.

    For example, bsp/stm32/drv_usart.c registers INT_TX | uart_dma_flag.

  • The reverse sequence is also problematic. If the device is first opened in DMA mode and then reopened in interrupt mode, the existing rt_serial_tx_dma object is incorrectly treated as an rt_serial_tx_fifo.

Other additional context

No response

Activity

  1. added
    bug
    on Nov 20, 2026
  2. github-actions commented on Nov 20, 2026

    @github-actions

    如需认领并处理此 Issue,请评论 /claim。

    To claim and work on this issue, comment /claim.

  3. Finder16 commented on Nov 20, 2026

    @Finder16
    ContributorAuthor

    /claim

  4. added
    in progress
    on Nov 20, 2026
  5. github-actions commented on Nov 20, 2026

    @github-actions

    @Finder16 has claimed this issue.

  6. added a commit that references this issue on Nov 23, 2026
    13291f9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Componentbugcomponent: driversin progress

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions