Input: joydev - avoid an undefined shift in the legacy 0x read
Subsystems (8)
PATCH 1/1 Input: joydev - avoid an undefined shift in the legacy 0x read
The legacy 0.x read path shifts the axis values by the calibration constants that JS_SET_CAL installs:
data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x; data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y;
JS_CORR is copied straight from userspace as three s32 values and never validated, so the shift count can be negative or wider than the type, which is undefined behavior: the shift compiles to whatever the architecture does with an out-of-range count, and the compiler is free to assume it cannot happen.
Clamp the shift count to the width of the shifted value.
--- drivers/input/joydev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/joydev.c +2 -2
@@ -353,8 +353,8 @@ static ssize_t joydev_0x_read(struct joydev_client *client,for (data.buttons = i =0 ; i <32 && i < joydev->nkey; i++) data.buttons |=test_bit (joydev->keypam[i], input->key) ? (1 << i) :0 ; - data.x = (joydev->abs[0 ] /256 +128 ) >> joydev->glue.JS_CORR.x; - data.y = (joydev->abs[1 ] /256 +128 ) >> joydev->glue.JS_CORR.y; + data.x = (joydev->abs[0 ] /256 +128 ) >>clamp (joydev->glue.JS_CORR.x,0 ,31 ); + data.y = (joydev->abs[1 ] /256 +128 ) >>clamp (joydev->glue.JS_CORR.y,0 ,31 );/* * Reset reader's event queue
Signature · 1 line
2.50.1
PATCH 1/1 Input: joydev - avoid signed overflow in the broken-axis correction
The JS_CORR_BROKEN correction multiplies two user-controlled 32-bit values before shifting and clamping the result:
value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 : ((corr->coef[3] * (value - corr->coef[1])) >> 14)) : ((corr->coef[2] * (value - corr->coef[0])) >> 14);
The coefficients come from JSIOCSCORR without any range check, so the product can overflow the type, which is undefined behavior: the compiler is allowed to assume it does not happen and optimize accordingly.
Do the multiplication in 64 bits, which is bit-identical for every in-range product.
--- drivers/input/joydev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/joydev.c +2 -2
@@ -74,8 +74,8 @@ static int joydev_correct(int value, struct js_corr *corr)case JS_CORR_BROKEN : value = value > corr->coef[0 ] ? (value < corr->coef[1 ] ?0 : - ((corr->coef[3 ] * (value - corr->coef[1 ])) >>14 )) : - ((corr->coef[2 ] * (value - corr->coef[0 ])) >>14 ); + (((s64 )corr->coef[3 ] * (value - corr->coef[1 ])) >>14 )) : + (((s64 )corr->coef[2 ] * (value - corr->coef[0 ])) >>14 );break ;default :
Signature · 1 line
2.50.1
[email protected]
AI reviewRe: [PATCH] Input: joydev - avoid signed overflow in the broken-axis correction
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Input: joydev - 32-bit overflow in joydev_correct() subtraction - [Medium] Input: joydev - s64…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Input: joydev - 32-bit overflow in joydev_correct() subtraction
- [Medium] Input: joydev - s64 truncation before clamp() in joydev_correct() --
commit 1426ece8bea8b467a9629c0559c54b6693b90b43
Author: Guanglei Zhu <[email protected]>
Input: joydev - avoid signed overflow in the broken-axis correction
This commit casts the coefficient operands in the JS_CORR_BROKEN calculation in joydev_correct() to s64 to avoid 32-bit signed integer overflow during multiplication when user-controlled coefficients are set via JSIOCSCORR.
drivers/input/joydev.c static int joydev_correct(int value, struct js_corr *corr) · line 78
Quoted 12 lines
drivers/input/joydev.c +2
-2@@ -74,8 +74,8 @@ static int joydev_correct(int value, struct js_corr *corr)case JS_CORR_BROKEN : value = value > corr->coef[0 ] ? (value < corr->coef[1 ] ?0 : - ((corr->coef[3 ] * (value - corr->coef[1 ])) >>14 )) : - ((corr->coef[2 ] * (value - corr->coef[0 ])) >>14 ); + (((s64 )corr->coef[3 ] * (value - corr->coef[1 ])) >>14 )) : + (((s64 )corr->coef[2 ] * (value - corr->coef[0 ])) >>14 );
[Severity: Medium]
Can the parenthesized subtractions (value - corr->coef[1]) and (value - corr->coef[0]) in joydev_correct() in drivers/input/joydev.c still overflow 32-bit signed arithmetic before being promoted to s64 for the multiplication?
When joydev_ioctl_common() processes JSIOCSCORR or joydev_event() handles an EV_ABS event and calls joydev_correct(), both value (int) and corr->coef[i] (__s32) are 32-bit signed integers. In the first branch, where value > corr->coef[0] and value >= corr->coef[1], if value >= 0 and corr->coef[0] and corr->coef[1] are large negative values such as INT_MIN, evaluating value - corr->coef[1] overflows a 32-bit signed int before promoting to s64. Likewise, in the second branch where value <= corr->coef[0], if value < 0 and corr->coef[0] is a large positive value such as INT_MAX, value - corr->coef[0] underflows a 32-bit signed int.
Would casting value to s64 inside the subtraction, such as (s64)value - corr->coef[1] and (s64)value - corr->coef[0], avoid this overflow as well?
[Severity: Medium]
What happens in joydev_correct() in drivers/input/joydev.c when the shifted 64-bit product exceeds the 32-bit signed int range before reaching clamp()?
Because two 32-bit signed values multiplied in s64 can have a magnitude up to roughly 2^62, the result after >> 14 can still have a magnitude up to roughly 2^48. Assigning that s64 expression back into the 32-bit int variable value:
drivers/input/joydev.c:joydev_correct() {
...
case JS_CORR_BROKEN:
value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 :
(((s64)corr->coef[3] * (value - corr->coef[1])) >> 14)) :
(((s64)corr->coef[2] * (value - corr->coef[0])) >> 14);
break;
default:
return 0;
}
return clamp(value, -32767, 32767);
}truncates the upper bits before clamp(value, -32767, 32767) is evaluated, causing large positive or negative results to wrap around rather than saturate to 32767 or -32767. Should value or the intermediate result be kept as s64 until clamp_val() or clamp_t(s64, ...) is applied?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 1/1 Input: joydev - avoid signed overflow in the broken-axis correction
In reply to Guanglei ZhuThe JS_CORR_BROKEN correction multiplies two user-controlled 32-bit values before shifting and clamping the result:
value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 : ((corr->coef[3] * (value - corr->coef[1])) >> 14)) : ((corr->coef[2] * (value - corr->coef[0])) >> 14);
The coefficients come from JSIOCSCORR without any range check, so three things can go wrong: the subtractions can overflow 32 bits (value = 0 against coef[1] = INT_MIN), the products can overflow the type, and a product that survives the multiplication in 64 bits can still wrap when the shifted result is assigned back to the int variable, which then slips through clamp() as a small value instead of saturating.
Compute the whole correction in 64 bits, including the subtractions, and clamp with clamp_t(s64, ...) before narrowing, which is bit-identical for every in-range input.
---
v2: per review, extend the 64-bit computation to cover the subtractions
((s64)value - coef, which could overflow 32 bits before the promotion in v1) and keep the shifted product in s64 through clamp_t(s64, ...) so an out-of-range result saturates instead of wrapping during the narrowing to int.
drivers/input/joydev.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/input/joydev.c +8 -4
@@ -72,11 +72,15 @@ static int joydev_correct(int value, struct js_corr *corr)case JS_CORR_NONE :break ; -case JS_CORR_BROKEN : - value = value > corr->coef[0 ] ? (value < corr->coef[1 ] ?0 : - ((corr->coef[3 ] * (value - corr->coef[1 ])) >>14 )) : - ((corr->coef[2 ] * (value - corr->coef[0 ])) >>14 ); +case JS_CORR_BROKEN : { +s64 tmp = value > corr->coef[0 ] ? + (value < corr->coef[1 ] ?0 : + (s64 )corr->coef[3 ] * ((s64 )value - corr->coef[1 ])) : + (s64 )corr->coef[2 ] * ((s64 )value - corr->coef[0 ]); + + value =clamp_t (s64, tmp >>14 ,-32767 ,32767 );break ; + }default :return 0 ;
Signature · 1 line
2.43.0
Dmitry Torokhov
maintainerRe: [PATCH v2] Input: joydev - avoid signed overflow in the broken-axis correction
Hi Guanglei,
Quoted 2 lines · Guanglei Zhu
The
JS_CORR_BROKENcorrection multiplies two user-controlled 32-bit values before shifting and clamping the result:
...
drivers/input/joydev.c static int joydev_correct(int value, struct js_corr *corr) · line 83
Quoted 13 lines
drivers/input/joydev.c +8
-4-case JS_CORR_BROKEN : - value = value > corr->coef[0 ] ? (value < corr->coef[1 ] ?0 : - ((corr->coef[3 ] * (value - corr->coef[1 ])) >>14 )) : - ((corr->coef[2 ] * (value - corr->coef[0 ])) >>14 ); +case JS_CORR_BROKEN : { +s64 tmp = value > corr->coef[0 ] ? + (value < corr->coef[1 ] ?0 : + (s64 )corr->coef[3 ] * ((s64 )value - corr->coef[1 ])) : + (s64 )corr->coef[2 ] * ((s64 )value - corr->coef[0 ]); + + value =clamp_t (s64, tmp >>14 ,-32767 ,32767 );break ; + }
Assigning to value and breaking out of the switch causes value to be clamped a second time at the end of joydev_correct(). Also, pre-converting value to s64 and replacing the nested ternary with if/else statements avoids all the explicit (s64) casts and returns 0 immediately when the input is in the deadzone:
static int joydev_correct(int value, struct js_corr *corr)
{
s64 v64;
switch (corr->type) {
case JS_CORR_NONE:
return clamp(value, -32767, 32767);
case JS_CORR_BROKEN:
v64 = value;
if (value < corr->coef[0])
v64 = (v64 - corr->coef[0]) * corr->coef[2];
else if (value > corr->coef[0] && value > corr->coef[1])
v64 = (v64 - corr->coef[1]) * corr->coef[3];
else
return 0;
return clamp_val(v64 >> 14, -32767, 32767);
default:
return 0;
}
}Also, please note that the kernel builds with -fno-strict-overflow, so signed overflow is defined as two's-complement wraparound rather than compiler undefined behavior, and please drop Fixes and Cc: stable as this only affects synthetic coefficients passed via JSIOCSCORR.
Thanks.
Signature · 1 line
Dmitry
PATCH v3 1/1 Input: joydev - avoid signed overflow in the broken-axis correction
The JS_CORR_BROKEN correction multiplies two 32-bit values taken from user space via JSIOCSCORR before shifting and clamping:
value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 : ((corr->coef[3] * (value - corr->coef[1])) >> 14)) : ((corr->coef[2] * (value - corr->coef[0])) >> 14);
Nothing bounds the coefficients, so with large enough values the products wrap around and the correction returns bogus axis values for the rest of the session.
Compute the correction in 64 bits and clamp once, and flatten the nested ternary so the dead zone returns 0 directly.
---
v3: rework per review - pre-convert value to s64 and use if/else
instead of the nested ternary with explicit casts, return 0 directly from the dead zone, and clamp once instead of passing the value through the trailing clamp a second time; drop Fixes and the stable backport request since the kernel is built with -fno-strict-overflow and this only affects synthetic coefficients passed via JSIOCSCORR.
drivers/input/joydev.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)
drivers/input/joydev.c +12 -7
@@ -67,22 +67,27 @@ struct joydev_client {static int joydev_correct (int value,struct js_corr *corr) { +s64 v64; +switch (corr->type) {case JS_CORR_NONE : -break ; +return clamp (value,-32767 ,32767 );case JS_CORR_BROKEN : - value = value > corr->coef[0 ] ? (value < corr->coef[1 ] ?0 : - ((corr->coef[3 ] * (value - corr->coef[1 ])) >>14 )) : - ((corr->coef[2 ] * (value - corr->coef[0 ])) >>14 ); -break ; + v64 = value; +if (value < corr->coef[0 ]) + v64 = (v64 - corr->coef[0 ]) * corr->coef[2 ]; +else if (value > corr->coef[0 ] && value > corr->coef[1 ]) + v64 = (v64 - corr->coef[1 ]) * corr->coef[3 ]; +else +return 0 ; + +return clamp_val (v64 >>14 ,-32767 ,32767 );default :return 0 ; } - -return clamp (value,-32767 ,32767 ); } static void joydev_pass_event(struct joydev_client *client,
Signature · 1 line
2.43.0
Re: [PATCH v3] Input: joydev - avoid signed overflow in the broken-axis correction
Quoted 7 lines · Guanglei Zhu
The
JS_CORR_BROKENcorrection multiplies two 32-bit values taken from user space via JSIOCSCORR before shifting and clamping:value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 : ((corr->coef[3] * (value - corr->coef[1])) >> 14)) : ((corr->coef[2] * (value - corr->coef[0])) >> 14);
Quoted 8 lines
Nothing bounds the coefficients, so with large enough values the products wrap around and the correction returns bogus axis values for the rest of the session.
Compute the correction in 64 bits and clamp once, and flatten the nested ternary so the dead zone returns 0 directly.
Signed-off-by Guanglei Zhu <[email protected]>
Applied, thank you.
Signature · 1 line
Dmitry
Re: [PATCH v3] Input: joydev - avoid signed overflow in the broken-axis correction
In reply to Dmitry TorokhovVerified your rework with a three-way comparison (old / v3 / a 64-bit reference), compiled with -fno-strict-overflow: identical output for in-range coefficients, all the boundaries (value == coef[0], value == coef[1], the dead zone, malformed coef[0] > coef[1]) and CORR_NONE, and the wrap case behaves as you described - coef[0] = coef[2] = INT_MAX with a large axis value makes the old code return -5 where the 64-bit math saturates to -32767, and v3 matches the reference throughout.
PATCH 1/1 Input: adi - bound the two-stream packet merge
In reply to Guanglei ZhuWhen a second device shares the wire, adi_move_bits() appends the second packet to the first one at an offset taken from the 10-bit length field of the ID packet:
for (i = 1; i <= adi[1].ret; i++) adi[0].data[((length - 1) >> 1) + i + 1] = adi[1].data[i]; adi[0].ret += adi[1].ret;
Nothing compares the merged packet against the size of the buffer:
adi[0].data is the last member of struct adi, and struct adi_port holds two of them back to back, so an oversized length field makes the loop write over the second struct adi - its device pointer, its length and its packet - and the inflated ret later walks the reads past the buffer as well. The connect path also calls adi_move_bits() before the length has been validated at all.
Reject a merge that does not fit in the buffer.
--- drivers/input/joystick/adi.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/input/joystick/adi.c +3 -0
@@ -168,6 +168,9 @@ static void adi_move_bits(struct adi_port *port, int length)if (adi[0 ].ret <=0 || adi[1 ].ret <=0 )return ;if (adi[0 ].data[0 ] &0x20 || ~adi[1 ].data[0 ] &0x20 )return ; +if (adi[0 ].ret + adi[1 ].ret >ADI_MAX_LENGTH || + ((length -1 ) >>1 ) + adi[1 ].ret +1 >=ADI_MAX_LENGTH ) +return ;for (i =1 ; i <= adi[1 ].ret; i++) adi[0 ].data[((length -1 ) >>1 ) + i +1 ] = adi[1 ].data[i];
Signature · 1 line
2.50.1
[email protected]
AI reviewRe: [PATCH] Input: adi - bound the two-stream packet merge
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Input: adi: off-by-one check and uninvalidated streams in adi_move_bits() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Input: adi: off-by-one check and uninvalidated streams in adi_move_bits() --
commit 72de23854de8740c131cf08cbda4e0025c231b6c
Author: Guanglei Zhu <[email protected]>
Input: adi - bound the two-stream packet merge
This commit adds bounds checks in adi_move_bits() before merging the second ADI stream into adi[0].data to prevent out-of-bounds reads and writes when the packet length is oversized.
drivers/input/joystick/adi.c static void adi_move_bits(struct adi_port *port, int length) · line 173
Quoted 11 lines
drivers/input/joystick/adi.c +3
-0@@ -168,6 +168,9 @@ static void adi_move_bits(struct adi_port *port, int length)if (adi[0 ].ret <=0 || adi[1 ].ret <=0 )return ;if (adi[0 ].data[0 ] &0x20 || ~adi[1 ].data[0 ] &0x20 )return ; +if (adi[0 ].ret + adi[1 ].ret >ADI_MAX_LENGTH || + ((length -1 ) >>1 ) + adi[1 ].ret +1 >=ADI_MAX_LENGTH ) +return ;
[Severity: Medium]
Should the first check here use >= ADI_MAX_LENGTH instead of > ADI_MAX_LENGTH, and should adi[0].ret and adi[1].ret be invalidated before returning?
In struct adi, data is declared as unsigned char data[ADI_MAX_LENGTH], with index 0 storing the start bit and payload bits stored at 1-based indices 1..ret. In adi_get_bits(), the index is incremented before checking against adi->ret:
if ((adi->idx += count) > adi->ret) return 0; for (i = 0; i < count; i++) bits |= ((adi->data[adi->idx - i] >> 5) & 1) << i;
Because adi[0].ret + adi[1].ret > ADI_MAX_LENGTH allows adi[0].ret to reach ADI_MAX_LENGTH (256), if adi_decode() consumes 256 bits, adi_get_bits() will reach adi->idx == adi->ret == 256 and read adi[0].data[256] one byte past the end of adi[0].data[].
In addition, once adi_move_bits() has verified that the packet is in 2-stream mode (!(adi[0].data[0] & 0x20) && (adi[1].data[0] & 0x20)), returning early without setting adi[0].ret = adi[1].ret = -1 leaves both unmerged half-streams marked valid (ret > 0). Wouldn't that cause adi_connect() and adi_read() to decode the unmerged half-streams in adi[0] and adi[1] as if they were valid standalone packets instead of rejecting the malformed 2-stream read?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 1/1 Input: adi - bound the two-stream packet merge
In reply to Guanglei ZhuWhen a second device shares the wire, adi_move_bits() appends the second packet to the first one at an offset taken from the 10-bit length field of the ID packet:
for (i = 1; i <= adi[1].ret; i++) adi[0].data[((length - 1) >> 1) + i + 1] = adi[1].data[i]; adi[0].ret += adi[1].ret;
Nothing compares the merged packet against the size of the buffer:
adi[0].data is the last member of struct adi, and struct adi_port holds two of them back to back, so an oversized length field makes the loop write over the second struct adi - its device pointer, its length and its packet - and the inflated ret later walks the reads past the buffer as well. The connect path also calls adi_move_bits() before the length has been validated at all.
Reject a merge that does not fit in the buffer. Use >= for the combined length so adi_get_bits() can never walk to data[ret] with ret == ADI_MAX_LENGTH, and invalidate both half-streams on the rejection so the unmerged packets are not decoded as standalone reads.
---
v2: per review, use >= so a combined ret of ADI_MAX_LENGTH is rejected
as well (adi_get_bits() allows idx == ret, and data[idx] with idx == ADI_MAX_LENGTH reads one byte past the array), and set adi[0].ret = adi[1].ret = -1 on the rejection path so the unmerged half-streams are not decoded as standalone packets.
drivers/input/joystick/adi.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/input/joystick/adi.c +5 -0
@@ -168,6 +168,11 @@ static void adi_move_bits(struct adi_port *port, int length)if (adi[0 ].ret <=0 || adi[1 ].ret <=0 )return ;if (adi[0 ].data[0 ] &0x20 || ~adi[1 ].data[0 ] &0x20 )return ; +if (adi[0 ].ret + adi[1 ].ret >=ADI_MAX_LENGTH || + ((length -1 ) >>1 ) + adi[1 ].ret +1 >=ADI_MAX_LENGTH ) { + adi[0 ].ret = adi[1 ].ret =-1 ; +return ; + }for (i =1 ; i <= adi[1 ].ret; i++) adi[0 ].data[((length -1 ) >>1 ) + i +1 ] = adi[1 ].data[i];
Signature · 1 line
2.43.0
Dmitry Torokhov
maintainerRe: [PATCH v2] Input: adi - bound the two-stream packet merge
Hi Guanglei,
Quoted 42 lines · Guanglei Zhu
When a second device shares the wire,
adi_move_bits()appends the second packet to the first one at an offset taken from the 10-bit length field of the ID packet:for (i = 1; i <= adi[1].ret; i++) adi[0].data[((length - 1) >> 1) + i + 1] = adi[1].data[i]; adi[0].ret += adi[1].ret;Nothing compares the merged packet against the size of the buffer:
adi[0].data is the last member of struct adi, and structadi_portholds two of them back to back, so an oversized length field makes the loop write over the second struct adi - its device pointer, its length and its packet - and the inflated ret later walks the reads past the buffer as well. The connect path also callsadi_move_bits()before the length has been validated at all.Reject a merge that does not fit in the buffer. Use >= for the combined length so
adi_get_bits()can never walk to data[ret] with ret ==ADI_MAX_LENGTH, and invalidate both half-streams on the rejection so the unmerged packets are not decoded as standalone reads.Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Guanglei Zhu <[email protected]>---
v2: per review, use >= so a combined ret ofADI_MAX_LENGTHis rejected
as well (adi_get_bits()allows idx == ret, and data[idx] with idx ==ADI_MAX_LENGTHreads one byte past the array), and set adi[0].ret = adi[1].ret = -1 on the rejection path so the unmerged half-streams are not decoded as standalone packets.drivers/input/joystick/adi.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/input/joystick/adi.c +0
-0@@ -168,6 +168,11 @@ static void adi_move_bits(struct adi_port *port, int length)
drivers/input/joystick/adi.c static void adi_move_bits(struct adi_port *port, int length) · line 178
Quoted 10 lines
drivers/input/joystick/adi.c +5
-0if (adi[0 ].ret <=0 || adi[1 ].ret <=0 )return ;if (adi[0 ].data[0 ] &0x20 || ~adi[1 ].data[0 ] &0x20 )return ; +if (adi[0 ].ret + adi[1 ].ret >=ADI_MAX_LENGTH || + ((length -1 ) >>1 ) + adi[1 ].ret +1 >=ADI_MAX_LENGTH ) { + adi[0 ].ret = adi[1 ].ret =-1 ; +return ; + }for (i =1 ; i <= adi[1 ].ret; i++) adi[0 ].data[((length -1 ) >>1 ) + i +1 ] = adi[1 ].data[i];
In adi_move_bits(), length can also be 0 (either from the 10-bit header in adi_connect() or when port->adi[0].length is 0 in adi_read()), so please reject length <= 0 as well. It would also be cleaner to compute the start offset (for example, offset = ((length - 1) >> 1) + 1) once and use offset + adi[1].ret and offset + i in both the bounds check and the copy loop.
Since you are hardening this driver against malformed packets on the gameport wire, there are a few more bounds issues in the same file worth addressing:
In adi_read_packet(), the condition adi[i].ret < ADI_MAX_LENGTH is off by one because adi[i].data[++adi[i].ret] pre-increments ret, allowing a write to adi[i].data[ADI_MAX_LENGTH] when ret is ADI_MAX_LENGTH - 1. It should check adi[i].ret < ADI_MAX_LENGTH - 1.
In adi_id_decode(), when ADI_FLAG_10BIT is set, adi->axes10 = adi->axes8 - i can underflow if i > adi->axes8, and for ADI_ID_TPD and ADI_ID_WGP, adi->buttons -= 4 can underflow if adi->buttons < 4. Please also switch the non-string byte fields and buffers in struct adi (such as id, buttons, axes10, axes8, hats, and data) from char/unsigned char to u8 (and s8 for pad) so their signedness does not vary across architectures. Finally, the decoded axis, hat, and button counts are not checked against the sizes of the static adi_abs[] and adi_key[] tables for the device ID before adi_init_input(), adi_init_center(), and adi_decode() walk adi->abs and adi->key.
Could you please fold these fixes in (or split them into a small series if you prefer)?
Thanks.
Signature · 1 line
Dmitry
PATCH 1/1 Input: db9 - enlarge the Saturn multitap packet buffer
In reply to Guanglei ZhuThe Saturn multitap path fills the packet buffer block by block:
for (i = 0; i < 60; i += 10) {
data[i] = db9_saturn_read_analog(port, type, powered);
if (data[i] != 0xff)
/* read each pad */
for (j = 0; j < (data[i] & 0x0f); j++)
data[i + j + 1] = db9_saturn_read_analog(port, type, powered);
}Each block is ten bytes apart, and the low nibble of the block header - taken from the device - allows up to fifteen more bytes, so the last block can write up to data[65]. The buffer, however, is sixty bytes, and the six bytes past it belong to the rest of the stack frame.
Give the buffer the size the protocol layout actually requires.
--- drivers/input/joystick/db9.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/input/joystick/db9.c +3 -2
@@ -253,7 +253,8 @@ static unsigned char db9_saturn_read_packet(struct parport *port, unsigned char/* * db9_saturn_report() analyzes packet and reports. */ -static int db9_saturn_report (unsigned char id,unsigned char data[60 ],struct input_dev *devs[],int n,int max_pads) +static int db9_saturn_report (unsigned char id,unsigned char data[66 ], +struct input_dev *devs[],int n,int max_pads) {struct input_dev *dev;int tmp, i, j;
@@ -321,7 +322,7 @@ static int db9_saturn_report(unsigned char id, unsigned char data[60], struct instatic int db9_saturn (int mode,struct parport *port,struct input_dev *devs[]) { -unsigned char id, data[60 ]; +unsigned char id, data[66 ];int type, n, max_pads;int tmp, i;
Signature · 1 line
2.50.1
Re: [PATCH] Input: db9 - enlarge the Saturn multitap packet buffer
Quoted 16 lines · Guanglei Zhu
The Saturn multitap path fills the packet buffer block by block:
for (i = 0; i < 60; i += 10) { data[i] = db9_saturn_read_analog(port, type, powered); if (data[i] != 0xff) /* read each pad */ for (j = 0; j < (data[i] & 0x0f); j++) data[i + j + 1] = db9_saturn_read_analog(port, type, powered); }Each block is ten bytes apart, and the low nibble of the block header - taken from the device - allows up to fifteen more bytes, so the last block can write up to data[65]. The buffer, however, is sixty bytes, and the six bytes past it belong to the rest of the stack frame.
Quoted 5 lines
Give the buffer the size the protocol layout actually requires.
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Guanglei Zhu <[email protected]>
Applied, thank you.
Signature · 1 line
Dmitry
PATCH 1/1 Input: mt6779-keypad - check the reported row and column
In reply to Guanglei ZhuThe interrupt handler turns register bits into a matrix position and indexes the keycode array with it:
keypad->calc_row_col(key, &row, &col); scancode = MATRIX_SCAN_CODE(row, col, row_shift); ... input_report_key(keypad->input_dev, keycode[scancode], pressed);
The keycode array is sized after the rows and columns the device tree declared, but neither the probe nor the handler checks that a reported position actually fits those dimensions. A register glitch or a matrix smaller than the hardware's fixed layout indexes past the array and reports whatever sits there as a key code.
Skip positions outside the declared matrix.
--- drivers/input/keyboard/mt6779-keypad.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/input/keyboard/mt6779-keypad.c +3 -0
@@ -73,6 +73,9 @@ static irqreturn_t mt6779_keypad_irq_handler(int irq, void *dev_id) key = bit_nr /32 *16 + bit_nr %32 ; keypad->calc_row_col (key, &row, &col); +if (row >= keypad->n_rows || col >= keypad->n_cols) +continue ; + scancode =MATRIX_SCAN_CODE (row, col, row_shift);/* 1: not pressed, 0: pressed */ pressed = !test_bit (bit_nr, new_state);
Signature · 1 line
2.50.1
Re: [PATCH] Input: mt6779-keypad - check the reported row and column
Quoted 15 lines · Guanglei Zhu
The interrupt handler turns register bits into a matrix position and indexes the keycode array with it:
keypad->calc_row_col(key, &row, &col); scancode = MATRIX_SCAN_CODE(row, col, row_shift); ... input_report_key(keypad->input_dev, keycode[scancode], pressed);The keycode array is sized after the rows and columns the device tree declared, but neither the probe nor the handler checks that a reported position actually fits those dimensions. A register glitch or a matrix smaller than the hardware's fixed layout indexes past the array and reports whatever sits there as a key code.
Quoted 5 lines
Skip positions outside the declared matrix.
Fixes f28af984e771 ("Input: mt6779-keypad - add MediaTek keypad driver")Signed-off-by Guanglei Zhu <[email protected]>
Applied, thank you.
Signature · 1 line
Dmitry
PATCH 1/1 Input: lpc32xx-keys - reject matrices bigger than the hardware has
In reply to Guanglei ZhuThe scanner state array holds the last state of eight matrix columns, and the interrupt handler walks the matrix size that the device tree declared:
u8 lastkeystates[8]; ... for (i = 0; i < kscandat->matrix_sz; i++) lpc32xx_mod_states(kscandat, i);
The probe path only checks that rows and columns match; the hardware supports square matrices from 1x1 up to 8x8, but nothing stops a device tree from declaring more, which makes the state array index run past the field into the rest of the driver data - including the keymap pointer the same code dereferences.
Reject matrices larger than the hardware supports.
--- drivers/input/keyboard/lpc32xx-keys.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/input/keyboard/lpc32xx-keys.c +5 -0
@@ -142,6 +142,11 @@ static int lpc32xx_parse_dt(struct device *dev,return -EINVAL ; } +if (rows >8 ) { +dev_err (dev,"rows and columns must be at most 8!\n" ); +return -EINVAL ; + } + kscandat->matrix_sz = rows; kscandat->row_shift =get_count_order (columns);
Signature · 1 line
2.50.1
Dmitry Torokhov
maintainerRe: [PATCH] Input: lpc32xx-keys - reject matrices bigger than the hardware has
Quoted 29 lines · Guanglei Zhu
The scanner state array holds the last state of eight matrix columns, and the interrupt handler walks the matrix size that the device tree declared:
u8 lastkeystates[8]; ... for (i = 0; i < kscandat->matrix_sz; i++) lpc32xx_mod_states(kscandat, i);The probe path only checks that rows and columns match; the hardware supports square matrices from 1x1 up to 8x8, but nothing stops a device tree from declaring more, which makes the state array index run past the field into the rest of the driver data - including the keymap pointer the same code dereferences.
Reject matrices larger than the hardware supports.
Fixes 69690bec400e ("Input: add support for key scan interface of the LPC32xx SoC")Signed-off-by Guanglei Zhu <[email protected]>--- drivers/input/keyboard/lpc32xx-keys.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/input/keyboard/lpc32xx-keys.c +0
-0
drivers/input/keyboard/lpc32xx-keys.c static int lpc32xx_parse_dt(struct device *dev, · line 145
Quoted 4 lines
drivers/input/keyboard/lpc32xx-keys.c +1
-0@@ -142,6 +142,11 @@ static int lpc32xx_parse_dt(struct device *dev,return -EINVAL ; } +if (rows >8 ) {
This should be a constant/#define.
drivers/input/keyboard/lpc32xx-keys.c static int lpc32xx_parse_dt(struct device *dev, · line 151
Quoted 6 lines
drivers/input/keyboard/lpc32xx-keys.c +4
-0+dev_err (dev,"rows and columns must be at most 8!\n" ); +return -EINVAL ; + } + kscandat->matrix_sz = rows; kscandat->row_shift =get_count_order (columns);
Thanks.
Signature · 1 line
Dmitry
PATCH v2 1/1 Input: lpc32xx-keys - reject matrices bigger than the hardware has
The scanner state array holds the last state of eight matrix columns, and the interrupt handler walks the matrix size that the device tree declared:
u8 lastkeystates[8]; ... for (i = 0; i < kscandat->matrix_sz; i++) lpc32xx_mod_states(kscandat, i);
The probe path only checks that rows and columns match; the hardware supports square matrices from 1x1 up to 8x8, but nothing stops a device tree from declaring more, which makes the state array index run past the field into the rest of the driver data - including the keymap pointer the same code dereferences.
Reject matrices larger than the hardware supports.
---
v2: use LPC32XX_MAX_MATRIX_SIZE for the state array and the new
check instead of a literal 8, per review.
drivers/input/keyboard/lpc32xx-keys.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/input/keyboard/lpc32xx-keys.c +7 -1
@@ -65,7 +65,7 @@ struct lpc32xx_kscan_drv {unsigned int row_shift;unsigned short *keymap;/* Pointer to key map for the scan matrix */ -u8 lastkeystates[8 ]; +u8 lastkeystates[LPC32XX_MAX_MATRIX_SIZE ]; };static void lpc32xx_mod_states (struct lpc32xx_kscan_drv *kscandat,int col)
@@ -141,6 +141,12 @@ static int lpc32xx_parse_dt(struct device *dev,dev_err (dev,"rows and columns must be equal!\n" );return -EINVAL ; } +if (rows >LPC32XX_MAX_MATRIX_SIZE ) { +dev_err (dev,"rows and columns must be at most %d!\n" , +LPC32XX_MAX_MATRIX_SIZE ); +return -EINVAL ; + } + kscandat->matrix_sz = rows; kscandat->row_shift =get_count_order (columns);
Signature · 1 line
2.43.0
Dmitry Torokhov
maintainerRe: [PATCH v2] Input: lpc32xx-keys - reject matrices bigger than the hardware has
Quoted 32 lines · Guanglei Zhu
The scanner state array holds the last state of eight matrix columns, and the interrupt handler walks the matrix size that the device tree declared:
u8 lastkeystates[8]; ... for (i = 0; i < kscandat->matrix_sz; i++) lpc32xx_mod_states(kscandat, i);The probe path only checks that rows and columns match; the hardware supports square matrices from 1x1 up to 8x8, but nothing stops a device tree from declaring more, which makes the state array index run past the field into the rest of the driver data - including the keymap pointer the same code dereferences.
Reject matrices larger than the hardware supports.
Fixes 69690bec400e ("Input: add support for key scan interface of the LPC32xx SoC")Signed-off-by Guanglei Zhu <[email protected]>---
v2: useLPC32XX_MAX_MATRIX_SIZEfor the state array and the new
check instead of a literal 8, per review.drivers/input/keyboard/lpc32xx-keys.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/input/keyboard/lpc32xx-keys.c +0
-0
drivers/input/keyboard/lpc32xx-keys.c struct lpc32xx_kscan_drv { · line 68
Quoted 5 lines
drivers/input/keyboard/lpc32xx-keys.c +1
-1@@ -65,7 +65,7 @@ struct lpc32xx_kscan_drv {unsigned int row_shift;unsigned short *keymap;/* Pointer to key map for the scan matrix */ -u8 lastkeystates[8 ]; +u8 lastkeystates[LPC32XX_MAX_MATRIX_SIZE ];
Compiling this would be helpful...
Quoted 16 lines
drivers/input/keyboard/lpc32xx-keys.c +6
-0};static void lpc32xx_mod_states (struct lpc32xx_kscan_drv *kscandat,int col)@@ -141,6 +141,12 @@ static int lpc32xx_parse_dt(struct device *dev,dev_err (dev,"rows and columns must be equal!\n" );return -EINVAL ; } +if (rows >LPC32XX_MAX_MATRIX_SIZE ) { +dev_err (dev,"rows and columns must be at most %d!\n" , +LPC32XX_MAX_MATRIX_SIZE ); +return -EINVAL ; + } + kscandat->matrix_sz = rows; kscandat->row_shift =get_count_order (columns);
Signature · 1 line
Dmitry
PATCH v3 1/1 Input: lpc32xx-keys - reject matrices bigger than the hardware has
The scanner state array holds the last state of eight matrix columns, and the interrupt handler walks the matrix size that the device tree declared:
u8 lastkeystates[8]; ... for (i = 0; i < kscandat->matrix_sz; i++) lpc32xx_mod_states(kscandat, i);
The probe path only checks that rows and columns match; the hardware supports square matrices from 1x1 up to 8x8, but nothing stops a device tree from declaring more, which makes the state array index run past the field into the rest of the driver data - including the keymap pointer the same code dereferences.
Reject matrices larger than the hardware supports.
---
v3: actually include the LPC32XX_MAX_MATRIX_SIZE definition that v2
referenced but forgot to add; compile-tested with CONFIG_KEYBOARD_LPC32XX enabled.
drivers/input/keyboard/lpc32xx-keys.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/input/keyboard/lpc32xx-keys.c +9 -1
@@ -39,6 +39,8 @@#define LPC32XX_KSCAN_DEB_NUM_DEB_PASS (n) ((n) & 0xFF) +#define LPC32XX_MAX_MATRIX_SIZE 8 +#define LPC32XX_KSCAN_SCOND_IN_IDLE 0x0#define LPC32XX_KSCAN_SCOND_IN_SCANONCE 0x1#define LPC32XX_KSCAN_SCOND_IN_IRQGEN 0x2
@@ -65,7 +67,7 @@ struct lpc32xx_kscan_drv {unsigned int row_shift;unsigned short *keymap;/* Pointer to key map for the scan matrix */ -u8 lastkeystates[8 ]; +u8 lastkeystates[LPC32XX_MAX_MATRIX_SIZE ]; };static void lpc32xx_mod_states (struct lpc32xx_kscan_drv *kscandat,int col)
@@ -141,6 +143,12 @@ static int lpc32xx_parse_dt(struct device *dev,dev_err (dev,"rows and columns must be equal!\n" );return -EINVAL ; } +if (rows >LPC32XX_MAX_MATRIX_SIZE ) { +dev_err (dev,"rows and columns must be at most %d!\n" , +LPC32XX_MAX_MATRIX_SIZE ); +return -EINVAL ; + } + kscandat->matrix_sz = rows; kscandat->row_shift =get_count_order (columns);
Signature · 1 line
2.43.0
Re: [PATCH v3] Input: lpc32xx-keys - reject matrices bigger than the hardware has
Quoted 15 lines · Guanglei Zhu
The scanner state array holds the last state of eight matrix columns, and the interrupt handler walks the matrix size that the device tree declared:
u8 lastkeystates[8]; ... for (i = 0; i < kscandat->matrix_sz; i++) lpc32xx_mod_states(kscandat, i);The probe path only checks that rows and columns match; the hardware supports square matrices from 1x1 up to 8x8, but nothing stops a device tree from declaring more, which makes the state array index run past the field into the rest of the driver data - including the keymap pointer the same code dereferences.
Quoted 5 lines
Reject matrices larger than the hardware supports.
Fixes 69690bec400e ("Input: add support for key scan interface of the LPC32xx SoC")Signed-off-by Guanglei Zhu <[email protected]>
Applied, thank you (but dropped stable).
Signature · 1 line
Dmitry
[email protected]
AI reviewRe: [PATCH v2] Input: lpc32xx-keys - reject matrices bigger than the hardware has
In reply to Guanglei ZhuThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Input: lpc32xx-keys: undeclared LPC32XX_MAX_MATRIX_SIZE in lpc32xx_parse_dt() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Input: lpc32xx-keys: undeclared LPC32XX_MAX_MATRIX_SIZE in lpc32xx_parse_dt() --
commit ae2a42c5ed13c05919e7d77f5f4978ad957e69a7
Author: Guanglei Zhu <[email protected]>
Input: lpc32xx-keys - reject matrices bigger than the hardware has
This commit adds an upper-bound check in lpc32xx_parse_dt() to reject device tree keypad matrices larger than LPC32XX_MAX_MATRIX_SIZE, preventing out-of-bounds accesses to the lastkeystates array.
Quoted 14 lines
drivers/input/keyboard/lpc32xx-keys.c +1
-1@@ -65,7 +65,7 @@ struct lpc32xx_kscan_drv {unsigned int row_shift;unsigned short *keymap;/* Pointer to key map for the scan matrix */ -u8 lastkeystates[8 ]; +u8 lastkeystates[LPC32XX_MAX_MATRIX_SIZE ]; };static void lpc32xx_mod_states (struct lpc32xx_kscan_drv *kscandat,int col)
drivers/input/keyboard/lpc32xx-keys.c static int lpc32xx_parse_dt(struct device *dev, · line 148
Quoted 8 lines
drivers/input/keyboard/lpc32xx-keys.c +5
-0@@ -141,6 +141,12 @@ static int lpc32xx_parse_dt(struct device *dev,dev_err (dev,"rows and columns must be equal!\n" );return -EINVAL ; } +if (rows >LPC32XX_MAX_MATRIX_SIZE ) { +dev_err (dev,"rows and columns must be at most %d!\n" , +LPC32XX_MAX_MATRIX_SIZE ); +return -EINVAL ; + }
[Severity: Low]
Does this break the build when CONFIG_KEYBOARD_LPC32XX is enabled? Both struct lpc32xx_kscan_drv and lpc32xx_parse_dt() in drivers/input/keyboard/lpc32xx-keys.c reference LPC32XX_MAX_MATRIX_SIZE, which is not defined anywhere in the file or included headers. Was a #define LPC32XX_MAX_MATRIX_SIZE 8 macro definition omitted from the patch?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH 1/1 Input: tegra-kbc - check the pin numbers from the device tree
In reply to Guanglei ZhuThe row and column pin lists are read straight from the device tree and used to index the fixed pin configuration array:
num_rows = of_property_read_variable_u32_array(np, "nvidia,kbc-row-pins",
rows_cfg, 1, KBC_MAX_GPIO);
...
for (i = 0; i < num_rows; i++) {
kbc->pin_cfg[rows_cfg[i]].type = PIN_CFG_ROW;
kbc->pin_cfg[rows_cfg[i]].num = i;
}The helper only limits how many entries it reads, not what they contain, and the pin_cfg array holds KBC_MAX_GPIO entries, so a pin number from the device tree indexes past the array into the rest of the driver data. The later configuration check validates the row and column counts stored this way, not the index itself, so it cannot undo the write.
Reject pin numbers the array does not have.
--- drivers/input/keyboard/tegra-kbc.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/input/keyboard/tegra-kbc.c +8 -0
@@ -522,6 +522,10 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc) }for (i =0 ; i < num_rows; i++) { +if (rows_cfg[i] >=KBC_MAX_GPIO ) { +dev_err (kbc->dev,"invalid row pin %u\n" , rows_cfg[i]); +return -EINVAL ; + } kbc->pin_cfg[rows_cfg[i]].type =PIN_CFG_ROW ; kbc->pin_cfg[rows_cfg[i]].num = i; }
@@ -538,6 +542,10 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc) }for (i =0 ; i < num_cols; i++) { +if (cols_cfg[i] >=KBC_MAX_GPIO ) { +dev_err (kbc->dev,"invalid col pin %u\n" , cols_cfg[i]); +return -EINVAL ; + } kbc->pin_cfg[cols_cfg[i]].type =PIN_CFG_COL ; kbc->pin_cfg[cols_cfg[i]].num = i; }
Signature · 1 line
2.50.1
Dmitry Torokhov
maintainerRe: [PATCH] Input: tegra-kbc - check the pin numbers from the device tree
Hi Guanglei,
Quoted 43 lines · Guanglei Zhu
The row and column pin lists are read straight from the device tree and used to index the fixed pin configuration array:
num_rows = of_property_read_variable_u32_array(np, "nvidia,kbc-row-pins", rows_cfg, 1, KBC_MAX_GPIO); ... for (i = 0; i < num_rows; i++) { kbc->pin_cfg[rows_cfg[i]].type = PIN_CFG_ROW; kbc->pin_cfg[rows_cfg[i]].num = i; }The helper only limits how many entries it reads, not what they contain, and the
pin_cfgarray holdsKBC_MAX_GPIOentries, so a pin number from the device tree indexes past the array into the rest of the driver data. The later configuration check validates the row and column counts stored this way, not the index itself, so it cannot undo the write.Reject pin numbers the array does not have.
Fixes 88390243a993 ("Input: tegra-kbc - add support for rows/columns configuration from dt")Signed-off-by Guanglei Zhu <[email protected]>--- drivers/input/keyboard/tegra-kbc.c | 8 ++++++++ 1 file changed, 8 insertions(+)drivers/input/keyboard/tegra-kbc.c +4
-0@@ -522,6 +522,10 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc) }for (i =0 ; i < num_rows; i++) { +if (rows_cfg[i] >=KBC_MAX_GPIO ) { +dev_err (kbc->dev,"invalid row pin %u\n" , rows_cfg[i]); +return -EINVAL ; + } kbc->pin_cfg[rows_cfg[i]].type =PIN_CFG_ROW ; kbc->pin_cfg[rows_cfg[i]].num = i; }
drivers/input/keyboard/tegra-kbc.c static int tegra_kbc_parse_dt(struct tegra_kbc *kbc) · line 551
Quoted 10 lines
drivers/input/keyboard/tegra-kbc.c +4
-0@@ -538,6 +542,10 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc) }for (i =0 ; i < num_cols; i++) { +if (cols_cfg[i] >=KBC_MAX_GPIO ) { +dev_err (kbc->dev,"invalid col pin %u\n" , cols_cfg[i]); +return -EINVAL ; + } kbc->pin_cfg[cols_cfg[i]].type =PIN_CFG_COL ; kbc->pin_cfg[cols_cfg[i]].num = i; }
Could you please check against kbc->num_rows_and_columns instead of KBC_MAX_GPIO? Not all Tegra variants have 24 KBC pins (for example, Tegra114 only has 11 rows + 8 columns = 19 pins), and the loop right above only initializes kbc->pin_cfg[] up to kbc->num_rows_and_columns.
Thanks.
Signature · 1 line
Dmitry
PATCH v2 1/1 Input: tegra-kbc - check the pin numbers from the device tree
In reply to Guanglei ZhuThe row and column pin lists are read straight from the device tree and used to index the pin configuration array:
for (i = 0; i < num_rows; i++) {
kbc->pin_cfg[rows_cfg[i]].type = PIN_CFG_ROW;
kbc->pin_cfg[rows_cfg[i]].num = i;
}The helper only limits how many entries it reads, not what they contain. Not all Tegra variants have KBC_MAX_GPIO pins - Tegra114, for example, has 11 rows + 8 columns = 19 - and the setup loop only initializes pin_cfg[] up to kbc->num_rows_and_columns, so a device tree that names a pin the variant does not have makes the row and column stores write over pin_cfg entries that were never touched, and the later check_pin_cfg() walk reads that stale data back as valid configuration.
Reject pin numbers the variant does not have.
---
v2: validate against kbc->num_rows_and_columns instead of
KBC_MAX_GPIO, per review - Tegra114 only has 19 KBC pins and the setup loop above only initializes pin_cfg[] up to num_rows_and_columns, so pins in [19, 24) must be rejected too.
drivers/input/keyboard/tegra-kbc.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/input/keyboard/tegra-kbc.c +8 -0
@@ -522,6 +522,10 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc) }for (i =0 ; i < num_rows; i++) { +if (rows_cfg[i] >= kbc->num_rows_and_columns) { +dev_err (kbc->dev,"invalid row pin %u\n" , rows_cfg[i]); +return -EINVAL ; + } kbc->pin_cfg[rows_cfg[i]].type =PIN_CFG_ROW ; kbc->pin_cfg[rows_cfg[i]].num = i; }
@@ -538,6 +542,10 @@ static int tegra_kbc_parse_dt(struct tegra_kbc *kbc) }for (i =0 ; i < num_cols; i++) { +if (cols_cfg[i] >= kbc->num_rows_and_columns) { +dev_err (kbc->dev,"invalid col pin %u\n" , cols_cfg[i]); +return -EINVAL ; + } kbc->pin_cfg[cols_cfg[i]].type =PIN_CFG_COL ; kbc->pin_cfg[cols_cfg[i]].num = i; }
Signature · 1 line
2.43.0
Re: [PATCH v2] Input: tegra-kbc - check the pin numbers from the device tree
Quoted 17 lines · Guanglei Zhu
The row and column pin lists are read straight from the device tree and used to index the pin configuration array:
for (i = 0; i < num_rows; i++) { kbc->pin_cfg[rows_cfg[i]].type = PIN_CFG_ROW; kbc->pin_cfg[rows_cfg[i]].num = i; }The helper only limits how many entries it reads, not what they contain. Not all Tegra variants have
KBC_MAX_GPIOpins - Tegra114, for example, has 11 rows + 8 columns = 19 - and the setup loop only initializes pin_cfg[] up tokbc->num_rows_and_columns, so a device tree that names a pin the variant does not have makes the row and column stores write overpin_cfgentries that were never touched, and the latercheck_pin_cfg()walk reads that stale data back as valid configuration.
Quoted 5 lines
Reject pin numbers the variant does not have.
Fixes 88390243a993 ("Input: tegra-kbc - add support for rows/columns configuration from dt")Signed-off-by Guanglei Zhu <[email protected]>
Applied, thank you (but dropped stable).
Signature · 1 line
Dmitry
PATCH 1/1 Input: cyapa_gen3 - clamp the reported finger count to the array
In reply to Guanglei ZhuThe finger count is taken from the top nibble of a status byte the trackpad firmware fills in, and used to walk the touch array:
num_fingers = (data->finger_btn >> 4) & 0x0f;
for (i = 0; i < num_fingers; i++) {
const struct cyapa_touch *touch = &data->touches[i];The nibble allows up to fifteen fingers, but the array holds the five touches the chip is documented to report, so a firmware that counts beyond that makes the driver report coordinates read from the stack past the register block.
Clamp the finger count to the size of the array.
--- drivers/input/mouse/cyapa_gen3.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/input/mouse/cyapa_gen3.c +2 -1
@@ -1149,7 +1149,8 @@ static int cyapa_gen3_event_process(struct cyapa *cyapa,int num_fingers;int i; - num_fingers = (data->finger_btn >>4 ) &0x0f ; + num_fingers =min ((data->finger_btn >>4 ) &0x0f , +ARRAY_SIZE (data->touches));for (i =0 ; i < num_fingers; i++) {const struct cyapa_touch *touch = &data->touches[i];/* Note: touch->id range is 1 to 15; slots are 0 to 14. */
Signature · 1 line
2.50.1
Dmitry Torokhov
maintainerRe: [PATCH] Input: cyapa_gen3 - clamp the reported finger count to the array
Hi Guanglei,
Quoted 26 lines · Guanglei Zhu
The finger count is taken from the top nibble of a status byte the trackpad firmware fills in, and used to walk the touch array:
num_fingers = (data->finger_btn >> 4) & 0x0f; for (i = 0; i < num_fingers; i++) { const struct cyapa_touch *touch = &data->touches[i];The nibble allows up to fifteen fingers, but the array holds the five touches the chip is documented to report, so a firmware that counts beyond that makes the driver report coordinates read from the stack past the register block.
Clamp the finger count to the size of the array.
Fixes 9f1cd857518f ("Input: cyapa - re-design driver to support multi-trackpad in one driver")Signed-off-by Guanglei Zhu <[email protected]>--- drivers/input/mouse/cyapa_gen3.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/input/mouse/cyapa_gen3.c +0
-0
drivers/input/mouse/cyapa_gen3.c static int cyapa_gen3_event_process(struct cyapa *cyapa, · line 1156
Quoted 9 lines
drivers/input/mouse/cyapa_gen3.c +2
-1@@ -1149,7 +1149,8 @@ static int cyapa_gen3_event_process(struct cyapa *cyapa,int num_fingers;int i; - num_fingers = (data->finger_btn >>4 ) &0x0f ; + num_fingers =min ((data->finger_btn >>4 ) &0x0f , +ARRAY_SIZE (data->touches));for (i =0 ; i < num_fingers; i++) {const struct cyapa_touch *touch = &data->touches[i];/* Note: touch->id range is 1 to 15; slots are 0 to 14. */
If the packet reports more than 5 fingers (for example, if an I2C read glitches and returns 0xff bytes, which would otherwise pass the bit checks for OP_STATUS_SRC, CYAPA_DEV_NORMAL, and OP_DATA_VALID), the packet is corrupted and we should not clamp the count and report 5 bogus touches to userspace.
Instead, please reject packets reporting more than GEN3_MAX_FINGERS with -EINVAL during the header validation in cyapa_gen3_irq_handler() and cyapa_gen3_try_poll_handler() (or in cyapa_gen3_event_process()), so the driver treats it as an invalid state and reinitializes the device. While at it, please also use GEN3_MAX_FINGERS to size data->touches[] in struct cyapa_reg_data, and either fix GEN3_FINGER_NUM() to mask with 0x0f and use it, or drop the unused macro.
Thanks.
Signature · 1 line
Dmitry
Dmitry Torokhov
maintainerHi Guanglei,
Quoted 12 lines · Guanglei Zhu
The legacy 0.x read path shifts the axis values by the calibration constants that
JS_SET_CALinstalls:data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x; data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y;
JS_CORRis copied straight from userspace as three s32 values and never validated, so the shift count can be negative or wider than the type, which is undefined behavior: the shift compiles to whatever the architecture does with an out-of-range count, and the compiler is free to assume it cannot happen.
Quoted 4 lines
Clamp the shift count to the width of the shifted value.
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Why do all these need to be in stable?
Quoted 10 lines
Signed-off-by Guanglei Zhu <[email protected]>--- drivers/input/joydev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/input/joydev.c +0
-0
drivers/input/joydev.c static ssize_t joydev_0x_read(struct joydev_client *client, · line 360
Quoted 10 lines
drivers/input/joydev.c +2
-2@@ -353,8 +353,8 @@ static ssize_t joydev_0x_read(struct joydev_client *client,for (data.buttons = i =0 ; i <32 && i < joydev->nkey; i++) data.buttons |=test_bit (joydev->keypam[i], input->key) ? (1 << i) :0 ; - data.x = (joydev->abs[0 ] /256 +128 ) >> joydev->glue.JS_CORR.x; - data.y = (joydev->abs[1 ] /256 +128 ) >> joydev->glue.JS_CORR.y; + data.x = (joydev->abs[0 ] /256 +128 ) >>clamp (joydev->glue.JS_CORR.x,0 ,31 ); + data.y = (joydev->abs[1 ] /256 +128 ) >>clamp (joydev->glue.JS_CORR.y,0 ,31 );/* * Reset reader's event queue
Thanks.
Signature · 1 line
Dmitry
PATCH v2 1/1 Input: joydev - avoid an undefined shift in the legacy 0x read
The legacy 0.x read path shifts the axis values by the calibration constants that JS_SET_CAL installs:
data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x;
data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y;JS_CORR is copied straight from userspace as three s32 values and never validated, so the shift count can be negative or wider than the type, which is undefined behavior: the shift compiles to whatever the architecture does with an out-of-range count, and the compiler is free to assume it cannot happen.
Clamp the shift count to the width of the shifted value.
---
v2: drop the stable backport request - the shift result is just an
out-of-range axis value, not a memory safety issue.
drivers/input/joydev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/joydev.c +2 -2
@@ -353,8 +353,8 @@ static ssize_t joydev_0x_read(struct joydev_client *client,for (data.buttons = i =0 ; i <32 && i < joydev->nkey; i++) data.buttons |=test_bit (joydev->keypam[i], input->key) ? (1 << i) :0 ; - data.x = (joydev->abs[0 ] /256 +128 ) >> joydev->glue.JS_CORR.x; - data.y = (joydev->abs[1 ] /256 +128 ) >> joydev->glue.JS_CORR.y; + data.x = (joydev->abs[0 ] /256 +128 ) >>clamp (joydev->glue.JS_CORR.x,0 ,31 ); + data.y = (joydev->abs[1 ] /256 +128 ) >>clamp (joydev->glue.JS_CORR.y,0 ,31 );/* * Reset reader's event queue
Signature · 1 line
2.43.0
Quoted 12 lines · Guanglei Zhu
The legacy 0.x read path shifts the axis values by the calibration constants that
JS_SET_CALinstalls:data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x; data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y;
JS_CORRis copied straight from userspace as three s32 values and never validated, so the shift count can be negative or wider than the type, which is undefined behavior: the shift compiles to whatever the architecture does with an out-of-range count, and the compiler is free to assume it cannot happen.
Quoted 4 lines
Clamp the shift count to the width of the shifted value.
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Guanglei Zhu <[email protected]>
Applied, thank you.
Signature · 1 line
Dmitry