Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp2179982pxb; Fri, 25 Mar 2022 12:31:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyp+VlHe33dMQ3t3CbKk6G6PPAzsE/s/7HnNgTCEv9DsYlOGzUGJd06tmw1FhuuGJWnUG/x X-Received: by 2002:a17:90a:1db:b0:1bf:711d:267a with SMTP id 27-20020a17090a01db00b001bf711d267amr14587417pjd.155.1648236677759; Fri, 25 Mar 2022 12:31:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648236677; cv=none; d=google.com; s=arc-20160816; b=EEBtYGYyo3QcN4ukIzjdT16QmoZ+ETgu4Qidd5dT7T9QULBailaWxRM8Ar8MZrRWXi FVYI2H2hSQ5/NHiHERo3vGnNaTYbXc7d2MgipEQYw8CQZS+BfLI5yNdiBajWiaN3eS0k AIl3vVCpBT85Pbb0QMvn749nvFQUqxAJMskDUFB6zCJ3F/Svnz8F/25rxFQTWEGLRbKt YaaEPHaNor5wXQScAe3UnqloC9iyAZZzSVY9To8y0moLBVFCGUdseTD+bBgPFjLFksUy bTnORP1yJvfxWa3kpivH3dKeT4HJ0xL+ZImm+Nm2VkNPpV3iwiZaBQ0s19q0Nzb5nX7G w2qQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=i2pKkNoUm0nBV/GTnhK0sGfguQizXrSHMHhyo6HyYqo=; b=pg3z78WzwO9IZ2JdFYq+ubXHjcWjF/sBd+bZd0NG7HiCBuTa5yH9xG6NtrTsC6nvNF hbL5biFTjj9320urizJnn9gAn9dKzVDKsV4f7QyRfpZ/oqeuoMLwNLZjUmG30GfEvQ31 SQK+mnqLdnH81DXosU7RF/JH4GuD/AIHQII+x5stSzQAVfDEtrQAbfppan1/W9GCjRxl wgElGAEhft3o6RbEmJHJIrjdz1lJgIqzFf6bP3pRbvJuYxMAwFxdpjf+8wc6rGdu3NKo O3kNTkTKI3ksykpJyjwQuQdLEK3PxoQwkci7CVMFY4huaHU5hLgQo8yJbTEZPxFRgx9Q oWUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=XAYh0xzn; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id mh18-20020a17090b4ad200b001c680dc9e6asi7507096pjb.61.2022.03.25.12.31.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Mar 2022 12:31:17 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=XAYh0xzn; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D64072EF0F2; Fri, 25 Mar 2022 11:44:49 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359711AbiCYPLA (ORCPT + 99 others); Fri, 25 Mar 2022 11:11:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359742AbiCYPJ0 (ORCPT ); Fri, 25 Mar 2022 11:09:26 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7886DDA6E0; Fri, 25 Mar 2022 08:07:20 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id B4BE161C14; Fri, 25 Mar 2022 15:07:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B8C62C340E9; Fri, 25 Mar 2022 15:07:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1648220839; bh=qewUrMqWkmRRIRadnW13+t9nVrfxgZxO+OT6rf8Gnis=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XAYh0xznX4cEc+VlA+zoQKbq5AThOkGT446pn4xnSkf/6iZ57tDV1rWGqwJWeGZEk ANl7Km1j3GWk4D/VuFbffmDniaJ5Nadt3V8xdpoDOVC35cfAaa+mB8qCpJiCfQNEkk Bex1Ruyx/vCLXYP3NVdVzODLWSRXF5RH9BhPwxz8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+72732c532ac1454eeee9@syzkaller.appspotmail.com, Linus Torvalds , Takashi Iwai Subject: [PATCH 4.19 08/20] ALSA: oss: Fix PCM OSS buffer allocation overflow Date: Fri, 25 Mar 2022 16:04:46 +0100 Message-Id: <20220325150417.251274796@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220325150417.010265747@linuxfoundation.org> References: <20220325150417.010265747@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Takashi Iwai commit efb6402c3c4a7c26d97c92d70186424097b6e366 upstream. We've got syzbot reports hitting INT_MAX overflow at vmalloc() allocation that is called from snd_pcm_plug_alloc(). Although we apply the restrictions to input parameters, it's based only on the hw_params of the underlying PCM device. Since the PCM OSS layer allocates a temporary buffer for the data conversion, the size may become unexpectedly large when more channels or higher rates is given; in the reported case, it went over INT_MAX, hence it hits WARN_ON(). This patch is an attempt to avoid such an overflow and an allocation for too large buffers. First off, it adds the limit of 1MB as the upper bound for period bytes. This must be large enough for all use cases, and we really don't want to handle a larger temporary buffer than this size. The size check is performed at two places, where the original period bytes is calculated and where the plugin buffer size is calculated. In addition, the driver uses array_size() and array3_size() for multiplications to catch overflows for the converted period size and buffer bytes. Reported-by: syzbot+72732c532ac1454eeee9@syzkaller.appspotmail.com Suggested-by: Linus Torvalds Cc: Link: https://lore.kernel.org/r/00000000000085b1b305da5a66f3@google.com Link: https://lore.kernel.org/r/20220318082036.29699-1-tiwai@suse.de Signed-off-by: Takashi Iwai Signed-off-by: Greg Kroah-Hartman --- sound/core/oss/pcm_oss.c | 12 ++++++++---- sound/core/oss/pcm_plugin.c | 5 ++++- 2 files changed, 12 insertions(+), 5 deletions(-) --- a/sound/core/oss/pcm_oss.c +++ b/sound/core/oss/pcm_oss.c @@ -789,6 +789,11 @@ static int snd_pcm_oss_period_size(struc if (oss_period_size < 16) return -EINVAL; + + /* don't allocate too large period; 1MB period must be enough */ + if (oss_period_size > 1024 * 1024) + return -ENOMEM; + runtime->oss.period_bytes = oss_period_size; runtime->oss.period_frames = 1; runtime->oss.periods = oss_periods; @@ -1060,10 +1065,9 @@ static int snd_pcm_oss_change_params_loc goto failure; } #endif - oss_period_size *= oss_frame_size; - - oss_buffer_size = oss_period_size * runtime->oss.periods; - if (oss_buffer_size < 0) { + oss_period_size = array_size(oss_period_size, oss_frame_size); + oss_buffer_size = array_size(oss_period_size, runtime->oss.periods); + if (oss_buffer_size <= 0) { err = -EINVAL; goto failure; } --- a/sound/core/oss/pcm_plugin.c +++ b/sound/core/oss/pcm_plugin.c @@ -61,7 +61,10 @@ static int snd_pcm_plugin_alloc(struct s } if ((width = snd_pcm_format_physical_width(format->format)) < 0) return width; - size = frames * format->channels * width; + size = array3_size(frames, format->channels, width); + /* check for too large period size once again */ + if (size > 1024 * 1024) + return -ENOMEM; if (snd_BUG_ON(size % 8)) return -ENXIO; size /= 8;