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

Offen Anfängerfreundlich
#12,115 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 2 Tagen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
74/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
c

Rechercherichtung

Lies bsp/gd32/arm/libraries/gd32_drivers/drv_can.c und konzentriere dich auf _can_sendmsg() und _can_sendmsg_nonblocking() sowie deren transmit_message.tx_data-Puffer. Prüfe, wie diese Pfade Nachrichtenlängen über 8 behandeln und wie sie ungültige Eingaben melden. Das Ziel ist erreicht, wenn beide Pfade übergroße Nachrichten vor dem Kopieren zurückweisen; überprüfe das Verhalten mit einem gezielten Treibertest oder den verfügbaren CAN-Tests.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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)

Vorherrschende Sprache
C
Sterne
12.4k
Forks
5.5k
Ø Merge
3 T. 13 Std.
Gemergte PRs (30 T.)
73

Entwicklungsumgebung

In Codespaces öffnen

Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus RT-Thread/rt-thread

Alle Issues in RT-Thread/rt-thread

Ähnliche Issues

Weitere Issues zu C

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.