public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/3] Input: atmel_mxt_ts - fix boundary check in mxt_prepare_cfg_mem
@ 2026-05-04 18:54 Dmitry Torokhov
  2026-05-04 18:54 ` [PATCH 2/3] Input: atmel_mxt_ts - check mem_size before calculating config memory size Dmitry Torokhov
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Dmitry Torokhov @ 2026-05-04 18:54 UTC (permalink / raw)
  To: Nick Dyer, linux-input; +Cc: Ricardo Ribalda, linux-kernel, stable

When a configuration file provides an object size that is larger than the
driver's known mxt_obj_size(object), the driver intends to discard the
extra bytes.

The loop iterates using for (i = 0; i < size; i++). Inside the loop, the
condition to skip processing extra bytes is:

    if (i > mxt_obj_size(object))
        continue;

Since i is a 0-based index, the valid indices for the object are 0 through
mxt_obj_size(object) - 1.

When i == mxt_obj_size(object), the condition evaluates to false, and the
code processes the byte instead of discarding it.

This causes the code to calculate byte_offset = reg + i - cfg->start_ofs
and writes the byte there, overwriting exactly one byte of the adjacent
instance or object.

Update the boundary check to skip extra bytes correctly by using >=.

Fixes: 50a77c658b80 ("Input: atmel_mxt_ts - download device config using firmware loader")
Cc: stable@vger.kernel.org
Assisted-by: Gemini:gemini-3.1-pro
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c b/drivers/input/touchscreen/atmel_mxt_ts.c
index d62bf2c95578..28b2bd889c70 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -1503,7 +1503,7 @@ static int mxt_prepare_cfg_mem(struct mxt_data *data, struct mxt_cfg *cfg)
 			}
 			cfg->raw_pos += offset;
 
-			if (i > mxt_obj_size(object))
+			if (i >= mxt_obj_size(object))
 				continue;
 
 			byte_offset = reg + i - cfg->start_ofs;
-- 
2.54.0.545.g6539524ca2-goog


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

* [PATCH 2/3] Input: atmel_mxt_ts - check mem_size before calculating config memory size
  2026-05-04 18:54 [PATCH 1/3] Input: atmel_mxt_ts - fix boundary check in mxt_prepare_cfg_mem Dmitry Torokhov
@ 2026-05-04 18:54 ` Dmitry Torokhov
  2026-05-04 22:59   ` Dmitry Torokhov
  2026-05-04 18:54 ` [PATCH 3/3] Input: atmel_mxt_ts - use __free() for obuf in mxt_object_show Dmitry Torokhov
  2026-05-05  9:08 ` [PATCH 1/3] Input: atmel_mxt_ts - fix boundary check in mxt_prepare_cfg_mem Ricardo Ribalda
  2 siblings, 1 reply; 6+ messages in thread
From: Dmitry Torokhov @ 2026-05-04 18:54 UTC (permalink / raw)
  To: Nick Dyer, linux-input; +Cc: Ricardo Ribalda, linux-kernel

In mxt_update_cfg(), the driver calculates the memory size needed to store
the configuration as data->mem_size - cfg.start_ofs. If data->mem_size is
less than or equal to cfg.start_ofs, this calculation will underflow or
result in a zero-size buffer, neither of which is valid for a configuration
update.

Add a check to return -EINVAL if data->mem_size is too small. While at it,
change the types of start_ofs and mem_size in struct mxt_cfg to u16 to
match the device address space.

Assisted-by: Gemini:gemini-3.1-pro
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c b/drivers/input/touchscreen/atmel_mxt_ts.c
index 28b2bd889c70..d660cc5b5fe3 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -275,8 +275,8 @@ struct mxt_cfg {
 	off_t raw_pos;
 
 	u8 *mem;
-	size_t mem_size;
-	int start_ofs;
+	u16 mem_size;
+	u16 start_ofs;
 
 	struct mxt_info info;
 };
@@ -1657,6 +1657,13 @@ static int mxt_update_cfg(struct mxt_data *data, const struct firmware *fw)
 	cfg.start_ofs = MXT_OBJECT_START +
 			data->info->object_num * sizeof(struct mxt_object) +
 			MXT_INFO_CHECKSUM_SIZE;
+
+	if (data->mem_size < cfg.start_ofs) {
+		dev_err(dev, "Memory size too small: %u < %u\n",
+			data->mem_size, cfg.start_ofs);
+		return -EINVAL;
+	}
+
 	cfg.mem_size = data->mem_size - cfg.start_ofs;
 
 	u8 *mem_buf __free(kfree) = cfg.mem = kzalloc(cfg.mem_size, GFP_KERNEL);
-- 
2.54.0.545.g6539524ca2-goog


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

* [PATCH 3/3] Input: atmel_mxt_ts - use __free() for obuf in mxt_object_show
  2026-05-04 18:54 [PATCH 1/3] Input: atmel_mxt_ts - fix boundary check in mxt_prepare_cfg_mem Dmitry Torokhov
  2026-05-04 18:54 ` [PATCH 2/3] Input: atmel_mxt_ts - check mem_size before calculating config memory size Dmitry Torokhov
@ 2026-05-04 18:54 ` Dmitry Torokhov
  2026-05-05  9:10   ` Ricardo Ribalda
  2026-05-05  9:08 ` [PATCH 1/3] Input: atmel_mxt_ts - fix boundary check in mxt_prepare_cfg_mem Ricardo Ribalda
  2 siblings, 1 reply; 6+ messages in thread
From: Dmitry Torokhov @ 2026-05-04 18:54 UTC (permalink / raw)
  To: Nick Dyer, linux-input; +Cc: Ricardo Ribalda, linux-kernel

Use the __free(kfree) macro for the obuf allocation in mxt_object_show()
to simplify the code.

Assisted-by: Gemini:gemini-3.1-pro
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c b/drivers/input/touchscreen/atmel_mxt_ts.c
index d660cc5b5fe3..6af4db5ed117 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -2870,14 +2870,12 @@ static ssize_t mxt_object_show(struct device *dev,
 	int count = 0;
 	int i, j;
 	int error;
-	u8 *obuf;
 
 	/* Pre-allocate buffer large enough to hold max sized object. */
-	obuf = kmalloc(256, GFP_KERNEL);
+	u8 *obuf __free(kfree) = kmalloc(256, GFP_KERNEL);
 	if (!obuf)
 		return -ENOMEM;
 
-	error = 0;
 	for (i = 0; i < data->info->object_num; i++) {
 		object = data->object_table + i;
 
@@ -2892,15 +2890,13 @@ static ssize_t mxt_object_show(struct device *dev,
 
 			error = __mxt_read_reg(data->client, addr, size, obuf);
 			if (error)
-				goto done;
+				return error;
 
 			count = mxt_show_instance(buf, count, object, j, obuf);
 		}
 	}
 
-done:
-	kfree(obuf);
-	return error ?: count;
+	return count;
 }
 
 static int mxt_check_firmware_format(struct device *dev,
-- 
2.54.0.545.g6539524ca2-goog


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

* Re: [PATCH 2/3] Input: atmel_mxt_ts - check mem_size before calculating config memory size
  2026-05-04 18:54 ` [PATCH 2/3] Input: atmel_mxt_ts - check mem_size before calculating config memory size Dmitry Torokhov
@ 2026-05-04 22:59   ` Dmitry Torokhov
  0 siblings, 0 replies; 6+ messages in thread
From: Dmitry Torokhov @ 2026-05-04 22:59 UTC (permalink / raw)
  To: Nick Dyer, linux-input; +Cc: Ricardo Ribalda, linux-kernel

On Mon, May 04, 2026 at 11:54:46AM -0700, Dmitry Torokhov wrote:
> In mxt_update_cfg(), the driver calculates the memory size needed to store
> the configuration as data->mem_size - cfg.start_ofs. If data->mem_size is
> less than or equal to cfg.start_ofs, this calculation will underflow or
> result in a zero-size buffer, neither of which is valid for a configuration
> update.
> 
> Add a check to return -EINVAL if data->mem_size is too small. While at it,
> change the types of start_ofs and mem_size in struct mxt_cfg to u16 to
> match the device address space.
> 
> Assisted-by: Gemini:gemini-3.1-pro
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> ---
>  drivers/input/touchscreen/atmel_mxt_ts.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c b/drivers/input/touchscreen/atmel_mxt_ts.c
> index 28b2bd889c70..d660cc5b5fe3 100644
> --- a/drivers/input/touchscreen/atmel_mxt_ts.c
> +++ b/drivers/input/touchscreen/atmel_mxt_ts.c
> @@ -275,8 +275,8 @@ struct mxt_cfg {
>  	off_t raw_pos;
>  
>  	u8 *mem;
> -	size_t mem_size;
> -	int start_ofs;
> +	u16 mem_size;
> +	u16 start_ofs;
>  
>  	struct mxt_info info;
>  };
> @@ -1657,6 +1657,13 @@ static int mxt_update_cfg(struct mxt_data *data, const struct firmware *fw)
>  	cfg.start_ofs = MXT_OBJECT_START +
>  			data->info->object_num * sizeof(struct mxt_object) +
>  			MXT_INFO_CHECKSUM_SIZE;
> +
> +	if (data->mem_size < cfg.start_ofs) {

This is supposed to be "<=", like the commit message says.

> +		dev_err(dev, "Memory size too small: %u < %u\n",
> +			data->mem_size, cfg.start_ofs);
> +		return -EINVAL;
> +	}
> +
>  	cfg.mem_size = data->mem_size - cfg.start_ofs;
>  
>  	u8 *mem_buf __free(kfree) = cfg.mem = kzalloc(cfg.mem_size, GFP_KERNEL);

-- 
Dmitry

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

* Re: [PATCH 1/3] Input: atmel_mxt_ts - fix boundary check in mxt_prepare_cfg_mem
  2026-05-04 18:54 [PATCH 1/3] Input: atmel_mxt_ts - fix boundary check in mxt_prepare_cfg_mem Dmitry Torokhov
  2026-05-04 18:54 ` [PATCH 2/3] Input: atmel_mxt_ts - check mem_size before calculating config memory size Dmitry Torokhov
  2026-05-04 18:54 ` [PATCH 3/3] Input: atmel_mxt_ts - use __free() for obuf in mxt_object_show Dmitry Torokhov
@ 2026-05-05  9:08 ` Ricardo Ribalda
  2 siblings, 0 replies; 6+ messages in thread
From: Ricardo Ribalda @ 2026-05-05  9:08 UTC (permalink / raw)
  To: Dmitry Torokhov; +Cc: Nick Dyer, linux-input, linux-kernel, stable

HI Dmitry

FWIW this patch looks correct to me...

Reviewed-by: Ricardo Ribalda <ribalda@chromium.org>

But there are a couple of things that look weird.

1) The patch line (1503) does not seem to match your tree
https://git.kernel.org/pub/scm/linux/kernel/git/dtor/input.git/tree/drivers/input/touchscreen/atmel_mxt_ts.c#n1503

2) The sscanf just before this check has two conversions (val and
offset), but you only check for ret != 1. Should't it be ret !=2? or I
am missing something?

On Mon, 4 May 2026 at 20:54, Dmitry Torokhov <dmitry.torokhov@gmail.com> wrote:
>
> When a configuration file provides an object size that is larger than the
> driver's known mxt_obj_size(object), the driver intends to discard the
> extra bytes.
>
> The loop iterates using for (i = 0; i < size; i++). Inside the loop, the
> condition to skip processing extra bytes is:
>
>     if (i > mxt_obj_size(object))
>         continue;
>
> Since i is a 0-based index, the valid indices for the object are 0 through
> mxt_obj_size(object) - 1.
>
> When i == mxt_obj_size(object), the condition evaluates to false, and the
> code processes the byte instead of discarding it.
>
> This causes the code to calculate byte_offset = reg + i - cfg->start_ofs
> and writes the byte there, overwriting exactly one byte of the adjacent
> instance or object.
>
> Update the boundary check to skip extra bytes correctly by using >=.
>
> Fixes: 50a77c658b80 ("Input: atmel_mxt_ts - download device config using firmware loader")
> Cc: stable@vger.kernel.org
> Assisted-by: Gemini:gemini-3.1-pro
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> ---
>  drivers/input/touchscreen/atmel_mxt_ts.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c b/drivers/input/touchscreen/atmel_mxt_ts.c
> index d62bf2c95578..28b2bd889c70 100644
> --- a/drivers/input/touchscreen/atmel_mxt_ts.c
> +++ b/drivers/input/touchscreen/atmel_mxt_ts.c
> @@ -1503,7 +1503,7 @@ static int mxt_prepare_cfg_mem(struct mxt_data *data, struct mxt_cfg *cfg)
>                         }
>                         cfg->raw_pos += offset;
>
> -                       if (i > mxt_obj_size(object))
> +                       if (i >= mxt_obj_size(object))
>                                 continue;
>
>                         byte_offset = reg + i - cfg->start_ofs;
> --
> 2.54.0.545.g6539524ca2-goog
>


-- 
Ricardo Ribalda

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

* Re: [PATCH 3/3] Input: atmel_mxt_ts - use __free() for obuf in mxt_object_show
  2026-05-04 18:54 ` [PATCH 3/3] Input: atmel_mxt_ts - use __free() for obuf in mxt_object_show Dmitry Torokhov
@ 2026-05-05  9:10   ` Ricardo Ribalda
  0 siblings, 0 replies; 6+ messages in thread
From: Ricardo Ribalda @ 2026-05-05  9:10 UTC (permalink / raw)
  To: Dmitry Torokhov; +Cc: Nick Dyer, linux-input, linux-kernel

On Mon, 4 May 2026 at 20:54, Dmitry Torokhov <dmitry.torokhov@gmail.com> wrote:
>
> Use the __free(kfree) macro for the obuf allocation in mxt_object_show()
> to simplify the code.
>
> Assisted-by: Gemini:gemini-3.1-pro
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Reviewed-by: Ricardo Ribalda <ribalda@chromium.org>
> ---
>  drivers/input/touchscreen/atmel_mxt_ts.c | 10 +++-------
>  1 file changed, 3 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c b/drivers/input/touchscreen/atmel_mxt_ts.c
> index d660cc5b5fe3..6af4db5ed117 100644
> --- a/drivers/input/touchscreen/atmel_mxt_ts.c
> +++ b/drivers/input/touchscreen/atmel_mxt_ts.c
> @@ -2870,14 +2870,12 @@ static ssize_t mxt_object_show(struct device *dev,
>         int count = 0;
>         int i, j;
>         int error;
> -       u8 *obuf;
>
>         /* Pre-allocate buffer large enough to hold max sized object. */
> -       obuf = kmalloc(256, GFP_KERNEL);
> +       u8 *obuf __free(kfree) = kmalloc(256, GFP_KERNEL);
>         if (!obuf)
>                 return -ENOMEM;
>
> -       error = 0;
>         for (i = 0; i < data->info->object_num; i++) {
>                 object = data->object_table + i;
>
> @@ -2892,15 +2890,13 @@ static ssize_t mxt_object_show(struct device *dev,
>
>                         error = __mxt_read_reg(data->client, addr, size, obuf);
>                         if (error)
> -                               goto done;
> +                               return error;
>
>                         count = mxt_show_instance(buf, count, object, j, obuf);
>                 }
>         }
>
> -done:
> -       kfree(obuf);
> -       return error ?: count;
> +       return count;
>  }
>
>  static int mxt_check_firmware_format(struct device *dev,
> --
> 2.54.0.545.g6539524ca2-goog
>


-- 
Ricardo Ribalda

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

end of thread, other threads:[~2026-05-05  9:10 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-04 18:54 [PATCH 1/3] Input: atmel_mxt_ts - fix boundary check in mxt_prepare_cfg_mem Dmitry Torokhov
2026-05-04 18:54 ` [PATCH 2/3] Input: atmel_mxt_ts - check mem_size before calculating config memory size Dmitry Torokhov
2026-05-04 22:59   ` Dmitry Torokhov
2026-05-04 18:54 ` [PATCH 3/3] Input: atmel_mxt_ts - use __free() for obuf in mxt_object_show Dmitry Torokhov
2026-05-05  9:10   ` Ricardo Ribalda
2026-05-05  9:08 ` [PATCH 1/3] Input: atmel_mxt_ts - fix boundary check in mxt_prepare_cfg_mem Ricardo Ribalda

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