[Bug] Out-of-bounds stack read and callback signature mismatch in TM4C123 ADC driver (drv_adc.c)
Les mainteneurs répondent en général sous 2 jours
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 2/5
- Temps estimé
- 1-3 heures
- Accessibilité débutants
- 74/100
- Type d'issue
- Bug
- Clarté
- Clairement spécifiée
- Activité
- Active
- Stack technique
- c
- Domaine
- backend, embedded-iot
Piste de recherche
Lisez bsp/tm4c123bsp/libraries/Drivers/drv_adc.c en parallèle avec components/drivers/include/drivers/adc.h pour comparer les signatures des callbacks ADC et les types de canal. Vérifiez comment tm4c123_get_adc_value() indexe son tableau de quatre éléments, puis confirmez que la correction rejette les canaux non valides et correspond à l’interface ; l’issue fournit un reproducer AddressSanitizer pour un accès hors limites.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
RT-Thread Version
5.3.0
Affected area
Device drivers
Hardware/BSP vendor
Not applicable / Other
Architecture
Not applicable / Other
Board and hardware details
TI Tiva C Series TM4C123 BSP ADC driver (bsp/tm4c123bsp/libraries/Drivers/drv_adc.c, ADC0/ADC1, RT_USING_ADC)
Develop Toolchain
GCC
Describe the bug
Summary
In bsp/tm4c123bsp/libraries/Drivers/drv_adc.c, tm4c123_get_adc_value() indexes a 4-element stack array uint32_t pui32ADC0Value[4] (line 67) using an unchecked channel parameter (line 84). Furthermore, both tm4c123_adc_enabled() (line 44) and tm4c123_get_adc_value() (line 61) declare rt_uint32_t channel instead of rt_int8_t channel as required by struct rt_adc_ops.
Details
In bsp/tm4c123bsp/libraries/Drivers/drv_adc.c:61-86:
static rt_err_t tm4c123_get_adc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
{
uint32_t pui32ADC0Value[4] = {0};
...
*value = (rt_uint32_t)pui32ADC0Value[channel];
struct rt_adc_ops (components/drivers/include/drivers/adc.h:76-77) passes rt_int8_t channel (including negative internal-channel constants -1, -2, -3). Passing channel >= 4 reads past pui32ADC0Value[4] on the stack, and passing a negative rt_int8_t sign-extends to a large rt_uint32_t index under ARM AAPCS, causing an out-of-bounds read or bus fault.
Suggested fix: Match struct rt_adc_ops (rt_int8_t channel) and validate 0 <= channel < 4:
--- a/bsp/tm4c123bsp/libraries/Drivers/drv_adc.c
+++ b/bsp/tm4c123bsp/libraries/Drivers/drv_adc.c
@@ -41,7 +41,7 @@ struct tm4c123_adc
-static rt_err_t tm4c123_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
+static rt_err_t tm4c123_adc_enabled(struct rt_adc_device *device, rt_int8_t channel, rt_bool_t enabled)
@@ -58,7 +58,7 @@ static rt_err_t tm4c123_adc_enabled(struct rt_adc_device *device, rt_uint32_t ch
-static rt_err_t tm4c123_get_adc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
+static rt_err_t tm4c123_get_adc_value(struct rt_adc_device *device, rt_int8_t channel, rt_uint32_t *value)
@@ -66,6 +66,11 @@ static rt_err_t tm4c123_get_adc_value(struct rt_adc_device *device, rt_uint32_t
uint32_t pui32ADC0Value[4] = {0};
struct tm4c123_adc_config *config = (struct tm4c123_adc_config *)device->parent.user_data;
+
+ if (channel < 0 || (rt_size_t)channel >= (sizeof(pui32ADC0Value) / sizeof(pui32ADC0Value[0])))
+ {
+ return -RT_EINVAL;
+ }
PoC
Identified via symbolic execution on commit c39e92f4c1f1fa2e991c6c52838f85eb93d44415 (not run on physical hardware). Standalone C reproducer:
#include <stdint.h>
static volatile uint32_t sink;
static int tm4c123_get_adc_value(int8_t channel, uint32_t *value)
{
uint32_t pui32ADC0Value[4] = {10, 20, 30, 40};
uint32_t ch = (uint32_t)(int32_t)channel;
*value = pui32ADC0Value[ch];
return 0;
}
int main(void)
{
uint32_t out = 0;
tm4c123_get_adc_value(5, &out);
sink = out;
return 0;
}
Compile and run with clang -fsanitize=address,undefined -g -O0 repro.c -o repro && ./repro, which reports UBSan index 5 out of bounds for type 'uint32_t[4]' and AddressSanitizer stack-buffer-overflow (READ of size 4).
Impact
Calling rt_adc_read() (or FinSH adc read <channel>) with channel >= 4 or channel < 0 reads out-of-bounds stack memory or triggers a memory fault (information disclosure or local denial of service).
Other additional context
Required configuration: RT_USING_ADC and BSP_USING_ADC0 or BSP_USING_ADC1
CWE: CWE-125 (Out-of-bounds Read), CWE-686 (Function Call With Incorrect Argument Type)
- Langage dominant
- C
- Étoiles
- 12.4k
- Forks
- 5.5k
- Merge moyen
- 3 j 13 h
- PR mergées (30 j)
- 73
Préparer son environnement
Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de RT-Thread/rt-thread
-
BSP BSP: NXP bug Component component: drivers
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
RT-Thread/rt-thread#12117 · 1 commentaire ·
Les mainteneurs répondent en général sous 2 jours
-
BSP BSP: GD32 bug Component component: drivers
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
RT-Thread/rt-thread#12115 · 1 commentaire ·
Les mainteneurs répondent en général sous 2 jours
-
Arch: ARM/AArch64 BSP BSP: GD32 bug Component component: drivers
Difficulté 2/5 1-3 heures Accessibilité débutants 66/100
RT-Thread/rt-thread#12109 · 5 commentaires ·
Les mainteneurs répondent en général sous 2 jours
-
action bug
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
RT-Thread/rt-thread#12106 · 1 commentaire ·
Les mainteneurs répondent en général sous 2 jours
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 67/100
RT-Thread/rt-thread#11951 · 2 commentaires ·
Les mainteneurs répondent en général sous 2 jours
Toutes les issues de RT-Thread/rt-thread
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
signalwire/freeswitch#3329 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 82/100
Les mainteneurs répondent en général sous 1 jour
-
Status: Opened
Difficulté 2/5 1-3 heures Accessibilité débutants 83/100
espressif/esp-protocols#1224 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 83/100
Les mainteneurs répondent en général sous 1 jour
-
bot-triaged module: bfloat16 module: cuda module: half module: numerical-stability triage review
Difficulté 2/5 1-3 heures Accessibilité débutants 77/100
Les mainteneurs répondent en général sous 1 jour