Skip to contentSkip to conversation

media: cec: extron-da-hd-4k-plus: add sanity check

PATCH In review Hans Verkuil 3 participants 4 messages Jun 18 – Jun 29

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: cec: extron-da-hd-4k-plus: add sanity check

Add check to prevent overflowing msg.msg[] in case the incoming data is malformed.

---

drivers/media/cec/usb/extron-da-hd-4k-plus/extron-da-hd-4k-plus.c +2 -1
index 3381d86096a1..3c6ce6f3d93e 100644
@@ -657,7 +657,8 @@ static void extron_process_received(struct extron_port *port, const char *data)
 	if (!port || port->disconnected)
 		return;
 
-	if (len < 5 || (len - 2) % 3 || data[len - 2] != '*')
+	if (len < 5 || ((len - 2) / 3 > sizeof(msg.msg)) ||
+	    (len - 2) % 3 || data[len - 2] != '*')
 		goto malformed;
 
 	while (*data != '*') {
#1 lore raw
Quoted 4 lines · Hans Verkuil

Add check to prevent overflowing msg.msg[] in case the incoming data is malformed.

Fixes 056f2821b631 ("media: cec: extron-da-hd-4k-plus: add the Extron DA HD 4K Plus CEC driver")

Regards,

Hans

Quoted 16 lines

---

drivers/media/cec/usb/extron-da-hd-4k-plus/extron-da-hd-4k-plus.c +2 -1
index 3381d86096a1..3c6ce6f3d93e 100644
@@ -657,7 +657,8 @@ static void extron_process_received(struct extron_port *port, const char *data)
 	if (!port || port->disconnected)
 		return;
 
-	if (len < 5 || (len - 2) % 3 || data[len - 2] != '*')
+	if (len < 5 || ((len - 2) / 3 > sizeof(msg.msg)) ||
+	    (len - 2) % 3 || data[len - 2] != '*')
 		goto malformed;
 
 	while (*data != '*') {
Reviewed-by
#2 lore raw
Quoted 8 lines · Hans Verkuil
Quoted 4 lines · Hans Verkuil

Add check to prevent overflowing msg.msg[] in case the incoming data is malformed.

Fixes 056f2821b631 ("media: cec: extron-da-hd-4k-plus: add the Extron DA HD 4K Plus CEC driver")

Looks good.

Quoted 22 lines

Regards,

Hans

Quoted 16 lines

---

drivers/media/cec/usb/extron-da-hd-4k-plus/extron-da-hd-4k-plus.c +2 -1
index 3381d86096a1..3c6ce6f3d93e 100644
@@ -657,7 +657,8 @@ static void extron_process_received(struct extron_port *port, const char *data)
 	if (!port || port->disconnected)
 		return;
 
-	if (len < 5 || (len - 2) % 3 || data[len - 2] != '*')
+	if (len < 5 || ((len - 2) / 3 > sizeof(msg.msg)) ||
+	    (len - 2) % 3 || data[len - 2] != '*')
 		goto malformed;
 
 	while (*data != '*') {
#3 lore raw
In reply to Hans Verkuil

Hi Hans,

Thank you for the patch.

Quoted 10 lines · Hans Verkuil

Add check to prevent overflowing msg.msg[] in case the incoming data is malformed.

---

drivers/media/cec/usb/extron-da-hd-4k-plus/extron-da-hd-4k-plus.c +0 -0
index 3381d86096a1..3c6ce6f3d93e 100644

drivers/media/cec/usb/extron-da-hd-4k-plus/extron-da-hd-4k-plus.c static void extron_process_received(struct extron_port *port, const char *data) · line 660

Quoted 5 lines
drivers/media/cec/usb/extron-da-hd-4k-plus/extron-da-hd-4k-plus.c +1 -1
@@ -657,7 +657,8 @@ static void extron_process_received(struct extron_port *port, const char *data)
 	if (!port || port->disconnected)
 		return;
 
-	if (len < 5 || (len - 2) % 3 || data[len - 2] != '*')
+	if (len < 5 || ((len - 2) / 3 > sizeof(msg.msg)) ||

I think you should use ARRAY_SIZE

drivers/media/cec/usb/extron-da-hd-4k-plus/extron-da-hd-4k-plus.c static void extron_process_received(struct extron_port *port, const char *data) · line 661

Quoted 1 line
drivers/media/cec/usb/extron-da-hd-4k-plus/extron-da-hd-4k-plus.c +1 -0
+	    (len - 2) % 3 || data[len - 2] != '*')

This seems correct, but I think the code would be easier to read and maintain if you wrote

	int len;

	/* The last two bytes are ignored because ... */
	len = strlen(data) - 2;

	/*
	 * Ensure the data has at least one message, at most the number
	 * of messages that fit in msg.msg, and no extra bytes.
	 */
	if (len < 3 || len / 3 > ARRAY_SIZE(msg.msg) || len % 3)

goto malformed;

	if (data[len] != '*')

goto malformed;

Quoted 4 lines
drivers/media/cec/usb/extron-da-hd-4k-plus/extron-da-hd-4k-plus.c +0 -0
 		goto malformed;
 
 	while (*data != '*') {
Signature · 3 lines
Regards,

Laurent Pinchart