[Bug] Stack buffer overflow in GD32 CAN driver (_can_sendmsg / _can_sendmsg_nonblocking) when pmsg->len > 8

Open Beginner friendly
#12,115 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
74/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c

Research direction

Read bsp/gd32/arm/libraries/gd32_drivers/drv_can.c, focusing on _can_sendmsg() and _can_sendmsg_nonblocking() and their transmit_message.tx_data buffer. Check how these paths handle message lengths above 8 and how they report invalid input. Done means both paths reject oversized messages before copying; verify the behavior with a focused driver test or the available CAN tests.

Written by the indexing model from the issue text.

Description

BSP BSP: GD32 bug Component component: drivers
RT-Thread Version

5.3.0

Affected area

Device drivers

Hardware/BSP vendor

GD32

Architecture

Not applicable / Other

Board and hardware details

GD32 ARM BSP CAN driver (bsp/gd32/arm/libraries/gd32_drivers/drv_can.c, CAN0/CAN1, RT_USING_CAN)

Develop Toolchain

GCC

Describe the bug
Summary

In bsp/gd32/arm/libraries/gd32_drivers/drv_can.c, both _can_sendmsg() (line 575) and _can_sendmsg_nonblocking() (line 710) call memcpy(transmit_message.tx_data, pmsg->data, pmsg->len) without validating pmsg->len <= sizeof(transmit_message.tx_data) (8 bytes). Because struct rt_can_msg.len is an 8-bit field (0..255) and dev_can.c performs no length check, passing pmsg->len > 8 to rt_device_write() causes a stack buffer overflow of up to 247 bytes.

Details
  • components/drivers/include/drivers/dev_can.h:510: rt_uint32_t len : 8; allows values 0..255.
  • components/drivers/can/dev_can.c:645-672: rt_can_write() forwards pmsg to _can_int_tx() / _can_nonblocking_tx() without checking pmsg->len.
  • bsp/gd32/arm/libraries/gd32_drivers/drv_can.c:528,575 (_can_sendmsg) and :687,710 (_can_sendmsg_nonblocking): transmit_message is a local stack variable (can_trasnmit_message_struct) containing uint8_t tx_data[8]:
if (RT_CAN_DTR == pmsg->rtr)
{
    transmit_message.tx_ft = CAN_FT_DATA;
    memcpy(transmit_message.tx_data, pmsg->data, pmsg->len);
}

Suggested fix: Reject pmsg == RT_NULL || pmsg->len > sizeof(transmit_message.tx_data) with -RT_EINVAL in both _can_sendmsg() and _can_sendmsg_nonblocking():

--- a/bsp/gd32/arm/libraries/gd32_drivers/drv_can.c
+++ b/bsp/gd32/arm/libraries/gd32_drivers/drv_can.c
@@ -529,6 +529,11 @@ static rt_ssize_t _can_sendmsg(struct rt_can_device *can, const void *buf, rt_ui
     can_struct_para_init(CAN_TX_MESSAGE_STRUCT, &transmit_message);
     rt_uint32_t can_x = ((struct gd32_can_device *)can->parent.user_data)->can_x;
     struct rt_can_msg *pmsg = (struct rt_can_msg *)buf;
+
+    if (pmsg == RT_NULL || pmsg->len > sizeof(transmit_message.tx_data))
+    {
+        return -RT_EINVAL;
+    }
@@ -688,6 +693,11 @@ rt_ssize_t _can_sendmsg_nonblocking(struct rt_can_device *can, const void *buf)
     can_struct_para_init(CAN_TX_MESSAGE_STRUCT, &transmit_message);
     rt_uint32_t can_x = ((struct gd32_can_device *)can->parent.user_data)->can_x;
     struct rt_can_msg *pmsg = (struct rt_can_msg *)buf;
+
+    if (pmsg == RT_NULL || pmsg->len > sizeof(transmit_message.tx_data))
+    {
+        return -RT_EINVAL;
+    }
PoC

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

#include <stdint.h>
#include <string.h>

typedef struct {
	uint32_t tx_sfid;
	uint32_t tx_efid;
	uint8_t tx_ff;
	uint8_t tx_ft;
	uint8_t tx_dlen;
	uint8_t tx_data[8];
} can_trasnmit_message_struct;

struct rt_can_msg {
	uint32_t id : 29;
	uint32_t ide : 1;
	uint32_t rtr : 1;
	uint32_t rsv : 1;
	uint32_t len : 8;
	uint8_t data[64];
};

static volatile uint8_t sink;

static int gd32_can_sendmsg(const struct rt_can_msg *pmsg)
{
	can_trasnmit_message_struct transmit_message;

	memset(&transmit_message, 0, sizeof(transmit_message));
	if (pmsg->rtr == 0) {
		memcpy(transmit_message.tx_data, pmsg->data, pmsg->len);
	}
	transmit_message.tx_dlen = (uint8_t)pmsg->len;
	sink = transmit_message.tx_data[0];
	return 0;
}

int main(void)
{
	struct rt_can_msg msg;

	memset(&msg, 0x41, sizeof(msg));
	msg.rtr = 0;
	msg.len = 64;
	return gd32_can_sendmsg(&msg);
}

Compile and run with clang -fsanitize=address,undefined -g -O0 repro.c -o repro && ./repro, which reports AddressSanitizer: stack-buffer-overflow (WRITE of size 64) overflowing transmit_message.

Impact

Calling rt_device_write() on a GD32 CAN device with pmsg->len > 8 overwrites up to 247 bytes of stack memory in thread or ISR context (and over-reads pmsg->data[8] when RT_CAN_USING_CANFD is disabled), causing memory corruption, crash, or potential control-flow hijacking.

Other additional context

Required configuration: RT_USING_CAN and GD32 BSP CAN driver (BSP_USING_CAN0 / BSP_USING_CAN1)
CWE: CWE-121 (Stack-based Buffer Overflow), CWE-787 (Out-of-bounds Write), CWE-125 (Out-of-bounds Read)

Dominant language
C
Stars
12.4k
Forks
5.5k
Avg merge
3d 10h
Merged PRs (30d)
68

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from RT-Thread/rt-thread

All issues in RT-Thread/rt-thread

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.