Skip to content

[Bug] RT-Thread Bouffalo ADC reads outside its DMA sample buffer #11949

Closed

Description

@zephyr-saxon

RT-Thread Version

v5.3.0

Affected area

Other / Not sure

Hardware/BSP vendor

Not applicable / Other

Architecture

Not applicable / Other

Board and hardware details

Bouffalo ADC

Develop Toolchain

Other

Describe the bug

RT-Thread Bouffalo ADC reads outside its DMA sample buffer

Affected version

RT-Thread v5.3.0.

Describe the bug

bl_adc_get_value() reads raw_data[10 + channel] without checking the channel. raw_data has 26 words. A channel of 22 selects word 32, past the end of the buffer.

static volatile uint32_t raw_data[16 + 10];

rt_sem_take(_adc->sem, RT_WAITING_FOREVER);
*value = (rt_uint32_t)raw_data[10 + channel];

rt_adc_read() does not validate the channel either. The semaphore is created with an initial count of zero and released from the DMA completion handler, so the read runs after that wait returns. A channel below -10 selects before the start of raw_data.

Staying inside the 26-word array is only the memory bound. A supported channel also has to be one the board actually samples, with a defined slot in the DMA result.

Locations:

bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.c:26
bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.c:155
bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.c:166
bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.c:177
bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.c:201
components/drivers/misc/adc.c:118

Threat model

The boundary is a caller of the public ADC API. This report does not treat a network or physical-bus peer as the attacker. The value that caller controls is the channel argument of rt_adc_read(dev, channel). dev only selects the ADC instance. raw_data is a driver buffer; the caller does not supply its address or length.

channel has type rt_int8_t, so the values defined by the API are -128 through 127. The read uses index 10 + channel. Channels 0 through 15 stay inside the 26-word array. Channel 22, which fits in rt_int8_t, selects word 32. A channel below -10 selects before the array. The negative constants RT_ADC_INTERN_CH_TEMPER, RT_ADC_INTERN_CH_VREF, and RT_ADC_INTERN_CH_VBAT (-1, -2, and -3) are inside the array by this formula, but that does not make them valid sample slots.

With RT_USING_FINSH, adc read <channel> passes atoi(argv[2]) into rt_adc_read(). The shell is an entry point only when an untrusted user can reach it. A product task that forwards an untrusted channel into the same API is the same boundary. The read still waits on the ADC semaphore, which starts at zero and is released by the DMA completion handler.

Steps to reproduce

I have not reproduced this on Bouffalo hardware. The reduced check keeps the array length and the 10 + channel index. It does not model the semaphore or DMA.

#include <stdint.h>

static volatile uint32_t raw_data[16 + 10];

static uint32_t read_channel(int8_t channel)
{
    return raw_data[10 + channel];
}

int main(void)
{
    return (int)read_channel(22);
}

Compile it with a bounds sanitizer, for example:

clang -fsanitize=address,undefined -g -O1 repro.c -o repro && ./repro

Rejecting the channel before the index avoids the report. Channel -11 is also out of range; channels 0 and 10 stay inside this array.

Relevant log output

runtime error: index 32 out of bounds for type 'volatile uint32_t[26]'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior

Impact

The callback returns the loaded word through rt_adc_read(). A caller that can supply the channel, including the adc shell command when FinSH is enabled, can request a word outside raw_data once the DMA wait has completed. The reduced check shows the out-of-bounds read. It does not show which adjacent data a product would return, or a network entry point.

This is a read. raw_data is the source, and the store is to the caller's output pointer.

Suggested fix

Validate the channel against the configured channel list before waiting on the semaphore. Map that channel to a documented sample slot and check the slot against the DMA buffer length. Reject negative and unsupported channels. Keep the DMA producer and the reader on the same result layout.

Other additional context

No response

Activity

  1. added
    bugThis PR/issue is a bug in the current code.
    on Oct 29, 2026
  2. github-actions commented on Oct 29, 2026

    @github-actions

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

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

  3. BernardXiong commented on Nov 8, 2026

    @BernardXiong
    Member

    已在 master 和 v5.3.0 上确认该代码路径仍然存在:

    • bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.c 的 bl_adc_get_value() 直接读取 raw_data[10 + channel],raw_data 只有 26 个 32 位元素,且没有校验 channel;
    • components/drivers/adc/adc.c 的 rt_adc_read() 也不校验通道号,直接调用驱动的 convert 回调(报告中的 components/drivers/misc/adc.c 已移动到该路径);
    • 因此 channel = 22 会读到数组之后,channel < -10 会读到数组之前,与报告一致。

    这是 Bouffalo BSP 驱动自身的边界问题,通用 ADC 框架无法知道每块板卡实际配置了哪些通道。建议在 bl_adc_get_value() 中按已配置通道数(sizeof(chan) / sizeof(chan[0]))校验 channel,越界时返回 -RT_EINVAL;同时注意 rt_adc_read() 在 convert 返回错误时会返回 0,调用方需自行区分。BSP 专属问题本轮不提交修复 PR,建议由 BSP 维护者处理。


    Confirmed on master and v5.3.0: bl_adc_get_value() indexes raw_data[10 + channel] without validating channel; raw_data has 26 entries and rt_adc_read() in the generic ADC framework does not validate the channel either. This is a BSP-driver boundary issue, so this triage run does not submit a fix PR; the BSP maintainer should validate the channel against the configured channel count and return -RT_EINVAL on out-of-range input.

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

    bugThis PR/issue is a bug in the current code.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions