* [PATCH] ALSA: compress: Fix task resource leak on copy_to_user() failure
@ 2026-11-13 10:45 Sreeraj S Kurup
2026-11-13 11:04 ` [PATCH v2] " Sreeraj S Kurup
0 siblings, 1 reply; 5+ messages in thread
From: Sreeraj S Kurup @ 2026-11-13 10:45 UTC (permalink / raw)
To: perex, tiwai; +Cc: linux-kernel, linux-sound, 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.
Signed-off-by: Sreeraj S Kurup <sreekuttan2156239@gmail.com>
---
sound/core/compress_offload.c | 34 ++++++++++++++++++++++++++--------
1 file changed, 26 insertions(+), 8 deletions(-)
diff --git a/sound/core/compress_offload.c b/sound/core/compress_offload.c
index 5e5e0f78f26d..432717d5d433 100644
--- a/sound/core/compress_offload.c
+++ b/sound/core/compress_offload.c
@@ -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,
--
2.55.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2] ALSA: compress: Fix task resource leak on copy_to_user() failure
2026-11-13 10:45 [PATCH] ALSA: compress: Fix task resource leak on copy_to_user() failure Sreeraj S Kurup
@ 2026-11-13 11:04 ` Sreeraj S Kurup
2026-11-17 6:28 ` Sreeraj S Kurup
0 siblings, 1 reply; 5+ messages in thread
From: Sreeraj S Kurup @ 2026-11-13 11:04 UTC (permalink / raw)
To: sreekuttan2156239; +Cc: linux-kernel, linux-sound, perex, tiwai
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")
Signed-off-by: Sreeraj S Kurup <sreekuttan2156239@gmail.com>
---
v2:
- Add Fixes trailer.
sound/core/compress_offload.c | 34 ++++++++++++++++++++++++++--------
1 file changed, 26 insertions(+), 8 deletions(-)
diff --git a/sound/core/compress_offload.c b/sound/core/compress_offload.c
index 5e5e0f78f26d..432717d5d433 100644
--- a/sound/core/compress_offload.c
+++ b/sound/core/compress_offload.c
@@ -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,
--
2.55.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] ALSA: compress: Fix task resource leak on copy_to_user() failure
2026-11-13 11:04 ` [PATCH v2] " Sreeraj S Kurup
@ 2026-11-17 6:28 ` Sreeraj S Kurup
2026-11-17 15:13 ` Takashi Iwai
0 siblings, 1 reply; 5+ messages in thread
From: Sreeraj S Kurup @ 2026-11-17 6:28 UTC (permalink / raw)
To: sreekuttan2156239, tiwai; +Cc: linux-kernel, linux-sound, perex
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] ALSA: compress: Fix task resource leak on copy_to_user() failure
2026-11-17 6:28 ` Sreeraj S Kurup
@ 2026-11-17 15:13 ` Takashi Iwai
2026-11-17 16:01 ` Sreeraj S Kurup
0 siblings, 1 reply; 5+ messages in thread
From: Takashi Iwai @ 2026-11-17 15:13 UTC (permalink / raw)
To: Sreeraj S Kurup; +Cc: tiwai, linux-kernel, linux-sound, perex
On Tue, 17 Nov 2026 07:28:08 +0100,
Sreeraj S Kurup wrote:
>
> 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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] ALSA: compress: Fix task resource leak on copy_to_user() failure
2026-11-17 15:13 ` Takashi Iwai
@ 2026-11-17 16:01 ` Sreeraj S Kurup
0 siblings, 0 replies; 5+ messages in thread
From: Sreeraj S Kurup @ 2026-11-17 16:01 UTC (permalink / raw)
To: tiwai; +Cc: linux-kernel, linux-sound, perex, sreekuttan2156239
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
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-17 16:02 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-13 10:45 [PATCH] ALSA: compress: Fix task resource leak on copy_to_user() failure Sreeraj S Kurup
2026-11-13 11:04 ` [PATCH v2] " Sreeraj S Kurup
2026-11-17 6:28 ` Sreeraj S Kurup
2026-11-17 15:13 ` Takashi Iwai
2026-11-17 16:01 ` Sreeraj S Kurup
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®