The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] iio: proximity: hx9023s: validate firmware size
@ 2026-07-10 14:22 Laxman Acharya Padhya
  2026-07-10 14:54 ` Joshua Crofts
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Laxman Acharya Padhya @ 2026-07-10 14:22 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá, Andy Shevchenko, Yasin Lee,
	linux-iio, linux-kernel, stable

hx9023s_send_cfg() copies the firmware into a counted flexible array and
then reads fixed offsets from the copied data before walking register/value
pairs starting at FW_DATA_OFFSET. A truncated firmware image can therefore
make the driver read past the copied buffer during probe-time configuration
loading.

Reject firmware images that cannot contain the fixed header, reject images
too large for the u16 fw_size field, and validate that the advertised
register count fits in the remaining payload.

Move release_firmware() to the callback so the firmware object is released
on all hx9023s_send_cfg() error paths.

Fixes: e9ed97be4fcc ("iio: proximity: hx9023s: Added firmware file parsing functionality")
Cc: stable@vger.kernel.org
Signed-off-by: Laxman Acharya Padhya <acharyalaxman8848@gmail.com>
---
 drivers/iio/proximity/hx9023s.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index a6ff7cbe9e6..a2f9c077e58 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -18,6 +18,7 @@
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
 #include <linux/irqreturn.h>
+#include <linux/limits.h>
 #include <linux/math64.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
@@ -25,6 +26,7 @@
 #include <linux/property.h>
 #include <linux/regmap.h>
 #include <linux/regulator/consumer.h>
+#include <linux/slab.h>
 #include <linux/types.h>
 #include <linux/units.h>
 
@@ -1031,8 +1033,12 @@ static int hx9023s_bin_load(struct hx9023s_data *data, struct hx9023s_bin *bin)
 
 static int hx9023s_send_cfg(const struct firmware *fw, struct hx9023s_data *data)
 {
-	struct hx9023s_bin *bin __free(kfree) =
-		kzalloc(fw->size + sizeof(*bin), GFP_KERNEL);
+	struct hx9023s_bin *bin __free(kfree) = NULL;
+
+	if (fw->size < FW_DATA_OFFSET || fw->size > U16_MAX)
+		return -EINVAL;
+
+	bin = kzalloc(sizeof(*bin) + fw->size, GFP_KERNEL);
 	if (!bin)
 		return -ENOMEM;
 
@@ -1041,7 +1047,8 @@ static int hx9023s_send_cfg(const struct firmware *fw, struct hx9023s_data *data
 	bin->fw_ver = bin->data[FW_VER_OFFSET];
 	bin->reg_count = get_unaligned_le16(bin->data + FW_REG_CNT_OFFSET);
 
-	release_firmware(fw);
+	if (bin->reg_count > (bin->fw_size - FW_DATA_OFFSET) / 2)
+		return -EINVAL;
 
 	return hx9023s_bin_load(data, bin);
 }
@@ -1058,6 +1065,7 @@ static void hx9023s_cfg_update(const struct firmware *fw, void *context)
 	}
 
 	ret = hx9023s_send_cfg(fw, data);
+	release_firmware(fw);
 	if (ret) {
 		dev_warn(dev, "Firmware update failed: %d\n", ret);
 		goto no_fw;

base-commit: 0e35b9b6ec0ffcc5e23cbdec09f5c622ad532b53
-- 
2.51.2


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

* Re: [PATCH] iio: proximity: hx9023s: validate firmware size
  2026-07-10 14:22 [PATCH] iio: proximity: hx9023s: validate firmware size Laxman Acharya Padhya
@ 2026-07-10 14:54 ` Joshua Crofts
  2026-07-10 15:22   ` David Lechner
  2026-07-10 15:21 ` David Lechner
  2026-07-10 15:28 ` [PATCH v2] " Laxman Acharya Padhya
  2 siblings, 1 reply; 6+ messages in thread
From: Joshua Crofts @ 2026-07-10 14:54 UTC (permalink / raw)
  To: Laxman Acharya Padhya
  Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
	Yasin Lee, linux-iio, linux-kernel, stable

On Fri, 10 Jul 2026 20:07:12 +0545
Laxman Acharya Padhya <acharyalaxman8848@gmail.com> wrote:
> @@ -1058,6 +1065,7 @@ static void hx9023s_cfg_update(const struct firmware *fw, void *context)
>  	}
>  
>  	ret = hx9023s_send_cfg(fw, data);
> +	release_firmware(fw);

Why not move this after the if? Keep the call/retval check coupled.

>  	if (ret) {
>  		dev_warn(dev, "Firmware update failed: %d\n", ret);
>  		goto no_fw;
> 

Otherwise this looks good. Feel free to add my tag with the v2:

Reviewed-by: Joshua Crofts <joshua.crofts1@gmail.com>

-- 
Kind regards

CJD

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

* Re: [PATCH] iio: proximity: hx9023s: validate firmware size
  2026-07-10 14:22 [PATCH] iio: proximity: hx9023s: validate firmware size Laxman Acharya Padhya
  2026-07-10 14:54 ` Joshua Crofts
@ 2026-07-10 15:21 ` David Lechner
  2026-07-10 15:28 ` [PATCH v2] " Laxman Acharya Padhya
  2 siblings, 0 replies; 6+ messages in thread
From: David Lechner @ 2026-07-10 15:21 UTC (permalink / raw)
  To: Laxman Acharya Padhya, Jonathan Cameron
  Cc: Nuno Sá, Andy Shevchenko, Yasin Lee, linux-iio, linux-kernel,
	stable

On 7/10/26 9:22 AM, Laxman Acharya Padhya wrote:
> hx9023s_send_cfg() copies the firmware into a counted flexible array and
> then reads fixed offsets from the copied data before walking register/value
> pairs starting at FW_DATA_OFFSET. A truncated firmware image can therefore
> make the driver read past the copied buffer during probe-time configuration
> loading.
> 
> Reject firmware images that cannot contain the fixed header, reject images
> too large for the u16 fw_size field, and validate that the advertised
> register count fits in the remaining payload.
> 
> Move release_firmware() to the callback so the firmware object is released
> on all hx9023s_send_cfg() error paths.

This could probably be considered a separate fix (and therefore separate
patch) since it looks like there is an existing code path (return -ENOMEM)
where it would not be released.

> 
> Fixes: e9ed97be4fcc ("iio: proximity: hx9023s: Added firmware file parsing functionality")
> Cc: stable@vger.kernel.org
> Signed-off-by: Laxman Acharya Padhya <acharyalaxman8848@gmail.com>
> ---
>  drivers/iio/proximity/hx9023s.c | 14 +++++++++++---
>  1 file changed, 11 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
> index a6ff7cbe9e6..a2f9c077e58 100644
> --- a/drivers/iio/proximity/hx9023s.c
> +++ b/drivers/iio/proximity/hx9023s.c
> @@ -18,6 +18,7 @@
>  #include <linux/i2c.h>
>  #include <linux/interrupt.h>
>  #include <linux/irqreturn.h>
> +#include <linux/limits.h>
>  #include <linux/math64.h>
>  #include <linux/module.h>
>  #include <linux/mutex.h>
> @@ -25,6 +26,7 @@
>  #include <linux/property.h>
>  #include <linux/regmap.h>
>  #include <linux/regulator/consumer.h>
> +#include <linux/slab.h>
>  #include <linux/types.h>
>  #include <linux/units.h>
>  
> @@ -1031,8 +1033,12 @@ static int hx9023s_bin_load(struct hx9023s_data *data, struct hx9023s_bin *bin)
>  
>  static int hx9023s_send_cfg(const struct firmware *fw, struct hx9023s_data *data)
>  {
> -	struct hx9023s_bin *bin __free(kfree) =
> -		kzalloc(fw->size + sizeof(*bin), GFP_KERNEL);
> +	struct hx9023s_bin *bin __free(kfree) = NULL;

We don't initialize autocleanup variables to NULL. So leave this as-is
and just put the size check before it.

> +
> +	if (fw->size < FW_DATA_OFFSET || fw->size > U16_MAX)
> +		return -EINVAL;
> +
> +	bin = kzalloc(sizeof(*bin) + fw->size, GFP_KERNEL);
>  	if (!bin)
>  		return -ENOMEM;
>  
> @@ -1041,7 +1047,8 @@ static int hx9023s_send_cfg(const struct firmware *fw, struct hx9023s_data *data
>  	bin->fw_ver = bin->data[FW_VER_OFFSET];
>  	bin->reg_count = get_unaligned_le16(bin->data + FW_REG_CNT_OFFSET);
>  
> -	release_firmware(fw);
> +	if (bin->reg_count > (bin->fw_size - FW_DATA_OFFSET) / 2)
> +		return -EINVAL;
>  
>  	return hx9023s_bin_load(data, bin);
>  }
> @@ -1058,6 +1065,7 @@ static void hx9023s_cfg_update(const struct firmware *fw, void *context)
>  	}
>  
>  	ret = hx9023s_send_cfg(fw, data);
> +	release_firmware(fw);
>  	if (ret) {
>  		dev_warn(dev, "Firmware update failed: %d\n", ret);
>  		goto no_fw;
> 
> base-commit: 0e35b9b6ec0ffcc5e23cbdec09f5c622ad532b53


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

* Re: [PATCH] iio: proximity: hx9023s: validate firmware size
  2026-07-10 14:54 ` Joshua Crofts
@ 2026-07-10 15:22   ` David Lechner
  0 siblings, 0 replies; 6+ messages in thread
From: David Lechner @ 2026-07-10 15:22 UTC (permalink / raw)
  To: Joshua Crofts, Laxman Acharya Padhya
  Cc: Jonathan Cameron, Nuno Sá, Andy Shevchenko, Yasin Lee,
	linux-iio, linux-kernel, stable

On 7/10/26 9:54 AM, Joshua Crofts wrote:
> On Fri, 10 Jul 2026 20:07:12 +0545
> Laxman Acharya Padhya <acharyalaxman8848@gmail.com> wrote:
>> @@ -1058,6 +1065,7 @@ static void hx9023s_cfg_update(const struct firmware *fw, void *context)
>>  	}
>>  
>>  	ret = hx9023s_send_cfg(fw, data);
>> +	release_firmware(fw);
> 
> Why not move this after the if? Keep the call/retval check coupled.

Because the if branch contains a goto and would therefore skip calling
release_firmware() in case of error.

> 
>>  	if (ret) {
>>  		dev_warn(dev, "Firmware update failed: %d\n", ret);
>>  		goto no_fw;
>>
> 
> Otherwise this looks good. Feel free to add my tag with the v2:
> 
> Reviewed-by: Joshua Crofts <joshua.crofts1@gmail.com>
> 


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

* [PATCH v2] iio: proximity: hx9023s: validate firmware size
  2026-07-10 14:22 [PATCH] iio: proximity: hx9023s: validate firmware size Laxman Acharya Padhya
  2026-07-10 14:54 ` Joshua Crofts
  2026-07-10 15:21 ` David Lechner
@ 2026-07-10 15:28 ` Laxman Acharya Padhya
  2026-07-10 20:21   ` Jonathan Cameron
  2 siblings, 1 reply; 6+ messages in thread
From: Laxman Acharya Padhya @ 2026-07-10 15:28 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá, Andy Shevchenko, Yasin Lee,
	Joshua Crofts, linux-iio, linux-kernel, stable

hx9023s_send_cfg() copies the firmware into a counted flexible array and
then reads fixed offsets from the copied data before walking register/value
pairs starting at FW_DATA_OFFSET. A truncated firmware image can therefore
make the driver read past the copied buffer during probe-time configuration
loading.

Reject firmware images that cannot contain the fixed header, reject images
too large for the u16 fw_size field, and validate that the advertised
register count fits in the remaining payload.

Fixes: e9ed97be4fcc ("iio: proximity: hx9023s: Added firmware file parsing functionality")
Cc: stable@vger.kernel.org
Reviewed-by: Joshua Crofts <joshua.crofts1@gmail.com>
Signed-off-by: Laxman Acharya Padhya <acharyalaxman8848@gmail.com>
---
 drivers/iio/proximity/hx9023s.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index a6ff7cbe9e6..9d91ce681ac 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -18,6 +18,7 @@
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
 #include <linux/irqreturn.h>
+#include <linux/limits.h>
 #include <linux/math64.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
@@ -1031,8 +1032,11 @@ static int hx9023s_bin_load(struct hx9023s_data *data, struct hx9023s_bin *bin)
 
 static int hx9023s_send_cfg(const struct firmware *fw, struct hx9023s_data *data)
 {
+	if (fw->size < FW_DATA_OFFSET || fw->size > U16_MAX)
+		return -EINVAL;
+
 	struct hx9023s_bin *bin __free(kfree) =
-		kzalloc(fw->size + sizeof(*bin), GFP_KERNEL);
+		kzalloc(sizeof(*bin) + fw->size, GFP_KERNEL);
 	if (!bin)
 		return -ENOMEM;
 
@@ -1041,7 +1045,8 @@ static int hx9023s_send_cfg(const struct firmware *fw, struct hx9023s_data *data
 	bin->fw_ver = bin->data[FW_VER_OFFSET];
 	bin->reg_count = get_unaligned_le16(bin->data + FW_REG_CNT_OFFSET);
 
-	release_firmware(fw);
+	if (bin->reg_count > (bin->fw_size - FW_DATA_OFFSET) / 2)
+		return -EINVAL;
 
 	return hx9023s_bin_load(data, bin);
 }
@@ -1058,6 +1063,7 @@ static void hx9023s_cfg_update(const struct firmware *fw, void *context)
 	}
 
 	ret = hx9023s_send_cfg(fw, data);
+	release_firmware(fw);
 	if (ret) {
 		dev_warn(dev, "Firmware update failed: %d\n", ret);
 		goto no_fw;
-- 
2.51.2


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

* Re: [PATCH v2] iio: proximity: hx9023s: validate firmware size
  2026-07-10 15:28 ` [PATCH v2] " Laxman Acharya Padhya
@ 2026-07-10 20:21   ` Jonathan Cameron
  0 siblings, 0 replies; 6+ messages in thread
From: Jonathan Cameron @ 2026-07-10 20:21 UTC (permalink / raw)
  To: Laxman Acharya Padhya
  Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
	Yasin Lee, Joshua Crofts, linux-iio, linux-kernel, stable

On Fri, 10 Jul 2026 21:13:42 +0545
Laxman Acharya Padhya <acharyalaxman8848@gmail.com> wrote:

> hx9023s_send_cfg() copies the firmware into a counted flexible array and
> then reads fixed offsets from the copied data before walking register/value
> pairs starting at FW_DATA_OFFSET. A truncated firmware image can therefore
> make the driver read past the copied buffer during probe-time configuration
> loading.
> 
> Reject firmware images that cannot contain the fixed header, reject images
> too large for the u16 fw_size field, and validate that the advertised
> register count fits in the remaining payload.
> 
> Fixes: e9ed97be4fcc ("iio: proximity: hx9023s: Added firmware file parsing functionality")
> Cc: stable@vger.kernel.org
> Reviewed-by: Joshua Crofts <joshua.crofts1@gmail.com>
> Signed-off-by: Laxman Acharya Padhya <acharyalaxman8848@gmail.com>
A few minor comments.

Thanks,

Jonathan

> ---
>  drivers/iio/proximity/hx9023s.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
> index a6ff7cbe9e6..9d91ce681ac 100644
> --- a/drivers/iio/proximity/hx9023s.c
> +++ b/drivers/iio/proximity/hx9023s.c
> @@ -18,6 +18,7 @@
>  #include <linux/i2c.h>
>  #include <linux/interrupt.h>
>  #include <linux/irqreturn.h>
> +#include <linux/limits.h>
>  #include <linux/math64.h>
>  #include <linux/module.h>
>  #include <linux/mutex.h>
> @@ -1031,8 +1032,11 @@ static int hx9023s_bin_load(struct hx9023s_data *data, struct hx9023s_bin *bin)
>  
>  static int hx9023s_send_cfg(const struct firmware *fw, struct hx9023s_data *data)
>  {
> +	if (fw->size < FW_DATA_OFFSET || fw->size > U16_MAX)

Add a comment on why you've picked that upper limit.

> +		return -EINVAL;
> +
>  	struct hx9023s_bin *bin __free(kfree) =
> -		kzalloc(fw->size + sizeof(*bin), GFP_KERNEL);
> +		kzalloc(sizeof(*bin) + fw->size, GFP_KERNEL);

This doesn't belong in the fix given it is just a reorder. Maybe it makes sense
but if it does, separate patch with an explanation of why.

>  	if (!bin)
>  		return -ENOMEM;
>  
> @@ -1041,7 +1045,8 @@ static int hx9023s_send_cfg(const struct firmware *fw, struct hx9023s_data *data
>  	bin->fw_ver = bin->data[FW_VER_OFFSET];
>  	bin->reg_count = get_unaligned_le16(bin->data + FW_REG_CNT_OFFSET);
>  
> -	release_firmware(fw);
> +	if (bin->reg_count > (bin->fw_size - FW_DATA_OFFSET) / 2)
> +		return -EINVAL;
>  
>  	return hx9023s_bin_load(data, bin);
>  }
> @@ -1058,6 +1063,7 @@ static void hx9023s_cfg_update(const struct firmware *fw, void *context)
>  	}
>  
>  	ret = hx9023s_send_cfg(fw, data);
> +	release_firmware(fw);
>  	if (ret) {
>  		dev_warn(dev, "Firmware update failed: %d\n", ret);
>  		goto no_fw;


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

end of thread, other threads:[~2026-07-10 20:21 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-10 14:22 [PATCH] iio: proximity: hx9023s: validate firmware size Laxman Acharya Padhya
2026-07-10 14:54 ` Joshua Crofts
2026-07-10 15:22   ` David Lechner
2026-07-10 15:21 ` David Lechner
2026-07-10 15:28 ` [PATCH v2] " Laxman Acharya Padhya
2026-07-10 20:21   ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox