diff mbox series

gpio: davinci: Fix potential buffer overflow

Message ID 20240328091021.18027-1-amishin@t-argos.ru
State New
Headers show
Series gpio: davinci: Fix potential buffer overflow | expand

Commit Message

Aleksandr Mishin March 28, 2024, 9:10 a.m. UTC
In davinci_gpio_probe() accessing an element of array 'chips->regs' of size 5 and
array 'offset_array' of size 5 can lead to a buffer overflow, since the index
'bank' can have an out of range value 63.
Fix this bug by limiting top index value.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: c809e37a3b5a ("gpio: davinci: Allocate the correct amount of memory for controller")
Signed-off-by: Aleksandr Mishin <amishin@t-argos.ru>
---
 drivers/gpio/gpio-davinci.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

Alexander Lobakin March 28, 2024, 3:27 p.m. UTC | #1
From: Aleksandr Mishin <amishin@t-argos.ru>
Date: Thu, 28 Mar 2024 12:10:21 +0300

> In davinci_gpio_probe() accessing an element of array 'chips->regs' of size 5 and
> array 'offset_array' of size 5 can lead to a buffer overflow, since the index
> 'bank' can have an out of range value 63.
> Fix this bug by limiting top index value.
> 
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
> 
> Fixes: c809e37a3b5a ("gpio: davinci: Allocate the correct amount of memory for controller")
> Signed-off-by: Aleksandr Mishin <amishin@t-argos.ru>
> ---
>  drivers/gpio/gpio-davinci.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/gpio/gpio-davinci.c b/drivers/gpio/gpio-davinci.c
> index bb499e362912..b65df1f2b83f 100644
> --- a/drivers/gpio/gpio-davinci.c
> +++ b/drivers/gpio/gpio-davinci.c
> @@ -257,6 +257,9 @@ static int davinci_gpio_probe(struct platform_device *pdev)
>  	spin_lock_init(&chips->lock);
>  
>  	nbank = DIV_ROUND_UP(ngpio, 32);
> +    if (nbank > MAX_REGS_BANKS || nbank > 5) {
> +        nbank = MAX_REGS_BANKS < 5 ? MAX_REGS_BANKS : 5;
> +	}

Static analysis warnings make no sense until you provide a reliable way
to trigger the problem on real systems.

>  	for (bank = 0; bank < nbank; bank++)
>  		chips->regs[bank] = gpio_base + offset_array[bank];
>  

Thanks,
Olek
Alexander Lobakin March 28, 2024, 4:32 p.m. UTC | #2
From: Aleksandr Mishin <amishin@t-argos.ru>
Date: Thu, 28 Mar 2024 19:23:56 +0300

> 
> 
> 28.03.2024 18:27, Alexander Lobakin пишет:
>> From: Aleksandr Mishin <amishin@t-argos.ru>
>> Date: Thu, 28 Mar 2024 12:10:21 +0300
>>
>>> In davinci_gpio_probe() accessing an element of array 'chips->regs'
>>> of size 5 and
>>> array 'offset_array' of size 5 can lead to a buffer overflow, since
>>> the index
>>> 'bank' can have an out of range value 63.
>>> Fix this bug by limiting top index value.
>>>
>>> Found by Linux Verification Center (linuxtesting.org) with SVACE.
>>>
>>> Fixes: c809e37a3b5a ("gpio: davinci: Allocate the correct amount of
>>> memory for controller")
>>> Signed-off-by: Aleksandr Mishin <amishin@t-argos.ru>
>>> ---
>>>   drivers/gpio/gpio-davinci.c | 3 +++
>>>   1 file changed, 3 insertions(+)
>>>
>>> diff --git a/drivers/gpio/gpio-davinci.c b/drivers/gpio/gpio-davinci.c
>>> index bb499e362912..b65df1f2b83f 100644
>>> --- a/drivers/gpio/gpio-davinci.c
>>> +++ b/drivers/gpio/gpio-davinci.c
>>> @@ -257,6 +257,9 @@ static int davinci_gpio_probe(struct
>>> platform_device *pdev)
>>>       spin_lock_init(&chips->lock);
>>>         nbank = DIV_ROUND_UP(ngpio, 32);
>>> +    if (nbank > MAX_REGS_BANKS || nbank > 5) {
>>> +        nbank = MAX_REGS_BANKS < 5 ? MAX_REGS_BANKS : 5;
>>> +    }
>>
>> Static analysis warnings make no sense until you provide a reliable way
>> to trigger the problem on real systems.
>>
>>>       for (bank = 0; bank < nbank; bank++)
>>>           chips->regs[bank] = gpio_base + offset_array[bank];
>>>   
>>
>> Thanks,
>> Olek
>>
> 
> I can only see the code at this time. And I see the following:
> 1. In some configurations CONFIG_ARCH_NR_GPIO value is 2048. So nbank
> value can be 64.
> 2. Previously, a patch was proposed that removes restrictions on the
> number of GPIOs
> (https://lore.kernel.org/all/cb540a9d73cad36d288664f8b275c6308a4a3168.1662116601.git.christophe.leroy@csgroup.eu/).
> 

But no real hardware / device tree which declares such huge number of
GPIOs, right?

CONFIG_ARCH_NR_GPIO is architecture-specific. Davinci is a platform, not
an architecture. If no Davinci board can have a number that would
overflow, the fix doesn't make sense.

Thanks,
Olek
diff mbox series

Patch

diff --git a/drivers/gpio/gpio-davinci.c b/drivers/gpio/gpio-davinci.c
index bb499e362912..b65df1f2b83f 100644
--- a/drivers/gpio/gpio-davinci.c
+++ b/drivers/gpio/gpio-davinci.c
@@ -257,6 +257,9 @@  static int davinci_gpio_probe(struct platform_device *pdev)
 	spin_lock_init(&chips->lock);
 
 	nbank = DIV_ROUND_UP(ngpio, 32);
+    if (nbank > MAX_REGS_BANKS || nbank > 5) {
+        nbank = MAX_REGS_BANKS < 5 ? MAX_REGS_BANKS : 5;
+	}
 	for (bank = 0; bank < nbank; bank++)
 		chips->regs[bank] = gpio_base + offset_array[bank];