Skip to contentSkip to conversation

media: allegro: fix potential null dereference on header

PATCH Superseded Colin King 2 participants 2 messages Jul 20, 2020 – Jul 22, 2020

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 media: allegro: fix potential null dereference on header

From: Colin Ian King <[email protected]>

The pointer header is an alias to msg and msg is being null checked. However, if msg is null then header is also null and this can lead to a null pointer dereference on the assignment type = header->type. Fix this by only dereferencing header after the null check on msg.

Addresses-Coverity: ("Dereference before null check")

Fixes 3de16839669f ("media: allegro: add explicit mail encoding and decoding")
---
 drivers/staging/media/allegro-dvt/allegro-mail.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/staging/media/allegro-dvt/allegro-mail.c +3 -1
index 4ac65de12463..4496e2a4da5c 100644
@@ -462,12 +462,14 @@ allegro_dec_encode_frame(struct mcu_msg_encode_frame_response *msg, u32 *src)
 ssize_t allegro_encode_mail(u32 *dst, void *msg)
 {
 	const struct mcu_msg_header *header = msg;
-	enum mcu_msg_type type = header->type;
+	enum mcu_msg_type type;
 	ssize_t size;
 
 	if (!msg || !dst)
 		return -EINVAL;
 
+	type = header->type;
+
 	switch (type) {
 	case MCU_MSG_TYPE_INIT:
 		size = allegro_enc_init(&dst[1], msg);
Signature · 1 line
2.27.0
#1 lore raw
Quoted 20 lines · Colin King

From: Colin Ian King <[email protected]>

The pointer header is an alias to msg and msg is being null checked. However, if msg is null then header is also null and this can lead to a null pointer dereference on the assignment type = header->type. Fix this by only dereferencing header after the null check on msg.

Addresses-Coverity: ("Dereference before null check")

Fixes 3de16839669f ("media: allegro: add explicit mail encoding and decoding")
---
 drivers/staging/media/allegro-dvt/allegro-mail.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/staging/media/allegro-dvt/allegro-mail.c +0 -0
index 4ac65de12463..4496e2a4da5c 100644
@@ -462,12 +462,14 @@ allegro_dec_encode_frame(struct mcu_msg_encode_frame_response *msg, u32 *src)
 ssize_t allegro_encode_mail(u32 *dst, void *msg)

drivers/staging/media/allegro-dvt/allegro-mail.c allegro_dec_encode_frame(struct mcu_msg_encode_frame_response *msg, u32 *src) · line 471

Quoted 10 lines
drivers/staging/media/allegro-dvt/allegro-mail.c +2 -1
 {
 	const struct mcu_msg_header *header = msg;
-	enum mcu_msg_type type = header->type;
+	enum mcu_msg_type type;
 	ssize_t size;
 
 	if (!msg || !dst)
 		return -EINVAL;
 
+	type = header->type;

type is only used in the switch statement right below the assignment. Thus, you could remove the local variable and directly use header->type in the switch.

Michael

Quoted 8 lines
drivers/staging/media/allegro-dvt/allegro-mail.c +1 -0
+
 	switch (type) {
 	case MCU_MSG_TYPE_INIT:
 		size = allegro_enc_init(&dst[1], msg);
Signature · 1 line
2.27.0