diff mbox series

HID: cp2112: prevent a buffer overflow in cp2112_xfer()

Message ID 20220608122609.70861-1-harshit.m.mogalapalli@oracle.com (mailing list archive)
State Mainlined
Commit 381583845d19cb4bd21c8193449385f3fefa9caf
Delegated to: Jiri Kosina
Headers show
Series HID: cp2112: prevent a buffer overflow in cp2112_xfer() | expand

Commit Message

Harshit Mogalapalli June 8, 2022, 12:26 p.m. UTC
Smatch warnings:
drivers/hid/hid-cp2112.c:793 cp2112_xfer() error: __memcpy()
'data->block[1]' too small (33 vs 255)
drivers/hid/hid-cp2112.c:793 cp2112_xfer() error: __memcpy() 'buf' too
small (64 vs 255)

The 'read_length' variable is provided by 'data->block[0]' which comes
from user and it(read_length) can take a value between 0-255. Add an
upper bound to 'read_length' variable to prevent a buffer overflow in
memcpy().

Fixes: 542134c0375b ("HID: cp2112: Fix I2C_BLOCK_DATA transactions")
Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
---
 drivers/hid/hid-cp2112.c | 5 +++++
 1 file changed, 5 insertions(+)

Comments

Jiri Kosina June 9, 2022, 8:55 a.m. UTC | #1
On Wed, 8 Jun 2022, Harshit Mogalapalli wrote:

> Smatch warnings:
> drivers/hid/hid-cp2112.c:793 cp2112_xfer() error: __memcpy()
> 'data->block[1]' too small (33 vs 255)
> drivers/hid/hid-cp2112.c:793 cp2112_xfer() error: __memcpy() 'buf' too
> small (64 vs 255)
> 
> The 'read_length' variable is provided by 'data->block[0]' which comes
> from user and it(read_length) can take a value between 0-255. Add an
> upper bound to 'read_length' variable to prevent a buffer overflow in
> memcpy().
> 
> Fixes: 542134c0375b ("HID: cp2112: Fix I2C_BLOCK_DATA transactions")
> Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
> ---
>  drivers/hid/hid-cp2112.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/drivers/hid/hid-cp2112.c b/drivers/hid/hid-cp2112.c
> index ece147d1a278..1e16b0fa310d 100644
> --- a/drivers/hid/hid-cp2112.c
> +++ b/drivers/hid/hid-cp2112.c
> @@ -790,6 +790,11 @@ static int cp2112_xfer(struct i2c_adapter *adap, u16 addr,
>  		data->word = le16_to_cpup((__le16 *)buf);
>  		break;
>  	case I2C_SMBUS_I2C_BLOCK_DATA:
> +		if (read_length > I2C_SMBUS_BLOCK_MAX) {
> +			ret = -EINVAL;
> +			goto power_normal;
> +		}
> +
>  		memcpy(data->block + 1, buf, read_length);
>  		break;

Good catch, now applied. Thanks,
diff mbox series

Patch

diff --git a/drivers/hid/hid-cp2112.c b/drivers/hid/hid-cp2112.c
index ece147d1a278..1e16b0fa310d 100644
--- a/drivers/hid/hid-cp2112.c
+++ b/drivers/hid/hid-cp2112.c
@@ -790,6 +790,11 @@  static int cp2112_xfer(struct i2c_adapter *adap, u16 addr,
 		data->word = le16_to_cpup((__le16 *)buf);
 		break;
 	case I2C_SMBUS_I2C_BLOCK_DATA:
+		if (read_length > I2C_SMBUS_BLOCK_MAX) {
+			ret = -EINVAL;
+			goto power_normal;
+		}
+
 		memcpy(data->block + 1, buf, read_length);
 		break;
 	case I2C_SMBUS_BLOCK_DATA: