linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] regmap: spi: Reserve space for register address/padding
@ 2022-08-17 21:38 Cristian Ciocaltea
  2022-08-18  9:22 ` Lucas tanure
  0 siblings, 1 reply; 3+ messages in thread
From: Cristian Ciocaltea @ 2022-08-17 21:38 UTC (permalink / raw)
  To: Mark Brown, Greg Kroah-Hartman, Rafael J. Wysocki,
	André Almeida, Lucas Tanure, Charles Keepax
  Cc: linux-kernel, kernel

Currently the max_raw_read and max_raw_write limits in regmap_spi struct
do not take into account the additional size of the transmitted register
address and padding.  This may result in exceeding the maximum permitted
SPI transfer size, which could cause undefined behaviour, e.g. data
corruption.

Fix regmap_get_spi_bus() to properly adjust the above mentioned limits
by reserving space for the register address/padding as set in the regmap
configuration.

Fixes: f231ff38b7b2 ("regmap: spi: Set regmap max raw r/w from max_transfer_size")

Signed-off-by: Cristian Ciocaltea <cristian.ciocaltea@collabora.com>
---
 drivers/base/regmap/regmap-spi.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/base/regmap/regmap-spi.c b/drivers/base/regmap/regmap-spi.c
index 719323bc6c7f..6fb94c06a447 100644
--- a/drivers/base/regmap/regmap-spi.c
+++ b/drivers/base/regmap/regmap-spi.c
@@ -113,6 +113,7 @@ static const struct regmap_bus *regmap_get_spi_bus(struct spi_device *spi,
 						   const struct regmap_config *config)
 {
 	size_t max_size = spi_max_transfer_size(spi);
+	size_t max_msg_size, reg_reserve_size;
 	struct regmap_bus *bus;

 	if (max_size != SIZE_MAX) {
@@ -120,9 +121,15 @@ static const struct regmap_bus *regmap_get_spi_bus(struct spi_device *spi,
 		if (!bus)
 			return ERR_PTR(-ENOMEM);

+		max_msg_size = spi_max_message_size(spi);
+		reg_reserve_size = config->reg_bits / 8 + config->pad_bits / 8;
+		if (max_size + reg_reserve_size > max_msg_size)
+			max_size -= reg_reserve_size;
+
 		bus->free_on_exit = true;
 		bus->max_raw_read = max_size;
 		bus->max_raw_write = max_size;
+
 		return bus;
 	}

base-commit: 568035b01cfb107af8d2e4bd2fb9aea22cf5b868
--
2.37.2

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] regmap: spi: Reserve space for register address/padding
  2022-08-17 21:38 [PATCH] regmap: spi: Reserve space for register address/padding Cristian Ciocaltea
@ 2022-08-18  9:22 ` Lucas tanure
  2022-08-18 10:53   ` Cristian Ciocaltea
  0 siblings, 1 reply; 3+ messages in thread
From: Lucas tanure @ 2022-08-18  9:22 UTC (permalink / raw)
  To: Cristian Ciocaltea, Mark Brown, Greg Kroah-Hartman,
	Rafael J. Wysocki, André Almeida, Charles Keepax
  Cc: linux-kernel, kernel

On 8/17/22 22:38, Cristian Ciocaltea wrote:
> Currently the max_raw_read and max_raw_write limits in regmap_spi struct
> do not take into account the additional size of the transmitted register
> address and padding.  This may result in exceeding the maximum permitted
> SPI transfer size, which could cause undefined behaviour, e.g. data
> corruption.
> 
> Fix regmap_get_spi_bus() to properly adjust the above mentioned limits
> by reserving space for the register address/padding as set in the regmap
> configuration.
> 
> Fixes: f231ff38b7b2 ("regmap: spi: Set regmap max raw r/w from max_transfer_size")
> 
> Signed-off-by: Cristian Ciocaltea <cristian.ciocaltea@collabora.com>
> ---
>   drivers/base/regmap/regmap-spi.c | 7 +++++++
>   1 file changed, 7 insertions(+)
> 
> diff --git a/drivers/base/regmap/regmap-spi.c b/drivers/base/regmap/regmap-spi.c
> index 719323bc6c7f..6fb94c06a447 100644
> --- a/drivers/base/regmap/regmap-spi.c
> +++ b/drivers/base/regmap/regmap-spi.c
> @@ -113,6 +113,7 @@ static const struct regmap_bus *regmap_get_spi_bus(struct spi_device *spi,
>   						   const struct regmap_config *config)
>   {
>   	size_t max_size = spi_max_transfer_size(spi);
> +	size_t max_msg_size, reg_reserve_size;
>   	struct regmap_bus *bus;
> 
>   	if (max_size != SIZE_MAX) {
> @@ -120,9 +121,15 @@ static const struct regmap_bus *regmap_get_spi_bus(struct spi_device *spi,
>   		if (!bus)
>   			return ERR_PTR(-ENOMEM);
> 
> +		max_msg_size = spi_max_message_size(spi);
> +		reg_reserve_size = config->reg_bits / 8 + config->pad_bits / 8;
I think you can use BITS_PER_BYTE here instead of 8.

> +		if (max_size + reg_reserve_size > max_msg_size)
> +			max_size -= reg_reserve_size;
> +
>   		bus->free_on_exit = true;
>   		bus->max_raw_read = max_size;
>   		bus->max_raw_write = max_size;
> +
>   		return bus;
>   	}
> 
> base-commit: 568035b01cfb107af8d2e4bd2fb9aea22cf5b868
> --
> 2.37.2


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] regmap: spi: Reserve space for register address/padding
  2022-08-18  9:22 ` Lucas tanure
@ 2022-08-18 10:53   ` Cristian Ciocaltea
  0 siblings, 0 replies; 3+ messages in thread
From: Cristian Ciocaltea @ 2022-08-18 10:53 UTC (permalink / raw)
  To: Lucas tanure, Mark Brown, Greg Kroah-Hartman, Rafael J. Wysocki,
	André Almeida, Charles Keepax
  Cc: linux-kernel, kernel


On 8/18/22 12:22, Lucas tanure wrote:
> On 8/17/22 22:38, Cristian Ciocaltea wrote:
>> Currently the max_raw_read and max_raw_write limits in regmap_spi struct
>> do not take into account the additional size of the transmitted register
>> address and padding.  This may result in exceeding the maximum permitted
>> SPI transfer size, which could cause undefined behaviour, e.g. data
>> corruption.
>>
>> Fix regmap_get_spi_bus() to properly adjust the above mentioned limits
>> by reserving space for the register address/padding as set in the regmap
>> configuration.
>>
>> Fixes: f231ff38b7b2 ("regmap: spi: Set regmap max raw r/w from 
>> max_transfer_size")
>>
>> Signed-off-by: Cristian Ciocaltea <cristian.ciocaltea@collabora.com>
>> ---
>>   drivers/base/regmap/regmap-spi.c | 7 +++++++
>>   1 file changed, 7 insertions(+)
>>
>> diff --git a/drivers/base/regmap/regmap-spi.c 
>> b/drivers/base/regmap/regmap-spi.c
>> index 719323bc6c7f..6fb94c06a447 100644
>> --- a/drivers/base/regmap/regmap-spi.c
>> +++ b/drivers/base/regmap/regmap-spi.c
>> @@ -113,6 +113,7 @@ static const struct regmap_bus 
>> *regmap_get_spi_bus(struct spi_device *spi,
>>                              const struct regmap_config *config)
>>   {
>>       size_t max_size = spi_max_transfer_size(spi);
>> +    size_t max_msg_size, reg_reserve_size;
>>       struct regmap_bus *bus;
>>
>>       if (max_size != SIZE_MAX) {
>> @@ -120,9 +121,15 @@ static const struct regmap_bus 
>> *regmap_get_spi_bus(struct spi_device *spi,
>>           if (!bus)
>>               return ERR_PTR(-ENOMEM);
>>
>> +        max_msg_size = spi_max_message_size(spi);
>> +        reg_reserve_size = config->reg_bits / 8 + config->pad_bits / 8;
> I think you can use BITS_PER_BYTE here instead of 8.

Right, thanks for hint. Applied to v2:

https://lore.kernel.org/all/20220818104851.429479-1-cristian.ciocaltea@collabora.com/

>> +        if (max_size + reg_reserve_size > max_msg_size)
>> +            max_size -= reg_reserve_size;
>> +
>>           bus->free_on_exit = true;
>>           bus->max_raw_read = max_size;
>>           bus->max_raw_write = max_size;
>> +
>>           return bus;
>>       }
>>
>> base-commit: 568035b01cfb107af8d2e4bd2fb9aea22cf5b868
>> -- 
>> 2.37.2
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-08-18 10:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-17 21:38 [PATCH] regmap: spi: Reserve space for register address/padding Cristian Ciocaltea
2022-08-18  9:22 ` Lucas tanure
2022-08-18 10:53   ` Cristian Ciocaltea

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).