Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp487085yba; Mon, 1 Apr 2019 10:16:16 -0700 (PDT) X-Google-Smtp-Source: APXvYqzH27TeBHFawy2rJKcSmJou2/w/xYqn9QVCDeBCxvBdeTLcFFl/fRokY3urhdtCbUJJZvTV X-Received: by 2002:a17:902:ba88:: with SMTP id k8mr63351874pls.268.1554138976505; Mon, 01 Apr 2019 10:16:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554138976; cv=none; d=google.com; s=arc-20160816; b=Yxe/Ge7dW8pwNsPHqn1RoeAdnY4D1HFj4zXnzhY131v3HzeYXCDGEerxcr8N7u8Vuz OU+etIWy9ts/0k8ndl8PXlP5EyMKCzgHGscD2nXuAJrgGtO5gUI0onmE8A65gasrLio1 2wynB6Z3AjSMWvy7zmxx0bqYbM67CqKfxoriIFlsl9C2UmCmaF20KgX80o/PyYZHNcau guzQcasa/4PYRtimkseJuVajSYU54Pt0xqQ1UC/TBLRGO6Aw7iVqhdw9nQdjWXEWQfLJ akjQycIkn/LIq/rgc6TZ4Tk0U6LjkfQ/3GfB3/PcGA4SWPlXOdbo7PDuuTQFksTft2kq C3Bg== 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=0owkMoafjl8RRbDw1jDMU+AQcbhAkFopE5QWsMmesQA=; b=vMMtWohY6h3o9sAkRUsLe56YUu/Z73SSVyrzZ7w2jXLBDj7atIPPWvQjRZzTNV7M1T Q4iuMwYTc8lfAaTrysC4LIAD9tpBwx/GGOAcMnwVTtkL/g5zxIY3TNRZTinFmL98B6Ix JueMgEXAtixifFVk+iV6ivljcaFpYqyLe/2irx60Du5J/LMXUDkXBHHlGmZ8sOS4SXE1 2YMCCj/tpJhH82XaK52YAcZN11BwFIFv0rbX4J6gD2g6CagahYxNEBnAsZ93f3qz3EwI td8apf7S2fg6VTiQZw88MLZgWA2lnt1MOi+qzLZiGCJm9OXMmLPbobociTbgyhf66mex fEQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Vyuxr4M3; 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 r190si9424318pfc.14.2019.04.01.10.16.01; Mon, 01 Apr 2019 10:16:16 -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=Vyuxr4M3; 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 S1730893AbfDARP1 (ORCPT + 99 others); Mon, 1 Apr 2019 13:15:27 -0400 Received: from mail.kernel.org ([198.145.29.99]:39542 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729969AbfDARPX (ORCPT ); Mon, 1 Apr 2019 13:15:23 -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 84D2121473; Mon, 1 Apr 2019 17:15:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1554138922; bh=rTiFpj/8Qx5DZxlRkCe9k3kQ3xDE78/nJy61eI+Wkoc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Vyuxr4M3/Im7QTkFMp8Y1XfQwolzc+u9/um8drZoQtVVRsDlqzAE139SZhWhrVcDL p+vwaDiIczL3RGN0YBvhpZAp5Gf6jXUZwdPz3ic0nQxsSSnxILClHtEorcHRZIdDij FwdToBHWJM719JSMIMdBmdd+wa6dvneiCvyf4YfY= 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.19 059/134] ALSA: pcm: Fix possible OOB access in PCM oss plugins Date: Mon, 1 Apr 2019 19:01:35 +0200 Message-Id: <20190401170050.115818456@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190401170044.243719205@linuxfoundation.org> References: <20190401170044.243719205@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.19-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 @@ -940,6 +940,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) { @@ -974,27 +996,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 {