Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp2200776pxb; Fri, 25 Mar 2022 12:56:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyFBU5ob5SgueuyFjOSbW5vpSmXIatbU041gZIiN5ib7MTgtRo6O6IgPOYmPULWCvQOsL0O X-Received: by 2002:a17:90a:ab08:b0:1bc:3ece:bdc with SMTP id m8-20020a17090aab0800b001bc3ece0bdcmr14701736pjq.32.1648238170659; Fri, 25 Mar 2022 12:56:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648238170; cv=none; d=google.com; s=arc-20160816; b=j3kZFCtyyCtzdBC00sG2VrczvEmUBlipixVuISSsI2k8IJE5vq5EUF+kLdDVveR8J6 yjGg2IiJwwpR86yzndkCE4qTjJ+sGgn3t0fm/zao2IgGmGWRqNde5uJWq1Qz5inO5Jwa bmovDkllER9nA6mYqprK1EZirdOt7v6AnFK1S/bjHJA5z+UhQ/jmnfBGY4fqJujBMdB2 lGx72mvy7qWJ5UtiIyBlHP6PSiVyog3u9IEL+F4rK8M91xwe9Rz47kQWW+W6nPrxPwry wLDSarZI+gqxh0gx3681Iwu5I5Cec+CLXLI5b1+JIHFVDz4DCqsYCxs+kJBY0YFQxLRL uEbA== 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=6OLyuTp6dkm6RFDo5n2Sxlo0ZDhgLAU8xIgVxxLz6Qw=; b=o9QY12NAN0RyKmW9r5ZpyVs96A/8Nlq+ShxfPOf5AW4i32GgEJptIHgjOV/V+8k7MF gYx2prxkf4SoawddeqLd0guc1fWw6HyVC9A3XXE+gWZaWyJ1KMYkq0+5smrNJdrBe3i6 V76/gnMN3J2YG9fsLpaj/22VISQ83x7oZGlZ6ZBxhKgsxvRW3ADxv8LX8kt+BMKZIQrj vj2r9LM+1id2M0PYfCdS7DPd/V9MuCZjquoyPohOSd7uh9T1P3ubfdC9vKfbDoR5Jx5g K9E+Mqtd9d4CWNP42HC1iGIkkffvVxrAW1d1ThcHt48HkRiQS0GdqrD9uMEMHlkyeBG7 hZTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=iSEhNi4Q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id x9-20020a170902ec8900b00153b2d16660si3300832plg.616.2022.03.25.12.56.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Mar 2022 12:56:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=iSEhNi4Q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id BF18528996; Fri, 25 Mar 2022 11:45:13 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356375AbiCYPPl (ORCPT + 99 others); Fri, 25 Mar 2022 11:15:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359842AbiCYPMf (ORCPT ); Fri, 25 Mar 2022 11:12:35 -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 9DA6463BD8; Fri, 25 Mar 2022 08:09:09 -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 F1C3361C11; Fri, 25 Mar 2022 15:09:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0BAEAC340F3; Fri, 25 Mar 2022 15:09:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1648220946; bh=tLbiilRWNJwkevOSpD6zxL2E1sFPTPqO0uRjQlAFKtc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iSEhNi4Q9z/xDPdRzkukh7Vvr9GboWIy3abxWkNP4UWD59ayFbwxj4N58VOX+S6rQ GnB6xVJzy2FC6BmAU1JVRRfLLRpsDakVCr3gbVpE6dRhg/+RHPmejPswQONqf/WovO x0M8WL9J4jkaw1jbCdu3cKdidRmxGSOdnoMgmJwo= 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 5.10 13/38] ALSA: oss: Fix PCM OSS buffer allocation overflow Date: Fri, 25 Mar 2022 16:04:57 +0100 Message-Id: <20220325150420.141432967@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220325150419.757836392@linuxfoundation.org> References: <20220325150419.757836392@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 @@ -774,6 +774,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; @@ -1042,10 +1047,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;