Input: atmel_mxt_ts - fix boundary check in mxt_prepare_cfg_mem
Subsystems (4)
Dmitry Torokhov
maintainerPATCH 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 >=.
--- drivers/input/touchscreen/atmel_mxt_ts.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/touchscreen/atmel_mxt_ts.c +1 -1
@@ -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
Dmitry Torokhov
maintainerPATCH 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.
--- drivers/input/touchscreen/atmel_mxt_ts.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/input/touchscreen/atmel_mxt_ts.c +9 -2
@@ -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
Dmitry Torokhov
maintainerRe: [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 asdata->mem_size- cfg.start_ofs. Ifdata->mem_sizeis 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
-EINVALifdata->mem_sizeis too small. While at it, change the types ofstart_ofsandmem_sizein structmxt_cfgto u16 to match the device address space.Assisted-by Gemini:gemini-3.1-proSigned-off-by Dmitry Torokhov <[email protected]>--- drivers/input/touchscreen/atmel_mxt_ts.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)drivers/input/touchscreen/atmel_mxt_ts.c +2
-2@@ -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
Dmitry Torokhov
maintainerPATCH 3/3 Input: atmel_mxt_ts - use __free() for obuf in mxt_object_show
In reply to Dmitry TorokhovUse the __free(kfree) macro for the obuf allocation in mxt_object_show() to simplify the code.
--- drivers/input/touchscreen/atmel_mxt_ts.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/input/touchscreen/atmel_mxt_ts.c +3 -7
@@ -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
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 inmxt_object_show()to simplify the code.Assisted-by Gemini:gemini-3.1-proSigned-off-by Dmitry Torokhov <[email protected]>
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@@ -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
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_ofsand 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-proSigned-off-by Dmitry Torokhov <[email protected]>--- drivers/input/touchscreen/atmel_mxt_ts.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/input/touchscreen/atmel_mxt_ts.c +1
-2@@ -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
Dmitry Torokhov
maintainerHi Ricardo,
Quoted 3 lines · Ricardo Ribalda
HI Dmitry
FWIW this patch looks correct to me...
Thank you for looking this over.
Quoted 7 lines
Reviewed-by Ricardo Ribalda <[email protected]>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