Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ALSA: pcm: Clear the full allocated memory at hw_params

The PCM hw_params core function tries to clear up the PCM buffer
before actually using for avoiding the information leak from the
previous usages or the usage before a new allocation. It performs the
memset() with runtime->dma_bytes, but this might still leave some
remaining bytes untouched; namely, the PCM buffer size is aligned in
page size for mmap, hence runtime->dma_bytes doesn't necessarily cover
all PCM buffer pages, and the remaining bytes are exposed via mmap.

This patch changes the memory clearance to cover the all buffer pages
if the stream is supposed to be mmap-ready (that guarantees that the
buffer size is aligned in page size).

Reviewed-by: Lars-Peter Clausen <lars@metafoo.de>
Link: https://lore.kernel.org/r/20201218145625.2045-3-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>

+7 -2
+7 -2
sound/core/pcm_native.c
··· 755 755 runtime->boundary *= 2; 756 756 757 757 /* clear the buffer for avoiding possible kernel info leaks */ 758 - if (runtime->dma_area && !substream->ops->copy_user) 759 - memset(runtime->dma_area, 0, runtime->dma_bytes); 758 + if (runtime->dma_area && !substream->ops->copy_user) { 759 + size_t size = runtime->dma_bytes; 760 + 761 + if (runtime->info & SNDRV_PCM_INFO_MMAP) 762 + size = PAGE_ALIGN(size); 763 + memset(runtime->dma_area, 0, size); 764 + } 760 765 761 766 snd_pcm_timer_resolution_change(substream); 762 767 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);