[Bug] Out-of-bounds stack read and callback signature mismatch in TM4C123 ADC driver (drv_adc.c)

未关闭 适合新手
#12,118 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
74/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
c

调研方向

阅读 bsp/tm4c123bsp/libraries/Drivers/drv_adc.c,并对照 components/drivers/include/drivers/adc.h,比较 ADC 回调签名和通道类型。检查 tm4c123_get_adc_value() 如何索引其包含四个元素的数组,然后确认修复会拒绝无效通道并与接口一致;issue 提供了一个使用 AddressSanitizer 重现越界访问的示例。

由索引模型根据 Issue 内容生成。

描述

bug Component component: drivers
RT-Thread Version

5.3.0

Affected area

Device drivers

Hardware/BSP vendor

Not applicable / Other

Architecture

Not applicable / Other

Board and hardware details

TI Tiva C Series TM4C123 BSP ADC driver (bsp/tm4c123bsp/libraries/Drivers/drv_adc.c, ADC0/ADC1, RT_USING_ADC)

Develop Toolchain

GCC

Describe the bug
Summary

In bsp/tm4c123bsp/libraries/Drivers/drv_adc.c, tm4c123_get_adc_value() indexes a 4-element stack array uint32_t pui32ADC0Value[4] (line 67) using an unchecked channel parameter (line 84). Furthermore, both tm4c123_adc_enabled() (line 44) and tm4c123_get_adc_value() (line 61) declare rt_uint32_t channel instead of rt_int8_t channel as required by struct rt_adc_ops.

Details

In bsp/tm4c123bsp/libraries/Drivers/drv_adc.c:61-86:

static rt_err_t tm4c123_get_adc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
{
    uint32_t pui32ADC0Value[4] = {0};
    ...
    *value = (rt_uint32_t)pui32ADC0Value[channel];

struct rt_adc_ops (components/drivers/include/drivers/adc.h:76-77) passes rt_int8_t channel (including negative internal-channel constants -1, -2, -3). Passing channel >= 4 reads past pui32ADC0Value[4] on the stack, and passing a negative rt_int8_t sign-extends to a large rt_uint32_t index under ARM AAPCS, causing an out-of-bounds read or bus fault.

Suggested fix: Match struct rt_adc_ops (rt_int8_t channel) and validate 0 <= channel < 4:

--- a/bsp/tm4c123bsp/libraries/Drivers/drv_adc.c
+++ b/bsp/tm4c123bsp/libraries/Drivers/drv_adc.c
@@ -41,7 +41,7 @@ struct tm4c123_adc
-static rt_err_t tm4c123_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
+static rt_err_t tm4c123_adc_enabled(struct rt_adc_device *device, rt_int8_t channel, rt_bool_t enabled)
@@ -58,7 +58,7 @@ static rt_err_t tm4c123_adc_enabled(struct rt_adc_device *device, rt_uint32_t ch
-static rt_err_t tm4c123_get_adc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
+static rt_err_t tm4c123_get_adc_value(struct rt_adc_device *device, rt_int8_t channel, rt_uint32_t *value)
@@ -66,6 +66,11 @@ static rt_err_t tm4c123_get_adc_value(struct rt_adc_device *device, rt_uint32_t
     uint32_t    pui32ADC0Value[4] = {0};
     struct tm4c123_adc_config   *config = (struct tm4c123_adc_config *)device->parent.user_data;
+
+    if (channel < 0 || (rt_size_t)channel >= (sizeof(pui32ADC0Value) / sizeof(pui32ADC0Value[0])))
+    {
+        return -RT_EINVAL;
+    }
PoC

Identified via symbolic execution on commit c39e92f4c1f1fa2e991c6c52838f85eb93d44415 (not run on physical hardware). Standalone C reproducer:

#include <stdint.h>

static volatile uint32_t sink;

static int tm4c123_get_adc_value(int8_t channel, uint32_t *value)
{
	uint32_t pui32ADC0Value[4] = {10, 20, 30, 40};
	uint32_t ch = (uint32_t)(int32_t)channel;

	*value = pui32ADC0Value[ch];
	return 0;
}

int main(void)
{
	uint32_t out = 0;

	tm4c123_get_adc_value(5, &out);
	sink = out;
	return 0;
}

Compile and run with clang -fsanitize=address,undefined -g -O0 repro.c -o repro && ./repro, which reports UBSan index 5 out of bounds for type 'uint32_t[4]' and AddressSanitizer stack-buffer-overflow (READ of size 4).

Impact

Calling rt_adc_read() (or FinSH adc read <channel>) with channel >= 4 or channel < 0 reads out-of-bounds stack memory or triggers a memory fault (information disclosure or local denial of service).

Other additional context

Required configuration: RT_USING_ADC and BSP_USING_ADC0 or BSP_USING_ADC1
CWE: CWE-125 (Out-of-bounds Read), CWE-686 (Function Call With Incorrect Argument Type)

主要语言
C
星标
12.4k
派生
5.5k
平均合并
3 天 13 小时
30 天内合并 PR
73

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

RT-Thread/rt-thread 的其他 Issue

查看 RT-Thread/rt-thread 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。