Skip to contentSkip to conversation

Input: atmel_mxt_ts - fix boundary check in mxt_prepare_cfg_mem

PATCH In review Dmitry Torokhov 2 participants 7 messages May 4 – May 5

Subsystems (4)
lore
Share & tools
lore

Dmitry Torokhov

author maintainer
#0 lore raw

PATCH 1/3 Input: atmel_mxt_ts - fix boundary check in mxt_prepare_cfg_mem

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")
Assisted-by Gemini:gemini-3.1-pro
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/touchscreen/atmel_mxt_ts.c +1 -1
index d62bf2c95578..28b2bd889c70 100644
@@ -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;
Signature · 1 line
2.54.0.545.g6539524ca2-goog
author maintainer
#1 lore raw

PATCH 2/3 Input: atmel_mxt_ts - check mem_size before calculating config memory size

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
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/input/touchscreen/atmel_mxt_ts.c +9 -2
index 28b2bd889c70..d660cc5b5fe3 100644
@@ -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);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
author maintainer
#2 lore raw

Re: [PATCH 2/3] Input: atmel_mxt_ts - check mem_size before calculating config memory size

Quoted 32 lines · Dmitry Torokhov

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
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/input/touchscreen/atmel_mxt_ts.c +2 -2
index 28b2bd889c70..d660cc5b5fe3 100644
@@ -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;
 };

drivers/input/touchscreen/atmel_mxt_ts.c static int mxt_update_cfg(struct mxt_data *data, const struct firmware *fw) · line 1661

Quoted 5 lines
drivers/input/touchscreen/atmel_mxt_ts.c +2 -0
@@ -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.

Quoted 8 lines
drivers/input/touchscreen/atmel_mxt_ts.c +5 -0
+		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);
Signature · 1 line
Dmitry
author maintainer
#3 lore raw

PATCH 3/3 Input: atmel_mxt_ts - use __free() for obuf in mxt_object_show

In reply to Dmitry Torokhov

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

Assisted-by Gemini:gemini-3.1-pro
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/input/touchscreen/atmel_mxt_ts.c +3 -7
index d660cc5b5fe3..6af4db5ed117 100644
@@ -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,
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Reviewed-by on v1 3/3
#4 lore raw

Re: [PATCH 3/3] Input: atmel_mxt_ts - use __free() for obuf in mxt_object_show

Quoted 6 lines · Dmitry Torokhov

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

Assisted-by Gemini:gemini-3.1-pro
Quoted 45 lines
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/input/touchscreen/atmel_mxt_ts.c +3 -8
index d660cc5b5fe3..6af4db5ed117 100644
@@ -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

Signature · 1 line
Ricardo Ribalda
Reviewed-by on v1 1/3
#5 lore raw
In reply to Dmitry Torokhov

HI Dmitry

FWIW this patch looks correct to me...

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?

Quoted 47 lines · Dmitry Torokhov

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")
Assisted-by Gemini:gemini-3.1-pro
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/touchscreen/atmel_mxt_ts.c +1 -2
index d62bf2c95578..28b2bd889c70 100644
@@ -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

Signature · 1 line
Ricardo Ribalda
author maintainer
#6 lore raw

Hi Ricardo,

Quoted 3 lines · Ricardo Ribalda

HI Dmitry

FWIW this patch looks correct to me...

Thank you for looking this over.

Quoted 7 lines

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

Yeah, I have an unrelated path in my queue that affects line offsets,

Quoted 4 lines

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?

"%n" format specifier does not increment number of successfully parsed elements returned by sscanf(). It kind of makes sense although may look surprising.

Thanks.

Signature · 1 line
Dmitry