Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp496504yba; Mon, 1 Apr 2019 10:28:09 -0700 (PDT) X-Google-Smtp-Source: APXvYqwNWhUnP5/sflGccMVBcndl7uJtOkI4f2pDe7qthUJSutZ1wzXTmYatfpxQUaXJyWT6KXH5 X-Received: by 2002:a62:ed10:: with SMTP id u16mr23442726pfh.187.1554139689315; Mon, 01 Apr 2019 10:28:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554139689; cv=none; d=google.com; s=arc-20160816; b=Io9aJbhnYPsrRmE0RNADwt33dpchj2h11YabznE3IlEgeay1J8HOPydaQSTiH9ATpJ i8XHkXYixxFftHjQkQkW2smpRyfqPDSps99VgXxh4L/js8cVN9sFgEay2LqpCLyNUcyh JrvCspwyT2HJg7Ga+1hTx8ra9kbknf1j8kUowk9uNfLU6ujQGrmnGCFrbJitvIFwhZf4 7FhFf18hORNuv4nipbuFgqPuNW9dXzJQFgTMaoP/lV/J54pyKHKlHx9chzHT9cgC7vj2 rgIyhPY/T8FyPCYeuGKDLG3aAF2KWYlZUnBLgrteYnADsZ4IWb1O3QGql0rUGWeJ8BgX B9Zg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=0/pFfCBvRU5KUr/QbeujA5CTMG1xSlbXrybNHMuJldI=; b=XiJUUz5krHHkr5rMoK7o5FOKr6KKvqxEZLcVkcWTHS9XciWVUSVkdz6eLHivkfbvex PO4wAFCxG1UOYhv/3Z8jEo2kJjbXTRoHTTdCgAK1rCjWedHDm43ZpLObnym5iH4dJjb9 1hgySPl5CUfygMk1i+j1OzK6Iins8jVvKPaxzx2/GDD8EZQwlgAqUziT0fSTmWLP45Dm MOP4zAdXzGaexg0ER73Frkln2A+udZrfbJEIeUXX416QuBBgrILXFdQfwepfNb5gX3af /z6mGh+BW39kU14HOvAL9o3ViiLS1OJrXKFWi+uKX8/fj1yI1hrdEPuBbC+wfNZO5gKp d7yA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ohwNTsCH; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q1si4947066pgh.396.2019.04.01.10.27.54; Mon, 01 Apr 2019 10:28:09 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ohwNTsCH; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732314AbfDAR1S (ORCPT + 99 others); Mon, 1 Apr 2019 13:27:18 -0400 Received: from mail.kernel.org ([198.145.29.99]:59910 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732648AbfDAR1P (ORCPT ); Mon, 1 Apr 2019 13:27:15 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0929D21852; Mon, 1 Apr 2019 17:27:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1554139634; bh=+2Yf9qJ/aNlKlaTt8nQKEvuZh2NjNlmhj/e7xFgnzIE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ohwNTsCHE9InFrXad0oERSqFP9mr/TwnEHpm+YDxb7fTlBjxr95lqUBqRRaOXAlCN enWq0J13jgg5q5blE9IoY9F1j1rCTckqMsL5MbpomMw2gkcFzoBFpZa9aJL4ERaJlS sJHF6+IRRV7kbazbk8Z4kUEFwvVSSpCN11sI67Fw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+d4503ae45b65c5bc1194@syzkaller.appspotmail.com, Takashi Iwai Subject: [PATCH 4.9 25/56] ALSA: pcm: Fix possible OOB access in PCM oss plugins Date: Mon, 1 Apr 2019 19:02:41 +0200 Message-Id: <20190401170105.339234260@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190401170103.398401360@linuxfoundation.org> References: <20190401170103.398401360@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ From: Takashi Iwai commit ca0214ee2802dd47239a4e39fb21c5b00ef61b22 upstream. The PCM OSS emulation converts and transfers the data on the fly via "plugins". The data is converted over the dynamically allocated buffer for each plugin, and recently syzkaller caught OOB in this flow. Although the bisection by syzbot pointed out to the commit 65766ee0bf7f ("ALSA: oss: Use kvzalloc() for local buffer allocations"), this is merely a commit to replace vmalloc() with kvmalloc(), hence it can't be the cause. The further debug action revealed that this happens in the case where a slave PCM doesn't support only the stereo channels while the OSS stream is set up for a mono channel. Below is a brief explanation: At each OSS parameter change, the driver sets up the PCM hw_params again in snd_pcm_oss_change_params_lock(). This is also the place where plugins are created and local buffers are allocated. The problem is that the plugins are created before the final hw_params is determined. Namely, two snd_pcm_hw_param_near() calls for setting the period size and periods may influence on the final result of channels, rates, etc, too, while the current code has already created plugins beforehand with the premature values. So, the plugin believes that channels=1, while the actual I/O is with channels=2, which makes the driver reading/writing over the allocated buffer size. The fix is simply to move the plugin allocation code after the final hw_params call. Reported-by: syzbot+d4503ae45b65c5bc1194@syzkaller.appspotmail.com Cc: Signed-off-by: Takashi Iwai Signed-off-by: Greg Kroah-Hartman --- sound/core/oss/pcm_oss.c | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) --- a/sound/core/oss/pcm_oss.c +++ b/sound/core/oss/pcm_oss.c @@ -951,6 +951,28 @@ static int snd_pcm_oss_change_params_loc oss_frame_size = snd_pcm_format_physical_width(params_format(params)) * params_channels(params) / 8; + err = snd_pcm_oss_period_size(substream, params, sparams); + if (err < 0) + goto failure; + + n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size); + err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL); + if (err < 0) + goto failure; + + err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS, + runtime->oss.periods, NULL); + if (err < 0) + goto failure; + + snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL); + + err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams); + if (err < 0) { + pcm_dbg(substream->pcm, "HW_PARAMS failed: %i\n", err); + goto failure; + } + #ifdef CONFIG_SND_PCM_OSS_PLUGINS snd_pcm_oss_plugin_clear(substream); if (!direct) { @@ -985,27 +1007,6 @@ static int snd_pcm_oss_change_params_loc } #endif - err = snd_pcm_oss_period_size(substream, params, sparams); - if (err < 0) - goto failure; - - n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size); - err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL); - if (err < 0) - goto failure; - - err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS, - runtime->oss.periods, NULL); - if (err < 0) - goto failure; - - snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL); - - if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams)) < 0) { - pcm_dbg(substream->pcm, "HW_PARAMS failed: %i\n", err); - goto failure; - } - if (runtime->oss.trigger) { sw_params->start_threshold = 1; } else {