[Bug] Stack buffer overflow in GD32 CAN driver (_can_sendmsg / _can_sendmsg_nonblocking) when pmsg->len > 8
Mantenedores costumam responder em até 2 dias
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 2/5
- Tempo estimado
- 1-3 horas
- Facilidade para iniciantes
- 74/100
- Tipo de issue
- Bug
- Clareza
- Claramente especificada
- Status de atividade
- Ativa
- Stack de tecnologia
- c
- Domínio
- embedded-iot, security
Direção de pesquisa
Leia bsp/gd32/arm/libraries/gd32_drivers/drv_can.c, com foco em _can_sendmsg() e _can_sendmsg_nonblocking() e no buffer transmit_message.tx_data. Verifique como esses caminhos lidam com comprimentos de mensagem maiores que 8 e como informam entradas inválidas. O trabalho estará concluído quando ambos os caminhos rejeitarem mensagens grandes demais antes de copiá-las; verifique o comportamento com um teste direcionado do driver ou com os testes CAN disponíveis.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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)
- Linguagem predominante
- C
- Estrelas
- 12.4k
- Forks
- 5.5k
- Merge médio
- 3d 13h
- PRs com merge (30d)
- 73
Preparar o ambiente
Inicia o contêiner de desenvolvimento do projeto no navegador, com a sua própria conta do GitHub.
- Sem Dockerfile nem arquivo Docker Compose
- Tem um modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de RT-Thread/rt-thread
-
[Bug] Out-of-bounds stack read and callback signature mismatch in TM4C123 ADC driver (drv_adc.c)Abertabug Component component: drivers
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 74/100
RT-Thread/rt-thread#12118 · 1 comentário ·
Mantenedores costumam responder em até 2 dias
-
BSP BSP: NXP bug Component component: drivers
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
RT-Thread/rt-thread#12117 · 1 comentário ·
Mantenedores costumam responder em até 2 dias
-
Arch: ARM/AArch64 BSP BSP: GD32 bug Component component: drivers
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 66/100
RT-Thread/rt-thread#12109 · 5 comentários ·
Mantenedores costumam responder em até 2 dias
-
action bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
RT-Thread/rt-thread#12106 · 1 comentário ·
Mantenedores costumam responder em até 2 dias
-
bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 67/100
RT-Thread/rt-thread#11951 · 2 comentários ·
Mantenedores costumam responder em até 2 dias
Todas as issues de RT-Thread/rt-thread
Issues semelhantes
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
signalwire/freeswitch#3329 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 82/100
Mantenedores costumam responder em até 1 dia
-
Status: Opened
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 83/100
espressif/esp-protocols#1224 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 83/100
Mantenedores costumam responder em até 1 dia
-
bot-triaged module: bfloat16 module: cuda module: half module: numerical-stability triage review
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 77/100
Mantenedores costumam responder em até 1 dia