Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp532991yba; Mon, 1 Apr 2019 11:13:09 -0700 (PDT) X-Google-Smtp-Source: APXvYqwHYQtxxu9+aT5jcOK3CuoD3bIDnCrIeMV+zoNVoxtWnrq+GDMNclGfbSngXxvZwQh/ZVKK X-Received: by 2002:a63:720e:: with SMTP id n14mr13612382pgc.93.1554142388949; Mon, 01 Apr 2019 11:13:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554142388; cv=none; d=google.com; s=arc-20160816; b=CET89d1BEcm31pmHT3VlTNCT+OnMOAbLa1f9/DEkEUMqca1qpCbtFQsSZPhBKYoBHa O7R85FDv1cLH7oebZTiHh5iMQhukWHf8RU40H/OSzAeoIvOZnxSQhCSEmri5dy8neZyO 3gJtm5qyDs8re65PwMlzw1b/xUW/cLy4/qz5bEmgPnI/BQoo9e7p9S8dUn0MSZdj7Z3N QGy5JaxWoAFhIeS0fNcD1JXfvbhuia88Qf/4wUs4afs29d1sGq47BjswZ5hV23JYo2Ow 6PVZ6ntdnf9ti1PwxNP6PDhwM1MWAG2LHZoiLPVoE6x9OS7RIyZT/reT390bEVgeCh8W JvUw== 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=mg4Wp0LdJQo/nwZfhg7KXObGNA8WA7P/9GB0WFBZhyo=; b=zsR97RfgLM1wFigoaV5DtYa4nEvofC0oswDvT3l3ZEoKVZiNmLX8WBDy1KaQfBRCbv SmrHxnVDFEJtB/3uZz7Y5vam37fStbGGDZ8rsQH1T4iFLDV74k8Fi9Nx/35eTk2elZGX bVu/NUZ0fIzDLx8VKFR52O1x1ffoJxfTs5HDFtI7OBDGiliEJNgk/pbO4p3LdiHxmY0w IaIRpCfsAhNWtuxWMlgiHLTxFqqrW1nyIJRlSg2S50+u8bwV2W4oUUKnVPptHzLZ91fi 7UNzHwEzpBi4ib14Wo1XF3x9sHHo14j92T2Fy7m7mvLzx1rRcyj6GTtv8TRjs9wIkXHa dT+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=uMnmo41Q; 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 70si9555467pla.128.2019.04.01.11.12.53; Mon, 01 Apr 2019 11:13:08 -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=uMnmo41Q; 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 S1728820AbfDARGq (ORCPT + 99 others); Mon, 1 Apr 2019 13:06:46 -0400 Received: from mail.kernel.org ([198.145.29.99]:52346 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729254AbfDARGl (ORCPT ); Mon, 1 Apr 2019 13:06:41 -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 305892192C; Mon, 1 Apr 2019 17:06:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1554138400; bh=5h314WCyHcdstThku8LdZsHBQz4uThCvfemPwtTbhOA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uMnmo41QyYWIo7EOuYAD/IBTI3sm8PAEylVU1noeS5C2utDSd+sD/lEWu58qUzCw6 Iuriaq/H9tvK8zjLOZBEh5V7OJIa4TWwP77+nuDw/GsNRMDeCPgFl/G1sVRIxBD6gq 6+KJ4f9Nd5PKTVEbPRzoWdlSk4vzAspaAJtC+epE= 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 5.0 056/146] ALSA: pcm: Fix possible OOB access in PCM oss plugins Date: Mon, 1 Apr 2019 19:01:08 +0200 Message-Id: <20190401170053.373762751@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190401170048.449559024@linuxfoundation.org> References: <20190401170048.449559024@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 5.0-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 {