Skip to contentSkip to conversation

ALSA: more fixes for timer

PATCH New Takashi Iwai 1 participant 4 messages Jun 9 Sound

lore
Share & tools
lore
#0 lore raw

cover ALSA: more fixes for timer

Hi,

as there are still open holes in ALSA timer code that may lead to UAFs, here is a patch to address more drastically, by introducing the refcounting for the timer object's lifecycle. With this change, the previous workaround is no longer needed, hence it's reverted in the second patch. And, the third one is to assure the pending work being finished at freeing. I don't think we've received a report about it, but just to be sure.

This is targeted for 7.2.

Takashi

===

Takashi Iwai (3):
ALSA: timer: Manage timer object with kref Revert "ALSA: timer: Fix UAF at snd_timer_user_params()"
ALSA: timer: Disable work at freeing timer object

 include/sound/timer.h      |   6 +++
 sound/core/seq/seq_timer.c |  11 ++--
 sound/core/timer.c         | 107 +++++++++++++++++++++++++++----------
 sound/core/timer_compat.c  |   5 +-
 4 files changed, 97 insertions(+), 32 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH 1/3 ALSA: timer: Manage timer object with kref

So far we've tried to address UAFs in ALSA timer code by applying the locks at various places, but the fundamental problem is that the timer object may be released while the belonging timer instance objects are still present and accessing to it. This patch is a more proper fix to address that issue, namely, by refcounting and keeping the timer object.

The basic implementation is to use kref for the refcount of the timer object, and take/release the reference at assigning/releasing the instance, as well as at referring from ioctls or ALSA sequencer code. The reference from ioctl or ALSA sequencer is abstracted with snd_timeri_timer auto-cleanup.

Note that this change assumes that the code already took the fix commit da3039e91d1f ("ALSA: timer: Forcibly close timer instances at closing"); otherwise the refcount may be unbalanced when the timer is freed while slave instances are still present.

---
 include/sound/timer.h      |   6 +++
 sound/core/seq/seq_timer.c |  11 ++--
 sound/core/timer.c         | 104 +++++++++++++++++++++++++++----------
 sound/core/timer_compat.c  |   5 +-
 4 files changed, 95 insertions(+), 31 deletions(-)
include/sound/timer.h +6 -0
index 83bafe70cf33..e549eab7145b 100644
@@ -75,6 +75,7 @@ struct snd_timer {
 	struct list_head ack_list_head;
 	struct list_head sack_list_head; /* slow ack list head */
 	struct work_struct task_work;
+	struct kref kref;
 	int max_instances;	/* upper limit of timer instances */
 	int num_instances;	/* current number of timer instances */
 };
@@ -131,4 +132,9 @@ int snd_timer_pause(struct snd_timer_instance *timeri);
 
 void snd_timer_interrupt(struct snd_timer *timer, unsigned long ticks_left);
 
+struct snd_timer *snd_timeri_timer_get(struct snd_timer_instance *timeri);
+void snd_timeri_timer_put(struct snd_timer *timer);
+
+DEFINE_FREE(snd_timeri_timer, struct snd_timer *, if (_T) snd_timeri_timer_put(_T))
+
 #endif /* __SOUND_TIMER_H */
sound/core/seq/seq_timer.c +8 -3
index 9bef2f792498..4cd7211ccf48 100644
@@ -333,10 +333,10 @@ int snd_seq_timer_stop(struct snd_seq_timer *tmr)
 
 static int initialize_timer(struct snd_seq_timer *tmr)
 {
-	struct snd_timer *t;
 	unsigned long freq;
 
-	t = tmr->timeri->timer;
+	struct snd_timer *t __free(snd_timeri_timer) =
+		snd_timeri_timer_get(tmr->timeri);
 	if (!t)
 		return -EINVAL;
 
@@ -456,7 +456,12 @@ void snd_seq_info_timer_read(struct snd_info_entry *entry,
 			ti = tmr->timeri;
 			if (!ti)
 				break;
-			snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name);
+
+			struct snd_timer *t __free(snd_timeri_timer) =
+				snd_timeri_timer_get(ti);
+			snd_iprintf(buffer, "Timer for queue %i : %s\n",
+				    q->queue,
+				    t ? t->name : "DEAD");
 			resolution = snd_timer_resolution(ti) * tmr->ticks;
 			snd_iprintf(buffer, "  Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000);
 			snd_iprintf(buffer, "  Skew : %u / %u\n", tmr->skew, tmr->skew_base);
sound/core/timer.c +78 -26

189 lines not on this page · raw message

sound/core/timer_compat.c +3 -2
index 4ae9eaeb5afb..25ee81c1668b 100644
@@ -49,12 +49,13 @@ static int snd_timer_user_info_compat(struct file *file,
 {
 	struct snd_timer_user *tu;
 	struct snd_timer_info32 info;
-	struct snd_timer *t;
 
 	tu = file->private_data;
 	if (!tu->timeri)
 		return -EBADFD;
-	t = tu->timeri->timer;
+
+	struct snd_timer *t __free(snd_timeri_timer) =
+		snd_timeri_timer_get(tu->timeri);
 	if (!t)
 		return -EBADFD;
 	memset(&info, 0, sizeof(info));
Signature · 1 line
2.54.0
#2 lore raw

PATCH 2/3 Revert "ALSA: timer: Fix UAF at snd_timer_user_params()"

In reply to Takashi Iwai

This reverts commit 053a401b592be424fea9d57c789f66cd5d8cec11.

With the change of the timer object lifecycle with kref, this temporary workaround is no longer needed.

---
 sound/core/timer.c | 1 -
 1 file changed, 1 deletion(-)
sound/core/timer.c +0 -1
index 6d8fc1ee29f5..6baa63a3bad0 100644
@@ -1861,7 +1861,6 @@ static int snd_timer_user_params(struct file *file,
 	struct snd_timer_params params;
 	int err;
 
-	guard(mutex)(&register_mutex);
 	tu = file->private_data;
 	if (!tu->timeri)
 		return -EBADFD;
Signature · 1 line
2.54.0
#3 lore raw

PATCH 3/3 ALSA: timer: Disable work at freeing timer object

In reply to Takashi Iwai

There might be a pending work at freeing a timer object, hence clean it up properly.

---
 sound/core/timer.c | 2 ++
 1 file changed, 2 insertions(+)
sound/core/timer.c +2 -0
index 6baa63a3bad0..a3ae5416485e 100644
@@ -1045,6 +1045,8 @@ static int snd_timer_free(struct snd_timer *timer)
 		list_del(&timer->device_list);
 	}
 
+	disable_work_sync(&timer->task_work);
+
 	snd_timer_ref_put(timer);
 	return 0;
 }
Signature · 1 line
2.54.0