Skip to contentSkip to conversation

ALSA: ump: Don't accept an invalid UMP protocol number

PATCH New Takashi Iwai 1 participant 2 messages May 29, 2024 Sound

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 ALSA: ump: Don't accept an invalid UMP protocol number

When a UMP Stream Configuration message is received, the driver tries to switch the protocol, but there was no sanity check of the protocol, hence it can pass an invalid value. Add the check and bail out if a wrong value is passed.

Fixes a79807683781 ("ALSA: ump: Add helper to change MIDI protocol")

---
v1->v2: Fix the line dropped mistakenly in v1 patch

 sound/core/ump.c | 7 +++++++
 1 file changed, 7 insertions(+)
sound/core/ump.c +7 -0
index fd6a68a54278..117c7ecc4856 100644
@@ -685,10 +685,17 @@ static void seq_notify_protocol(struct snd_ump_endpoint *ump)
  */
 int snd_ump_switch_protocol(struct snd_ump_endpoint *ump, unsigned int protocol)
 {
+	unsigned int type;
+
 	protocol &= ump->info.protocol_caps;
 	if (protocol == ump->info.protocol)
 		return 0;
 
+	type = protocol & SNDRV_UMP_EP_INFO_PROTO_MIDI_MASK;
+	if (type != SNDRV_UMP_EP_INFO_PROTO_MIDI1 &&
+	    type != SNDRV_UMP_EP_INFO_PROTO_MIDI2)
+		return 0;
+
 	ump->info.protocol = protocol;
 	ump_dbg(ump, "New protocol = %x (caps = %x)\n",
 		protocol, ump->info.protocol_caps);
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 2/2 ALSA: ump: Set default protocol when not given explicitly

When an inquiry of the current protocol via UMP Stream Configuration message fails by some reason, we may leave the current protocol undefined, which may lead to unexpected behavior. Better to assume a valid protocol found in the protocol capability bits instead.

For a device that doesn't support the UMP v1.2 feature, it won't reach to this code path, and USB MIDI GTB descriptor would be used for determining the protocol, instead.

---
v1->v2: no change

 sound/core/ump.c | 8 ++++++++
 1 file changed, 8 insertions(+)
sound/core/ump.c +8 -0
index 117c7ecc4856..3f61220c23b4 100644
@@ -967,6 +967,14 @@ int snd_ump_parse_endpoint(struct snd_ump_endpoint *ump)
 	if (err < 0)
 		ump_dbg(ump, "Unable to get UMP EP stream config\n");
 
+	/* If no protocol is set by some reason, assume the valid one */
+	if (!(ump->info.protocol & SNDRV_UMP_EP_INFO_PROTO_MIDI_MASK)) {
+		if (ump->info.protocol_caps & SNDRV_UMP_EP_INFO_PROTO_MIDI2)
+			ump->info.protocol |= SNDRV_UMP_EP_INFO_PROTO_MIDI2;
+		else if (ump->info.protocol_caps & SNDRV_UMP_EP_INFO_PROTO_MIDI1)
+			ump->info.protocol |= SNDRV_UMP_EP_INFO_PROTO_MIDI1;
+	}
+
 	/* Query and create blocks from Function Blocks */
 	for (blk = 0; blk < ump->info.num_blocks; blk++) {
 		err = create_block_from_fb_info(ump, blk);
Signature · 1 line
2.43.0