Skip to contentSkip to conversation

ALSA: 6fire: Fix undefined behavior bug in struct comm_runtime

PATCH In review Gustavo A. R. Silva 4 participants 4 messages Sep 29, 2023 – Oct 6, 2023 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: 6fire: Fix undefined behavior bug in struct comm_runtime

struct urb is a flexible structure, which means that it contains a flexible-array member at the bottom. This could potentially lead to an overwrite of the objects following receiver in struct comm_runtime, among them some function pointers.

Fix this by placing the declaration of object receiver at the end of struct comm_runtime.

Fixes ddb6b5a96437 ("ALSA: 6fire: fix DMA issues with URB transfer_buffer usage")
---
 sound/usb/6fire/comm.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/usb/6fire/comm.h +1 -1
index 2447d7ecf179..ee81572a4eec 100644
@@ -19,7 +19,6 @@ enum /* settings for comm */
 struct comm_runtime {
 	struct sfire_chip *chip;
 
-	struct urb receiver;
 	u8 *receiver_buffer;
 
 	u8 serial; /* urb serial */
@@ -30,6 +29,7 @@ struct comm_runtime {
 	int (*write8)(struct comm_runtime *rt, u8 request, u8 reg, u8 value);
 	int (*write16)(struct comm_runtime *rt, u8 request, u8 reg,
 			u8 vh, u8 vl);
+	struct urb receiver;
 };
 
 int usb6fire_comm_init(struct sfire_chip *chip);
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Gustavo A. R. Silva

struct urb is a flexible structure, which means that it contains a flexible-array member at the bottom. This could potentially lead to an overwrite of the objects following receiver in struct comm_runtime, among them some function pointers.

Fix this by placing the declaration of object receiver at the end of struct comm_runtime.

Fixes ddb6b5a96437 ("ALSA: 6fire: fix DMA issues with URB transfer_buffer usage")

Should these mention -Wflex-array-member-not-at-end ?

Signature · 1 line
Kees Cook
#2 lore raw
Quoted 6 lines · Kees Cook
Quoted 5 lines · Gustavo A. R. Silva

struct urb is a flexible structure, which means that it contains a flexible-array member at the bottom. This could potentially lead to an overwrite of the objects following receiver in struct comm_runtime, among them some function pointers.

Quoted 8 lines
Quoted 6 lines

Fix this by placing the declaration of object receiver at the end of struct comm_runtime.

Fixes ddb6b5a96437 ("ALSA: 6fire: fix DMA issues with URB transfer_buffer usage")

Should these mention -Wflex-array-member-not-at-end ?

Probably, yes. I wasn't sure about mentioning it because it's not been released yet.

Quoted 3 lines

Thanks!
--
Gustavo

#3 lore raw
In reply to Gustavo A. R. Silva
Quoted 6 lines · Gustavo A. R. Silva

struct urb is a flexible structure, which means that it contains a flexible-array member at the bottom. This could potentially lead to an overwrite of the objects following receiver in struct comm_runtime, among them some function pointers.

Quoted 6 lines

Fix this by placing the declaration of object receiver at the end of struct comm_runtime.

Fixes ddb6b5a96437 ("ALSA: 6fire: fix DMA issues with URB transfer_buffer usage")

Sorry for the late reply, as I've been (still) off since the last week.

Through a quick glance, I don't mind much to apply this, but I still wonder how this "fixes" anything. Does it silence compiler warnings or such?

Certainly struct urb *may* have flex array, but in this case, it's clearly not used, so it's fixed-size. And, even if we shuffle the member to put to the last, it doesn't fix anything automagically alone. If a flex array were used, it still leads to memory corruption unless we implement the allocation properly. So I find the patch description is somehow misleading.

thanks,

Takashi

Quoted 27 lines
---
 sound/usb/6fire/comm.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/usb/6fire/comm.h +1 -1
index 2447d7ecf179..ee81572a4eec 100644
@@ -19,7 +19,6 @@ enum /* settings for comm */
 struct comm_runtime {
 	struct sfire_chip *chip;
 
-	struct urb receiver;
 	u8 *receiver_buffer;
 
 	u8 serial; /* urb serial */
@@ -30,6 +29,7 @@ struct comm_runtime {
 	int (*write8)(struct comm_runtime *rt, u8 request, u8 reg, u8 value);
 	int (*write16)(struct comm_runtime *rt, u8 request, u8 reg,
 			u8 vh, u8 vl);
+	struct urb receiver;
 };
 
 int usb6fire_comm_init(struct sfire_chip *chip);
Signature · 1 line
2.34.1