[Bug] Stack buffer overflow in GD32 CAN driver (_can_sendmsg / _can_sendmsg_nonblocking) when pmsg->len > 8
Los mantenedores suelen responder en 2 días
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 74/100
- Tipo de issue
- Error
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Stack tecnológico
- c
- Área
- embedded-iot, security
Línea de trabajo
Lee bsp/gd32/arm/libraries/gd32_drivers/drv_can.c, centrándote en _can_sendmsg() y _can_sendmsg_nonblocking() y en su búfer transmit_message.tx_data. Comprueba cómo gestionan estas rutas las longitudes de mensaje superiores a 8 y cómo notifican las entradas no válidas. El objetivo se cumple cuando ambas rutas rechazan los mensajes demasiado grandes antes de copiarlos; verifica el comportamiento con una prueba específica del controlador o con las pruebas CAN disponibles.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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 values0..255.components/drivers/can/dev_can.c:645-672:rt_can_write()forwardspmsgto_can_int_tx()/_can_nonblocking_tx()without checkingpmsg->len.bsp/gd32/arm/libraries/gd32_drivers/drv_can.c:528,575(_can_sendmsg) and:687,710(_can_sendmsg_nonblocking):transmit_messageis a local stack variable (can_trasnmit_message_struct) containinguint8_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)
- Lenguaje dominante
- C
- Estrellas
- 12.4k
- Forks
- 5.5k
- Merge medio
- 3 d 13 h
- PR fusionados (30 d)
- 73
Preparar el entorno
Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de RT-Thread/rt-thread
-
[Bug] Out-of-bounds stack read and callback signature mismatch in TM4C123 ADC driver (drv_adc.c)Abiertobug Component component: drivers
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
RT-Thread/rt-thread#12118 · 1 comentario ·
Los mantenedores suelen responder en 2 días
-
BSP BSP: NXP bug Component component: drivers
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
RT-Thread/rt-thread#12117 · 1 comentario ·
Los mantenedores suelen responder en 2 días
-
Arch: ARM/AArch64 BSP BSP: GD32 bug Component component: drivers
Dificultad 2/5 1-3 horas Aptitud para principiantes 66/100
RT-Thread/rt-thread#12109 · 5 comentarios ·
Los mantenedores suelen responder en 2 días
-
action bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
RT-Thread/rt-thread#12106 · 1 comentario ·
Los mantenedores suelen responder en 2 días
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 67/100
RT-Thread/rt-thread#11951 · 2 comentarios ·
Los mantenedores suelen responder en 2 días
Todos los issues de RT-Thread/rt-thread
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
signalwire/freeswitch#3329 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 82/100
Los mantenedores suelen responder en 1 día
-
Status: Opened
Dificultad 2/5 1-3 horas Aptitud para principiantes 83/100
espressif/esp-protocols#1224 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 83/100
Los mantenedores suelen responder en 1 día
-
bot-triaged module: bfloat16 module: cuda module: half module: numerical-stability triage review
Dificultad 2/5 1-3 horas Aptitud para principiantes 77/100
Los mantenedores suelen responder en 1 día