[PATCH] media: dtv5100: fix buffer overflow in dtv5100_i2c_msg()

Sameeksha Sankpal posted 1 patch 1 week, 2 days ago
drivers/media/usb/dvb-usb/dtv5100.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
[PATCH] media: dtv5100: fix buffer overflow in dtv5100_i2c_msg()
Posted by Sameeksha Sankpal 1 week, 2 days ago
syzbot reported a field-spanning write where rlen (4096 bytes) is
copied into st->data, which is only 80 bytes in size. This causes a
buffer overflow and triggers KASAN warnings.

Clamp the copy size using min_t() to ensure we never write beyond the
bounds of st->data, and pass the clamped size to usb_control_msg().

This fixes the following syzbot report:
Reported-by: syzbot+a83ee2dae0e6cc6cd3aa@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=a83ee2dae0e6cc6cd3aa
Signed-off-by: Sameeksha Sankpal <sameekshasankpal@gmail.com>
---
 drivers/media/usb/dvb-usb/dtv5100.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/media/usb/dvb-usb/dtv5100.c b/drivers/media/usb/dvb-usb/dtv5100.c
index 3d85c6f7f6ec..df1ce9b49ecf 100644
--- a/drivers/media/usb/dvb-usb/dtv5100.c
+++ b/drivers/media/usb/dvb-usb/dtv5100.c
@@ -55,10 +55,13 @@ static int dtv5100_i2c_msg(struct dvb_usb_device *d, u8 addr,
 	}
 	index = (addr << 8) + wbuf[0];
 
-	memcpy(st->data, rbuf, rlen);
+	/* Prevent buffer overflow: st->data is 80 bytes */
+	size_t copy_len = min_t(size_t, rlen, sizeof(st->data));
+
+	memcpy(st->data, rbuf, copy_len);
 	msleep(1); /* avoid I2C errors */
 	return usb_control_msg(d->udev, pipe, request,
-			       type, value, index, st->data, rlen,
+			       type, value, index, st->data, copy_len,
 			       DTV5100_USB_TIMEOUT);
 }
 
-- 
2.43.0