Skip to contentSkip to conversation

ALSA: compress: Fix task resource leak on copy_to_user() failure

PATCH Superseded Sreeraj S Kurup 3 participants 8 messages Nov 13 – Nov 19

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

PATCH 1/1 ALSA: compress: Fix task resource leak on copy_to_user() failure

The task creation path allocates resources and installs file descriptors before copying the task information back to userspace.

If copy_to_user() fails, the function returns -EFAULT without releasing the allocated task, DMA-BUF references, or file descriptors. This leaves the task registered and leaks resources.

Delay fd_install() until copy_to_user() succeeds. If the copy fails, release the reserved file descriptors and their DMA-BUF references, then free the task using snd_compr_task_free_one().

This ensures that task resources are properly released when copying task information to userspace fails.

---
 sound/core/compress_offload.c | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)
sound/core/compress_offload.c +26 -8
index 5e5e0f78f26d..432717d5d433 100644
@@ -1124,6 +1124,9 @@ static void snd_compr_task_free(struct snd_compr_task_runtime *task)
 	kfree(task);
 }
 
+static void snd_compr_task_free_one(struct snd_compr_stream *stream,
+				    struct snd_compr_task_runtime *task);
+
 static u64 snd_compr_seqno_next(struct snd_compr_stream *stream)
 {
 	u64 seqno = ++stream->runtime->task_seqno;
@@ -1132,7 +1135,9 @@ static u64 snd_compr_seqno_next(struct snd_compr_stream *stream)
 	return seqno;
 }
 
-static int snd_compr_task_new(struct snd_compr_stream *stream, struct snd_compr_task *utask)
+static int snd_compr_task_new(struct snd_compr_stream *stream,
+			      struct snd_compr_task *utask,
+			      struct snd_compr_task_runtime **runtime_task)
 {
 	struct snd_compr_task_runtime *task;
 	int retval, fd_i, fd_o;
@@ -1169,12 +1174,11 @@ static int snd_compr_task_new(struct snd_compr_stream *stream, struct snd_compr_
 	/* keep dmabuf reference until freed with task free ioctl */
 	get_dma_buf(task->input);
 	get_dma_buf(task->output);
-	fd_install(fd_i, task->input->file);
-	fd_install(fd_o, task->output->file);
 	utask->input_fd = fd_i;
 	utask->output_fd = fd_o;
 	list_add_tail(&task->list, &stream->runtime->tasks);
 	stream->runtime->total_tasks++;
+	*runtime_task = task;
 	return 0;
 free_driver_task:
 	stream->ops->task_free(stream, task);
@@ -1185,6 +1189,7 @@ static int snd_compr_task_new(struct snd_compr_stream *stream, struct snd_compr_
 
 static int snd_compr_task_create(struct snd_compr_stream *stream, unsigned long arg)
 {
+	struct snd_compr_task_runtime *runtime_task;
 	int retval;
 
 	if (stream->runtime->state != SNDRV_PCM_STATE_SETUP)
@@ -1194,11 +1199,24 @@ static int snd_compr_task_create(struct snd_compr_stream *stream, unsigned long
 		memdup_user((void __user *)arg, sizeof(*task));
 	if (IS_ERR(task))
 		return PTR_ERR(task);
-	retval = snd_compr_task_new(stream, task);
-	if (retval >= 0)
-		if (copy_to_user((void __user *)arg, task, sizeof(*task)))
-			retval = -EFAULT;
-	return retval;
+	retval = snd_compr_task_new(stream, task, &runtime_task);
+	if (retval < 0)
+		return retval;
+
+	if (copy_to_user((void __user *)arg, task, sizeof(*task))) {
+		put_unused_fd(task->input_fd);
+		put_unused_fd(task->output_fd);
+
+		dma_buf_put(runtime_task->input);
+		dma_buf_put(runtime_task->output);
+		snd_compr_task_free_one(stream, runtime_task);
+		return -EFAULT;
+	}
+
+	fd_install(task->input_fd, runtime_task->input->file);
+	fd_install(task->output_fd, runtime_task->output->file);
+
+	return 0;
 }
 
 static int snd_compr_task_start_prepare(struct snd_compr_task_runtime *task,
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] ALSA: compress: missing Fixes trailer for task resource leak fix --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] ALSA: compress: missing Fixes trailer for task resource leak fix --

commit f84b819c6c323f37e0be05907b075a949d352ae1
Author: Sreeraj S Kurup <[email protected]>

Quoted 9 lines

ALSA: compress: Fix task resource leak on copy_to_user() failure

The task creation path allocates resources and installs file descriptors before copying the task information back to userspace.

If copy_to_user() fails, the function returns -EFAULT without releasing the allocated task, DMA-BUF references, or file descriptors. This leaves the task registered and leaks resources.

Quoted 8 lines

Delay fd_install() until copy_to_user() succeeds. If the copy fails, release the reserved file descriptors and their DMA-BUF references, then free the task using snd_compr_task_free_one().

This ensures that task resources are properly released when copying task information to userspace fails.

[Severity: Low]
This fixes a resource leak in the task creation path introduced by commit 04177158cf98 ("ALSA: compress_offload: introduce accel operation mode"). Should this include:

Fixes 04177158cf98 ("ALSA: compress_offload: introduce accel operation mode")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw

PATCH v2 1/1 ALSA: compress: Fix task resource leak on copy_to_user() failure

In reply to Sreeraj S Kurup

The task creation path allocates resources and installs file descriptors before copying the task information back to userspace.

If copy_to_user() fails, the function returns -EFAULT without releasing the allocated task, DMA-BUF references, or file descriptors. This leaves the task registered and leaks resources.

Delay fd_install() until copy_to_user() succeeds. If the copy fails, release the reserved file descriptors and their DMA-BUF references, then free the task using snd_compr_task_free_one().

This ensures that task resources are properly released when copying task information to userspace fails.

Fixes 04177158cf98 ("ALSA: compress_offload: introduce accel operation mode")

---
v2:
- Add Fixes trailer.

 sound/core/compress_offload.c | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)
sound/core/compress_offload.c +26 -8
index 5e5e0f78f26d..432717d5d433 100644
@@ -1124,6 +1124,9 @@ static void snd_compr_task_free(struct snd_compr_task_runtime *task)
 	kfree(task);
 }
 
+static void snd_compr_task_free_one(struct snd_compr_stream *stream,
+				    struct snd_compr_task_runtime *task);
+
 static u64 snd_compr_seqno_next(struct snd_compr_stream *stream)
 {
 	u64 seqno = ++stream->runtime->task_seqno;
@@ -1132,7 +1135,9 @@ static u64 snd_compr_seqno_next(struct snd_compr_stream *stream)
 	return seqno;
 }
 
-static int snd_compr_task_new(struct snd_compr_stream *stream, struct snd_compr_task *utask)
+static int snd_compr_task_new(struct snd_compr_stream *stream,
+			      struct snd_compr_task *utask,
+			      struct snd_compr_task_runtime **runtime_task)
 {
 	struct snd_compr_task_runtime *task;
 	int retval, fd_i, fd_o;
@@ -1169,12 +1174,11 @@ static int snd_compr_task_new(struct snd_compr_stream *stream, struct snd_compr_
 	/* keep dmabuf reference until freed with task free ioctl */
 	get_dma_buf(task->input);
 	get_dma_buf(task->output);
-	fd_install(fd_i, task->input->file);
-	fd_install(fd_o, task->output->file);
 	utask->input_fd = fd_i;
 	utask->output_fd = fd_o;
 	list_add_tail(&task->list, &stream->runtime->tasks);
 	stream->runtime->total_tasks++;
+	*runtime_task = task;
 	return 0;
 free_driver_task:
 	stream->ops->task_free(stream, task);
@@ -1185,6 +1189,7 @@ static int snd_compr_task_new(struct snd_compr_stream *stream, struct snd_compr_
 
 static int snd_compr_task_create(struct snd_compr_stream *stream, unsigned long arg)
 {
+	struct snd_compr_task_runtime *runtime_task;
 	int retval;
 
 	if (stream->runtime->state != SNDRV_PCM_STATE_SETUP)
@@ -1194,11 +1199,24 @@ static int snd_compr_task_create(struct snd_compr_stream *stream, unsigned long
 		memdup_user((void __user *)arg, sizeof(*task));
 	if (IS_ERR(task))
 		return PTR_ERR(task);
-	retval = snd_compr_task_new(stream, task);
-	if (retval >= 0)
-		if (copy_to_user((void __user *)arg, task, sizeof(*task)))
-			retval = -EFAULT;
-	return retval;
+	retval = snd_compr_task_new(stream, task, &runtime_task);
+	if (retval < 0)
+		return retval;
+
+	if (copy_to_user((void __user *)arg, task, sizeof(*task))) {
+		put_unused_fd(task->input_fd);
+		put_unused_fd(task->output_fd);
+
+		dma_buf_put(runtime_task->input);
+		dma_buf_put(runtime_task->output);
+		snd_compr_task_free_one(stream, runtime_task);
+		return -EFAULT;
+	}
+
+	fd_install(task->input_fd, runtime_task->input->file);
+	fd_install(task->output_fd, runtime_task->output->file);
+
+	return 0;
 }
 
 static int snd_compr_task_start_prepare(struct snd_compr_task_runtime *task,
Signature · 1 line
2.55.0
#3 lore raw

Hi Takashi,

Thanks for reviewing my other patches.

I noticed that this patch is still pending review. Could you please take a look when you get a chance?

Thanks,
Sreeraj

#4 lore raw
Quoted 7 lines · Sreeraj S Kurup

Hi Takashi,

Thanks for reviewing my other patches.

I noticed that this patch is still pending review. Could you please take a look when you get a chance?

I'm still considering whether it's safe. The open code in the error path looks a bit scary.

Aren't dma_buf_put() called already in snd_compr_task_free() that is called from snd_compr_task_free_one()?

Also, a bit concern is that we expose to user-space before actually installing the file descriptor. I don't think there will be so much race here, but just need to be sure...

thanks,

Takashi

#5 lore raw

Hi Takashi,

Thanks for reviewing.

Regarding dma_buf_put(), get_dma_buf() takes an extra reference for each DMA-BUF. This reference is intended to be transferred to the file descriptor through fd_install(). If copy_to_user() fails before fd_install(), these extra references must be released separately.

snd_compr_task_free_one() releases the task's original DMA-BUF references through snd_compr_task_free(). So both the explicit dma_buf_put() calls and snd_compr_task_free_one() are needed.

Regarding the file descriptor ordering, I moved fd_install() after copy_to_user() so that the file descriptors are installed only after the task information is successfully copied to userspace.

There is a small time gap between copying the descriptor numbers and installing the file descriptors. Userspace should use these descriptors only after the ioctl completes.

Please let me know if you have any further concerns.

Best regards,
Sreeraj

#6 lore raw
Quoted 19 lines · Sreeraj S Kurup

Hi Takashi,

Thanks for reviewing.

Regarding dma_buf_put(), get_dma_buf() takes an extra reference for each DMA-BUF. This reference is intended to be transferred to the file descriptor through fd_install(). If copy_to_user() fails before fd_install(), these extra references must be released separately.

snd_compr_task_free_one() releases the task's original DMA-BUF references through snd_compr_task_free(). So both the explicit dma_buf_put() calls and snd_compr_task_free_one() are needed.

Regarding the file descriptor ordering, I moved fd_install() after copy_to_user() so that the file descriptors are installed only after the task information is successfully copied to userspace.

Quoted 8 lines

There is a small time gap between copying the descriptor numbers and installing the file descriptors. Userspace should use these descriptors only after the ioctl completes.

Please let me know if you have any further concerns.

Best regards,
Sreeraj

OK, now I applied to for-next branch. Thanks.

Takashi