[Bug] Unchecked hdr_bank and RX_MB_COUNT mismatch in NXP FlexCAN drivers (imxrt / mcxa / mcxn) cause shift UB, OOB MMIO writes, and .bss overflow
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ó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 55/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
Hướng nghiên cứu
Read the three drv_can.c files named in the issue, focusing on each RT_CAN_CMD_SET_FILTER handler and the RX_MB_COUNT definitions. Check how caller supplied and auto assigned mailbox indices are validated, then trace the callback and receive paths for MCXA and MCXN. Done means filter indices stay within each driver's mailbox count and invalid or exhausted assignments return an error.
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 i.MX RT, MCXA, and MCXN BSP FlexCAN drivers (bsp/nxp/{imx/imxrt,mcx/mcxa,mcx/mcxn}/.../drv_can.c, RT_USING_CAN + RT_CAN_USING_HDR)
Develop Toolchain
GCC
Describe the bug
Summary
The RT_CAN_CMD_SET_FILTER handler in bsp/nxp/imx/imxrt/libraries/drivers/drv_can.c, bsp/nxp/mcx/mcxa/Libraries/drivers/drv_can.c, and bsp/nxp/mcx/mcxn/Libraries/drivers/drv_can.c does not validate item->hdr_bank against RX_MB_COUNT (or 0..31). On MCXA and MCXN (RX_MB_COUNT == 1), the auto-assignment loop also hardcodes i < 32, allowing mailbox indices >= 1 that cause out-of-bounds .bss writes and reads past static flexcan_frame_t frame[1] in flexcan_callback() and _can_recvmsg().
Details
- Unchecked
hdr_bank(imxrt:315-332, mcxa:253-270, mcxn:259-276):item->hdr_bank(rt_int32_t) is used directly in1 << item->hdr_bank,1 << index,FLEXCAN_SetRxIndividualMask(can->base, index + 1, mask), andFLEXCAN_SetRxMbConfig(can->base, index + 1, &mbConfig, true)with no range check. .bssoverflow on MCXA & MCXN: Both drivers declare#define RX_MB_COUNT (1)andstatic flexcan_frame_t frame[RX_MB_COUNT], yet iteratefor (i = 0; i < 32; i++)onhdr_bank == -1. When two or more filters are configured,flexcan_callback()setsrxXfer.frame = &frame[result - 1]and_can_recvmsg()readsframe[boxno - 1]past the end offrame[1].
Suggested fix: Bound both auto-assigned and caller-supplied hdr_bank to 0 <= index < RX_MB_COUNT using unsigned shifts 1U << index across imxrt, mcxa, and mcxn:
--- a/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_can.c
+++ b/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_can.c
@@ -238,24 +238,31 @@ static rt_err_t _can_control(struct rt_can_device *can_dev, int cmd, void *arg)
if (item->hdr_bank == -1)
{
- for (i = 0; i < 32; i++)
+ rt_bool_t found = RT_FALSE;
+ for (i = 0; i < RX_MB_COUNT; i++)
{
- if (!(filter_mask & (1 << i)))
+ if (!(filter_mask & (1U << i)))
{
index = i;
+ found = RT_TRUE;
break;
}
}
+ if (!found)
+ {
+ res = -RT_EFULL;
+ break;
+ }
}
else
{
- if (filter_mask & (1 << item->hdr_bank))
+ if (item->hdr_bank < 0 || item->hdr_bank >= RX_MB_COUNT)
+ {
+ res = -RT_EINVAL;
+ break;
+ }
+ if (filter_mask & (1U << (rt_uint32_t)item->hdr_bank))
PoC
Identified via symbolic execution on commit c39e92f4c1f1fa2e991c6c52838f85eb93d44415 (not run on physical hardware). Standalone C reproducer:
#include <stdint.h>
#define RX_MB_COUNT 1
typedef struct {
uint32_t words[4];
} flexcan_frame_t;
static flexcan_frame_t frame[RX_MB_COUNT];
static uint32_t filter_mask = 0;
static int nxp_set_filter(int32_t hdr_bank, uint32_t *out_mb_idx)
{
uint8_t index = 0;
if (hdr_bank == -1) {
for (uint8_t i = 0; i < 32; i++) {
if (!(filter_mask & (1U << i))) {
index = i;
break;
}
}
} else {
if (filter_mask & (1 << hdr_bank)) {
return -1;
}
index = (uint8_t)hdr_bank;
}
*out_mb_idx = (uint32_t)index + 1U;
filter_mask |= (1U << index);
return 0;
}
int main(void)
{
uint32_t mb1 = 0, mb2 = 0;
nxp_set_filter(-1, &mb1);
nxp_set_filter(-1, &mb2);
frame[mb2 - 1].words[0] = 0xdeadbeefU;
return 0;
}
Compile and run with clang -fsanitize=address,undefined -g -O0 repro.c -o repro && ./repro, which reports UBSan index 1 out of bounds for type 'flexcan_frame_t[1]' and AddressSanitizer global-buffer-overflow (WRITE of size 4) past frame[1].
Impact
Configuring two or more CAN filters on MCXA/MCXN corrupts .bss memory past frame[1] during RX handling; passing out-of-range hdr_bank values on i.MX RT, MCXA, or MCXN triggers signed shift UB and out-of-bounds FlexCAN MMIO register writes.
Other additional context
Required configuration: RT_USING_CAN, RT_CAN_USING_HDR, and NXP i.MX RT / MCXA / MCXN CAN driver
CWE: CWE-787 (Out-of-bounds Write), CWE-125 (Out-of-bounds Read), CWE-1335 (Incorrect Bitwise Shift of Integer)
- 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: NXP bug Component component: drivers
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
RT-Thread/rt-thread#12117 · 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
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