Skip to content

Commit 02b2b69

Browse files
tiwaigregkh
authored andcommitted
ALSA: pcm: oss: Fix negative period/buffer sizes
commit 9d2479c upstream. The period size calculation in OSS layer may receive a negative value as an error, but the code there assumes only the positive values and handle them with size_t. Due to that, a too big value may be passed to the lower layers. This patch changes the code to handle with ssize_t and adds the proper error checks appropriately. Reported-by: syzbot+bb348e9f9a954d42746f@syzkaller.appspotmail.com Reported-by: Bixuan Cui <cuibixuan@linux.alibaba.com> Cc: <stable@vger.kernel.org> Link: https://lore.kernel.org/r/1638270978-42412-1-git-send-email-cuibixuan@linux.alibaba.com Link: https://lore.kernel.org/r/20211201073606.11660-2-tiwai@suse.de Signed-off-by: Takashi Iwai <tiwai@suse.de> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6760e6d commit 02b2b69

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

‎sound/core/oss/pcm_oss.c‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params *params,
147147
*
148148
* Return the maximum value for field PAR.
149149
*/
150-
static unsigned int
150+
static int
151151
snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params *params,
152152
snd_pcm_hw_param_t var, int *dir)
153153
{
@@ -682,18 +682,24 @@ static int snd_pcm_oss_period_size(struct snd_pcm_substream *substream,
682682
struct snd_pcm_hw_params *oss_params,
683683
struct snd_pcm_hw_params *slave_params)
684684
{
685-
size_t s;
686-
size_t oss_buffer_size, oss_period_size, oss_periods;
687-
size_t min_period_size, max_period_size;
685+
ssize_t s;
686+
ssize_t oss_buffer_size;
687+
ssize_t oss_period_size, oss_periods;
688+
ssize_t min_period_size, max_period_size;
688689
struct snd_pcm_runtime *runtime = substream->runtime;
689690
size_t oss_frame_size;
690691

691692
oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
692693
params_channels(oss_params) / 8;
693694

695+
oss_buffer_size = snd_pcm_hw_param_value_max(slave_params,
696+
SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
697+
NULL);
698+
if (oss_buffer_size <= 0)
699+
return -EINVAL;
694700
oss_buffer_size = snd_pcm_plug_client_size(substream,
695-
snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL)) * oss_frame_size;
696-
if (!oss_buffer_size)
701+
oss_buffer_size * oss_frame_size);
702+
if (oss_buffer_size <= 0)
697703
return -EINVAL;
698704
oss_buffer_size = rounddown_pow_of_two(oss_buffer_size);
699705
if (atomic_read(&substream->mmap_count)) {
@@ -730,7 +736,7 @@ static int snd_pcm_oss_period_size(struct snd_pcm_substream *substream,
730736

731737
min_period_size = snd_pcm_plug_client_size(substream,
732738
snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
733-
if (min_period_size) {
739+
if (min_period_size > 0) {
734740
min_period_size *= oss_frame_size;
735741
min_period_size = roundup_pow_of_two(min_period_size);
736742
if (oss_period_size < min_period_size)
@@ -739,7 +745,7 @@ static int snd_pcm_oss_period_size(struct snd_pcm_substream *substream,
739745

740746
max_period_size = snd_pcm_plug_client_size(substream,
741747
snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
742-
if (max_period_size) {
748+
if (max_period_size > 0) {
743749
max_period_size *= oss_frame_size;
744750
max_period_size = rounddown_pow_of_two(max_period_size);
745751
if (oss_period_size > max_period_size)
@@ -752,7 +758,7 @@ static int snd_pcm_oss_period_size(struct snd_pcm_substream *substream,
752758
oss_periods = substream->oss.setup.periods;
753759

754760
s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
755-
if (runtime->oss.maxfrags && s > runtime->oss.maxfrags)
761+
if (s > 0 && runtime->oss.maxfrags && s > runtime->oss.maxfrags)
756762
s = runtime->oss.maxfrags;
757763
if (oss_periods > s)
758764
oss_periods = s;

0 commit comments

Comments
 (0)