[Bug] Out-of-bounds read in NXP MCXA and MCXN LPADC drivers (drv_adc.c) due to unchecked channel index
Maintainer thường phản hồi trong vòng 2 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 76/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- c
- Lĩnh vực
- embedded-iot, security
Hướng nghiên cứu
Đọc bsp/nxp/mcx/mcxa/Libraries/drivers/drv_adc.c và bsp/nxp/mcx/mcxn/Libraries/drivers/drv_adc.c, tập trung vào các callback .enabled và .convert cùng những lần tra cứu adc_chl2cmd của chúng. Thêm kiểm tra phạm vi kênh trước khi truy cập bảng, sau đó xác minh rằng các kênh âm không hợp lệ và các kênh ngoài phạm vi trả về -RT_EINVAL mà không bắt đầu chuyển đổi; issue không nêu kiểm thử nào.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
RT-Thread Version
5.3.0
Affected area
Device drivers
Hardware/BSP vendor
NXP
Architecture
Not applicable / Other
Board and hardware details
NXP MCXA (FRDM-MCXA153/156/346) and MCXN (FRDM-MCXN236/947) LPADC drivers
Develop Toolchain
GCC
Describe the bug
Summary
In bsp/nxp/mcx/mcxa/Libraries/drivers/drv_adc.c and bsp/nxp/mcx/mcxn/Libraries/drivers/drv_adc.c, the .enabled and .convert ADC callbacks index the 30-byte static table adc_chl2cmd[30] using a caller-supplied signed rt_int8_t channel without bounds checks, and then use the resulting byte to index adc_cmd2trig[16]. Calling rt_adc_enable(), rt_adc_read(), or rt_adc_voltage() with channel < 0 or channel >= 30 triggers an out-of-bounds read and can hang conversion polling.
Details
components/drivers/include/drivers/adc.h:66-77 defines channel as rt_int8_t (including negative internal-channel constants -1, -2, -3), and components/drivers/misc/adc.c forwards channel directly to the driver without range checks.
In bsp/nxp/mcx/mcxa/Libraries/drivers/drv_adc.c:27-28,84,94,96,119:
static uint8_t adc_chl2cmd[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
static uint8_t adc_cmd2trig[] = {0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3};
/* a153_adc_enabled() */
LPADC_SetConvCommandConfig(adc->adc_base, adc_chl2cmd[channel], &cmd_cfg);
trig_config.targetCommandId = adc_chl2cmd[channel];
LPADC_SetConvTriggerConfig(adc->adc_base, adc_cmd2trig[trig_config.targetCommandId], &trig_config);
/* a153_get_adc_value() */
LPADC_DoSoftwareTrigger(adc->adc_base, 1 << (adc_cmd2trig[adc_chl2cmd[channel]]));
while (!LPADC_GetConvResult(adc->adc_base, &mLpadcResultConfigStruct));
The identical unvalidated lookups exist in bsp/nxp/mcx/mcxn/Libraries/drivers/drv_adc.c:71,75,77,88.
Suggested fix: Reject channel < 0 || (rt_size_t)channel >= sizeof(adc_chl2cmd) with -RT_EINVAL at the start of both .enabled and .convert callbacks in the MCXA and MCXN drivers:
--- a/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_adc.c
+++ b/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_adc.c
@@ -57,6 +57,11 @@ static rt_err_t a153_adc_enabled(struct rt_adc_device *device, rt_int8_t channel
{
struct mcx_adc *adc = (struct mcx_adc *)device->parent.user_data;
+ if (channel < 0 || (rt_size_t)channel >= sizeof(adc_chl2cmd))
+ {
+ return -RT_EINVAL;
+ }
+
if (enabled)
{
@@ -116,6 +121,11 @@ static rt_err_t a153_get_adc_value(struct rt_adc_device *device, rt_int8_t chann
lpadc_conv_result_t mLpadcResultConfigStruct;
+ if (channel < 0 || (rt_size_t)channel >= sizeof(adc_chl2cmd))
+ {
+ return -RT_EINVAL;
+ }
+
LPADC_DoSoftwareTrigger(adc->adc_base, 1 << (adc_cmd2trig[adc_chl2cmd[channel]])); /* 1U is trigger0 mask. */
PoC
Identified via symbolic execution on commit c39e92f4c1f1fa2e991c6c52838f85eb93d44415 (not run on physical hardware). Standalone C reproducer:
#include <stdint.h>
static uint8_t adc_chl2cmd[] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
};
static uint8_t adc_cmd2trig[] = {
0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3
};
static volatile uint32_t sink;
static int a153_get_adc_value(int8_t channel)
{
uint8_t cmd = adc_chl2cmd[channel];
uint8_t trig = adc_cmd2trig[cmd];
sink = 1U << trig;
return 0;
}
int main(void)
{
return a153_get_adc_value(30);
}
Compile and run with clang -fsanitize=address,undefined -g -O0 repro.c -o repro && ./repro, which reports UBSan index 30 out of bounds for type 'uint8_t[30]' and AddressSanitizer global-buffer-overflow (READ of size 1).
Impact
A caller supplying an out-of-range or negative channel (e.g., via the FinSH adc command or public ADC API) triggers out-of-bounds static memory reads, invalid LPADC trigger configuration, and potential indefinite polling hang in LPADC_GetConvResult() (local denial of service).
Other additional context
Required configuration: RT_USING_ADC and BSP_USING_ADC0 (MCXA) or BSP_USING_ADC0 / BSP_USING_ADC1 (MCXN)
CWE: CWE-125 (Out-of-bounds Read)
- Ngôn ngữ chính
- C
- Star
- 12.4k
- Fork
- 5.5k
- Merge trung bình
- 3 ngày 13 giờ
- Pull request đã merge (30 ngày)
- 73
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của RT-Thread/rt-thread
-
[Bug] Out-of-bounds stack read and callback signature mismatch in TM4C123 ADC driver (drv_adc.c)Đang mởbug Component component: drivers
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
RT-Thread/rt-thread#12118 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
BSP BSP: GD32 bug Component component: drivers
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
RT-Thread/rt-thread#12115 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Arch: ARM/AArch64 BSP BSP: GD32 bug Component component: drivers
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
RT-Thread/rt-thread#12109 · 5 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
action bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
RT-Thread/rt-thread#12106 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
RT-Thread/rt-thread#11951 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
Tất cả issue của RT-Thread/rt-thread
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
signalwire/freeswitch#3329 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Status: Opened
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 83/100
espressif/esp-protocols#1224 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 83/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bot-triaged module: bfloat16 module: cuda module: half module: numerical-stability triage review
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
Maintainer thường phản hồi trong vòng 1 ngày