Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1334538

Re: sound: memory leak in snd_seq_pool_init

From Takashi Iwai <tiwai@suse.de>
Newsgroups linux.kernel
Subject Re: sound: memory leak in snd_seq_pool_init
Date 2016-02-15 16:40 +0100
Message-ID <r2tpM-2SR-31@gated-at.bofh.it> (permalink)
References <r2nNo-7tb-19@gated-at.bofh.it> <r2ozM-824-15@gated-at.bofh.it> <r2t6q-2Md-17@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Mon, 15 Feb 2016 16:15:49 +0100,
Dmitry Vyukov wrote:
> 
> > Hm, this looks like the issue triggered by concurrent writes to the
> > same client.  Does the patch fix the problem?
> 
> Yes, it fixes the leak for me.
> 
> Tested-by: Dmitry Vyukov <dvyukov@google.com>

Thanks for the quick test.  FWIW, below is the final patch I'm going
to merge.


Takashi

-- 8< --
From: Takashi Iwai <tiwai@suse.de>
Subject: [PATCH] ALSA: seq: Fix leak of pool buffer at concurrent writes

When multiple concurrent writes happen on the ALSA sequencer device
right after the open, it may try to allocate vmalloc buffer for each
write and leak some of them.  It's because the presence check and the
assignment of the buffer is done outside the spinlock for the pool.

The fix is to move the check and the assignment into the spinlock.

(The current implementation is suboptimal, as there can be multiple
 unnecessary vmallocs because the allocation is done before the check
 in the spinlock.  But the pool size is already checked beforehand, so
 this isn't a big problem; that is, the only possible path is the
 multiple writes before any pool assignment, and practically seen, the
 current coverage should be "good enough".)

The issue was triggered by syzkaller fuzzer.

BugLink: http://lkml.kernel.org/r/CACT4Y+bSzazpXNvtAr=WXaL8hptqjHwqEyFA+VN2AWEx=aurkg@mail.gmail.com
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Tested-by: Dmitry Vyukov <dvyukov@google.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 sound/core/seq/seq_memory.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/sound/core/seq/seq_memory.c b/sound/core/seq/seq_memory.c
index 801076687bb1..c850345c43b5 100644
--- a/sound/core/seq/seq_memory.c
+++ b/sound/core/seq/seq_memory.c
@@ -383,15 +383,20 @@ int snd_seq_pool_init(struct snd_seq_pool *pool)
 
 	if (snd_BUG_ON(!pool))
 		return -EINVAL;
-	if (pool->ptr)			/* should be atomic? */
-		return 0;
 
-	pool->ptr = vmalloc(sizeof(struct snd_seq_event_cell) * pool->size);
-	if (!pool->ptr)
+	cellptr = vmalloc(sizeof(struct snd_seq_event_cell) * pool->size);
+	if (!cellptr)
 		return -ENOMEM;
 
 	/* add new cells to the free cell list */
 	spin_lock_irqsave(&pool->lock, flags);
+	if (pool->ptr) {
+		spin_unlock_irqrestore(&pool->lock, flags);
+		vfree(cellptr);
+		return 0;
+	}
+
+	pool->ptr = cellptr;
 	pool->free = NULL;
 
 	for (cell = 0; cell < pool->size; cell++) {
-- 
2.7.1

Back to linux.kernel | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

sound: memory leak in snd_seq_pool_init Dmitry Vyukov <dvyukov@google.com> - 2016-02-15 10:40 +0100
  Re: sound: memory leak in snd_seq_pool_init Takashi Iwai <tiwai@suse.de> - 2016-02-15 11:30 +0100
    Re: sound: memory leak in snd_seq_pool_init Dmitry Vyukov <dvyukov@google.com> - 2016-02-15 16:20 +0100
      Re: sound: memory leak in snd_seq_pool_init Takashi Iwai <tiwai@suse.de> - 2016-02-15 16:40 +0100

csiph-web