Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp2178407pxb; Fri, 25 Mar 2022 12:29:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyrG/Pfy5m2p7pQtmyYM5YKtBj8xusaAi4qfK+/xZsvrWxOxfIA6dnDtu6MoZQ8xfANGIpX X-Received: by 2002:a17:90a:bf0f:b0:1c7:c762:5892 with SMTP id c15-20020a17090abf0f00b001c7c7625892mr10224802pjs.234.1648236575663; Fri, 25 Mar 2022 12:29:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648236575; cv=none; d=google.com; s=arc-20160816; b=ZB53bymZechHxxN2R7jceZGv1HRm2W2/+kz2G97P6P0myphUnBiNbgFTD6hcCLovCD HIrn3wvGOCvhIXq5GNtREyQUl6L+59SzLiMKguYdlf0pMYTzyoJokwoHLdfFnEAoGtbQ WtXuNdTP2QE0a5ZiIrGQEOnkG6pjo+QQVacOzTMH51bUyHdHBDVUS14G+NOMrvwCu6i8 3wcxFJ52C4p5RT+NhDaoU6+5SJymo+2vDY0r4agbyBn65Rl/ESKjtlhLZeY2QMbx4LkX t+F8pqKAjAQVXSJYfzYy6HAwiIuxnGRWC/ftRB5lrMLFuB906Jk65j6OTKxFgMd5O/hR k5nw== 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=bK3HYBbkhv6kiDHVTI4DR5xFN8Jg0HnuZX7f//GjGkQ=; b=nEdbUx3rz059U2FCleho4o1/g3EDfvHHV/ltPNquOwD1QO6+u63rzFMZ/BmvkmvWQn 2RJkqxF29yhCKWdDC1WQe1RPyKcIm7t2jbxE7/h3b+TjyrU4FXbK38JrdZblCSpmZp0L Ng6BgmDq03JhBboVpTYa6xOkYy7UAs/5UjBWSvnjQ7pv1le6NxzYr0q0ziKILqlPcpcJ dQjVVAdR5JXn4wWIJF8u5xSUnjYaTH1/pk+wZc1iRvUJLDKxth4RrtB0KTTL5+TZBbUw aHvcRU0xabYSejC9m+5aVVlqFbMyY5FtxaS29M87cV4f48++NEhNefvjKtiwir91PTaH xlfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=acTjrazq; 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 64-20020a630243000000b003821cf2feafsi3100456pgc.421.2022.03.25.12.29.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Mar 2022 12:29:35 -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=acTjrazq; 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 9C38E2D25AB; Fri, 25 Mar 2022 11:40:59 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376533AbiCYP1D (ORCPT + 99 others); Fri, 25 Mar 2022 11:27:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376601AbiCYPWw (ORCPT ); Fri, 25 Mar 2022 11:22:52 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB490E3897; Fri, 25 Mar 2022 08:16:28 -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 ams.source.kernel.org (Postfix) with ESMTPS id E43FAB828FF; Fri, 25 Mar 2022 15:16:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3C689C340EE; Fri, 25 Mar 2022 15:16:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1648221383; bh=+K+8ORVeew4aVIzDkKIXqrFRlhb+qisqa78eUU2k22Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=acTjrazqGoFycsu/IsL4Jq4taQPBQ9rdExNmAhkxP0oRgVM0KMZcVxPkxHpq4Nqob 7EdYYbmG1OCcWPDdsMaIfsiySxEFrfyck6WnfL2PhWNTeyLcRrD4SWLf6+zlvDFbqc FRMqRKoRTfL/EBgiujrYCNhklVflRA26SJg2NcTc= 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.15 07/37] ALSA: oss: Fix PCM OSS buffer allocation overflow Date: Fri, 25 Mar 2022 16:14:08 +0100 Message-Id: <20220325150420.146125782@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220325150419.931802116@linuxfoundation.org> References: <20220325150419.931802116@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; @@ -1043,10 +1048,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 @@ -62,7 +62,10 @@ static int snd_pcm_plugin_alloc(struct s width = snd_pcm_format_physical_width(format->format); if (width < 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;