[Bug] RT-Thread Bouffalo ADC reads outside its DMA sample buffer

Aberta Para iniciantes
#11,949 1 comentário 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
72/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
c

Direção de pesquisa

Leia bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.c nos locais indicados e acompanhe como os canais ADC configurados são associados aos slots de resultado do DMA. Confira components/drivers/misc/adc.c para entender o caminho público de leitura e, em seguida, inspecione a configuração ADC da placa e os testes relevantes. O trabalho estará concluído quando canais não suportados ou fora do intervalo forem rejeitados antes da indexação e os canais válidos forem associados aos slots amostrados.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

bug
RT-Thread Version

v5.3.0

Affected area

Other / Not sure

Hardware/BSP vendor

Not applicable / Other

Architecture

Not applicable / Other

Board and hardware details

Bouffalo ADC

Develop Toolchain

Other

Describe the bug

RT-Thread Bouffalo ADC reads outside its DMA sample buffer

Affected version

RT-Thread v5.3.0.

Describe the bug

bl_adc_get_value() reads raw_data[10 + channel] without checking the channel. raw_data has 26 words. A channel of 22 selects word 32, past the end of the buffer.

static volatile uint32_t raw_data[16 + 10];

rt_sem_take(_adc->sem, RT_WAITING_FOREVER);
*value = (rt_uint32_t)raw_data[10 + channel];

rt_adc_read() does not validate the channel either. The semaphore is created with an initial count of zero and released from the DMA completion handler, so the read runs after that wait returns. A channel below -10 selects before the start of raw_data.

Staying inside the 26-word array is only the memory bound. A supported channel also has to be one the board actually samples, with a defined slot in the DMA result.

Locations:

bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.c:26
bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.c:155
bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.c:166
bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.c:177
bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.c:201
components/drivers/misc/adc.c:118

Threat model

The boundary is a caller of the public ADC API. This report does not treat a network or physical-bus peer as the attacker. The value that caller controls is the channel argument of rt_adc_read(dev, channel). dev only selects the ADC instance. raw_data is a driver buffer; the caller does not supply its address or length.

channel has type rt_int8_t, so the values defined by the API are -128 through 127. The read uses index 10 + channel. Channels 0 through 15 stay inside the 26-word array. Channel 22, which fits in rt_int8_t, selects word 32. A channel below -10 selects before the array. The negative constants RT_ADC_INTERN_CH_TEMPER, RT_ADC_INTERN_CH_VREF, and RT_ADC_INTERN_CH_VBAT (-1, -2, and -3) are inside the array by this formula, but that does not make them valid sample slots.

With RT_USING_FINSH, adc read <channel> passes atoi(argv[2]) into rt_adc_read(). The shell is an entry point only when an untrusted user can reach it. A product task that forwards an untrusted channel into the same API is the same boundary. The read still waits on the ADC semaphore, which starts at zero and is released by the DMA completion handler.

Steps to reproduce

I have not reproduced this on Bouffalo hardware. The reduced check keeps the array length and the 10 + channel index. It does not model the semaphore or DMA.

#include <stdint.h>

static volatile uint32_t raw_data[16 + 10];

static uint32_t read_channel(int8_t channel)
{
    return raw_data[10 + channel];
}

int main(void)
{
    return (int)read_channel(22);
}

Compile it with a bounds sanitizer, for example:

clang -fsanitize=address,undefined -g -O1 repro.c -o repro && ./repro

Rejecting the channel before the index avoids the report. Channel -11 is also out of range; channels 0 and 10 stay inside this array.

Relevant log output

runtime error: index 32 out of bounds for type 'volatile uint32_t[26]'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior

Impact

The callback returns the loaded word through rt_adc_read(). A caller that can supply the channel, including the adc shell command when FinSH is enabled, can request a word outside raw_data once the DMA wait has completed. The reduced check shows the out-of-bounds read. It does not show which adjacent data a product would return, or a network entry point.

This is a read. raw_data is the source, and the store is to the caller's output pointer.

Suggested fix

Validate the channel against the configured channel list before waiting on the semaphore. Map that channel to a documented sample slot and check the slot against the DMA buffer length. Reject negative and unsupported channels. Keep the DMA producer and the reader on the same result layout.

Other additional context

No response

Linguagem predominante
C
Estrelas
12.3k
Forks
5.5k
Merge médio
4d 9h
PRs com merge (30d)
38

Preparar o ambiente

Abrir no Codespaces

Inicia o contêiner de desenvolvimento do projeto no navegador, com a sua própria conta do GitHub.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de RT-Thread/rt-thread

Todas as issues de RT-Thread/rt-thread

Issues semelhantes

Mais issues de C

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.