Input: elan_i2c - prevent division by zero on invalid device parameters
Subsystems (2)
PATCH 1/1 Input: elan_i2c - prevent division by zero on invalid device parameters
The Elan I2C touchpad driver queries the device for its physical dimensions and trace counts to calculate the device resolution and width. However, if the device firmware or device tree provides invalid zero values for x_traces, y_traces, x_mm, or y_mm, it results in a fatal division-by-zero exception leading to a kernel panic during device probe.
Add sanity checks to ensure these physical parameters are non-zero before performing the division. If invalid values are detected, log an error and return -EINVAL to gracefully abort the initialization and maintain system stability.
--- drivers/input/mouse/elan_i2c_core.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
drivers/input/mouse/elan_i2c_core.c +16 -0
@@ -425,6 +425,14 @@ static int elan_query_device_parameters(struct elan_tp_data *data)if (error)return error; } + +if (unlikely (x_traces ==0 || y_traces ==0 )) { +dev_err (&client->dev, +"Invalid trace numbers: x=%u, y=%u\n" , + x_traces, y_traces); +return -EINVAL ; + } + data->width_x = data->max_x / x_traces; data->width_y = data->max_y / y_traces;
@@ -440,6 +448,14 @@ static int elan_query_device_parameters(struct elan_tp_data *data) data->x_res =elan_convert_resolution (hw_x_res, data->pattern); data->y_res =elan_convert_resolution (hw_y_res, data->pattern); }else { + +if (unlikely (x_mm ==0 || y_mm ==0 )) { +dev_err (&client->dev, +"Invalid physical dimensions: x_mm=%u, y_mm=%u\n" , + x_mm, y_mm); +return -EINVAL ; + } + data->x_res = (data->max_x +1 ) / x_mm; data->y_res = (data->max_y +1 ) / y_mm; }
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
Dmitry Torokhov
maintainerHi Ranjan,
Quoted 6 lines · Ranjan Kumar
The Elan I2C touchpad driver queries the device for its physical dimensions and trace counts to calculate the device resolution and width. However, if the device firmware or device tree provides invalid zero values for
x_traces,y_traces,x_mm, ory_mm, it results in a fatal division-by-zero exception leading to a kernel panic during device probe.
Neither crashing nor aborting probe with -EINVAL is great as we will not be able to flash another firmware.
How do we get into this state? Is this something actually observed or just theoretical?
Quoted 4 lines
Add sanity checks to ensure these physical parameters are non-zero before performing the division. If invalid values are detected, log an error and return
-EINVALto gracefully abort the initialization and maintain system stability.
That is hardly "stability" for a laptop when its pointer is not working.
Thanks.
Signature · 1 line
Dmitry
PATCH v2 1/1 Input: elan_i2c - prevent division by zero on invalid device parameters
The Elan I2C touchpad driver queries the device for its physical dimensions and trace counts to calculate the device resolution and width. However, if the device firmware or device tree provides invalid zero values for x_traces, y_traces, x_mm, or y_mm, it results in a fatal division-by-zero exception leading to a kernel panic during device probe.
Add sanity checks to ensure these physical parameters are non-zero before performing the division. If invalid values are detected, log a warning and fall back to safe default values (1) to safely bypass the division.
This prevents the kernel panic while allowing the probe to complete successfully. Completing the probe ensures the sysfs nodes are created, keeping the firmware update path intact so a recovery firmware can be flashed to the device.
---
Changes in v2:
- Changed error handling from aborting probe with -EINVAL to logging a warning and falling back to default values (1).
- Bypassing the division instead of aborting preserves the sysfs interface for firmware updates and keeps the pointer functional.
drivers/input/mouse/elan_i2c_core.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-)
drivers/input/mouse/elan_i2c_core.c +21 -4
@@ -425,8 +425,17 @@ static int elan_query_device_parameters(struct elan_tp_data *data)if (error)return error; } - data->width_x = data->max_x / x_traces; - data->width_y = data->max_y / y_traces; + +if (unlikely (x_traces ==0 || y_traces ==0 )) { +dev_warn (&client->dev, +"invalid trace numbers: x=%u, y=%u\n" , + x_traces, y_traces); + data->width_x =1 ; + data->width_y =1 ; + }else { + data->width_x = data->max_x / x_traces; + data->width_y = data->max_y / y_traces; + } if (device_property_read_u32(&client->dev, "touchscreen-x-mm", &x_mm) ||
@@ -440,8 +449,16 @@ static int elan_query_device_parameters(struct elan_tp_data *data) data->x_res =elan_convert_resolution (hw_x_res, data->pattern); data->y_res =elan_convert_resolution (hw_y_res, data->pattern); }else { - data->x_res = (data->max_x +1 ) / x_mm; - data->y_res = (data->max_y +1 ) / y_mm; +if (unlikely (x_mm ==0 || y_mm ==0 )) { +dev_warn (&client->dev, +"invalid physical dimensions: x_mm=%u, y_mm=%u\n" , + x_mm, y_mm); + data->x_res =1 ; + data->y_res =1 ; + }else { + data->x_res = (data->max_x +1 ) / x_mm; + data->y_res = (data->max_y +1 ) / y_mm; + } }if (device_property_read_bool (&client->dev,"elan,clickpad" ))
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Setting `width_x` and `width_y` to 1 as a fallback introduces a severe arithmetic underflow when…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Setting width_x and width_y to 1 as a fallback introduces a severe arithmetic underflow when calculating touch area sizes. --
commit 592cc1df694c917c489ca1a6dcea8bd8b7a8d985
Author: Ranjan Kumar <[email protected]>
Input: elan_i2c - prevent division by zero on invalid device parameters
The Elan I2C touchpad driver queries the device for its physical dimensions and trace counts to calculate the device resolution and width. If the device provides invalid zero values, this commit adds checks to fall back to safe default values of 1 to bypass the division and prevent a kernel panic.
Quoted 7 lines
drivers/input/mouse/elan_i2c_core.c +0
-0@@ -425,8 +425,17 @@ static int elan_query_device_parameters(struct elan_tp_data *data)if (error)return error;
drivers/input/mouse/elan_i2c_core.c static int elan_query_device_parameters(struct elan_tp_data *data) · line 434
Quoted 10 lines
drivers/input/mouse/elan_i2c_core.c +7
-2} - data->width_x = data->max_x / x_traces; - data->width_y = data->max_y / y_traces; + +if (unlikely (x_traces ==0 || y_traces ==0 )) { +dev_warn (&client->dev, +"invalid trace numbers: x=%u, y=%u\n" , + x_traces, y_traces); + data->width_x =1 ; + data->width_y =1 ;
Does setting these fallback values to 1 introduce an arithmetic underflow regression when calculating touch area sizes?
In elan_report_absolute() and elan_report_contact(), the touch area sizes are calculated using an expression like:
area_x = mk_x * (data->width_x - ETP_FWIDTH_REDUCE);
Since ETP_FWIDTH_REDUCE is 90 and data->width_x is an unsigned int, setting the width to 1 causes the subtraction to underflow, resulting in a massive unsigned integer.
When passed to input_report_abs(), this value is cast back to a negative number, violating the 0 minimum boundary configured for ABS_MT_TOUCH_MAJOR.
Could this confuse userspace input handlers and trigger unwanted behaviors?
To prevent this underflow while maintaining the firmware update fallback path, should the fallback value be at least ETP_FWIDTH_REDUCE (90) instead of 1?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v3 1/1 Input: elan_i2c - prevent division by zero on invalid device parameters
The Elan I2C touchpad driver queries the device for its physical dimensions and trace counts to calculate the device resolution and width. However, if the device firmware or device tree provides invalid zero values for x_traces, y_traces, x_mm, or y_mm, it results in a fatal division-by-zero exception leading to a kernel panic during device probe.
Add sanity checks to ensure these physical parameters are non-zero before performing the division. If invalid trace values are detected, log a warning and fall back to ETP_FWIDTH_REDUCE to prevent arithmetic underflow during touch reporting. For invalid physical dimensions, fall back to a safe default of 1.
This prevents the kernel panic while allowing the probe to complete successfully. Completing the probe ensures the sysfs nodes are created, keeping the firmware update path intact so a recovery firmware can be flashed to the device.
---
Changes in v3:
- Changed trace fallback values from 1 to ETP_FWIDTH_REDUCE to prevent an unsigned integer underflow in elan_report_absolute().
Changes in v2:
- Changed error handling from aborting probe with -EINVAL to logging a warning and falling back to default values.
drivers/input/mouse/elan_i2c_core.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-)
drivers/input/mouse/elan_i2c_core.c +21 -4
@@ -425,8 +425,17 @@ static int elan_query_device_parameters(struct elan_tp_data *data)if (error)return error; } - data->width_x = data->max_x / x_traces; - data->width_y = data->max_y / y_traces; + +if (unlikely (x_traces ==0 || y_traces ==0 )) { +dev_warn (&client->dev, +"invalid trace numbers: x=%u, y=%u\n" , + x_traces, y_traces); + data->width_x =ETP_FWIDTH_REDUCE ; + data->width_y =ETP_FWIDTH_REDUCE ; + }else { + data->width_x = data->max_x / x_traces; + data->width_y = data->max_y / y_traces; + } if (device_property_read_u32(&client->dev, "touchscreen-x-mm", &x_mm) ||
@@ -440,8 +449,16 @@ static int elan_query_device_parameters(struct elan_tp_data *data) data->x_res =elan_convert_resolution (hw_x_res, data->pattern); data->y_res =elan_convert_resolution (hw_y_res, data->pattern); }else { - data->x_res = (data->max_x +1 ) / x_mm; - data->y_res = (data->max_y +1 ) / y_mm; +if (unlikely (x_mm ==0 || y_mm ==0 )) { +dev_warn (&client->dev, +"invalid physical dimensions: x_mm=%u, y_mm=%u\n" , + x_mm, y_mm); + data->x_res =1 ; + data->y_res =1 ; + }else { + data->x_res = (data->max_x +1 ) / x_mm; + data->y_res = (data->max_y +1 ) / y_mm; + } }if (device_property_read_bool (&client->dev,"elan,clickpad" ))
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
Dmitry Torokhov
maintainerHi Ranjan,
Quoted 10 lines · Ranjan Kumar
The Elan I2C touchpad driver queries the device for its physical dimensions and trace counts to calculate the device resolution and width. However, if the device firmware or device tree provides invalid zero values for
x_traces,y_traces,x_mm, ory_mm, it results in a fatal division-by-zero exception leading to a kernel panic during device probe.Add sanity checks to ensure these physical parameters are non-zero before performing the division. If invalid trace values are detected, log a warning and fall back to
ETP_FWIDTH_REDUCEto prevent arithmetic underflow during touch reporting.
I would define some defaults that are not necessarily the same as ETP_FWIDTH_REDUCE. The arithmetic overflow should be handled separately, as it may still happen if we read (or set up via device tree) some small (but non-zero) values.
Quoted 27 lines
For invalid physical dimensions, fall
back to a safe default of 1.This prevents the kernel panic while allowing the probe to complete successfully. Completing the probe ensures the sysfs nodes are created, keeping the firmware update path intact so a recovery firmware can be flashed to the device.
Fixes 6696777c6506 ("Input: add driver for Elan I2C/SMbus touchpad")Fixes e3a9a1290688 ("Input: elan_i2c - do not query the info if they are provided")Signed-off-by Ranjan Kumar <[email protected]>---
Changes in v3:
- Changed trace fallback values from 1 toETP_FWIDTH_REDUCEto prevent an unsigned integer underflow inelan_report_absolute().
Changes in v2:
- Changed error handling from aborting probe with-EINVALto logging a warning and falling back to default values.drivers/input/mouse/elan_i2c_core.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-)drivers/input/mouse/elan_i2c_core.c +0
-0
drivers/input/mouse/elan_i2c_core.c static int elan_query_device_parameters(struct elan_tp_data *data) · line 429
Quoted 7 lines
drivers/input/mouse/elan_i2c_core.c +2
-2@@ -425,8 +425,17 @@ static int elan_query_device_parameters(struct elan_tp_data *data)if (error)return error; } - data->width_x = data->max_x / x_traces; - data->width_y = data->max_y / y_traces; + +if (unlikely (x_traces ==0 || y_traces ==0 )) {
I'd say "if (!x_traces || !y_traces) ...". This is not hot path so annotating with unlikely does not buy us anything.
I wonder if we should be comparing with some threshold instead of 0. Something above 90 I guess.
Thanks.
Signature · 1 line
Dmitry
PATCH v4 1/1 Input: elan_i2c - prevent division by zero and arithmetic underflow
The Elan I2C touchpad driver queries the device for its physical dimensions and trace counts to calculate the device resolution and width. However, if the device firmware or device tree provides invalid zero values for x_traces or y_traces, it results in a fatal division-by-zero exception leading to a kernel panic during device probe.
Add checks to ensure these parameters are non-zero before performing the division. If invalid trace values are detected, fall back to a safe default of 1.
Additionally, prevent an arithmetic underflow in the touch reporting logic. Previously, if the calculated or fallback width was smaller than ETP_FWIDTH_REDUCE (90), the subtraction would underflow, resulting in a massive unsigned integer being reported to userspace. Clamp the adjusted width to a minimum of 0 to safely handle small physical dimensions and fallback scenarios.
Completing the probe with safe fallback values ensures the sysfs nodes are created, keeping the firmware update path intact so a recovery firmware can be flashed to the device.
---
Changes in v4:
- Reverted probe fallback width back to 1.
- Added check in elan_report_contact() to clamp adjusted widths to a minimum of 0, fixing the root cause of the arithmetic underflow.
Changes in v3:
- Changed fallback width from 1 to ETP_FWIDTH_REDUCE to prevent underflow.
Changes in v2:
- Added check for invalid trace values of 0 to prevent division by zero.
drivers/input/mouse/elan_i2c_core.c | 36 ++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 6 deletions(-)
drivers/input/mouse/elan_i2c_core.c +30 -6
@@ -428,8 +428,17 @@ static int elan_query_device_parameters(struct elan_tp_data *data)if (error)return error; } - data->width_x = data->max_x / x_traces; - data->width_y = data->max_y / y_traces; + +if (!x_traces || !y_traces) { +dev_warn (&client->dev, +"invalid trace numbers: x=%u, y=%u\n" , + x_traces, y_traces); + data->width_x =1 ; + data->width_y =1 ; + }else { + data->width_x = data->max_x / x_traces; + data->width_y = data->max_y / y_traces; + } if (device_property_read_u32(&client->dev, "touchscreen-x-mm", &x_mm) ||
@@ -443,8 +452,16 @@ static int elan_query_device_parameters(struct elan_tp_data *data) data->x_res =elan_convert_resolution (hw_x_res, data->pattern); data->y_res =elan_convert_resolution (hw_y_res, data->pattern); }else { - data->x_res = (data->max_x +1 ) / x_mm; - data->y_res = (data->max_y +1 ) / y_mm; +if (unlikely (x_mm ==0 || y_mm ==0 )) { +dev_warn (&client->dev, +"invalid physical dimensions: x_mm=%u, y_mm=%u\n" , + x_mm, y_mm); + data->x_res =1 ; + data->y_res =1 ; + }else { + data->x_res = (data->max_x +1 ) / x_mm; + data->y_res = (data->max_y +1 ) / y_mm; + } }if (device_property_read_bool (&client->dev,"elan,clickpad" ))
@@ -956,6 +973,7 @@ static void elan_report_contact(struct elan_tp_data *data, int contact_num,if (data->report_features &ETP_FEATURE_REPORT_MK ) {unsigned int mk_x, mk_y, area_x, area_y; +int adj_width_x, adj_width_y;u8 mk_data = high_precision ? packet[ETP_MK_DATA_OFFSET + contact_num] : finger_data[3 ];
@@ -967,8 +985,14 @@ static void elan_report_contact(struct elan_tp_data *data, int contact_num,* To avoid treating large finger as palm, let's reduce * the width x and y per trace. */ - area_x = mk_x * (data->width_x -ETP_FWIDTH_REDUCE ); - area_y = mk_y * (data->width_y -ETP_FWIDTH_REDUCE ); + + adj_width_x = data->width_x >ETP_FWIDTH_REDUCE ? + data->width_x -ETP_FWIDTH_REDUCE :0 ; + adj_width_y = data->width_y >ETP_FWIDTH_REDUCE ? + data->width_y -ETP_FWIDTH_REDUCE :0 ; + + area_x = mk_x * adj_width_x; + area_y = mk_y * adj_width_y;input_report_abs (input,ABS_TOOL_WIDTH , mk_x); input_report_abs(input, ABS_MT_TOUCH_MAJOR,
Signature · 1 line
2.54.0.1099.g489fc7bff1-goog
Re: [PATCH v4] Input: elan_i2c - prevent division by zero and arithmetic underflow
Quoted 17 lines · Ranjan Kumar
The Elan I2C touchpad driver queries the device for its physical dimensions and trace counts to calculate the device resolution and width. However, if the device firmware or device tree provides invalid zero values for
x_tracesory_traces, it results in a fatal division-by-zero exception leading to a kernel panic during device probe.Add checks to ensure these parameters are non-zero before performing the division. If invalid trace values are detected, fall back to a safe default of 1.
Additionally, prevent an arithmetic underflow in the touch reporting logic. Previously, if the calculated or fallback width was smaller than
ETP_FWIDTH_REDUCE(90), the subtraction would underflow, resulting in a massive unsigned integer being reported to userspace. Clamp the adjusted width to a minimum of 0 to safely handle small physical dimensions and fallback scenarios.
Quoted 7 lines
Completing the probe with safe fallback values ensures the sysfs nodes are created, keeping the firmware update path intact so a recovery firmware can be flashed to the device.
Fixes 6696777c6506 ("Input: add driver for Elan I2C/SMbus touchpad")Fixes e3a9a1290688 ("Input: elan_i2c - do not query the info if they are provided")Signed-off-by Ranjan Kumar <[email protected]>
Applied, thank you.
Signature · 1 line
Dmitry