Skip to contentSkip to conversation

ALSA: fireface: fix locking bug in ff400_copy_msg_to_user()

PATCH Applied Dan Carpenter 3 participants 3 messages Jan 17, 2023 – Jan 18, 2023

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

PATCH 1/1 ALSA: fireface: fix locking bug in ff400_copy_msg_to_user()

The ff400_copy_msg_to_user() function drops the spin lock to call copy_to_user(). However, if the copy_to_user() fails, then it must take the lock again before returning. Failure to take the lock leads to a double unlock in the caller, hwdep_read().

Fixes acdebd8b4c0c ("ALSA: fireface: implement message parser for Fireface 400")
---
 sound/firewire/fireface/ff-protocol-former.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
sound/firewire/fireface/ff-protocol-former.c +8 -6
index f58008762fe6..fa41de978756 100644
@@ -680,28 +680,30 @@ static long ff400_copy_msg_to_user(struct snd_ff *ff, char __user *buf, long cou
 	struct ff400_msg_parser *parser = ff->msg_parser;
 	u32 type = SNDRV_FIREWIRE_EVENT_FF400_MESSAGE;
 	long consumed = 0;
+	int ret = 0;
 
 	if (count < 8)
 		return 0;
 
 	spin_unlock_irq(&ff->lock);
-
 	if (copy_to_user(buf, &type, sizeof(type)))
-		return -EFAULT;
-
+		ret = -EFAULT;
 	spin_lock_irq(&ff->lock);
+	if (ret)
+		return ret;
 
 	count -= sizeof(type);
 	consumed += sizeof(type);
 
 	while (count >= sizeof(*parser->msgs) && parser->pull_pos != parser->push_pos) {
 		spin_unlock_irq(&ff->lock);
-
 		if (copy_to_user(buf + consumed, parser->msgs + parser->pull_pos,
 				 sizeof(*parser->msgs)))
-			return -EFAULT;
-
+			ret = -EFAULT;
 		spin_lock_irq(&ff->lock);
+		if (ret)
+			return ret;
+
 		++parser->pull_pos;
 		if (parser->pull_pos >= FF400_QUEUE_SIZE)
 			parser->pull_pos = 0;
Signature · 1 line
2.35.1
maintainer Acked-by on v1 1/1
#1 lore raw

Hi,

Quoted 10 lines · Dan Carpenter

The ff400_copy_msg_to_user() function drops the spin lock to call copy_to_user(). However, if the copy_to_user() fails, then it must take the lock again before returning. Failure to take the lock leads to a double unlock in the caller, hwdep_read().

Fixes acdebd8b4c0c ("ALSA: fireface: implement message parser for Fireface 400")
---
 sound/firewire/fireface/ff-protocol-former.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

Indeed. Thanks for your care.

Quoted 43 lines
sound/firewire/fireface/ff-protocol-former.c +8 -6
index f58008762fe6..fa41de978756 100644
@@ -680,28 +680,30 @@ static long ff400_copy_msg_to_user(struct snd_ff *ff, char __user *buf, long cou
 	struct ff400_msg_parser *parser = ff->msg_parser;
 	u32 type = SNDRV_FIREWIRE_EVENT_FF400_MESSAGE;
 	long consumed = 0;
+	int ret = 0;
 
 	if (count < 8)
 		return 0;
 
 	spin_unlock_irq(&ff->lock);
-
 	if (copy_to_user(buf, &type, sizeof(type)))
-		return -EFAULT;
-
+		ret = -EFAULT;
 	spin_lock_irq(&ff->lock);
+	if (ret)
+		return ret;
 
 	count -= sizeof(type);
 	consumed += sizeof(type);
 
 	while (count >= sizeof(*parser->msgs) && parser->pull_pos != parser->push_pos) {
 		spin_unlock_irq(&ff->lock);
-
 		if (copy_to_user(buf + consumed, parser->msgs + parser->pull_pos,
 				 sizeof(*parser->msgs)))
-			return -EFAULT;
-
+			ret = -EFAULT;
 		spin_lock_irq(&ff->lock);
+		if (ret)
+			return ret;
+
 		++parser->pull_pos;
 		if (parser->pull_pos >= FF400_QUEUE_SIZE)
 			parser->pull_pos = 0;
Signature · 1 line
2.35.1

Regards

Takashi Sakamoto

Applied
#2 lore raw
In reply to Dan Carpenter
Quoted 8 lines · Dan Carpenter

The ff400_copy_msg_to_user() function drops the spin lock to call copy_to_user(). However, if the copy_to_user() fails, then it must take the lock again before returning. Failure to take the lock leads to a double unlock in the caller, hwdep_read().

Fixes acdebd8b4c0c ("ALSA: fireface: implement message parser for Fireface 400")

Thanks, applied.

Takashi